OpenGL.cpp
上传用户:jsylhbnbhn
上传日期:2013-11-03
资源大小:119k
文件大小:6k
源码类别:

OpenCV

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "OpenGL.h"
  3. //////////////////////////////////////////////////////////////////////
  4. extern HWND hWnd;
  5. const float pi = 3.1415926f;
  6. GLUquadricObj* quadric=NULL;
  7. //////////////////////////////////////////////////////////////////////
  8. OpenGL::OpenGL()
  9. {
  10. x_arc = 0;
  11. y_arc = 0;
  12. z_arc = 0;
  13. }
  14. OpenGL::~OpenGL()
  15. { CleanUp();
  16. }
  17. BOOL OpenGL::SetupPixelFormat(HDC hDC0)//检测安装OpenGL
  18. { int nPixelFormat;   // 象素点格式
  19. hDC=hDC0;
  20. PIXELFORMATDESCRIPTOR pfd = { 
  21.     sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小 
  22.     1,                                // 版本号 
  23.     PFD_DRAW_TO_WINDOW |              // 支持在窗口中绘图 
  24.     PFD_SUPPORT_OPENGL |              // 支持 OpenGL 
  25.     PFD_DOUBLEBUFFER,                 // 双缓存模式 
  26.     PFD_TYPE_RGBA,                    // RGBA 颜色模式 
  27.     16,                               // 24 位颜色深度 
  28.     0, 0, 0, 0, 0, 0,                 // 忽略颜色位 
  29.     0,                                // 没有非透明度缓存 
  30.     0,                                // 忽略移位位 
  31.     0,                                // 无累加缓存 
  32.     0, 0, 0, 0,                       // 忽略累加位 
  33.     16,                               // 32 位深度缓存     
  34.     0,                                // 无模板缓存 
  35.     0,                                // 无辅助缓存 
  36.     PFD_MAIN_PLANE,                   // 主层 
  37.     0,                                // 保留 
  38.     0, 0, 0                           // 忽略层,可见性和损毁掩模 
  39. }; 
  40. if (!(nPixelFormat = ChoosePixelFormat(hDC, &pfd)))
  41. { MessageBox(NULL,"没找到合适的显示模式","Error",MB_OK|MB_ICONEXCLAMATION);
  42.       return FALSE;
  43. }
  44. SetPixelFormat(hDC,nPixelFormat,&pfd);//设置当前设备的像素点格式
  45. hRC = wglCreateContext(hDC);          //获取渲染描述句柄
  46. wglMakeCurrent(hDC, hRC);             //激活渲染描述句柄
  47. return TRUE;
  48. }
  49. void OpenGL::init(int Width, int Height)
  50. { glViewport(0,0,Width,Height); // 设置OpenGL视口大小。
  51. glMatrixMode(GL_PROJECTION); // 设置当前矩阵为投影矩阵。
  52. glLoadIdentity(); // 重置当前指定的矩阵为单位矩阵
  53. /*
  54. gluPerspective // 设置透视图
  55. ( 54.0f, // 透视角设置为 45 度
  56.   (GLfloat)Width/(GLfloat)Height, // 窗口的宽与高比
  57.   0.1f, // 视野透视深度:近点1.0f
  58.   3000.0f // 视野透视深度:始点0.1f远点1000.0f
  59. );
  60. */
  61. glOrtho(-3.0, 3.0, -3.0, 3.0,-3.0,3.0); 
  62. // 这和照象机很类似,第一个参数设置镜头广角度,第二个参数是长宽比,后面是远近剪切。
  63. glMatrixMode(GL_MODELVIEW); // 设置当前矩阵为模型视图矩阵
  64. glLoadIdentity(); // 重置当前指定的矩阵为单位矩阵
  65. //====================================================
  66. GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  67. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  68. glEnable(GL_LIGHTING);
  69. glEnable(GL_LIGHT0);
  70. glDepthFunc(GL_LESS);
  71. glEnable(GL_DEPTH_TEST);
  72. glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  73. glEnable(GL_COLOR_MATERIAL);
  74. }
  75. void OpenGL::Render()//OpenGL图形处理
  76. {
  77. glClearColor(0.0f, 0.0f, 0.3f, 1.0f);  // 设置刷新背景色
  78. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);// 刷新背景
  79. glLoadIdentity();  // 重置当前的模型观察矩阵
  80. Vector3f vVector1, vVector2;
  81. vVector1.x = 200;
  82. vVector1.y = 200;
  83. vVector1.z = 200;
  84. vVector2.x = 0;
  85. vVector2.y = 0;
  86. vVector2.z = 0;
  87. draw2point(vVector1,vVector2);
  88. vVector2.x = 440;
  89. vVector2.y = 440;
  90. vVector2.z = 440;
  91. draw2point(vVector1,vVector2);
  92. glFlush();  // 更新窗口
  93. SwapBuffers(hDC);  // 切换缓冲区
  94. }
  95. void OpenGL::CleanUp()//清除OpenGL
  96. {
  97.  wglMakeCurrent(hDC, NULL);                       //清除OpenGL
  98.  wglDeleteContext(hRC);                           //清除OpenGL
  99. }
  100. int OpenGL::Init()
  101. {return 0;}
  102. void OpenGL::display()
  103. {
  104. glFlush();
  105. SwapBuffers(hDC);  // 切换缓冲区
  106. }
  107. void OpenGL::draw2point(Vector3f vVector1, Vector3f vVector2)
  108. {
  109. float Zoom = 100;
  110. //printf("before unproject: %f,%f,%f n",vVector1.x,vVector1.y,vVector1.z);
  111. //printf("before unproject: %f,%f,%f n",vVector2.x,vVector2.y,vVector2.z);
  112. //printf("after  unproject: %f,%f,%f nn",xx,yy,zz);
  113. //需要考虑float 越界问题
  114. Vector3f *vt1 = new Vector3f(vVector1.x/Zoom,vVector1.y/Zoom,vVector1.z/Zoom);
  115. Vector3f *vt2 = new Vector3f(vVector2.x/Zoom,vVector2.y/Zoom,vVector2.z/Zoom);
  116. Vector3f vt4 = *vt2-*vt1;
  117. Vector3f *vty = new Vector3f(0,0,1);
  118. double arc12 = AngleBetweenVectors(*vty,vt4);
  119. double rarc12 = 180 + 180*arc12/pi;
  120. float len= Distance(*vt1,*vt2);
  121. Vector3f vt3 = Cross(*vty,vt4);
  122. glPushMatrix();
  123. glTranslatef (vt1->x,vt1->y,vt1->z);
  124. glColor3f(1.0f,0.5f,0.7f); 
  125. auxSolidSphere(0.09);
  126. glTranslatef (vt4.x,vt4.y,vt4.z);
  127. auxSolidSphere(0.09);
  128. glColor3f(1.0f,1.0f,0.7f);
  129. glRotatef ((float)rarc12,vt3.x,vt3.y,vt3.z);
  130. gluCylinder(quadric,0.06,0.06,len,35,35);
  131. glPopMatrix();
  132. delete vt1;
  133. delete vt2;
  134. delete vty;
  135. }
  136. void OpenGL::clear()
  137. {
  138. glClearColor(0.0f, 0.0f, 0.3f, 1.0f);  // 设置刷新背景色
  139. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);// 刷新背景
  140. glLoadIdentity();
  141. if(!quadric)
  142. {
  143. quadric=gluNewQuadric();
  144. gluQuadricNormals(quadric,GLU_SMOOTH);
  145. gluQuadricTexture(quadric,GL_TRUE);
  146. }
  147. //gluCylinder(quadric,0.06,0.06,0.9,35,35);
  148. glRotatef (25.0,1.0,1.0,0.0);
  149. glRotatef ((float)x_arc,1.0,0.0,0.0);
  150. glRotatef ((float)y_arc,0.0,1.0,0.0);
  151. glRotatef ((float)z_arc,0.0,0.0,1.0);
  152. draw_ground();
  153. }
  154. //将骨架旋转到y轴上来
  155. int OpenGL::Correct_3D_Layout(Vector3f vVector1, Vector3f vVector2)
  156. {
  157. Vector3f vt4 = vVector2-vVector1;
  158. Vector3f *vty = new Vector3f(0,1,0);
  159. double arc12 = AngleBetweenVectors(*vty,vt4);
  160. double rarc12 = 180*arc12/pi;
  161. Vector3f vt3 = Cross(*vty,vt4);
  162. glRotatef ((float)rarc12,vt3.x,vt3.y,vt3.z);
  163. delete vty;
  164. return 0;
  165. }
  166. void OpenGL::draw_ground()
  167. {
  168.   glPushAttrib(GL_CURRENT_BIT);
  169.   glEnable(GL_BLEND);
  170.   glPushMatrix();
  171.   glColor3f(0.5f, 0.7f, 1.0f);
  172.   glTranslatef(0,0.0f,0);
  173.   float size0=2;
  174.   glBegin(GL_LINES);
  175. for (float x = -size0; x < size0;x+=0.5f)
  176. {glVertex3f(x, -1.5, -size0); glVertex3f(x, -1.5,  size0);}
  177. for (float z = -size0; z < size0;z+=0.3f)
  178. {glVertex3f(-size0, -1.5, z); glVertex3f( size0, -1.5, z);}
  179.   glEnd();
  180.   glPopMatrix();
  181.   glDisable(GL_BLEND);
  182.   glPopAttrib();
  183. }