RTDialog.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 7k
Category:

Graph program

Development Platform:

Visual C++

  1. #include <stdafx.h>
  2. #include "RTDialog.h"
  3. #include "RTRenderer.h"
  4. #include <process.h>
  5. MyRenderer global_rend;
  6. HFONT CRTDialog::m_hFont = NULL;
  7. HWND  CRTDialog::m_hWndDialog = NULL;
  8. HWND  CRTDialog::m_hWndParent = NULL;
  9. HWND  CRTDialog::m_hWndStart = NULL;
  10. HWND  CRTDialog::m_hWndStop = NULL;
  11. HWND  CRTDialog::m_hWndFrame = NULL;
  12. HDC   CRTDialog::m_FrameDC = NULL;
  13. C3dCamera*  CRTDialog::m_camera = NULL;
  14. HINSTANCE CRTDialog::m_hInst = NULL;
  15. #define   DLG_WIDTH    500
  16. #define   DLG_HEIGHT   400
  17. #define   BTN_WIDTH    90
  18. #define   BTN_HEIGHT   25
  19. #define   CX_SHIFT     5
  20. #define   CY_SHIFT     5
  21. CRTDialog::CRTDialog(HWND hWndParent, C3dCamera*  cam)
  22. {
  23. HINSTANCE hInst = GetModuleHandle(NULL);
  24. WNDCLASSEX wcex;
  25. if (!GetClassInfoEx(hInst, "RTDialog", &wcex))
  26. {
  27. wcex.cbSize = sizeof(WNDCLASSEX); 
  28. wcex.style = CS_HREDRAW | CS_VREDRAW;
  29. wcex.lpfnWndProc = (WNDPROC)WndProc;
  30. wcex.cbClsExtra = 0;
  31. wcex.cbWndExtra = 0;
  32. wcex.hInstance = hInst;
  33. wcex.hIcon = NULL;
  34. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  35. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
  36. wcex.lpszMenuName = NULL;
  37. wcex.lpszClassName = "RTDialog";
  38. wcex.hIconSm = NULL;
  39. if (RegisterClassEx(&wcex) == 0)
  40. MessageBox(NULL, "Can't create CRTDialog!", "Error", MB_OK);
  41. }
  42.     m_hWndParent = hWndParent;
  43. m_camera = cam;
  44. }
  45. CRTDialog::~CRTDialog()
  46. {
  47. }
  48. LRESULT CALLBACK CRTDialog::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  49. {
  50.     LOGFONT lfont;
  51. RECT    clRect;
  52. LONG dlgH;
  53. LONG dlgW;
  54. switch (message) 
  55. {
  56. case WM_CREATE:
  57.             // font
  58.             memset(&lfont, 0, sizeof(lfont));
  59.             lstrcpy(lfont.lfFaceName, _T("Arial"));
  60.             lfont.lfHeight = 16;
  61.             lfont.lfWeight = FW_NORMAL;//FW_BOLD;
  62.             lfont.lfItalic = FALSE;
  63.             lfont.lfCharSet = DEFAULT_CHARSET;
  64.             lfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
  65.             lfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  66.             lfont.lfQuality = DEFAULT_QUALITY;
  67.             lfont.lfPitchAndFamily = DEFAULT_PITCH;
  68.         m_hFont = CreateFontIndirect(&lfont);
  69.         m_hInst = GetModuleHandle(NULL);
  70. GetClientRect(hWnd,&clRect);
  71. dlgH = clRect.bottom-clRect.top;
  72. dlgW = clRect.right-clRect.left;
  73. // button OK
  74. m_hWndStart = CreateWindowEx(WS_EX_STATICEDGE,
  75. "button","Start",
  76. WS_VISIBLE | WS_CHILD | WS_TABSTOP, 
  77. dlgW - BTN_WIDTH-CX_SHIFT, CY_SHIFT, BTN_WIDTH, BTN_HEIGHT, 
  78. hWnd, 
  79. NULL, 
  80. m_hInst, 
  81. NULL); 
  82.             // setting font
  83.             SendMessage(m_hWndStart, WM_SETFONT, (WPARAM)m_hFont, 0);
  84.             // button Cancel
  85. m_hWndStop = CreateWindowEx(WS_EX_STATICEDGE,
  86. "button","Stop",
  87. WS_VISIBLE | WS_CHILD | WS_TABSTOP, 
  88. dlgW - BTN_WIDTH-CX_SHIFT, CY_SHIFT+BTN_HEIGHT+CY_SHIFT, BTN_WIDTH, BTN_HEIGHT, 
  89. hWnd, 
  90. NULL, 
  91. m_hInst, 
  92. NULL); 
  93.             // setting font
  94.             SendMessage(m_hWndStop, WM_SETFONT, (WPARAM)m_hFont, 0);
  95.        
  96.             // static Propmpt
  97. m_hWndFrame = CreateWindowEx(WS_EX_STATICEDGE,
  98. "static","",
  99. WS_VISIBLE | WS_CHILD, 
  100. CX_SHIFT, CY_SHIFT, dlgW - 3*CX_SHIFT - BTN_WIDTH, dlgH - 2*CY_SHIFT, 
  101. hWnd, 
  102. NULL, 
  103. m_hInst, 
  104. NULL); 
  105.             // setting font
  106.             SendMessage(m_hWndFrame, WM_SETFONT, (WPARAM)m_hFont, 0);
  107.             SetFocus(m_hWndStart);
  108. break;
  109. case WM_DESTROY:
  110. OnDestroy();
  111. DeleteObject(m_hFont);
  112. EnableWindow(m_hWndParent, TRUE);
  113. SetForegroundWindow(m_hWndParent);
  114. DestroyWindow(hWnd);
  115. PostQuitMessage(0);
  116. break;
  117. case WM_SIZE:
  118. OnSize(LOWORD(lParam), HIWORD(lParam));
  119. break;
  120.         case WM_COMMAND:
  121.             switch (HIWORD(wParam))
  122.             {
  123.                 case BN_CLICKED:
  124.                     if ((HWND)lParam == m_hWndStart)
  125.                         OnStart();
  126.                     if ((HWND)lParam == m_hWndStop)
  127.                         OnStop();
  128.                     break;
  129.             }
  130.             break;
  131. default:
  132. return DefWindowProc(hWnd, message, wParam, lParam);
  133.    }
  134.    return 0;
  135. }
  136. static  void  StartRender(void* arg)
  137. {
  138. sgRayTracer::sgStart((SG_VIEW_PORT*)arg);
  139. }
  140. void CRTDialog::OnStart()
  141. {
  142. if (!m_hWndFrame || !m_camera)
  143. return;
  144. OnStop();
  145. RECT frame_rect;
  146. GetClientRect(m_hWndFrame,&frame_rect);
  147. if (m_FrameDC==NULL)
  148. {
  149. m_FrameDC = GetDC(m_hWndFrame);
  150. }
  151. global_rend.InitRender(m_FrameDC,frame_rect.right-frame_rect.left, 
  152. frame_rect.bottom-frame_rect.top,
  153. 0, 0);
  154. SG_VECTOR eye_loc;
  155. SG_VECTOR eye_look_at;
  156. SG_VECTOR eye_y;
  157. SG_VECTOR eye_x;
  158. m_camera->GetVectorsForRayTracingCamera(eye_loc,eye_look_at,eye_y,eye_x);
  159. sgRayTracer::sgSetMaterialsLibrary(&global_rend);
  160. sgRayTracer::sgSetLightSourcesContainer(&global_rend);
  161. sgRayTracer::sgSetAntialiasFlag(true);
  162. sgRayTracer::sgSetCamera(sgRayTracer::CT_PERSPECTIVE,eye_loc,eye_look_at,eye_y,eye_x,NULL);
  163. _beginthread(StartRender,1024,(void*)&global_rend);
  164. }
  165. void CRTDialog::OnStop()
  166. {
  167. sgRayTracer::sgStop();
  168. }
  169. void CRTDialog::OnDestroy()
  170. {
  171. sgRayTracer::sgStop();
  172. global_rend.DeInitRender();
  173. if (m_FrameDC)
  174. {
  175. ReleaseDC(m_hWndFrame, m_FrameDC);
  176. m_FrameDC = NULL;
  177. }
  178. }
  179. void CRTDialog::OnSize(int cx, int cy)
  180. {
  181. if (m_hWndStart)
  182. {
  183. MoveWindow(m_hWndStart,cx - BTN_WIDTH-CX_SHIFT, CY_SHIFT, BTN_WIDTH, BTN_HEIGHT, FALSE);
  184. }
  185. if (m_hWndStop)
  186. {
  187. MoveWindow(m_hWndStop, cx - BTN_WIDTH-CX_SHIFT, CY_SHIFT+BTN_HEIGHT+CY_SHIFT, BTN_WIDTH, BTN_HEIGHT, FALSE);
  188. }
  189. if (m_hWndFrame)
  190. {
  191. MoveWindow(m_hWndFrame,CX_SHIFT, CY_SHIFT, cx - 3*CX_SHIFT - BTN_WIDTH, cy - 2*CY_SHIFT, FALSE);
  192. }
  193. }
  194. BOOL CRTDialog::DoModal()
  195. {
  196. RECT r;
  197. GetWindowRect(GetDesktopWindow(), &r);
  198. m_hWndDialog = CreateWindowEx(WS_EX_TOOLWINDOW, 
  199.                 "RTDialog",
  200.                 "sgCore Ray Tracing",
  201.                 WS_POPUPWINDOW | WS_CAPTION | WS_SIZEBOX, 
  202.                 (r.right - DLG_WIDTH) / 2, (r.bottom - DLG_HEIGHT) / 2,
  203.                 DLG_WIDTH, DLG_HEIGHT,
  204.                 m_hWndParent,
  205.                 NULL,
  206.                 m_hInst,
  207.                 NULL);
  208.     if(m_hWndDialog == NULL)
  209.         return FALSE;
  210.     SetForegroundWindow(m_hWndDialog);
  211. EnableWindow(m_hWndParent, FALSE);
  212.     ShowWindow(m_hWndDialog, SW_SHOW); 
  213.     UpdateWindow(m_hWndDialog);
  214.     BOOL ret = 0;
  215. MSG msg;
  216.     while (GetMessage(&msg, NULL, 0, 0)) 
  217.     {       
  218. if (msg.message == WM_KEYDOWN) 
  219. {
  220. if (msg.wParam == VK_ESCAPE)
  221.             {
  222. SendMessage(m_hWndDialog, WM_DESTROY, 0, 0);
  223.                 ret = 0;
  224.             }         
  225. }
  226.         TranslateMessage(&msg);
  227. DispatchMessage(&msg);      
  228.     }
  229. return ret;
  230. }