ChildView.cpp
上传用户:lin85885
上传日期:2013-04-27
资源大小:796k
文件大小:29k
源码类别:

3D图形编程

开发平台:

Visual C++

  1. // ChildView.cpp : implementation of the CChildView class
  2. //
  3. #include "stdafx.h"
  4. #include <GL/gl.h>
  5. #include <GL/glu.h>
  6. #include <GL/glaux.h>
  7. #include "Simplifier.h"
  8. #include "ChildView.h"
  9. #include "MainFrm.h"
  10. #include "InitOpenGL.h"
  11. #include "SimplifyDiag.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CChildView
  19. CChildView::CChildView()
  20. {
  21. m_fViewHeight = 2.0;
  22. m_fViewNear = -2.0;
  23. m_fViewFar = 2.0;
  24. m_nMouseAct = MOUSE_SPIN;
  25. m_bAnimate = FALSE;
  26. m_bAxes = TRUE;
  27. m_spinAngle = 0.0;
  28. m_spinAxes[0] = m_spinAxes[2] = 0.0; m_spinAxes[1] = 1.0;
  29. m_sEye.origin.x = m_sEye.origin.y = m_sEye.origin.z = 0.0;
  30. m_sEye.center.x = m_sEye.center.y = 0.0; m_sEye.center.z = -1.0;
  31. m_sEye.up.x = m_sEye.up.z = 0.0; m_sEye.up.y = 1.0;
  32. m_sEye.axisX.y = m_sEye.axisX.z = 0.0; m_sEye.axisX.x = 1.0;
  33. m_sEye.axisY.x = m_sEye.axisY.z = 0.0; m_sEye.axisY.y = 1.0;
  34. m_sEye.axisZ.x = m_sEye.axisZ.y = 0.0; m_sEye.axisZ.z = -1.0;
  35. m_vProjectCenter[0] = 0.0f;
  36. m_vProjectCenter[1] = 0.0f;
  37. m_vProjectCenter[2] = (m_fViewNear+m_fViewFar)/2;
  38. m_nRenderModel = 1;
  39. m_nRenderMode = 1;
  40. m_pModel = NULL;
  41. m_nFaceNo = 0;
  42. }
  43. CChildView::~CChildView()
  44. {
  45. if (m_pModel)
  46. delete m_pModel;
  47. }
  48. BEGIN_MESSAGE_MAP(CChildView,CWnd )
  49. //{{AFX_MSG_MAP(CChildView)
  50. ON_WM_PAINT()
  51. ON_WM_SIZE()
  52. ON_WM_CREATE()
  53. ON_WM_MOUSEMOVE()
  54. ON_WM_LBUTTONDOWN()
  55. ON_WM_LBUTTONUP( )
  56. ON_WM_SETCURSOR()
  57. ON_WM_TIMER()
  58. ON_WM_CONTEXTMENU()
  59. ON_COMMAND(ID_MOUSE_SPIN, OnMouseSpin)
  60. ON_UPDATE_COMMAND_UI(ID_MOUSE_SPIN, OnUpdateMouseSpin)
  61. ON_COMMAND(ID_MOUSE_TRANSLATE, OnMouseTranslate)
  62. ON_UPDATE_COMMAND_UI(ID_MOUSE_TRANSLATE, OnUpdateMouseTranslate)
  63. ON_COMMAND(ID_MOUSE_ZOOM, OnMouseZoom)
  64. ON_UPDATE_COMMAND_UI(ID_MOUSE_ZOOM, OnUpdateMouseZoom)
  65. ON_COMMAND(ID_ANIMATE, OnAnimate)
  66. ON_UPDATE_COMMAND_UI(ID_ANIMATE, OnUpdateAnimate)
  67. ON_COMMAND(ID_SPIN_XCCW, OnSpinXccw)
  68. ON_COMMAND(ID_SPIN_XCW, OnSpinXcw)
  69. ON_COMMAND(ID_SPIN_YCCW, OnSpinYccw)
  70. ON_COMMAND(ID_SPIN_YCW, OnSpinYcw)
  71. ON_COMMAND(ID_SPIN_ZCCW, OnSpinZccw)
  72. ON_COMMAND(ID_SPIN_ZCW, OnSpinZcw)
  73. ON_COMMAND(ID_ZOOM_IN, OnZoomIn)
  74. ON_COMMAND(ID_ZOOM_OUT, OnZoomOut)
  75. ON_COMMAND(ID_OPEN_FILE, OnOpenFile)
  76. ON_COMMAND(IDM_ORIGINAL_MODEL, OnOriginalModel)
  77. ON_UPDATE_COMMAND_UI(IDM_ORIGINAL_MODEL, OnUpdateOriginalModel)
  78. ON_COMMAND(IDM_SIMPLIFIED_MODEL, OnSimplifiedModel)
  79. ON_UPDATE_COMMAND_UI(IDM_SIMPLIFIED_MODEL, OnUpdateSimplifiedModel)
  80. ON_COMMAND(IDM_WIREFRAME, OnWireframe)
  81. ON_UPDATE_COMMAND_UI(IDM_WIREFRAME, OnUpdateWireframe)
  82. ON_COMMAND(IDM_FLAT, OnFlat)
  83. ON_UPDATE_COMMAND_UI(IDM_FLAT, OnUpdateFlat)
  84. ON_COMMAND(ID_SIMPLIFY, OnSimplify)
  85. ON_COMMAND(ID_SAVE_FILE, OnSaveFile)
  86. ON_COMMAND(ID_AXES, OnAxes)
  87. ON_UPDATE_COMMAND_UI(ID_AXES, OnUpdateAxes)
  88. ON_COMMAND(IDM_SAVE_PICTURE, OnSavePicture)
  89. ON_UPDATE_COMMAND_UI(ID_SIMPLIFY, OnUpdateSimplify)
  90. ON_UPDATE_COMMAND_UI(ID_SAVE_FILE, OnUpdateSaveFile)
  91. ON_COMMAND(ID_CLOSE_FILE, OnCloseFile)
  92. ON_UPDATE_COMMAND_UI(ID_CLOSE_FILE, OnUpdateCloseFile)
  93. //}}AFX_MSG_MAP
  94. END_MESSAGE_MAP()
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CChildView message handlers
  97. int CChildView::OnCreate( LPCREATESTRUCT lpCreateStruct )
  98. {
  99. if (CWnd::OnCreate(lpCreateStruct) == -1)
  100. return -1;
  101. Init();
  102. return 0;
  103. }
  104. BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
  105. {
  106. cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN ;
  107. cs.dwExStyle |= WS_EX_CLIENTEDGE;
  108. cs.style &= ~WS_BORDER;
  109. cs.lpszClass = AfxRegisterWndClass(CS_OWNDC|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
  110. ::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);
  111. if (!CWnd::PreCreateWindow(cs))
  112. return FALSE;
  113. return TRUE;
  114. }
  115. void CChildView::OnPaint() 
  116. {
  117. CPaintDC dc(this); // device context for painting
  118. // TODO: Add your message handler code here
  119. DrawScene();
  120. // Do not call CWnd::OnPaint() for painting messages
  121. }
  122. void CChildView::OnSize( UINT nType, int cx, int cy )
  123. {
  124. CWnd::OnSize(nType, cx, cy);
  125. Project();
  126. }
  127. BOOL CChildView::OnCommand(WPARAM wParam, LPARAM lParam) 
  128. {
  129. WORD nID = wParam & 0x0000ffff;
  130. switch (nID)
  131. {
  132. case ID_ZOOM_IN:
  133. OnZoomIn();
  134. break;
  135. case ID_ZOOM_OUT:
  136. OnZoomOut();
  137. break;
  138. case ID_SPIN_XCW:
  139. OnSpinXcw();
  140. break;
  141. case ID_SPIN_XCCW:
  142. OnSpinXccw();
  143. break;
  144. case ID_SPIN_YCW:
  145. OnSpinYcw();
  146. break;
  147. case ID_SPIN_YCCW:
  148. OnSpinYccw();
  149. break;
  150. case ID_SPIN_ZCW:
  151. OnSpinZcw();
  152. break;
  153. case ID_SPIN_ZCCW:
  154. OnSpinZccw();
  155. break;
  156. }
  157. return CWnd ::OnCommand(wParam, lParam);
  158. }
  159. void CChildView::OnTimer( UINT nIDEvent )
  160. {
  161. if (nIDEvent == TIMER_ANIMATE)
  162. {
  163. if (m_bAnimate && (m_spinAngle!=0.0) )
  164. SpinGlobal(m_spinAxes, m_spinAngle);
  165. }
  166. else if (nIDEvent == TIMER_TOOLBAR)
  167. {
  168. CMainFrame *pFrameWnd = (CMainFrame*)AfxGetMainWnd();
  169. int count = (pFrameWnd->m_wndInteractRight).GetToolBarCtrl().GetButtonCount();
  170. int nIndex;
  171. for( nIndex = 0; nIndex < count; nIndex++)
  172. {
  173. if( (pFrameWnd->m_wndInteractRight).GetToolBarCtrl().IsButtonPressed( (pFrameWnd->m_wndInteractRight).GetItemID( nIndex)))
  174. {
  175. pFrameWnd->SendMessage( WM_COMMAND, 
  176. (pFrameWnd->m_wndInteractRight).GetItemID( nIndex), (LPARAM)m_hWnd);
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. void CChildView::OnContextMenu( CWnd* pWnd, CPoint pos )
  183. {
  184. CMenu menu;
  185. menu.LoadMenu(IDR_CONTEXTMENU);
  186. TRACE("%d  %d  n",pos.x,pos.y);
  187. menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
  188. pos.x,pos.y,this);
  189. }
  190. void CChildView::OnMouseMove( UINT nFlags, CPoint point )
  191. {
  192. if (m_nMouseAct == MOUSE_SPIN)
  193. {
  194. MouseSpinGlobal(nFlags, point.x, point.y, 0);
  195. }
  196. else if (m_nMouseAct == MOUSE_ZOOM)
  197. {
  198. MouseZoom(nFlags, point.x, point.y);
  199. }
  200. else if (m_nMouseAct == MOUSE_TRANSLATE)
  201. {
  202. MouseTranslate(nFlags, point.x, point.y);
  203. }
  204. CWnd::OnMouseMove(nFlags, point);
  205. }
  206. void CChildView::OnLButtonDown(UINT nFlags, CPoint point) 
  207. {
  208. m_spinAngle = 0.0f;
  209. if (m_nMouseAct == MOUSE_SPIN)
  210. {
  211. if (m_bAnimate)
  212. SetTimer(TIMER_ANIMATE,ANIMATE_RATE,NULL);
  213. MouseSpinGlobal(nFlags, point.x, point.y, 1);
  214. }
  215. else if (m_nMouseAct == MOUSE_ZOOM)
  216. {
  217. MouseZoom(nFlags, point.x, point.y);
  218. }
  219. else if (m_nMouseAct == MOUSE_TRANSLATE)
  220. {
  221. MouseTranslate(nFlags, point.x, point.y);
  222. }
  223. CWnd::OnLButtonDown(nFlags, point);
  224. MSG msg;
  225. while(::PeekMessage( &msg, m_hWnd,
  226. WM_MOUSEMOVE, WM_MOUSEMOVE,
  227. PM_REMOVE))
  228. ;
  229. m_bLButtonDown = TRUE;
  230. }
  231. void CChildView::OnLButtonUp(UINT nFlags, CPoint point)
  232. {
  233. m_bLButtonDown = FALSE;
  234. MSG msg;
  235. while(::PeekMessage( &msg, m_hWnd,
  236. WM_MOUSEMOVE, WM_MOUSEMOVE,
  237. PM_REMOVE))
  238. ;
  239. CWnd::OnLButtonUp(nFlags, point);
  240. }
  241. BOOL CChildView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  242. {
  243. HCURSOR hCursor;
  244. switch (m_nMouseAct)
  245. {
  246. case MOUSE_SPIN:
  247. hCursor = AfxGetApp()->LoadCursor( IDC_SPIN );
  248. SetCursor( hCursor );
  249. break;
  250. case MOUSE_ZOOM:
  251. hCursor = AfxGetApp()->LoadCursor( IDC_ZOOM );
  252. SetCursor( hCursor );
  253. break;
  254. case MOUSE_TRANSLATE:
  255. hCursor = AfxGetApp()->LoadCursor( IDC_TRANSLATE );
  256. SetCursor( hCursor );
  257. break;
  258. default:
  259. hCursor = AfxGetApp()->LoadCursor( IDC_ARROW );
  260. SetCursor( hCursor );
  261. break;
  262. }
  263. return TRUE;
  264. }
  265. void CChildView::OnOriginalModel() 
  266. {
  267. if (m_nRenderModel == 1)
  268. {
  269. m_nRenderModel = 0;
  270. DrawScene();
  271. }
  272. }
  273. void CChildView::OnUpdateOriginalModel(CCmdUI* pCmdUI) 
  274. {
  275. pCmdUI->SetCheck(m_nRenderModel == 0);
  276. }
  277. void CChildView::OnSimplifiedModel() 
  278. {
  279. if (m_nRenderModel == 0)
  280. {
  281. m_nRenderModel = 1;
  282. DrawScene();
  283. }
  284. }
  285. void CChildView::OnUpdateSimplifiedModel(CCmdUI* pCmdUI) 
  286. {
  287. pCmdUI->SetCheck(m_nRenderModel == 1);
  288. }
  289. void CChildView::OnWireframe() 
  290. {
  291. if (m_nRenderMode == 1)
  292. {
  293. m_nRenderMode = 0;
  294. DrawScene();
  295. }
  296. }
  297. void CChildView::OnUpdateWireframe(CCmdUI* pCmdUI) 
  298. {
  299. pCmdUI->SetCheck(m_nRenderMode == 0);
  300. }
  301. void CChildView::OnFlat() 
  302. {
  303. if (m_nRenderMode == 0)
  304. {
  305. m_nRenderMode = 1;
  306. DrawScene();
  307. }
  308. }
  309. void CChildView::OnUpdateFlat(CCmdUI* pCmdUI) 
  310. {
  311. pCmdUI->SetCheck(m_nRenderMode == 1);
  312. }
  313. void CChildView::OnOpenFile() 
  314. {
  315. CFileDialog loadmesh(TRUE,  "tm" , NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "tm文件(*.tm)|*.tm||");
  316. loadmesh.m_ofn.lpstrTitle ="打开模型文件";
  317. //SetCurrentDirectory(".\");
  318. if (loadmesh.DoModal() == IDOK )
  319. {
  320. CString filename;
  321. CString fileext;
  322. fileext  = loadmesh.GetFileExt();
  323. filename = loadmesh.GetPathName();
  324. if (!filename.IsEmpty()) {
  325. FILE *fp;
  326. fp = fopen(filename, "r");
  327. if (!fp) {
  328. AfxMessageBox("Can't open file to load");
  329. return;
  330. }
  331. if (!fileext.CompareNoCase("tm"))
  332. {
  333. if (m_pModel)
  334. delete m_pModel;
  335. m_pModel = new CMesh;
  336. HCURSOR hCursor, hOldCursor;
  337. hCursor = AfxGetApp()->LoadCursor( IDC_MY_WAIT );
  338. hOldCursor = SetCursor( hCursor );
  339. CMainFrame *pFrameWnd = (CMainFrame*)AfxGetMainWnd();
  340. pFrameWnd->m_Progress.SetPos(0);
  341. m_pModel->m_pProgress = &(pFrameWnd->m_Progress);
  342. m_nFaceNo = m_pModel->ReadData(fp);
  343. if (m_bAnimate)
  344. OnAnimate();
  345. m_spinAngle = 0.0;
  346. pFrameWnd->m_Progress.SetPos(0);
  347. m_pModel->m_pProgress = NULL;
  348. SetCursor(hOldCursor);
  349. }
  350. fclose(fp);
  351. CMainFrame *pFrameWnd = (CMainFrame*)AfxGetMainWnd();
  352. char tmps[80];
  353. sprintf(tmps,"原始模型:%d", m_nFaceNo);
  354. pFrameWnd->m_wndStatusBar.SetPaneText(1,tmps);
  355. m_nsFaceNo = m_nFaceNo;
  356. sprintf(tmps,"简化模型:%d", m_nsFaceNo);
  357. pFrameWnd->m_wndStatusBar.SetPaneText(2,tmps);
  358. DrawScene();
  359. }
  360. }
  361. }
  362. void CChildView::OnSaveFile() 
  363. {
  364. if (!m_pModel)
  365. return;
  366. CFileDialog savemesh(FALSE,  "tm" , NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "tm文件(*.tm)|*.tm||");
  367. savemesh.m_ofn.lpstrTitle ="保存模型文件";
  368. SetCurrentDirectory(".\Mesh");
  369. if (savemesh.DoModal() == IDOK )
  370. {
  371. CString filename;
  372. CString fileext;
  373. fileext  = savemesh.GetFileExt();
  374. filename = savemesh.GetPathName();
  375. if (!filename.IsEmpty()) {
  376. FILE *fp;
  377. fp = fopen(filename, "w");
  378. if (!fp) {
  379. AfxMessageBox("Can't open file to load");
  380. return;
  381. }
  382. if (!fileext.CompareNoCase("tm"))
  383. {
  384. HCURSOR hCursor, hOldCursor;
  385. hCursor = AfxGetApp()->LoadCursor( IDC_MY_WAIT );
  386. hOldCursor = SetCursor( hCursor );
  387. CMainFrame *pFrameWnd = (CMainFrame*)AfxGetMainWnd();
  388. pFrameWnd->m_Progress.SetPos(0);
  389. m_pModel->m_pProgress = &(pFrameWnd->m_Progress);
  390. m_pModel->SaveData(fp);
  391. pFrameWnd->m_Progress.SetPos(0);
  392. m_pModel->m_pProgress = NULL;
  393. SetCursor(hOldCursor);
  394. }
  395. fclose(fp);
  396. }
  397. }
  398. }
  399. void CChildView::OnUpdateSaveFile(CCmdUI* pCmdUI) 
  400. {
  401. pCmdUI->Enable(m_pModel != NULL);
  402. }
  403. void CChildView::OnSavePicture() 
  404. {
  405. CFileDialog savefile(FALSE, ".bmp", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "BMP文件(*.bmp)|*.bmp||"); 
  406. savefile.m_ofn.lpstrTitle = "存为BMP图象" ;
  407. if ( savefile.DoModal() == IDOK )
  408. {
  409. CString filename;
  410. char temp[80];
  411. filename = savefile.GetPathName();
  412. sprintf(temp,"%s",filename);
  413. SaveAsBMP(temp);
  414. }
  415. }
  416. void CChildView::OnSimplify() 
  417. {
  418. if (!m_pModel)
  419. return;
  420. HCURSOR hCursor, hOldCursor;
  421. CSimplifyDiag diag(m_nFaceNo);
  422. if (diag.DoModal() == IDOK)
  423. {
  424. hCursor = AfxGetApp()->LoadCursor( IDC_MY_WAIT );
  425. hOldCursor = SetCursor( hCursor );
  426. CMainFrame *pFrameWnd = (CMainFrame*)AfxGetMainWnd();
  427. pFrameWnd->m_Progress.SetPos(1);
  428. m_pModel->m_pProgress = &(pFrameWnd->m_Progress);
  429. if ((diag.m_nInputMode == 0)&&(diag.m_nFaceNo>0)&&(diag.m_nFaceNo<m_nFaceNo))
  430. m_nsFaceNo = m_pModel->Simplify(diag.m_nFaceNo);
  431. else if (diag.m_nInputMode == 1)
  432. m_nsFaceNo = m_pModel->Simplify(diag.m_fRate/100);
  433. if (m_nRenderModel == 1)
  434. DrawScene();
  435. pFrameWnd->m_Progress.SetPos(0);
  436. m_pModel->m_pProgress = NULL;
  437. char tmps[80];
  438. sprintf(tmps,"简化模型:%d", m_nsFaceNo);
  439. pFrameWnd->m_wndStatusBar.SetPaneText(2,tmps);
  440. SetCursor(hOldCursor);
  441. }
  442. }
  443. void CChildView::OnUpdateSimplify(CCmdUI* pCmdUI) 
  444. {
  445. pCmdUI->Enable(m_pModel != NULL);
  446. }
  447. void CChildView::OnCloseFile() 
  448. {
  449. if (m_pModel)
  450. delete m_pModel;
  451. m_pModel = NULL;
  452. DrawScene();
  453. }
  454. void CChildView::OnUpdateCloseFile(CCmdUI* pCmdUI) 
  455. {
  456. pCmdUI->Enable(m_pModel != NULL);
  457. }
  458. void CChildView::OnAxes() 
  459. {
  460. m_bAxes = !m_bAxes;
  461. DrawScene();
  462. }
  463. void CChildView::OnUpdateAxes(CCmdUI* pCmdUI) 
  464. {
  465. pCmdUI->SetCheck(m_bAxes);
  466. }
  467. void CChildView::OnAnimate() 
  468. {
  469. if (m_bAnimate)
  470. KillTimer(TIMER_ANIMATE);
  471. else
  472. SetTimer( TIMER_ANIMATE, ANIMATE_RATE, NULL );
  473. m_bAnimate = !m_bAnimate;
  474. }
  475. void CChildView::OnUpdateAnimate(CCmdUI* pCmdUI) 
  476. {
  477. pCmdUI->SetCheck(m_bAnimate);
  478. }
  479. void CChildView::OnMouseSpin() 
  480. {
  481. m_nMouseAct = MOUSE_SPIN;
  482. }
  483. void CChildView::OnUpdateMouseSpin(CCmdUI* pCmdUI) 
  484. {
  485. pCmdUI->SetCheck( m_nMouseAct == MOUSE_SPIN);
  486. }
  487. void CChildView::OnMouseTranslate() 
  488. {
  489. m_nMouseAct = MOUSE_TRANSLATE;
  490. }
  491. void CChildView::OnUpdateMouseTranslate(CCmdUI* pCmdUI) 
  492. {
  493. pCmdUI->SetCheck( m_nMouseAct == MOUSE_TRANSLATE);
  494. }
  495. void CChildView::OnMouseZoom() 
  496. {
  497. m_nMouseAct = MOUSE_ZOOM;
  498. }
  499. void CChildView::OnUpdateMouseZoom(CCmdUI* pCmdUI) 
  500. {
  501. pCmdUI->SetCheck( m_nMouseAct == MOUSE_ZOOM);
  502. }
  503. BOOL CChildView::SetThePixelFormat(CDC *pDC)
  504. {
  505.     static PIXELFORMATDESCRIPTOR pfd = {
  506. sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
  507. 1,                              // version number
  508. PFD_DRAW_TO_WINDOW |            // support window
  509. PFD_SUPPORT_OPENGL |            // support OpenGL
  510. PFD_DOUBLEBUFFER,               // double buffered
  511. PFD_TYPE_RGBA,                  // RGBA type
  512. 24,                             // 24-bit color depth
  513. 0, 0, 0, 0, 0, 0,               // color bits ignored
  514. 0,                              // no alpha buffer
  515. 0,                              // shift bit ignored
  516. 0,                              // no accumulation buffer
  517. 0, 0, 0, 0,                     // accum bits ignored
  518. 32,                             // 32-bit z-buffer
  519. 0,                              // no stencil buffer
  520. 0,                              // no auxiliary buffer
  521. PFD_MAIN_PLANE,                 // main layer
  522. 0,                              // reserved
  523. 0, 0, 0                         // layer masks ignored
  524.     };
  525. int pixelformat;
  526.     if((pixelformat = ChoosePixelFormat(m_hDc, &pfd)) == 0)
  527.     {
  528.         MessageBox( "ChoosePixelFormat failed", "Error", MB_OK);
  529.         return FALSE;
  530.     }
  531.     if(SetPixelFormat(m_hDc, pixelformat, &pfd) == FALSE)
  532.     {
  533.         MessageBox( "SetPixelFormat failed", "Error", MB_OK);
  534.         return FALSE;
  535.     }
  536.     CreateRGBPalette(m_hDc);
  537.     return TRUE;
  538. }
  539. void CChildView::MouseSpinGlobal(UINT nFlags, int x, int y, int init)
  540. {
  541. // use mouse to play 3D trackball transformation
  542. // see also Intel 3DR
  543. float centerX, centerY;
  544. float radius;
  545. float w, h;  // window
  546.     RECT rect;
  547.     GetClientRect( &rect);
  548.     w =(float)rect.right;
  549. h =(float)rect.bottom;
  550.     h = (h == 0.0f) ? 1.0f : h;
  551.     centerX = w / 2.0f;
  552. centerY = h / 2.0f;
  553. radius  = (w > h) ? centerY : centerX ;
  554. if (init)
  555. {
  556.  // And call Trackball() with current center, radius 
  557.  // and current cursor position (in client coordinates) 
  558.  // to initialize trackball internal parameters.
  559.  // Note that we must stop animation at that time.
  560. Trackball( 0, radius, centerX, centerY, (float)x, (float)(h-y), 
  561.            m_spinAxes[0], m_spinAxes[1], m_spinAxes[2], m_spinAngle);
  562. }
  563. else if ( nFlags & MK_LBUTTON )
  564. {
  565.     Trackball(1, radius, centerX, centerY, (float)x, (float)(h-y), 
  566.       m_spinAxes[0], m_spinAxes[1], m_spinAxes[2], m_spinAngle);
  567. SpinGlobal(m_spinAxes, m_spinAngle);
  568. }
  569. }
  570. void CChildView::SpinGlobal(float axes[], float angle)
  571. {
  572. EyeRotate(m_vProjectCenter, axes, angle, m_sEye );
  573. DrawScene(); 
  574. }
  575. void CChildView::MouseZoom(UINT nFlags, int x, int y)
  576. {
  577. float w, h;
  578. float scale;
  579. static float oldY, dy ;
  580. RECT rect;
  581. GetClientRect( &rect);
  582. w =(float)rect.right;
  583. h =(float)rect.bottom;
  584. h = (h == 0.0f) ? 1.0f : h;
  585. if( m_bLButtonDown && (nFlags & MK_LBUTTON))
  586. {
  587. dy=oldY-y;
  588. scale=1.0f+dy/h;
  589. Zoom(scale);
  590. oldY=(float)y;
  591. }
  592. else
  593. {
  594. oldY=(float)y;
  595. }
  596. }
  597. void CChildView::Zoom(float scale)
  598. {
  599. if(scale<0.001) return;
  600. m_fViewHeight/=scale;
  601. Project();
  602. DrawScene();
  603. }
  604. void CChildView::MouseTranslate(UINT nFlags, int x, int y)
  605. {
  606. float w, h;
  607. static float oldX, oldY, dx, dy, d[3] ;
  608. RECT rect;
  609. GetClientRect( &rect);
  610. w =(float)rect.right;
  611. h =(float)rect.bottom;
  612. h = (h == 0.0f) ? 1.0f : h;
  613. if( m_bLButtonDown && (nFlags & MK_LBUTTON))
  614. {
  615. dx=x-oldX;
  616. dy=oldY-y;
  617. d[0]=m_fViewWidth*dx/w*0.8f;
  618. d[1]=m_fViewHeight*dy/h*0.8f;           
  619. d[2]=0.0f;
  620. Translate(d);   
  621. oldX=(float)x;
  622. oldY=(float)y;
  623. }
  624. else
  625. {
  626. oldX=(float)x;
  627. oldY=(float)y;
  628. }
  629. }
  630. void CChildView::Translate(float d[])
  631. {
  632. EyeTranslate(d, m_sEye);
  633. DrawScene();
  634. }
  635. void CChildView::OnSpinXccw() 
  636. {
  637. KillTimer(TIMER_ANIMATE);
  638. glRotatef(ROTATE_RATE, 1.0f, 0.0f, 0.0f);
  639. DrawScene();
  640. }
  641. void CChildView::OnSpinXcw() 
  642. {
  643. KillTimer(TIMER_ANIMATE);
  644. MSG msg;
  645. while(::PeekMessage( &msg, m_hWnd,
  646. WM_TIMER, WM_TIMER,
  647. PM_REMOVE));
  648. glRotatef(-ROTATE_RATE, 1.0f, 0.0f, 0.0f);
  649. DrawScene();
  650. }
  651. void CChildView::OnSpinYccw() 
  652. {
  653. KillTimer(TIMER_ANIMATE);
  654. MSG msg;
  655. while(::PeekMessage( &msg, m_hWnd,
  656. WM_TIMER, WM_TIMER,
  657. PM_REMOVE));
  658. glRotatef(ROTATE_RATE, 0.0f, 1.0f, 0.0f);
  659. DrawScene();
  660. }
  661. void CChildView::OnSpinYcw() 
  662. {
  663. KillTimer(TIMER_ANIMATE);
  664. MSG msg;
  665. while(::PeekMessage( &msg, m_hWnd,
  666. WM_TIMER, WM_TIMER,
  667. PM_REMOVE));
  668. glRotatef(-ROTATE_RATE, 0.0f, 1.0f, 0.0f);
  669. DrawScene();
  670. }
  671. void CChildView::OnSpinZccw() 
  672. {
  673. KillTimer(TIMER_ANIMATE);
  674. MSG msg;
  675. while(::PeekMessage( &msg, m_hWnd,
  676. WM_TIMER, WM_TIMER,
  677. PM_REMOVE));
  678. glRotatef(ROTATE_RATE, 0.0f, 0.0f, 1.0f);
  679. DrawScene();
  680. }
  681. void CChildView::OnSpinZcw() 
  682. {
  683. KillTimer(TIMER_ANIMATE);
  684. MSG msg;
  685. while(::PeekMessage( &msg, m_hWnd,
  686. WM_TIMER, WM_TIMER,
  687. PM_REMOVE));
  688. glRotatef(-ROTATE_RATE, 0.0f, 0.0f, 1.0f);
  689. DrawScene();
  690. }
  691. void CChildView::OnZoomIn() 
  692. {
  693. KillTimer(TIMER_ANIMATE);
  694. MSG msg;
  695. while(::PeekMessage( &msg, m_hWnd,
  696. WM_TIMER, WM_TIMER,
  697. PM_REMOVE));
  698. Zoom(1.0f + ZOOM_RATE );
  699. }
  700. void CChildView::OnZoomOut() 
  701. {
  702. KillTimer(TIMER_ANIMATE);
  703. MSG msg;
  704. while(::PeekMessage( &msg, m_hWnd,
  705. WM_TIMER, WM_TIMER,
  706. PM_REMOVE));
  707. Zoom(1.0f - ZOOM_RATE );
  708. }
  709. void CChildView::Init()
  710. {
  711. CDC *pDC = GetDC();
  712. m_hDc = pDC->GetSafeHdc();
  713. SetThePixelFormat(pDC);
  714. m_hRc = wglCreateContext(m_hDc);
  715. wglMakeCurrent(m_hDc,m_hRc);
  716. ReleaseDC(pDC);
  717. glEnable(GL_DEPTH_TEST);
  718. glEnable(GL_LIGHTING);
  719. glEnable(GL_LIGHT0);
  720. glEnable(GL_AUTO_NORMAL);
  721. glColor3f(1.0f, 1.0f, 0.0f);
  722. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
  723. GLfloat light[] = {1.0f,1.0f,1.0f,1.0f};
  724. glLightfv(GL_LIGHT0,GL_DIFFUSE,light);
  725. float ambient[4] = {0.2f, 0.2f, 0.0f, 1.0f};
  726. float diffuse[4] = {0.95f, 0.95f, 0.0f, 1.0f};
  727. float specular[4] = {0.0f, 0.0f, 0.1f, 1.0f};
  728. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
  729. glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
  730. glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR,specular);
  731. SetTimer( TIMER_TOOLBAR, TOOLBAR_RATE, NULL);
  732. if (m_bAnimate)
  733. SetTimer( TIMER_ANIMATE, ANIMATE_RATE, NULL );
  734. CMainFrame *pFrameWnd = (CMainFrame*)AfxGetMainWnd();
  735. m_nsFaceNo = m_nFaceNo = 0;
  736. }
  737. void CChildView::DrawAxes()
  738. {
  739. glDisable(GL_LIGHTING);
  740. glMatrixMode(GL_MODELVIEW);
  741. glPushMatrix();
  742. glScalef(1.4f, 1.4f, 1.4f);
  743. glBegin(GL_LINES);
  744. // x axis
  745. glColor3f ( 1.0f, 0.0f, 0.0f);
  746. glVertex3f( 0.0f, 0.0f, 0.0f);
  747. glVertex3f( 1.0f, 0.0f, 0.0f);
  748. // y axis
  749. glColor3f ( 0.0f, 1.0f, 0.0f);
  750. glVertex3f( 0.0f, 0.0f, 0.0f);
  751. glVertex3f( 0.0f, 1.0f, 0.0f);
  752. // z axis
  753. glColor3f ( 0.0f, 0.0f, 1.0f);
  754. glVertex3f( 0.0f, 0.0f, 0.0f);
  755. glVertex3f( 0.0f, 0.0f, 1.0f);
  756. glEnd();
  757. glBegin(GL_TRIANGLES);
  758. // x axis arrow
  759. glColor3f ( 1.0f, 0.0f,  0.0f);
  760. glVertex3f(1.0f,    0.0f,    0.0f);
  761. glVertex3f(0.9f,    0.0f,    0.04f);
  762. glVertex3f(0.9f,    0.028f,  0.028f);
  763. glVertex3f(0.9f,    0.0f,    0.04f);
  764. glVertex3f(0.9f,    0.028f,  0.028f);
  765. glVertex3f(0.9f,    0.0f,    0.0f);
  766. glVertex3f(1.0f,    0.0f,    0.0f);
  767. glVertex3f(0.9f,    0.028f,  0.028f);
  768. glVertex3f(0.9f,    0.04f,   0.0f);
  769. glVertex3f(0.9f,    0.028f,  0.028f);
  770. glVertex3f(0.9f,    0.04f,   0.0f);
  771. glVertex3f(0.9f,    0.0f,    0.0f);
  772. glVertex3f(1.0f,    0.0f,    0.0f);
  773. glVertex3f(0.9f,    0.04f,   0.0f);
  774. glVertex3f(0.9f,    0.028f,  -0.028f);
  775. glVertex3f(0.9f,    0.04f,   0.0f);
  776. glVertex3f(0.9f,    0.028f,  -0.028f);
  777. glVertex3f(0.9f,    0.0f,    0.0f);
  778. glVertex3f(1.0f,    0.0f,    0.0f);
  779. glVertex3f(0.9f,    0.028f,  -0.028f);
  780. glVertex3f(0.9f,    0.0f,    -0.04f);
  781. glVertex3f(0.9f,    0.028f,  -0.028f);
  782. glVertex3f(0.9f,    0.0f,    -0.04f);
  783. glVertex3f(0.9f,    0.0f,    0.0f);
  784. glVertex3f(1.0f,    0.0f,    0.0f);
  785. glVertex3f(0.9f,    0.0f,    -0.04f);
  786. glVertex3f(0.9f,    -0.028f, -0.028f);
  787. glVertex3f(0.9f,    0.0f,    -0.04f);
  788. glVertex3f(0.9f,    -0.028f, -0.028f);
  789. glVertex3f(0.9f,    0.0f,    0.0f);
  790. glVertex3f(1.0f,    0.0f,    0.0f);
  791. glVertex3f(0.9f,    -0.028f, -0.028f);
  792. glVertex3f(0.9f,    -0.04f,  0.0f);
  793. glVertex3f(0.9f,    -0.028f, -0.028f);
  794. glVertex3f(0.9f,    -0.04f,  0.0f);
  795. glVertex3f(0.9f,    0.0f,    0.0f);
  796. glVertex3f(1.0f,    0.0f,    0.0f);
  797. glVertex3f(0.9f,    -0.04f,  0.0f);
  798. glVertex3f(0.9f,    -0.028f, 0.028f);
  799. glVertex3f(0.9f,    -0.04f,  0.0f);
  800. glVertex3f(0.9f,    -0.028f, 0.028f);
  801. glVertex3f(0.9f,    0.0f,    0.0f);
  802. glVertex3f(1.0f,    0.0f,    0.0f);
  803. glVertex3f(0.9f,    -0.028f, 0.028f);
  804. glVertex3f(0.9f,    0.0f,    0.04f);
  805. glVertex3f(0.9f,    -0.028f, 0.028f);
  806. glVertex3f(0.9f,    0.0f,    0.04f);
  807. glVertex3f(0.9f,    0.0f,    0.0f);
  808. // y axis arrow
  809. glColor3f ( 0.0f, 1.0f, 0.0f);
  810. glVertex3f(0.0f,    1.0f,    0.0f);
  811. glVertex3f(0.0f,    0.9f,    0.04f);
  812. glVertex3f(0.028f,  0.9f,    0.028f);
  813. glVertex3f(0.0f,    0.9f,    0.04f);
  814. glVertex3f(0.028f,  0.9f,    0.028f);
  815. glVertex3f(0.0f,    0.9f,    0.0f);
  816. glVertex3f(0.0f,    1.0f,    0.0f);
  817. glVertex3f(0.028f,  0.9f,    0.028f);
  818. glVertex3f(0.04f,   0.9f,    0.0f);
  819. glVertex3f(0.028f,  0.9f,    0.028f);
  820. glVertex3f(0.04f,   0.9f,    0.0f);
  821. glVertex3f(0.0f,    0.9f,    0.0f);
  822. glVertex3f(0.0f,    1.0f,    0.0f);
  823. glVertex3f(0.04f,   0.9f,    0.0f);
  824. glVertex3f(0.028f,  0.9f,    -0.028f);
  825. glVertex3f(0.04f,   0.9f,    0.0f);
  826. glVertex3f(0.028f,  0.9f,    -0.028f);
  827. glVertex3f(0.0f,    0.9f,    0.0f);
  828. glVertex3f(0.0f,    1.0f,    0.0f);
  829. glVertex3f(0.028f,  0.9f,    -0.028f);
  830. glVertex3f(0.0f,    0.9f,    -0.04f);
  831. glVertex3f(0.028f,  0.9f,    -0.028f);
  832. glVertex3f(0.0f,    0.9f,    -0.04f);
  833. glVertex3f(0.0f,    0.9f,    0.0f);
  834. glVertex3f(0.0f,    1.0f,    0.0f);
  835. glVertex3f(0.0f,    0.9f,    -0.04f);
  836. glVertex3f(-0.028f, 0.9f,    -0.028f);
  837. glVertex3f(0.0f,    0.9f,    -0.04f);
  838. glVertex3f(-0.028f, 0.9f,    -0.028f);
  839. glVertex3f(0.0f,    0.9f,    0.0f);
  840. glVertex3f(0.0f,    1.0f,    0.0f);
  841. glVertex3f(-0.028f, 0.9f,    -0.028f);
  842. glVertex3f(-0.04f,  0.9f,    0.0f);
  843. glVertex3f(-0.028f, 0.9f,    -0.028f);
  844. glVertex3f(-0.04f,  0.9f,    0.0f);
  845. glVertex3f(0.0f,    0.9f,    0.0f);
  846. glVertex3f(0.0f,    1.0f,    0.0f);
  847. glVertex3f(-0.04f,  0.9f,    0.0f);
  848. glVertex3f(-0.028f, 0.9f,    0.028f);
  849. glVertex3f(-0.04f,  0.9f,    0.0f);
  850. glVertex3f(-0.028f, 0.9f,    0.028f);
  851. glVertex3f(0.0f,    0.9f,    0.0f);
  852. glVertex3f(0.0f,    1.0f,    0.0f);
  853. glVertex3f(-0.028f, 0.9f,    0.028f);
  854. glVertex3f(0.0f,    0.9f,    0.04f);
  855. glVertex3f(-0.028f, 0.9f,    0.028f);
  856. glVertex3f(0.0f,    0.9f,    0.04f);
  857. glVertex3f(0.0f,    0.9f,    0.0f);
  858. // z axis arrow
  859. glColor3f ( 0.0f, 0.0f, 1.0f);
  860. glVertex3f(0.0f,    0.0f,    1.0f);
  861. glVertex3f(0.0f,    0.04f,   0.9f);
  862. glVertex3f(0.028f,  0.028f,  0.9f);
  863. glVertex3f(0.0f,    0.04f,   0.9f);
  864. glVertex3f(0.028f,  0.028f,  0.9f);
  865. glVertex3f(0.0f,    0.0f,    0.9f);
  866. glVertex3f(0.0f,    0.0f,    1.0f);
  867. glVertex3f(0.028f,  0.028f,  0.9f);
  868. glVertex3f(0.04f,   0.0f,    0.9f);
  869. glVertex3f(0.028f,  0.028f,  0.9f);
  870. glVertex3f(0.04f,   0.0f,    0.9f);
  871. glVertex3f(0.0f,    0.0f,    0.9f);
  872. glVertex3f(0.0f,    0.0f,    1.0f);
  873. glVertex3f(0.04f,   0.0f,    0.9f);
  874. glVertex3f(0.028f,  -0.028f, 0.9f);
  875. glVertex3f(0.04f,   0.0f,    0.9f);
  876. glVertex3f(0.028f,  -0.028f, 0.9f);
  877. glVertex3f(0.0f,    0.0f,    0.9f);
  878. glVertex3f(0.0f,    0.0f,    1.0f);
  879. glVertex3f(0.028f,  -0.028f, 0.9f);
  880. glVertex3f(0.0f,    -0.04f,  0.9f);
  881. glVertex3f(0.028f,  -0.028f, 0.9f);
  882. glVertex3f(0.0f,    -0.04f,  0.9f);
  883. glVertex3f(0.0f,    0.0f,    0.9f);
  884. glVertex3f(0.0f,    0.0f,    1.0f);
  885. glVertex3f(0.0f,    -0.04f,  0.9f);
  886. glVertex3f(-0.028f, -0.028f, 0.9f);
  887. glVertex3f(0.0f,    -0.04f,  0.9f);
  888. glVertex3f(-0.028f, -0.028f, 0.9f);
  889. glVertex3f(0.0f,    0.0f,    0.9f);
  890. glVertex3f(0.0f,    0.0f,    1.0f);
  891. glVertex3f(-0.028f, -0.028f, 0.9f);
  892. glVertex3f(-0.04f,  0.0f,    0.9f);
  893. glVertex3f(-0.028f, -0.028f, 0.9f);
  894. glVertex3f(-0.04f,  0.0f,    0.9f);
  895. glVertex3f(0.0f,    0.0f,    0.9f);
  896. glVertex3f(0.0f,    0.0f,    1.0f);
  897. glVertex3f(-0.04f,  0.0f,    0.9f);
  898. glVertex3f(-0.028f, 0.028f,  0.9f);
  899. glVertex3f(-0.04f,  0.0f,    0.9f);
  900. glVertex3f(-0.028f, 0.028f,  0.9f);
  901. glVertex3f(0.0f,    0.0f,    0.9f);
  902. glVertex3f(0.0f,    0.0f,    1.0f);
  903. glVertex3f(-0.028f, 0.028f,  0.9f);
  904. glVertex3f(0.0f,    0.04f,   0.9f);
  905. glVertex3f(-0.028f, 0.028f,  0.9f);
  906. glVertex3f(0.0f,    0.04f,   0.9f);
  907. glVertex3f(0.0f,    0.0f,    0.9f);
  908. glEnd();
  909. glPopMatrix();
  910. glEnable(GL_LIGHTING);
  911. glColor3f ( 1.0f, 1.0f, 0.0f);
  912. }
  913. void CChildView::SetupBmpHeader(BITMAPINFOHEADER *pbmih, int sx, int sy, int bpp)
  914. {
  915. pbmih->biSize = sizeof(BITMAPINFOHEADER);
  916. pbmih->biWidth = sx;
  917. pbmih->biHeight = sy;
  918. pbmih->biPlanes = 1;
  919. pbmih->biBitCount = bpp;
  920. pbmih->biCompression = BI_RGB;
  921. pbmih->biSizeImage = sx * sy * (bpp/8);
  922. pbmih->biXPelsPerMeter = 2925;
  923. pbmih->biYPelsPerMeter = 2925;
  924. pbmih->biClrUsed = 0;
  925. pbmih->biClrImportant = 0;
  926. }
  927. BOOL CChildView::SaveAsBMP(char *bmpFileName)
  928. {
  929. GLubyte  *lpBits;
  930. RECT  rect;
  931. GetClientRect(&rect);
  932. CDC *pDC = GetDC();
  933. ASSERT(pDC->m_hDC);
  934. // Creating Compatible Memory Device Context
  935. CDC *pMemDC = new CDC;
  936. if (!pMemDC->CreateCompatibleDC(pDC))
  937. {
  938. MessageBox("CompatibleDC Error");
  939. return 0;
  940. }
  941. // Preparing bitmap header for DIB section
  942. BITMAPINFO bi;
  943. int bpp, sx, sy;
  944. ZeroMemory(&bi, sizeof(BITMAPINFO));
  945. // Making a DIB image which is half size of GL window and full color(24 bpp)
  946. sx = (rect.right-rect.left) ;
  947. sy = (rect.bottom-rect.top) ;
  948. sx=sx-sx % 4;  // BMP 要求扫描线为 4 的倍数
  949.     bpp = 24;
  950. long dwSizeImage = sx * sy * bpp / 8; // 8 means 8 bit/byte
  951.     SetupBmpHeader(&(bi.bmiHeader), sx, sy, bpp);
  952. // Creating a DIB surface
  953. HBITMAP hBm, hBmOld;
  954.     hBm = CreateDIBSection(pDC->GetSafeHdc(), &bi, DIB_RGB_COLORS, 
  955.         (void **)&lpBits, NULL, (DWORD)0);
  956. if (!hBm)
  957. {
  958. MessageBox("CreateDIBSection Error");
  959. return 0;
  960. }
  961.     // Selecting the DIB Surface
  962. hBmOld = (HBITMAP)::SelectObject(pMemDC->GetSafeHdc(), hBm);
  963. if (!hBmOld)
  964. {
  965. MessageBox("Select Object Error");
  966. return 0;
  967. }
  968.     
  969. glReadBuffer(GL_BACK);
  970. glReadPixels(rect.left,rect.top,sx,//rect.right-rect.left,
  971. rect.bottom-rect.top,GL_BGR_EXT,GL_UNSIGNED_BYTE,lpBits);
  972. // Preparing BMP file header information
  973. BITMAPFILEHEADER bmfh;
  974. bmfh.bfType = 0x4d42;  // 'BM'
  975. long nSize =  sizeof(BITMAPINFOHEADER) +  dwSizeImage;
  976. bmfh.bfSize = nSize + sizeof(BITMAPFILEHEADER);
  977. bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
  978. bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  979.     CFile *pbmpFile;
  980.     CFileException fileException;
  981.     pbmpFile = new CFile;
  982.     if (!pbmpFile->Open(bmpFileName , 
  983.   CFile::modeCreate | CFile::modeWrite ) )
  984.     {
  985. TRACE( "Unable to open file: %sn, error = %un",
  986.         "" ,fileException.m_cause);
  987.     }
  988. // Wrinting the DIB image
  989. TRY 
  990. {
  991. pbmpFile->Write(&bmfh, sizeof(BITMAPFILEHEADER));
  992. pbmpFile->Write(&(bi.bmiHeader), sizeof(BITMAPINFOHEADER));
  993. pbmpFile->Write(lpBits,dwSizeImage);
  994. pbmpFile->Flush();
  995. }
  996. CATCH (CException, e) 
  997. {
  998. AfxMessageBox("write error");
  999. return 0;
  1000. }
  1001. END_CATCH
  1002. // Cleaning
  1003.     pbmpFile->Close();
  1004. delete pbmpFile;
  1005. hBm = (HBITMAP)::SelectObject(pMemDC->GetSafeHdc(), hBmOld);
  1006. DeleteObject(hBm);
  1007. delete pMemDC;
  1008. ReleaseDC(pDC);
  1009. return 1;
  1010. }
  1011. void CChildView::Project()
  1012. {
  1013. RECT rect;
  1014. GetClientRect( &rect);
  1015. GLsizei nWidth = rect.right;
  1016. GLsizei nHeight = rect.bottom;
  1017. nHeight = (nHeight == 0) ? 1 : nHeight;
  1018. float dAspect = (float)nWidth/(float)nHeight;
  1019. m_fViewWidth = m_fViewHeight * dAspect;
  1020. glMatrixMode(GL_PROJECTION);
  1021. glLoadIdentity();
  1022. glOrtho(-m_fViewWidth,m_fViewWidth,-m_fViewHeight,m_fViewHeight,m_fViewNear,m_fViewFar);
  1023. glViewport(0,0,nWidth,nHeight);
  1024. glMatrixMode(GL_MODELVIEW);
  1025. }
  1026. void CChildView::DrawScene()
  1027. {
  1028. int nTriangles = 0;
  1029. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1030. glMatrixMode(GL_MODELVIEW);
  1031. glPushMatrix();
  1032. if (m_pModel)
  1033. m_pModel->DrawModel(m_nRenderMode, m_nRenderModel);
  1034. if (m_bAxes)
  1035. DrawAxes();
  1036. glMatrixMode(GL_MODELVIEW);
  1037. glPopMatrix();
  1038. glFlush();
  1039. SwapBuffers(m_hDc);
  1040. }