07063257_GLView.cpp
上传用户:kuqidezhu
上传日期:2022-07-18
资源大小:4151k
文件大小:23k
源码类别:

3D图形编程

开发平台:

Visual C++

  1. // 07063257_GLView.cpp : implementation of the CMy07063257_GLView class
  2. //
  3. #include "stdafx.h"
  4. #include "07063257_GL.h"
  5. #include "07063257_GLDoc.h"
  6. #include "07063257_GLView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CMy07063257_GLView
  14. IMPLEMENT_DYNCREATE(CMy07063257_GLView, CView)
  15. BEGIN_MESSAGE_MAP(CMy07063257_GLView, CView)
  16. //{{AFX_MSG_MAP(CMy07063257_GLView)
  17. ON_COMMAND(ID_ADDDATA, OnAdddate)
  18. ON_COMMAND(ID_TieTu, OnTieTu)
  19. ON_WM_CREATE()
  20. ON_WM_DESTROY()
  21. ON_WM_PAINT()
  22. ON_WM_SIZE()
  23. ON_COMMAND(ID_LEFT, OnLeft)
  24. ON_COMMAND(ID_RIGHT, OnRight)
  25. ON_COMMAND(ID_UP, OnUp)
  26. ON_COMMAND(ID_DOWN, OnDown)
  27. ON_COMMAND(ID_LROTAEF, OnLrotaef)
  28. ON_COMMAND(ID_RROTATEF, OnRrotatef)
  29. ON_COMMAND(ID_ZOOMOUT, OnZoomout)
  30. ON_COMMAND(ID_ZOOMIN, OnZoomin)
  31. ON_COMMAND(ID_FRONT, OnFront)
  32. ON_COMMAND(ID_BACK, OnBack)
  33. //}}AFX_MSG_MAP
  34. // Standard printing commands
  35. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  36. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  37. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CMy07063257_GLView construction/destruction
  41. CMy07063257_GLView::CMy07063257_GLView()
  42. {
  43. // TODO: add construction code here
  44.     m_Rotateangle=0;
  45. MAX=624;
  46. IsOpen=0;
  47. n=0;
  48. }
  49. CMy07063257_GLView::~CMy07063257_GLView()
  50. {
  51. }
  52. BOOL CMy07063257_GLView::PreCreateWindow(CREATESTRUCT& cs)
  53. {
  54. // TODO: Modify the Window class or styles here by modifying
  55. //  the CREATESTRUCT cs
  56. cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS); 
  57. return CView::PreCreateWindow(cs);
  58. }
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CMy07063257_GLView drawing
  61. void CMy07063257_GLView::OnDraw(CDC* pDC)
  62. {
  63. CMy07063257_GLDoc* pDoc = GetDocument();
  64. ASSERT_VALID(pDoc);
  65. CClientDC dc(this);
  66. // TODO: add draw code for native data here
  67. }
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CMy07063257_GLView printing
  70. BOOL CMy07063257_GLView::OnPreparePrinting(CPrintInfo* pInfo)
  71. {
  72. // default preparation
  73. return DoPreparePrinting(pInfo);
  74. }
  75. void CMy07063257_GLView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  76. {
  77. // TODO: add extra initialization before printing
  78. }
  79. void CMy07063257_GLView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  80. {
  81. // TODO: add cleanup after printing
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CMy07063257_GLView diagnostics
  85. #ifdef _DEBUG
  86. void CMy07063257_GLView::AssertValid() const
  87. {
  88. CView::AssertValid();
  89. }
  90. void CMy07063257_GLView::Dump(CDumpContext& dc) const
  91. {
  92. CView::Dump(dc);
  93. }
  94. CMy07063257_GLDoc* CMy07063257_GLView::GetDocument() // non-debug version is inline
  95. {
  96. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy07063257_GLDoc)));
  97. return (CMy07063257_GLDoc*)m_pDocument;
  98. }
  99. #endif //_DEBUG
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CMy07063257_GLView message handlers
  102. void CMy07063257_GLView::OnAdddate() 
  103. {
  104. twc=1;
  105. // TODO: Add your command handler code here
  106. Invalidate(true);
  107. CAddData d1;
  108. CChooseTexture d2;
  109. BeginWaitCursor();
  110. if (d1.DoModal()==1)
  111. {
  112. FILE  *mm_vertexFile;
  113. CFileDialog location(true);
  114. location.DoModal();
  115. mm_vertexFile=fopen(location.GetPathName(),"r");
  116.     MAXX=0.0;
  117.         MINX=10.0;
  118.         MAXY=0.0;
  119.     MINY=10.0;
  120. for (int i=0;i<MAX;i++) 
  121. {
  122. float a,b,c;
  123. a=b=c=0.0;
  124. m_vertex[i].x=0.0;
  125. fscanf(mm_vertexFile,"%f%f%f",&a ,&b,&c);
  126. (m_vertex[i].x)=(a-87500)*1.5;
  127. // (m_vertex[i].x)=(a-87500)*1.5 ;
  128. (m_vertex[i].y)=(b-12800)*1.5;
  129. // (m_vertex[i].y)=(b-12800);
  130. (m_vertex[i].z)=c*1.5;
  131. if (m_vertex[i].x>MAXX) {MAXX=m_vertex[i].x;}
  132. if (m_vertex[i].x<MINX) {MINX=m_vertex[i].x;}
  133.             if (m_vertex[i].y<MINY) {MINY=m_vertex[i].y;}
  134. if (m_vertex[i].y>MAXY) {MAXY=m_vertex[i].y;}
  135. }
  136. fclose(mm_vertexFile);
  137. glClear(GL_COLOR_BUFFER_BIT);
  138. glClearColor(0.0,0.0,0.0,1.0);
  139. // glTranslatef(0,0,-2);
  140. glShadeModel(GL_SMOOTH );
  141. glTranslatef( 0, 0,-500);
  142. glPointSize(2);
  143. glColor4f(0,0.4,1,1);
  144. BuildTIN();
  145. }
  146. EndWaitCursor();
  147. }
  148. void CMy07063257_GLView::OnTieTu() 
  149. {
  150. twc=2;
  151. // TODO: Add your command handler code here
  152. n=1;
  153. CChooseTexture d2;
  154. d2.DoModal();
  155. CFileDialog mOpen(true);
  156. if (mOpen.DoModal()==IDOK)
  157. {
  158. AUX_RGBImageRec *Image=auxDIBImageLoad(mOpen.GetPathName());
  159. glTexImage2D(GL_TEXTURE_2D, 0, 3, Image->sizeX,Image->sizeY, 
  160. 0, GL_RGB, GL_UNSIGNED_BYTE,Image->data);
  161. }
  162. }
  163. void CMy07063257_GLView::BuildTIN()
  164. {
  165. if (MAX > 2)
  166. {
  167. for(int i=0;i<MAX-1;i++)          //确定基线
  168. {
  169. // glBegin(GL_LINE_LOOP);
  170. // glVertex3f(m_vertex[i].x,m_vertex[i].y,m_vertex[i].z);
  171. // glEnd();
  172. for(int j=i+1;j<MAX;j++)
  173. {
  174. if(m_vertex[i].x>m_vertex[j].x)
  175. {
  176. Vertex temp;
  177. temp.x=m_vertex[i].x;
  178. temp.y=m_vertex[i].y;
  179. temp.z=m_vertex[i].z;
  180. m_vertex[i].x=m_vertex[j].x;
  181. m_vertex[i].y=m_vertex[j].y;
  182. m_vertex[i].z=m_vertex[j].z;
  183. m_vertex[j].x=temp.x;
  184. m_vertex[j].y=temp.y;
  185. m_vertex[j].z=temp.z;
  186. }
  187. }
  188. }
  189. if(m_vertex[0].y>m_vertex[1].y)                //沿基线固定方向
  190. this->TriDelaunay(m_vertex[0],m_vertex[1]);//调用边扩展算法构网delaunay
  191. else
  192. this->TriDelaunay(m_vertex[1],m_vertex[0]);//调用边扩展算法构网delaunay
  193. }
  194. }
  195. //DEL void CMy07063257_GLView::CalculateNormal()
  196. //DEL {
  197. //DEL 
  198. //DEL }
  199. BOOL CMy07063257_GLView::CreateViewGLContext(HDC hDC)
  200. {
  201. m_hGLContext = wglCreateContext(hDC); 
  202. if (m_hGLContext == NULL) 
  203. return FALSE; 
  204. if (wglMakeCurrent(hDC, m_hGLContext)==FALSE)   return FALSE; 
  205. return TRUE;
  206. }
  207. Vertex CMy07063257_GLView::GetThirdPoint(Vertex p1, Vertex p2)
  208. {
  209.    bool sign=true;
  210. Edge edge;
  211. edge.p1.x=p1.x;
  212. edge.p1.y=p1.y;
  213. edge.p1.z=p1.z;
  214. edge.p2.x=p2.x;
  215. edge.p2.y=p2.y;
  216. edge.p2.z=p2.z;
  217. for(int i=0;i<m_edges.size();i++)
  218. {
  219. if(edge.p1.x==m_edges[i].p1.x &&
  220. edge.p1.y==m_edges[i].p1.y&&
  221. edge.p1.z==m_edges[i].p1.z&&
  222. edge.p2.x==m_edges[i].p2.x&&
  223. edge.p2.y==m_edges[i].p2.y&&
  224. edge.p2.z==m_edges[i].p2.z)
  225. sign=false;
  226. }
  227. double temp=1;   //采用最大张角,即最小余弦的判别准则
  228. int j=0; 
  229. for( i=0;i<MAX;i++)
  230. {  
  231. double equation;
  232. equation = ((p1.y - p2.y) * (m_vertex[i].x - p2.x))
  233. - ((m_vertex[i].y - p2.y) * (p1.x - p2.x));//确定基线的方向
  234. if(equation<=0) continue;                      //沿基线>0固定方向
  235. double a=sqrt(pow(p1.x-m_vertex[i].x,2)+pow(p1.y-m_vertex[i].y,2));
  236. double b=sqrt(pow(p2.x-m_vertex[i].x,2)+pow(p2.y-m_vertex[i].y,2));
  237. double c=sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
  238. double d=(a*a+b*b-c*c)/(2*a*b);
  239. if(d<temp && equation>0 && sign)
  240. {
  241. temp=d;
  242. j=i;
  243. }
  244. }
  245. if(j!=0)
  246. return m_vertex[j];
  247. else
  248. {
  249. Vertex temp_vertex;
  250. temp_vertex.x=0;
  251. temp_vertex.y=0;
  252. temp_vertex.z=0;
  253. return temp_vertex;
  254. }
  255. }
  256. void CMy07063257_GLView::initial()
  257. {
  258.     int i;
  259. for (i=0;i<m_edges.size();i++)
  260. {
  261.         m_edges[i].no=i+1;     
  262.         m_edges[i].length=1.0;
  263.         m_edges[i].usetimes=0;
  264. m_edges[i].p1.x=0;
  265. m_edges[i].p1.y=0;
  266. m_edges[i].p1.z=0;
  267. m_edges[i].p2.x=0;
  268. m_edges[i].p2.y=0;
  269. m_edges[i].p2.z=0;
  270. }
  271. /////////////////////////////////////////////////////////////
  272. for (i=0;i<m_triangles.size();i++)
  273. {
  274.         m_triangles[i].no=i+1;
  275. m_triangles[i].edgeno[0]=-1;
  276. m_triangles[i].edgeno[1]=-1;
  277.         m_triangles[i].edgeno[2]=-1;
  278.         m_triangles[i].Normal[0]=-1;
  279.         m_triangles[i].Normal[1]=-1;
  280. m_triangles[i].Normal[2]=-1;
  281. }
  282. }
  283. int CMy07063257_GLView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  284. {
  285. if (CView::OnCreate(lpCreateStruct) == -1)
  286. return -1;
  287. // TODO: Add your specialized creation code here
  288. HWND hWnd = GetSafeHwnd(); 
  289. HDC hDC = ::GetDC(hWnd); 
  290. if (SetWindowPixelFormat(hDC)==FALSE)  return 0; 
  291. if (CreateViewGLContext(hDC)==FALSE)  return 0; 
  292. return 0;
  293. }
  294. void CMy07063257_GLView::OnDestroy() 
  295. {
  296. if(wglGetCurrentContext()!=NULL) 
  297. wglMakeCurrent(NULL, NULL) ; 
  298. if (m_hGLContext!=NULL) 
  299. wglDeleteContext(m_hGLContext); 
  300. m_hGLContext = NULL; 
  301. CView::OnDestroy();
  302. }
  303. void CMy07063257_GLView::OnGetVertex()
  304. {
  305. IsOpen=1;
  306. // TODO: Add your command handler code here
  307. FILE  *mm_vertexFile;
  308. CFileDialog location(true);
  309. location.DoModal();
  310. mm_vertexFile=fopen(location.GetPathName(),"r");
  311. for (int i=0;i<MAX;i++) 
  312. {
  313. float a,b,c;
  314. a=b=c=0.0;
  315. m_vertex[i].x=0.0;
  316. fscanf(mm_vertexFile,"%f%f%f",&a ,&b,&c);
  317. (m_vertex[i].x)=(a-87500)*1.5;
  318. // (m_vertex[i].x)=(a-87500)*1.5 ;
  319. (m_vertex[i].y)=(b-12800)*1.5;
  320. // (m_vertex[i].y)=(b-12800);
  321. (m_vertex[i].z)=c*1.5;
  322. }
  323. fclose(mm_vertexFile);
  324. }
  325. void CMy07063257_GLView::OnOpenglLighting()
  326. {
  327. Invalidate(true);
  328. }
  329. void CMy07063257_GLView::OnPaint() 
  330. {
  331. CPaintDC dc(this); // device context for painting
  332. glColor3f(0,0.4,0.3);
  333. glClear(GL_COLOR_BUFFER_BIT);
  334. switch(twc)
  335. {
  336. case 1:
  337. GLfloat light_ambient0[]= { 1.0,1.0,1.0, 1.0 };
  338. GLfloat light_position0[]={500,-500,60,1.0f};
  339. GLfloat light_specular0[]={1,1,1,1};
  340. GLfloat light_diffuse0[]={1.0f,0.0f,1.0f,1.0f};
  341. GLfloat spot_direction0[]={ -1.0,1.0,0.0,1.0};
  342. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient0);
  343. glLightfv(GL_LIGHT0,GL_POSITION,light_position0);
  344. glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular0);
  345. glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse0);
  346. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,spot_direction0);
  347. glEnable(GL_LIGHTING);
  348. glEnable(GL_LIGHT0);
  349. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_FALSE);
  350. this->OPenGL_Lighting();
  351. float dVector1[3],dVector2[3],norm[3];
  352. for (int i=0; i<m_triangles.size(); i++)
  353. {
  354. dVector1[0] = m_triangles[i].ptr[1].x - m_triangles[i].ptr[0].x;
  355. dVector1[1] = m_triangles[i].ptr[1].y - m_triangles[i].ptr[0].y;
  356. dVector1[2] = m_triangles[i].ptr[1].z - m_triangles[i].ptr[0].z;
  357. dVector2[0] = m_triangles[i].ptr[2].x - m_triangles[i].ptr[0].x;
  358. dVector2[1] = m_triangles[i].ptr[2].y - m_triangles[i].ptr[0].y;
  359. dVector2[2] = m_triangles[i].ptr[2].z - m_triangles[i].ptr[0].z;
  360. norm[0]=dVector1[1]*dVector2[2]-dVector1[2]*dVector2[1];
  361. norm[1]=dVector1[2]*dVector2[0]-dVector1[0]*dVector2[2];
  362. norm[2]=dVector1[0]*dVector2[1]-dVector1[1]*dVector2[0];
  363. float dNormalLength=sqrt(norm[0]*norm[0] + norm[1]*norm[1] + norm[2]*norm[2]);
  364. aa=norm[0]/dNormalLength;
  365. bb=norm[1]/dNormalLength;
  366. cc=norm[2]/dNormalLength;
  367.       glNormal3f(aa,bb,cc);    //三角面法向量
  368. }
  369. }
  370. case 2:
  371. GLfloat light_ambient0[]= { 1.0,1.0,1.0, 1.0 };
  372. GLfloat light_position0[]={500,-500,60,1.0f};
  373. GLfloat light_specular0[]={1,1,1,1};
  374. GLfloat light_diffuse0[]={1.0f,0.0f,1.0f,1.0f};
  375. GLfloat spot_direction0[]={ -1.0,1.0,0.0,1.0};
  376. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient0);
  377. glLightfv(GL_LIGHT0,GL_POSITION,light_position0);
  378. glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular0);
  379. glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse0);
  380. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,spot_direction0);
  381. glEnable(GL_LIGHTING);
  382. glEnable(GL_LIGHT0);
  383. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_FALSE);
  384. this->OPenGL_Lighting();
  385.     glPushMatrix();
  386. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  387. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);  
  388. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  389. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  390. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  391. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_DECAL);    //在此处更改映射模式,选择GL_DECAL、GL_MODULATE、GL_BLEND分别察看演示效果
  392. glEnable(GL_TEXTURE_2D);
  393. glShadeModel(GL_SMOOTH);   //更改GL_SMOOTH、GL_FLAT进行试验,察看效果
  394. /*  设置纹理坐标和物体几何坐标 */
  395. glRotatef(m_Rotateangle, 1, 1, 1);
  396. //纹理坐标的定义与演示效果,该图位于左下,纹理与图形表面吻合
  397. for (int i=0; i<m_triangles.size(); i++)
  398. {
  399. float dVector1[3],dVector2[3],norm[3];
  400. Vertex Point1,Point3,Point2;
  401.         Point1.x=m_triangles[i].ptr[0].x;
  402.         Point1.y=m_triangles[i].ptr[0].y;
  403. Point1.z=0.0;
  404. Point2.x=m_triangles[i].ptr[1].x;
  405. Point2.y=m_triangles[i].ptr[1].y;
  406. Point2.z=0.0;
  407. Point3.x=m_triangles[i].ptr[2].x;
  408. Point3.y=m_triangles[i].ptr[2].y;
  409. Point3.z=0.0;
  410.      dVector1[0] = m_triangles[i].ptr[1].x - m_triangles[i].ptr[0].x;
  411. dVector1[1] = m_triangles[i].ptr[1].y - m_triangles[i].ptr[0].y;
  412. dVector1[2] = m_triangles[i].ptr[1].z - m_triangles[i].ptr[0].z;
  413. dVector2[0] = m_triangles[i].ptr[2].x - m_triangles[i].ptr[0].x;
  414. dVector2[1] = m_triangles[i].ptr[2].y - m_triangles[i].ptr[0].y;
  415. dVector2[2] = m_triangles[i].ptr[2].z - m_triangles[i].ptr[0].z;
  416. norm[0]=dVector1[1]*dVector2[2]-dVector1[2]*dVector2[1];
  417. norm[1]=dVector1[2]*dVector2[0]-dVector1[0]*dVector2[2];
  418. norm[2]=dVector1[0]*dVector2[1]-dVector1[1]*dVector2[0];
  419. float dNormalLength=sqrt(norm[0]*norm[0] + norm[1]*norm[1] + norm[2]*norm[2]);
  420. aa=norm[0]/dNormalLength;
  421. bb=norm[1]/dNormalLength;
  422. cc=norm[2]/dNormalLength;
  423. glBegin(GL_TRIANGLES);
  424. glTexCoord2f(CalTexture(Point1.x,Point1.y,Point1.z).x, CalTexture(Point1.x, Point1.y, Point1.z).y); 
  425. glVertex3f(m_triangles[i].ptr[0].x, m_triangles[i].ptr[0].y, m_triangles[i].ptr[0].z);
  426. glTexCoord2f(CalTexture(Point2.x,Point2.y, Point2.z).x, CalTexture(Point2.x, Point2.y, Point2.z).y); 
  427. glVertex3f(m_triangles[i].ptr[1].x, m_triangles[i].ptr[1].y, m_triangles[i].ptr[1].z);
  428. glTexCoord2f(CalTexture(Point3.x, Point3.y, Point3.z).x, CalTexture(Point3.x, Point3.y,Point3.z).y);
  429. glVertex3f(m_triangles[i].ptr[2].x, m_triangles[i].ptr[2].y, m_triangles[i].ptr[2].z);
  430. glNormal3f(aa,bb,cc);   
  431. glEnd();
  432. }
  433. }
  434. glFlush();
  435. glDisable(GL_TEXTURE_2D);
  436. glPopMatrix();
  437. }
  438. void CMy07063257_GLView::OnSize(UINT nType, int cx, int cy) 
  439. {
  440. CView::OnSize(nType, cx, cy);
  441. glViewport(0,0,cx,cy);
  442.     glMatrixMode(GL_PROJECTION);
  443.     glLoadIdentity();
  444. if(cx<=cy)
  445. glOrtho(-500.0,500.0,-500.0*(GLfloat)cy/(GLfloat)cx,
  446. 500.0*(GLfloat)cy/(GLfloat)cx,-500.0,500.0);
  447.     else
  448. glOrtho(-500.0*(GLfloat)cx/(GLfloat)cy,
  449. 500.0*(GLfloat)cx/(GLfloat)cy,-500.0,500.0,-500.0,500.0);
  450.     glMatrixMode(GL_MODELVIEW);
  451.     glLoadIdentity();
  452. // TODO: Add your message handler code here
  453. }
  454. void CMy07063257_GLView::OPenGL_Lighting()
  455. {
  456.     glPushMatrix();
  457. GLfloat mat_diffuse[]={0.0f,1.0f,0.4f,1.0f};
  458.     GLfloat mat_specular[]={0.0f,1.0f,0.4f,1.0f};
  459.     GLfloat mat_shininess[]={80.0f};
  460. glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);
  461.     glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
  462.     glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
  463. glClearColor(0.0,0.0,0.0,0.0);
  464.     glShadeModel(GL_SMOOTH);
  465.     glPolygonMode(GL_FRONT,GL_FILL);
  466. glColor3f(0.0f,0.4f,1.0f);
  467. for(int i=0;i<m_triangles.size();i++)
  468. {
  469. double dNormal[3];
  470. dNormal[0] = m_triangles[i].Normal[0];
  471. dNormal[1] = m_triangles[i].Normal[1];
  472. dNormal[2] = m_triangles[i].Normal[2];
  473. glBegin(GL_POLYGON);
  474. glNormal3dv(dNormal);
  475. glVertex3f(m_triangles[i].ptr[0].x, m_triangles[i].ptr[0].y, m_triangles[i].ptr[0].z);
  476. glVertex3f(m_triangles[i].ptr[1].x, m_triangles[i].ptr[1].y, m_triangles[i].ptr[1].z);
  477. glVertex3f(m_triangles[i].ptr[2].x, m_triangles[i].ptr[2].y, m_triangles[i].ptr[2].z);
  478. glEnd();
  479. glFlush();
  480. }
  481. glPopMatrix();
  482. }
  483. BOOL CMy07063257_GLView::SetWindowPixelFormat(HDC hDC)
  484. {
  485. PIXELFORMATDESCRIPTOR pixelDesc; 
  486. pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);  pixelDesc.nVersion = 1; 
  487. pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |         PFD_DRAW_TO_BITMAP |      PFD_SUPPORT_OPENGL |      PFD_SUPPORT_GDI |     PFD_STEREO_DONTCARE;  pixelDesc.iPixelType = PFD_TYPE_RGBA;  pixelDesc.cColorBits = 32; 
  488. pixelDesc.cRedBits = 8;
  489. pixelDesc.cRedShift = 16;
  490. pixelDesc.cGreenBits = 8; 
  491. pixelDesc.cGreenShift = 8; 
  492. pixelDesc.cBlueBits = 8; 
  493. pixelDesc.cBlueShift = 0;
  494. pixelDesc.cAlphaBits = 0;
  495. pixelDesc.cAlphaShift = 0;
  496. pixelDesc.cAccumBits = 64; 
  497. pixelDesc.cAccumRedBits = 16; 
  498. pixelDesc.cAccumGreenBits = 16; 
  499. pixelDesc.cAccumBlueBits = 16; 
  500. pixelDesc.cAccumAlphaBits = 0;
  501. pixelDesc.cDepthBits = 32; 
  502. pixelDesc.cStencilBits = 8; 
  503. pixelDesc.cAuxBuffers = 0; 
  504. pixelDesc.iLayerType = PFD_MAIN_PLANE; 
  505. pixelDesc.bReserved = 0; 
  506. pixelDesc.dwLayerMask = 0; 
  507. pixelDesc.dwVisibleMask = 0; 
  508. pixelDesc.dwDamageMask = 0; 
  509. m_GLPixelIndex = ChoosePixelFormat( hDC, &pixelDesc);
  510. if (m_GLPixelIndex==0) // Let's choose a default index. 
  511. {    m_GLPixelIndex = 1; 
  512. if (DescribePixelFormat(hDC, m_GLPixelIndex,      sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0) 
  513. return FALSE; 
  514. if (SetPixelFormat( hDC, m_GLPixelIndex,&pixelDesc)==FALSE)   return FALSE; 
  515. return TRUE; 
  516. }
  517. void CMy07063257_GLView::TriDelaunay(Vertex p1, Vertex p2)
  518. {
  519. CClientDC dc(this);
  520.     Vertex ThirdPoint;
  521.     ThirdPoint.x=(GetThirdPoint(p1,p2)).x;
  522.     ThirdPoint.y=(GetThirdPoint(p1,p2)).y;
  523.     ThirdPoint.z=(GetThirdPoint(p1,p2)).z;
  524.     if (ThirdPoint.x!=0 && ThirdPoint.y!=0)
  525. {
  526. glBegin(GL_LINE_LOOP);
  527. glVertex3f(p1.x,p1.y,p1.z);
  528. glVertex3f(p2.x,p2.y,p2.z);
  529. glVertex3f(ThirdPoint.x,ThirdPoint.y,ThirdPoint.z);
  530. glEnd();
  531. Edge l1;
  532. l1.p1.x=p1.x;
  533. l1.p1.y=p1.y;
  534. l1.p1.z=p1.z;
  535. l1.p2.x=p2.x;
  536. l1.p2.y=p2.y;
  537. l1.p2.z=p2.z;
  538. m_edges.push_back(l1);
  539. Triangle triangle;
  540. triangle.ptr[0].x=p1.x;
  541. triangle.ptr[0].y=p1.y;
  542. triangle.ptr[0].z=p1.z;
  543. triangle.ptr[1].x=p2.x;
  544. triangle.ptr[1].y=p2.y;
  545. triangle.ptr[1].z=p2.z;
  546. triangle.ptr[2].x=ThirdPoint.x;
  547. triangle.ptr[2].y=ThirdPoint.y;
  548. triangle.ptr[2].z=ThirdPoint.z;
  549. m_triangles.push_back(triangle);
  550. }
  551.     else 
  552. return;//储存已构成的直线 
  553. TriDelaunay(p1,ThirdPoint) ;             
  554. TriDelaunay(ThirdPoint,p2) ;  //标记已构三角形的边,对尚未构三角形的边用算法进行递归调用
  555. }
  556. void CMy07063257_GLView::OnLeft() 
  557. {
  558.     IsOpen++;
  559. type=IsOpen%4;
  560. switch(type)
  561. {
  562. case 0:
  563.           glTranslatef (-50.0, 0.0, 0.0);
  564.           OnPaint();
  565.   break;
  566. }
  567. case 1:
  568. {
  569.          glTranslatef (-50.0, 0.0, 0.0);
  570.          OnPaint();
  571. break;
  572. }
  573. case 2:
  574. {
  575.         glTranslatef (-50.0, 0.0, 0.0);
  576.          OnPaint();
  577. break;
  578. }
  579. case 3:
  580.         glTranslatef (-50.0, 0.0, 0.0);
  581.         OnPaint();
  582. }
  583. }
  584. void CMy07063257_GLView::OnRight() 
  585. {
  586.  IsOpen++;
  587. type=IsOpen%4;
  588. switch(type)
  589. {
  590. case 0:
  591.           glTranslatef (50.0, 0.0, 0.0);
  592.           OnPaint();
  593.   break;
  594. }
  595. case 1:
  596. {
  597.          glTranslatef (50.0, 0.0, 0.0);
  598.          OnPaint();
  599. break;
  600. }
  601. case 2:
  602. {
  603.         glTranslatef (50.0, 0.0, 0.0);
  604.          OnPaint();
  605. break;
  606. }
  607. case 3:
  608.         glTranslatef (50.0, 0.0, 0.0);
  609.         OnPaint();
  610. }
  611. }
  612. void CMy07063257_GLView::OnUp() 
  613. {
  614. // TODO: Add your command handler code here
  615.      IsOpen++;
  616. type=IsOpen%4;
  617. switch(type)
  618. {
  619. case 0:
  620.           glTranslatef (0.0, 50.0, 0.0);
  621.           OnPaint();
  622.   break;
  623. }
  624. case 1:
  625. {
  626.          glTranslatef (0.0, 50.0, 0.0);
  627.          OnPaint();
  628. break;
  629. }
  630. case 2:
  631. {
  632.         glTranslatef (0.0, 50.0, 0.0);
  633.          OnPaint();
  634. break;
  635. }
  636. case 3:
  637.         glTranslatef (0.0, 50.0, 0.0);
  638.         OnPaint();
  639. }
  640. }
  641. void CMy07063257_GLView::OnDown() 
  642. {
  643. // TODO: Add your command handler code here
  644.     IsOpen++;
  645. type=IsOpen%4;
  646. switch(type)
  647. {
  648. case 0:
  649.           glTranslatef (0.0, -50.0, 0.0);
  650.           OnPaint();
  651.   break;
  652. }
  653. case 1:
  654. {
  655.          glTranslatef (0.0, -50.0, 0.0);
  656.          OnPaint();
  657. break;
  658. }
  659. case 2:
  660. {
  661.         glTranslatef (0.0, -50.0, 0.0);
  662.          OnPaint();
  663. break;
  664. }
  665. case 3:
  666.         glTranslatef (0.0, -50.0, 0.0);
  667.         OnPaint();
  668. }
  669. }
  670. void CMy07063257_GLView::OnLrotaef() 
  671. {
  672. // TODO: Add your command handler code here
  673.     IsOpen++;
  674. type=IsOpen%4;
  675. switch(type)
  676. {
  677. case 0:
  678.           glRotatef (10.0, 0.0, 0.0, 1.0);
  679.           OnPaint();
  680.   break;
  681. }
  682. case 1:
  683. {
  684.          glRotatef (10.0, 0.0, 0.0, 1.0);
  685.          OnPaint();
  686. break;
  687. }
  688. case 2:
  689. {
  690.         glRotatef (10.0, 0.0, 0.0, 1.0);
  691.          OnPaint();
  692. break;
  693. }
  694. case 3:
  695.         glRotatef (10.0, 0.0, 0.0, 1.0);
  696.         OnPaint();
  697. }
  698. }
  699. void CMy07063257_GLView::OnRrotatef() 
  700. {
  701. // TODO: Add your command handler code here
  702.     IsOpen++;
  703. type=IsOpen%4;
  704. switch(type)
  705. {
  706. case 0:
  707.           glRotatef (-10.0, 0.0, 0.0, 1.0);
  708.           OnPaint();
  709.   break;
  710. }
  711. case 1:
  712. {
  713.          glRotatef (-10.0, 0.0, 0.0, 1.0);
  714.          OnPaint();
  715. break;
  716. }
  717. case 2:
  718. {
  719.         glRotatef (-10.0, 0.0, 0.0, 1.0);
  720.          OnPaint();
  721. break;
  722. }
  723. case 3:
  724.         glRotatef (-10.0, 0.0, 0.0, 1.0);
  725.         OnPaint();
  726. }
  727. }
  728. void CMy07063257_GLView::OnZoomout() 
  729. {
  730. // TODO: Add your command handler code here
  731.     IsOpen++;
  732. type=IsOpen%4;
  733. switch(type)
  734. {
  735. case 0:
  736.           glScalef (1.2, 1.2, 1.2);
  737.           OnPaint();
  738.   break;
  739. }
  740. case 1:
  741. {
  742.          glScalef (1.2, 1.2, 1.2);
  743.          OnPaint();
  744. break;
  745. }
  746. case 2:
  747. {
  748.          glScalef (1.2, 1.2, 1.2);
  749.          OnPaint();
  750. break;
  751. }
  752. case 3:
  753.         glScalef (1.2, 1.2, 1.2);
  754.         OnPaint();
  755. }
  756.   
  757. }
  758. void CMy07063257_GLView::OnZoomin() 
  759. {
  760. // TODO: Add your command handler code here
  761.     IsOpen++;
  762. type=IsOpen%4;
  763. switch(type)
  764. {
  765. case 0:
  766.           glScalef (0.8, 0.8, 0.8);
  767.           OnPaint();
  768.   break;
  769. }
  770. case 1:
  771. {
  772.          glScalef (0.8, 0.8, 0.8);
  773.          OnPaint();
  774. break;
  775. }
  776. case 2:
  777. {
  778.          glScalef (0.8, 0.8, 0.8);
  779.          OnPaint();
  780. break;
  781. }
  782. case 3:
  783.         glScalef (0.8, 0.8, 0.8);
  784.         OnPaint();
  785. }
  786.   
  787. }
  788. void CMy07063257_GLView::OnFront() 
  789. {
  790. // TODO: Add your command handler code here
  791. IsOpen++;
  792. type=IsOpen%4;
  793. switch(type)
  794. {
  795. case 0:
  796.           glRotatef (-10.0, 1.0, 0.0, 0.0);
  797.           OnPaint();
  798.   break;
  799. }
  800. case 1:
  801. {
  802.          glRotatef (-10.0, 1.0, 0.0, .0);
  803.          OnPaint();
  804. break;
  805. }
  806. case 2:
  807. {
  808.         glRotatef (-10.0, 1.0, 0.0, 0.0);
  809.          OnPaint();
  810. break;
  811. }
  812. case 3:
  813.         glRotatef (-10.0, 1.0, 0.0, 0.0);
  814.         OnPaint();
  815. }
  816. }
  817. void CMy07063257_GLView::OnBack() 
  818. {
  819. // TODO: Add your command handler code here
  820. IsOpen++;
  821. type=IsOpen%4;
  822. switch(type)
  823. {
  824. case 0:
  825.           glRotatef (10.0, 1.0, 0.0, 0.0);
  826.           OnPaint();
  827.   break;
  828. }
  829. case 1:
  830. {
  831.          glRotatef (10.0, 1.0, 0.0, .0);
  832.          OnPaint();
  833. break;
  834. }
  835. case 2:
  836. {
  837.         glRotatef (10.0, 1.0, 0.0, 0.0);
  838.          OnPaint();
  839. break;
  840. }
  841. case 3:
  842.         glRotatef (10.0, 1.0, 0.0, 0.0);
  843.         OnPaint();
  844. }
  845. }
  846. Vertex CMy07063257_GLView::CalTexture(float xx, float yy, float zz)
  847. {
  848.    Vertex CalTexture1;
  849.    CalTexture1.x=(MAXX-xx)/(MAXX-MINX);
  850.    CalTexture1.y=(MAXY-yy)/(MAXY-MINY);
  851.    CalTexture1.z=0.0;
  852.    return CalTexture1;
  853. }