Jhy3DView.cpp
上传用户:eehhbb
上传日期:2022-08-03
资源大小:2550k
文件大小:21k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // Jhy3DView.cpp : implementation of the CJhy3DView class
  2. //
  3. #include "stdafx.h"
  4. #include "Jhy3D.h"
  5. #include "glglut.h"
  6. #include "Jhy3DDoc.h"
  7. #include "Jhy3DView.h"
  8. #include "3DPolygon.h"
  9. #include "DlgLightSet.h"
  10. #include "DlgTeapotSet.h"
  11. #include "3DBezierSurface.h"
  12. #include <stdio.h>//对文件进行操作
  13. #include "Dlg3dBoxSizeSet.h"
  14. #include "Dlg3dBoxMaterialSet.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CJhy3DView
  22. IMPLEMENT_DYNCREATE(CJhy3DView, CView)
  23. BEGIN_MESSAGE_MAP(CJhy3DView, CView)
  24. //{{AFX_MSG_MAP(CJhy3DView)
  25. ON_WM_CREATE()
  26. ON_WM_SIZE()
  27. ON_WM_DESTROY()
  28. ON_COMMAND(ID_TRIANGLE, OnTriangle)
  29. ON_COMMAND(ID_POLYGON, OnPolygon)
  30. ON_COMMAND(ID_WirePolygon, OnWirePolygon)
  31. ON_COMMAND(ID_RENDER_WIRE_MODE, OnRenderWireMode)
  32. ON_COMMAND(ID_WireTeapot, OnWireTeapot)
  33. ON_WM_LBUTTONDOWN()
  34. ON_WM_CANCELMODE()
  35. ON_WM_MOUSEMOVE()
  36. ON_WM_CAPTURECHANGED()
  37. ON_WM_KEYDOWN()
  38. ON_WM_CHAR()
  39. ON_COMMAND(ID_RENDER_SOLID_MODE, OnRenderSolidMode)
  40. ON_COMMAND(ID_Pyramid, OnPyramid)
  41. ON_COMMAND(ID_LIGHTING_SET, OnLightingSet)
  42. ON_COMMAND(ID_3DBox, On3DBox)
  43. ON_COMMAND(ID_RENDER_Texture_Mode_Teapot, OnRender3DTextureModeTeapot)
  44. ON_COMMAND(ID_RenderLightSolid3dBox, OnRenderLightSolid3dBox)
  45. ON_COMMAND(ID_DRAW_3D_BEZIERSURFACE, OnDraw3dBeziersurface)
  46. ON_COMMAND(ID_RENDER_Texture_Mode_3dbox, OnRENDERTextureMode3dbox)
  47. ON_COMMAND(ID_SPHERE_MAP_MODE_TEAPOT, OnSphereMapModeTeapot)
  48. ON_COMMAND(ID_SPHERE_MAP_MODE_3dbox, OnSPHEREMAPMODE3dbox)
  49. ON_COMMAND(ID_DRAW_3D_NURBS_SURFACE, OnDraw3dNurbsSurface)
  50. //}}AFX_MSG_MAP
  51. // Standard printing commands
  52. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  53. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  54. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  55. END_MESSAGE_MAP()
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CJhy3DView construction/destruction
  58. CJhy3DView::CJhy3DView()
  59. {
  60. // TODO: add construction code here
  61.     m_nGraph=0;
  62. m_n3DRenderMode = 20;
  63. m_n3dRotate_x = m_n3dRotate_z = 0;
  64. m_n3dRotate_y = 180;
  65. }
  66. CJhy3DView::~CJhy3DView()
  67. {
  68. }
  69. BOOL CJhy3DView::PreCreateWindow(CREATESTRUCT& cs)
  70. {
  71. // TODO: Modify the Window class or styles here by modifying
  72. //  the CREATESTRUCT cs
  73. return CView::PreCreateWindow(cs);
  74. }
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CJhy3DView drawing
  77. void CJhy3DView::OnDraw(CDC* pDC)
  78. {
  79. CJhy3DDoc* pDoc = GetDocument();
  80. ASSERT_VALID(pDoc);
  81. // TODO: add draw code for native data here
  82. DrawOGLscene();
  83. }
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CJhy3DView printing
  86. BOOL CJhy3DView::OnPreparePrinting(CPrintInfo* pInfo)
  87. {
  88. // default preparation
  89. return DoPreparePrinting(pInfo);
  90. }
  91. void CJhy3DView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  92. {
  93. // TODO: add extra initialization before printing
  94. }
  95. void CJhy3DView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  96. {
  97. // TODO: add cleanup after printing
  98. }
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CJhy3DView diagnostics
  101. #ifdef _DEBUG
  102. void CJhy3DView::AssertValid() const
  103. {
  104. CView::AssertValid();
  105. }
  106. void CJhy3DView::Dump(CDumpContext& dc) const
  107. {
  108. CView::Dump(dc);
  109. }
  110. CJhy3DDoc* CJhy3DView::GetDocument() // non-debug version is inline
  111. {
  112. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CJhy3DDoc)));
  113. return (CJhy3DDoc*)m_pDocument;
  114. }
  115. #endif //_DEBUG
  116. /////////////////////////////////////////////////////////////////////////////
  117. // CJhy3DView message handlers
  118. bool CJhy3DView::InitOpenGL()
  119. {
  120. static PIXELFORMATDESCRIPTOR pfd= 
  121. {
  122. sizeof(PIXELFORMATDESCRIPTOR),//pfd的大小
  123. 1,                        //结构版本号
  124. PFD_DRAW_TO_WINDOW|       //支持窗口
  125. PFD_SUPPORT_OPENGL|       //支持OPENGL
  126. PFD_DOUBLEBUFFER,         //双缓存
  127. PFD_TYPE_RGBA,            //RGBA颜色
  128. 24,                       //24位颜色深度
  129. 0, 0, 0, 0, 0, 0,         //color bits ignored
  130. 0,                        //no alpha buffer
  131. 0,                        //shift bit ignored
  132. 0,                        //不使用累积缓存
  133. 0, 0, 0, 0,                //accum bits ignored
  134. 32,                        //32位z缓存
  135. 0,                         //不使用模板缓存
  136. 0,                         //no auxiliary buffer
  137. PFD_MAIN_PLANE,            //选择主层面
  138. 0,                          // 保留
  139. 0, 0, 0
  140. };
  141.      //得到客户设备上下文
  142. m_pDC = new CClientDC(this);
  143. if(m_pDC == NULL)
  144. {
  145. MessageBox("获得设备上下文失败!");
  146. return false;
  147. }
  148. int pixelFormat = ::ChoosePixelFormat(m_pDC -> GetSafeHdc(), &pfd);
  149. BOOL success = ::SetPixelFormat(m_pDC -> GetSafeHdc(), pixelFormat, &pfd);
  150. if(!success)
  151. return FALSE;
  152.  //创建绘图上下文RC
  153. m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
  154. if(m_hRC == 0)
  155. {
  156. MessageBox("绘图上下文RC创建失败!");
  157. return FALSE;
  158. }
  159. if(::wglMakeCurrent(m_pDC -> GetSafeHdc(), m_hRC) == FALSE)
  160. {
  161. MessageBox("关联当前RC失败!");
  162. return FALSE;
  163. }
  164. glShadeModel(GL_SMOOTH);             //启用广顺渲染
  165. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//黑色背景
  166.     return TRUE;
  167. }
  168. int CJhy3DView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  169. {
  170. if (CView::OnCreate(lpCreateStruct) == -1)
  171. return -1;
  172. // TODO: Add your specialized creation code here
  173. static PIXELFORMATDESCRIPTOR pfd= 
  174. {
  175. sizeof(PIXELFORMATDESCRIPTOR),//pfd的大小
  176. 1,                        //结构版本号
  177. PFD_DRAW_TO_WINDOW|       //支持窗口
  178. PFD_SUPPORT_OPENGL|       //支持OPENGL
  179. PFD_DOUBLEBUFFER,         //双缓存
  180. PFD_TYPE_RGBA,            //RGBA颜色
  181. 24,                       //24位颜色深度
  182. 0, 0, 0, 0, 0, 0,         //color bits ignored
  183. 0,                        //no alpha buffer
  184. 0,                        //shift bit ignored
  185. 0,                        //不使用累积缓存
  186. 0, 0, 0, 0,                //accum bits ignored
  187. 32,                        //32位z缓存
  188. 0,                         //不使用模板缓存
  189. 0,                         //no auxiliary buffer
  190. PFD_MAIN_PLANE,            //选择主层面
  191. 0,                          // 保留
  192. 0, 0, 0
  193. };
  194.      //得到客户设备上下文
  195. m_pDC = new CClientDC(this);
  196. if(m_pDC == NULL)
  197. {
  198. MessageBox("获得设备上下文失败!");
  199. return false;
  200. }
  201. int pixelFormat = ::ChoosePixelFormat(m_pDC -> GetSafeHdc(), &pfd);
  202. BOOL success = ::SetPixelFormat(m_pDC -> GetSafeHdc(), pixelFormat, &pfd);
  203. if(!success)
  204. return FALSE;
  205.  //创建绘图上下文RC
  206. m_hRC = ::wglCreateContext(m_pDC -> GetSafeHdc());
  207. if(m_hRC == 0)
  208. {
  209. MessageBox("绘图上下文RC创建失败!");
  210. return FALSE;
  211. }
  212. if(::wglMakeCurrent(m_pDC -> GetSafeHdc(), m_hRC) == FALSE)
  213. {
  214. MessageBox("关联当前RC失败!");
  215. return FALSE;
  216. }
  217. glShadeModel(GL_SMOOTH);             //启用广顺渲染
  218. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//黑色背景
  219.     return TRUE;
  220. return 0;
  221. }
  222. void CJhy3DView::OnSize(UINT nType, int cx, int cy) 
  223. {
  224. CView::OnSize(nType, cx, cy);
  225. // TODO: Add your message handler code here
  226. if(0 >= cx||0 >= cy)  return;
  227. //select the full client area
  228. //ModelView变换机制
  229. // gluPerspective(45.0f,2.0,1.0f,1000.0f);
  230. ::glViewport(0, 0, cx, cy);
  231. }
  232. void CJhy3DView::OnDestroy() 
  233. {
  234. CView::OnDestroy();
  235. // TODO: Add your message handler code here
  236. ::wglMakeCurrent(NULL, NULL);
  237. ::wglDeleteContext(m_hRC);
  238. if(m_pDC)
  239. {
  240. delete m_pDC;
  241. m_pDC = NULL;
  242. }
  243. }
  244. void CJhy3DView::DrawOGLscene()
  245. {
  246. glClearColor(0.4f,0.3f,0.4f,0.5f);
  247. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  248. gluLookAt(0,0,2,0,0,0,0,1,0);
  249. glLoadIdentity();
  250. glPushMatrix();
  251. glRotatef(m_n3dRotate_y,0.0,1.0,0.0);
  252. glRotatef(m_n3dRotate_x,1.0,0.0,0.0);
  253. glRotatef(m_n3dRotate_z,0.0,0.0,1.0);
  254. //在此处添加场景绘制代码如下
  255. if(m_nGraph==1)
  256. {
  257. //glShadeModel (GL_FLAT);
  258. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  259. glBegin(GL_TRIANGLES);//每三个顶点组成一个三角形
  260. glColor3f(1.0, 0.0, 0.0);//设置顶点颜色
  261. glVertex3f(-1.0, 0.0, 0.0);
  262. //顶点坐标
  263. glColor3f(0.0, 1.0, 0.0);
  264. glVertex3f(0.0, 1.0, 0.0);
  265. glColor3f(0.0, 0.0, 1.0);
  266. glVertex3f(1.0, 0.0, 0.0);
  267. glEnd();
  268. }
  269.  if(m_nGraph==2)
  270. {
  271. DrawPolygon();
  272. }
  273. if(m_nGraph==3)
  274. {
  275. //glShadeModel (GL_FLAT);
  276. m_3dpolygon.DrawWirePolygonwithOpenGL();
  277. }
  278. if(m_nGraph==4)
  279. {
  280. m_3dteapot.DrawWireTeapot();
  281. }
  282. if(m_nGraph ==5)
  283. {
  284. DrawPyramid();
  285. }
  286. if(m_nGraph ==6)
  287. {
  288. m_3dbox.Draw3dBox();
  289. }
  290. if(m_nGraph ==7)
  291. {
  292. if(m_n3DRenderMode == 1)
  293. {
  294. m_3dteapot.RenderLightSolidTeapot();
  295. m_3dbezierSuf.RenderTextureSolidBeizerSurface();
  296. }
  297. else if (m_n3DRenderMode == 0)
  298. {
  299. m_3dteapot.RenderWireTeapot();
  300. m_3dnurbsSuf.RenderWireNurbsSurface();
  301. }
  302. else if (m_n3DRenderMode == 2)
  303. {
  304. m_3dbox.RenderLightSolid3dBox();
  305. }
  306.     else if (m_n3DRenderMode == 3)//纹理映射茶壶
  307. {
  308. m_3dteapot.RenderLightSolidTeapot();
  309. }
  310. else if (m_n3DRenderMode == 4)//纹理映射长方体
  311. {
  312. m_3dbox.Draw3dBox();
  313. }
  314. else if (m_n3DRenderMode == 5)//环境映射茶壶
  315. {
  316. glEnable(GL_TEXTURE_GEN_S); 
  317. glEnable(GL_TEXTURE_GEN_T);
  318. m_3dteapot.RenderLightSolidTeapot();
  319. }
  320. else if (m_n3DRenderMode == 6)//环境映射长方体
  321. {
  322. glEnable(GL_TEXTURE_GEN_S); 
  323. glEnable(GL_TEXTURE_GEN_T);
  324. m_3dbox.Draw3dBox();
  325. }
  326. }
  327. glPopMatrix();
  328. glFinish();
  329. SwapBuffers(m_pDC -> GetSafeHdc());
  330. }
  331. void CJhy3DView::OnTriangle() //菜单响应
  332. {
  333. // TODO: Add your command handler code here
  334. m_nGraph=1;
  335. Invalidate();
  336. }
  337. void CJhy3DView::OnPolygon() //菜单响应
  338. {
  339. // TODO: Add your command handler code here
  340. m_nGraph=2;
  341. Invalidate();
  342. }
  343. //画多边形
  344. void CJhy3DView::DrawPolygon()
  345. {
  346.     glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  347. glDisable(GL_TEXTURE_2D);
  348. glBegin(GL_POLYGON); 
  349. glColor3f(0.8,0.0f,0.0f); 
  350. glVertex3f(-1.0,0.0,0.0); 
  351. glColor3f(0.3, 1.0, 0.0);
  352. glVertex3f(0.0,1.0,0.0);
  353. glColor3f(0.0, 0.2, 0.2);
  354. glVertex3f(1.0,0.0,0.0); 
  355. glColor3f(0.0, 0.5, 0.5);
  356. glVertex3f(0.1,-0.7,0.8);
  357. // glColor3f(0.2, 0.3, 0.5);
  358. // glVertex3f(0.1,-0.3,0.5);
  359. glEnd(); 
  360. }
  361. void CJhy3DView::OnWirePolygon() 
  362. {
  363. // TODO: Add your command handler code here
  364. m_nGraph=3;
  365. Invalidate();
  366. }
  367. void CJhy3DView::OnRenderWireMode() 
  368. {
  369. // TODO: Add your command handler code here
  370. m_nGraph=7;
  371. m_n3DRenderMode = 0;
  372. CloseLight();
  373. glDisable(GL_TEXTURE_2D);
  374. Invalidate(FALSE);
  375. }
  376. void CJhy3DView::OnWireTeapot() 
  377. {
  378. // TODO: Add your command handler code here
  379. CDlgTeapotSet theDlg;
  380. if(theDlg.DoModal() == IDOK)
  381. {
  382. m_3dteapot.m_matTeapot.m_vAmbient[0] = atof(theDlg.m_dlg_Ambient0);
  383. m_3dteapot.m_matTeapot.m_vAmbient[1] = atof(theDlg.m_dlg_Ambient1);
  384. m_3dteapot.m_matTeapot.m_vAmbient[2] = atof(theDlg.m_dlg_Ambient2);
  385. m_3dteapot.m_matTeapot.m_vAmbient[3] = 1.0f;
  386. m_3dteapot.m_matTeapot.m_vDiffuse[0] = atof(theDlg.m_dlg_Diffuse0);
  387. m_3dteapot.m_matTeapot.m_vDiffuse[1] = atof(theDlg.m_dlg_Diffuse1);
  388. m_3dteapot.m_matTeapot.m_vDiffuse[2] = atof(theDlg.m_dlg_Diffuse2);
  389. m_3dteapot.m_matTeapot.m_vDiffuse[3] = 1.0f;
  390. m_3dteapot.m_matTeapot.m_vSpecular[0] = atof(theDlg.m_dlg_Specular0);
  391. m_3dteapot.m_matTeapot.m_vSpecular[1] = atof(theDlg.m_dlg_Specular1);
  392. m_3dteapot.m_matTeapot.m_vSpecular[2] = atof(theDlg.m_dlg_Specular2);
  393. m_3dteapot.m_matTeapot.m_vSpecular[3] = 1.0f;
  394. m_3dteapot.m_matTeapot.m_vEmission[0] = atof(theDlg.m_dlg_Emission0);
  395. m_3dteapot.m_matTeapot.m_vEmission[1] = atof(theDlg.m_dlg_Emission1);
  396. m_3dteapot.m_matTeapot.m_vEmission[2] = atof(theDlg.m_dlg_Emission2);
  397. m_3dteapot.m_matTeapot.m_vEmission[3] = 1.0f;
  398. m_3dteapot.m_matTeapot.m_vShininess = atof(theDlg.m_dlg_Shininess);
  399. m_3dteapot.m_position.m_x = theDlg.m_teapot_tx;
  400. m_3dteapot.m_position.m_y = theDlg.m_teapot_ty;
  401. m_3dteapot.m_position.m_z = theDlg.m_teapot_tz;
  402. m_3dteapot.m_fRoundRadius = theDlg.m_teapot_radius;
  403. }
  404. m_nGraph=4;
  405. Invalidate();
  406. }
  407. void CJhy3DView::OnLButtonDown(UINT nFlags, CPoint point) 
  408. {
  409. // TODO: Add your message handler code here and/or call default
  410. m_TempMovePos = point;
  411. CView::OnLButtonDown(nFlags, point);
  412. }
  413. void CJhy3DView::OnCancelMode() 
  414. {
  415. CView::OnCancelMode();
  416. // TODO: Add your message handler code here
  417. }
  418. void CJhy3DView::OnMouseMove(UINT nFlags, CPoint point) 
  419. {
  420. // TODO: Add your message handler code here and/or call default
  421. if(nFlags == MK_LBUTTON)
  422. {
  423. m_n3dRotate_y+=point.x - m_TempMovePos.x;
  424. m_n3dRotate_x+=m_TempMovePos.y - point.y;
  425. m_TempMovePos = point;
  426. Invalidate(FALSE);
  427. }
  428. CView::OnMouseMove(nFlags, point);
  429. }
  430. void CJhy3DView::OnCaptureChanged(CWnd *pWnd) 
  431. {
  432. // TODO: Add your message handler code here
  433. CView::OnCaptureChanged(pWnd);
  434. }
  435. void CJhy3DView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  436. {
  437. // TODO: Add your message handler code here and/or call default
  438. switch(nChar)
  439. {
  440. case 'z':
  441. case 'Z':
  442. m_n3dRotate_z+=5.0f;
  443. Invalidate(FALSE);
  444. break;
  445. case 'c':
  446. case 'C':
  447. m_n3dRotate_z-=5.0f;
  448. Invalidate(FALSE);
  449. }
  450. CView::OnKeyDown(nChar, nRepCnt, nFlags);
  451. }
  452. void CJhy3DView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  453. {
  454. // TODO: Add your message handler code here and/or call default
  455. CView::OnChar(nChar, nRepCnt, nFlags);
  456. }
  457. void CJhy3DView::OnRenderSolidMode() 
  458. {
  459. // TODO: Add your command handler code here
  460. m_nGraph=7;
  461. m_n3DRenderMode = 1;
  462. OpenLight();
  463. glDisable(GL_TEXTURE_2D);
  464. Invalidate(FALSE);
  465. }
  466. void CJhy3DView::OpenLight()
  467. {
  468. glLightfv(GL_LIGHT0,GL_AMBIENT,m_light.m_vAmbient);
  469. glLightfv(GL_LIGHT0,GL_DIFFUSE,m_light.m_vDiffuse);
  470. glLightfv(GL_LIGHT0,GL_SPECULAR,m_light.m_vSpecular);
  471. glLightfv(GL_LIGHT0,GL_POSITION,m_light.m_vPosition);
  472. glEnable(GL_DEPTH_TEST);
  473. glEnable(GL_LIGHTING);
  474. glEnable(GL_LIGHT0);
  475. }
  476. void CJhy3DView::CloseLight()
  477. {
  478. glDisable(GL_DEPTH_TEST);
  479. glDisable(GL_LIGHTING);
  480. glDisable(GL_LIGHT0);
  481. }
  482. void CJhy3DView::OnPyramid() 
  483. {
  484. // TODO: Add your command handler code here
  485. m_nGraph=5;
  486. Invalidate();
  487. }
  488. void CJhy3DView::DrawPyramid()
  489. {
  490. // glTranslatef(0.0f,0.0f,0.0f);
  491. glBegin(GL_TRIANGLES);
  492. glColor3f(0.5f,0.0f,0.0f); // 红色
  493. glVertex3f( 0.0f, 0.5f, 0.0f); // 三角形的上顶点 (前侧面)
  494. glColor3f(0.0f,0.5f,0.0f); // 绿色
  495. glVertex3f(-0.5f,-0.5f, 0.5f); // 三角形的左下顶点 (前侧面)
  496. glColor3f(0.0f,0.0f,0.5f); // 蓝色
  497. glVertex3f( 0.5f,-0.5f, 0.5f); // 三角形的右下顶点 (前侧面)
  498. glColor3f(0.5f,0.0f,0.0f); // 红色
  499. glVertex3f( 0.0f, 0.5f, 0.0f); // 三角形的上顶点 (右侧面)
  500. glColor3f(0.0f,0.0f,0.5f); // 蓝色
  501. glVertex3f( 0.5f,-0.5f, 0.5f); // 三角形的左下顶点 (右侧面)
  502. glColor3f(0.0f,0.5f,0.0f); // 绿色
  503. glVertex3f( 0.5f,-0.5f, -0.5f); // 三角形的右下顶点 (右侧面)
  504. glColor3f(0.5f,0.0f,0.0f); // 红色
  505. glVertex3f( 0.0f, 0.5f, 0.0f); // 三角形的上顶点 (后侧面)
  506. glColor3f(0.0f,0.5f,0.0f); // 绿色
  507. glVertex3f( 0.5f,-0.5f, -0.5f); // 三角形的左下顶点 (后侧面)
  508. glColor3f(0.0f,0.0f,0.5f); // 蓝色
  509. glVertex3f(-0.5f,-0.5f, -0.5f); // 三角形的右下顶点 (后侧面)
  510. glColor3f(0.5f,0.0f,0.0f); // 红色
  511. glVertex3f( 0.0f, 0.5f, 0.0f); // 三角形的上顶点 (左侧面)
  512. glColor3f(0.0f,0.0f,0.5f);// 蓝色
  513. glVertex3f(-0.5f,-0.5f,-0.5f); // 三角形的左下顶点 (左侧面)
  514. glColor3f(0.0f,0.5f,0.0f); // 绿色
  515. glVertex3f(-0.5f,-0.5f, 0.5f); // 三角形的右下顶点 (左侧面)
  516. glEnd(); //金字塔绘制结束
  517. }
  518. void CJhy3DView::OnLightingSet() 
  519. {
  520. // TODO: Add your command handler code here
  521. CDlgLightSet dlg;
  522. if (dlg.DoModal() == IDOK)
  523. {
  524. m_light.m_vAmbient[0] = atof(dlg.m_dlg_Ambient0);
  525. m_light.m_vAmbient[1] = atof(dlg.m_dlg_Ambient1);
  526. m_light.m_vAmbient[2] = atof(dlg.m_dlg_Ambient2);
  527. m_light.m_vAmbient[3] = 1.0f;
  528. m_light.m_vDiffuse[0] = atof(dlg.m_dlg_Diffuse0);
  529. m_light.m_vDiffuse[1] = atof(dlg.m_dlg_Diffuse1);
  530. m_light.m_vDiffuse[2] = atof(dlg.m_dlg_Diffuse2);
  531. m_light.m_vDiffuse[3] = 1.0f;
  532. m_light.m_vSpecular[0] = atof(dlg.m_dlg_Specular0);
  533. m_light.m_vSpecular[1] = atof(dlg.m_dlg_Specular1);
  534. m_light.m_vSpecular[2] = atof(dlg.m_dlg_Specular2);
  535. m_light.m_vSpecular[3] = 1.0f;
  536. m_light.m_vPosition[0] = atof(dlg.m_dlg_Position0);
  537. m_light.m_vPosition[1] = atof(dlg.m_dlg_Position1);
  538. m_light.m_vPosition[2] = atof(dlg.m_dlg_Position2);
  539. m_light.m_vPosition[3] = 1.0f;
  540. }
  541. }
  542. void CJhy3DView::On3DBox() 
  543. {
  544. CDlg3dBoxSizeSet dlg;
  545. if (dlg.DoModal() == IDOK)
  546. {
  547. m_3dbox.m_vBox[0].m_x = dlg.m_3dboxx0;
  548. m_3dbox.m_vBox[0].m_y = dlg.m_3dboxy0;
  549. m_3dbox.m_vBox[0].m_z = dlg.m_3dboxz0;
  550. m_3dbox.m_vBox[1].m_x = dlg.m_3dboxx1;
  551. m_3dbox.m_vBox[1].m_y = dlg.m_3dboxy1;
  552. m_3dbox.m_vBox[1].m_z = dlg.m_3dboxz1;
  553. }
  554. m_nGraph=6;
  555. Invalidate();
  556. }
  557. void CJhy3DView::OnRenderLightSolid3dBox() 
  558. {
  559. CDlg3dBoxMaterialSet theDlg;
  560. if(theDlg.DoModal() == IDOK)
  561. {
  562. m_3dbox.m_matBox.m_vAmbient[0] = atof(theDlg.m_dlg_ambient0);
  563. m_3dbox.m_matBox.m_vAmbient[1] = atof(theDlg.m_dlg_ambient1);
  564. m_3dbox.m_matBox.m_vAmbient[2] = atof(theDlg.m_dlg_ambient2);
  565. m_3dbox.m_matBox.m_vAmbient[3] = 1.0f;
  566. m_3dbox.m_matBox.m_vDiffuse[0] = atof(theDlg.m_dlg_diffuse0);
  567. m_3dbox.m_matBox.m_vDiffuse[1] = atof(theDlg.m_dlg_diffuse1);
  568. m_3dbox.m_matBox.m_vDiffuse[2] = atof(theDlg.m_dlg_diffuse2);
  569. m_3dbox.m_matBox.m_vDiffuse[3] = 1.0f;
  570. m_3dbox.m_matBox.m_vSpecular[0] = atof(theDlg.m_dlg_specular0);
  571. m_3dbox.m_matBox.m_vSpecular[1] = atof(theDlg.m_dlg_specular1);
  572. m_3dbox.m_matBox.m_vSpecular[2] = atof(theDlg.m_dlg_specular2);
  573. m_3dbox.m_matBox.m_vSpecular[3] = 1.0f;
  574. m_3dbox.m_matBox.m_vEmission[0] = atof(theDlg.m_dlg_emission0);
  575. m_3dbox.m_matBox.m_vEmission[1] = atof(theDlg.m_dlg_emission1);
  576. m_3dbox.m_matBox.m_vEmission[2] = atof(theDlg.m_dlg_emission2);
  577. m_3dbox.m_matBox.m_vEmission[3] = 1.0f;
  578. m_3dbox.m_matBox.m_vShininess = atof(theDlg.m_dlg_shininess);
  579. }
  580. m_nGraph=7;
  581. m_n3DRenderMode = 2;
  582. OpenLight();
  583. glDisable(GL_TEXTURE_2D);
  584. Invalidate(FALSE);
  585. }
  586. AUX_RGBImageRec *CJhy3DView::LoadBMP(char *filename)     // 载入位图图象
  587. {
  588. FILE *file = NULL;
  589. if((file = fopen(filename,"r")) != NULL)
  590. {
  591. fclose(file);
  592. return auxDIBImageLoad(filename); 
  593. }
  594. return NULL;
  595. }
  596. int CJhy3DView::LoadGLTextures()
  597. {
  598. int status = FALSE;//Status 的变量用来跟踪是否能够载入位图以及能否创建纹理。 Status 缺省设为 FALSE (表示没有载入或创建任何东东)。
  599. AUX_RGBImageRec *TextureImage[1]; // 创建纹理的存储空间
  600. memset(TextureImage,0,sizeof(void*)*1);// 将指针设为 NULL
  601. if(TextureImage[0] = LoadBMP("test.bmp"))
  602. {
  603. status = TRUE;
  604. glGenTextures(1,&m_texture[0]);//生成一个纹理名字
  605. glBindTexture(GL_TEXTURE_2D,m_texture[0]);//将纹理名字 texture[0] 绑定到纹理目标上...指定内存
  606. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);//滤波方式
  607. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//滤波方式
  608. glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage[0]->sizeX,TextureImage[0]->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage[0]->data);
  609. }
  610. if(TextureImage[0] && TextureImage[0]->data)// 纹理是否存在 // 纹理图像是否存在
  611. {
  612. free(TextureImage[0]->data);// 释放纹理图像占用的内存
  613. free(TextureImage[0]);      // 释放图像结构
  614. }
  615. return status;
  616. }
  617. void CJhy3DView::OnRender3DTextureModeTeapot() 
  618. {
  619. m_nGraph=7;
  620. m_n3DRenderMode = 3;
  621. if(!glIsTexture(m_texture[0]))
  622. LoadGLTextures();
  623. glEnable(GL_DEPTH_TEST);
  624. glEnable(GL_TEXTURE_2D);
  625. Invalidate(FALSE);
  626. }
  627. void CJhy3DView::OnRENDERTextureMode3dbox() 
  628. {
  629.     m_nGraph=7;
  630. m_n3DRenderMode = 4;
  631. AfxMessageBox("请设置长方体的大小:绘图->长方体");
  632. if(!glIsTexture(m_texture[0]))
  633. LoadGLTextures();
  634. glEnable(GL_DEPTH_TEST);
  635. glEnable(GL_TEXTURE_2D);
  636. Invalidate(FALSE);
  637. }
  638. //茶壶环境映射
  639. void CJhy3DView::OnSphereMapModeTeapot() 
  640. {
  641. m_nGraph=7;
  642. m_n3DRenderMode = 5;
  643. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); // Set The Texture Generation Mode For S To Sphere Mapping (NEW)
  644. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); // Set The Texture Generation Mode For T To Sphere Mapping (NEW)
  645. if(!glIsTexture(m_texture[0]))
  646. LoadGLTextures();
  647. glEnable(GL_DEPTH_TEST);
  648. glEnable(GL_TEXTURE_2D);
  649. }
  650. //长方体环境映射
  651. void CJhy3DView::OnSPHEREMAPMODE3dbox() 
  652. {
  653. m_nGraph=7;
  654. m_n3DRenderMode = 6;
  655. AfxMessageBox("请设置长方体的大小:绘图->长方体");
  656. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); // Set The Texture Generation Mode For S To Sphere Mapping (NEW)
  657. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); // Set The Texture Generation Mode For T To Sphere Mapping (NEW)
  658. if(!glIsTexture(m_texture[0]))
  659. LoadGLTextures();
  660. glEnable(GL_DEPTH_TEST);
  661. glEnable(GL_TEXTURE_2D);
  662. }
  663. void CJhy3DView::OnDraw3dBeziersurface() 
  664. {
  665. // m_n3DRenderMode = 7;
  666. m_3dbezierSuf.DefaultBezierSuf();
  667. Invalidate();
  668. }
  669. void CJhy3DView::OnDraw3dNurbsSurface() 
  670. {
  671. m_3dnurbsSuf.DefaultNurbsSuf();
  672. Invalidate();
  673. }