RENDERVIEW.CPP
Upload User: nthssl
Upload Date: 2022-04-05
Package Size: 25357k
Code Size: 7k
Category:

OpenCV

Development Platform:

Visual C++

  1. // RenderView.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "3DEditor.h"
  5. #include "DlgRayTrace.h"
  6. #include "RenderView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // RenderView dialog
  14. int renderviewmode=0;
  15. extern CRITICAL_SECTION cs_rv;
  16. extern RenderView *renderview;
  17. RenderView::RenderView(CWnd* pParent /*=NULL*/)
  18. : CDialog(RenderView::IDD, pParent)
  19. {
  20. //{{AFX_DATA_INIT(RenderView)
  21. // NOTE: the ClassWizard will add member initialization here
  22. //}}AFX_DATA_INIT
  23. }
  24. void RenderView::DoDataExchange(CDataExchange* pDX)
  25. {
  26. CDialog::DoDataExchange(pDX);
  27. //{{AFX_DATA_MAP(RenderView)
  28. // NOTE: the ClassWizard will add DDX and DDV calls here
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(RenderView, CDialog)
  32. //{{AFX_MSG_MAP(RenderView)
  33. ON_WM_CLOSE()
  34. ON_WM_PAINT()
  35. ON_WM_SIZE()
  36. //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38. /////////////////////////////////////////////////////////////////////////////
  39. // RenderView message handlers
  40. void RenderView::Close()
  41. {
  42. OnClose();
  43. }
  44. void RenderView::OnClose() 
  45. {
  46. // TODO: Add your message handler code here and/or call default
  47. EnterCriticalSection(&cs_rv);
  48. renderview=0;
  49. if (buf)
  50. {
  51. ((DlgRayTrace *)rd)->buf=buf;
  52. ((DlgRayTrace *)rd)->bufsx=bufsx;
  53. ((DlgRayTrace *)rd)->bufsy=bufsy;
  54. buf=0;
  55. }
  56. GetWindowRect(&winpos3);
  57. ((DlgRayTrace *)rd)->GetDlgItem(IDC_PREVIEW)->EnableWindow(1);
  58. RenderView **d=&((DlgRayTrace *)rd)->rv;
  59. delete ((DlgRayTrace *)rd)->rv;
  60. *d=0;
  61. LeaveCriticalSection(&cs_rv);
  62. CDialog::OnClose();
  63. }
  64. BOOL RenderView::OnInitDialog() 
  65. {
  66. CDialog::OnInitDialog();
  67. // TODO: Add extra initialization here
  68. winpos3.left=-1;
  69. winpos3.top=-1;
  70. CMenu *menu=GetMenu();
  71. menu->CheckMenuRadioItem( ID_MODE1, ID_MODE2, ID_MODE1+renderviewmode, MF_BYCOMMAND);
  72. if (winpos3.left!=-1)
  73. SetWindowPos(0,winpos3.left,winpos3.top,0,0,SWP_NOSIZE|SWP_NOZORDER);
  74. SetIcon(AfxGetApp()->LoadIcon(IDR_MAINFRAME), FALSE);
  75. return TRUE;  // return TRUE unless you set the focus to a control
  76.               // EXCEPTION: OCX Property Pages should return FALSE
  77. }
  78. void RenderView::DrawPicture(CDC *dc,picture *p,int ssx,int ssy)
  79. {
  80. struct tagBITMAPINFO bmi;
  81. bmi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  82. bmi.bmiHeader.biCompression=BI_RGB;
  83. bmi.bmiHeader.biXPelsPerMeter=6000;
  84. bmi.bmiHeader.biYPelsPerMeter=6000;
  85. bmi.bmiHeader.biClrUsed=0;
  86. bmi.bmiHeader.biClrImportant=0;
  87. bmi.bmiHeader.biSizeImage=0;
  88. bmi.bmiHeader.biWidth=p?ssx:bufsx;
  89. bmi.bmiHeader.biHeight=p?ssy:bufsy;
  90. bmi.bmiHeader.biPlanes=1;
  91. bmi.bmiHeader.biBitCount=24;
  92. if (p)
  93. {
  94. int tx=ssx*3,x,y,yp,xx;
  95. byteRGB *rgb;
  96. if (tx%4) tx+=(4-tx%4);
  97. if (buf) 
  98. delete buf;
  99. buf=new unsigned char[tx*ssy];
  100. for( y=0;y<ssy;y++ )
  101.   {
  102. yp=(ssy-y-1)*tx;
  103. rgb=(byteRGB *)p->buf[y*(p->sy-1)/(ssy-1)];
  104. for( x=0;x<ssx;x++ )
  105.  {
  106. xx=x*(p->sx-1)/(ssx-1);
  107. buf[yp++]=rgb[xx].b;
  108. buf[yp++]=rgb[xx].g;
  109. buf[yp++]=rgb[xx].r;
  110.  }
  111.  }
  112. bufsx=ssx;
  113. bufsy=ssy;
  114. }
  115. if (buf)
  116. if (renderviewmode)
  117. {
  118. RECT rect;
  119. GetClientRect(&rect);
  120. StretchDIBits(dc->m_hDC,0,0,rect.right,rect.bottom,0,0,bufsx,bufsy,buf,&bmi,DIB_RGB_COLORS,SRCCOPY);
  121. }
  122. else SetDIBitsToDevice(dc->m_hDC, 0,0, bufsx,bufsy, 0,0,  0,bufsy, buf, &bmi, DIB_RGB_COLORS); 
  123. }
  124. void RenderView::DrawPicture(picture *p,int ssx,int ssy)
  125. {
  126. CDC *dc=GetDC();
  127. DrawPicture(dc,p,ssx,ssy);
  128. ReleaseDC(dc);
  129. }
  130. void RenderView::OnPaint() 
  131. {
  132. CPaintDC dc(this); // device context for painting
  133. // TODO: Add your message handler code here
  134. DrawPicture(&dc,0,0,0);
  135. // Do not call CDialog::OnPaint() for painting messages
  136. }
  137. BOOL RenderView::OnCommand(WPARAM wParam, LPARAM lParam) 
  138. {
  139. // TODO: Add your specialized code here and/or call the base class
  140. switch(wParam)
  141. {
  142. case ID_ASPECT:
  143. if (buf)
  144. {
  145. CRect rect, rectw;
  146. GetClientRect(&rect);
  147. GetWindowRect(&rectw);
  148. int newsx=rect.bottom*bufsx/bufsy;
  149. SetWindowPos(0, 0, 0, rectw.right-rectw.left-rect.right+newsx, rectw.bottom-rectw.top, SWP_NOMOVE | SWP_NOZORDER);
  150. }
  151. break;
  152. case ID_MODE1:
  153. {
  154. CMenu *menu=GetMenu();
  155. renderviewmode=0;
  156. menu->CheckMenuRadioItem( ID_MODE1, ID_MODE2, ID_MODE1+renderviewmode, MF_BYCOMMAND);
  157. Invalidate(1);
  158. menu->EnableMenuItem( ID_ASPECT, MF_GRAYED);
  159. }
  160. break;
  161. case ID_MODE2:
  162. {
  163. CMenu *menu=GetMenu();
  164. renderviewmode=1;
  165. menu->CheckMenuRadioItem( ID_MODE1, ID_MODE2, ID_MODE1+renderviewmode, MF_BYCOMMAND);
  166. Invalidate(0);
  167. menu->EnableMenuItem( ID_ASPECT, MF_ENABLED);
  168. }
  169. break;
  170. case ID_SAVEIMG:
  171. if (buf)
  172. {
  173. CString s;
  174. s.LoadString(IDS_FD_TGA);
  175. CFileDialog fd(FALSE,"tga",0,OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,s);
  176. if(fd.DoModal()==IDOK)
  177. if (SaveTGA((char *)(LPCSTR)fd.GetPathName())==0)
  178. AfxMessageBox(IDS_ERRORSAVING,MB_OK|MB_ICONEXCLAMATION);
  179. }
  180. break;
  181. case ID_COPY:
  182. Copy();
  183. break;
  184. }
  185. return CDialog::OnCommand(wParam, lParam);
  186. }
  187. int RenderView::SaveTGA(char *name)
  188. {
  189. if (buf==0)
  190. return 0;
  191. FILE *fp=fopen(name,"wb");
  192. if (fp==0)
  193. return 0;
  194.     
  195.     unsigned char TGA_INI[18];
  196. memset(&TGA_INI[0],0,18);
  197.     TGA_INI[12]=(unsigned char)(bufsx%256);
  198.     TGA_INI[13]=(unsigned char)(bufsx/256);
  199.     TGA_INI[14]=(unsigned char)(bufsy%256);
  200.     TGA_INI[15]=(unsigned char)(bufsy/256);
  201.     TGA_INI[2]=2;
  202.     TGA_INI[16]=0x18;
  203.     fwrite((char *)&TGA_INI[0],18,1,fp);
  204. int y,tx=3*bufsx;
  205. if (tx%4) tx+=(4-tx%4);
  206. for( y=0;y<bufsy;y++ )
  207. fwrite(&buf[tx*y],bufsx,3,fp);
  208. fclose(fp);
  209. return 1;
  210. }
  211. void RenderView::Copy()
  212. {
  213. if (buf==0)
  214. return;
  215. if ( !OpenClipboard() )
  216. return;
  217. if ( !EmptyClipboard() )
  218. {
  219. CloseClipboard();
  220. return;
  221. }
  222. HGLOBAL hData;
  223. BITMAPINFOHEADER *bi;
  224. int tx=bufsx*3;
  225. if (tx%4) tx+=(4-tx%4);
  226. hData=GlobalAlloc(GMEM_MOVEABLE,tx*bufsy+sizeof(BITMAPINFOHEADER));
  227. unsigned char *b=(unsigned char *)GlobalLock(hData);
  228. bi=(BITMAPINFOHEADER *)b;
  229. bi->biSize=sizeof(BITMAPINFOHEADER);
  230. bi->biWidth=bufsx;
  231. bi->biHeight=bufsy;
  232. bi->biPlanes=1;
  233. bi->biBitCount=24;
  234. bi->biCompression=BI_RGB;
  235. bi->biSizeImage=0;
  236. bi->biXPelsPerMeter=0;
  237. bi->biYPelsPerMeter=0;
  238. bi->biClrUsed=0;
  239. bi->biClrImportant=0;
  240. memcpy(&b[sizeof(BITMAPINFOHEADER)],buf,tx*bufsy);
  241. GlobalUnlock(hData);
  242. SetClipboardData( CF_DIB, hData );
  243. CloseClipboard();
  244. }
  245. void RenderView::OnSize(UINT nType, int cx, int cy) 
  246. {
  247. CDialog::OnSize(nType, cx, cy);
  248. // TODO: Add your message handler code here
  249. DrawPicture(0,0,0);
  250. }