glview.cpp
上传用户:lbr_007
上传日期:2019-05-31
资源大小:282k
文件大小:6k
源码类别:

传真(Fax)编程

开发平台:

Visual C++

  1. // glview.cpp
  2. //
  3. #include "stdafx.h"
  4. #include "glview.h"
  5. #include <math.h>
  6. GLView3D::GLView3D(void)
  7. {
  8. InitImage();
  9. }
  10. GLView3D::~GLView3D(void)
  11. {
  12. }
  13. void GLView3D::CreateImage(UINT32 width, UINT32 height)
  14. {
  15. if ((width > 0) && (height > 0))
  16. {
  17. m_dib.Create(width,height,24);
  18. if (!IsInitialized())
  19. {
  20. InitializeOpenGL();
  21. if (CreateView())
  22. {
  23. m_IsInitialized = 1;
  24. }
  25. }
  26. }
  27. }
  28. void GLView3D::SizeImage(UINT32 width, UINT32 height)
  29. {
  30. if ((width > 0) && (height > 0))
  31. {
  32. if (!IsInitialized())
  33. {
  34. CreateImage(width,height);
  35. }
  36. else
  37. {
  38. if (!m_dib.IsCreated())
  39. {
  40. m_dib.Create(width,height,24);
  41. if (SetImagePixelFormat(*m_dib.GetDC()))
  42. {
  43. CreateImageGLContext(*m_dib.GetDC());
  44. }
  45. }
  46. else if ((width != m_dib.Width()) || (height != m_dib.Height()))
  47. {
  48. m_dib.Create(width,height,24);
  49. CreateView();
  50. }
  51. }
  52. glViewport(0,0,width,height);
  53. double aspect;
  54. aspect = (height == 0) ? (double)width : (double)width/(double)height;
  55. glMatrixMode(GL_PROJECTION);
  56. glLoadIdentity();
  57. gluPerspective(45,aspect,0.000001,50.0);
  58. glMatrixMode(GL_MODELVIEW);
  59. glLoadIdentity();
  60. m_reRender = true;
  61. }
  62. }
  63. void GLView3D::InitializeOpenGL(void)
  64. {
  65. m_hGLContext = NULL;
  66. m_GLPixelIndex = 0;
  67. InitGeometry();
  68. }
  69. void GLView3D::InitGeometry(void)
  70. {
  71. m_model_matrix.SetRotation(30.0,0.0,0.0);
  72. m_model_matrix.SetScale(1.0,1.0,1.0);
  73. m_model_matrix.SetPosition(0.0,0.0,0.0);
  74. }
  75.   // Lights, camera, action...
  76. int GLView3D::CreateView(void)
  77. {
  78. int result = 0;
  79.   // do the standard OpenGL initialization
  80. if(SetImagePixelFormat(*m_dib.GetDC()) && CreateImageGLContext(*m_dib.GetDC()))
  81. result = 1;
  82. else
  83. return result;
  84. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  85. glShadeModel(GL_SMOOTH);
  86. glEnable(GL_NORMALIZE);
  87. glEnable(GL_COLOR_MATERIAL);
  88. // Lights, material properties
  89. GLfloat ambientProperties[]  = {0.3f, 0.3f, 0.3f, 0.7f};
  90. GLfloat diffuseProperties[]  = {0.9f, 0.9f, 0.9f, 1.0f};
  91. GLfloat specularProperties[] = {0.7f, 0.7f, 0.7f, 1.0f};
  92. GLfloat mat_amb_diff[] = { 0.7f, 0.7f, 0.7f, 1.0f};
  93. GLfloat shine[] = { 128.0f};
  94. GLfloat emissionProperties[] = {0.2f, 0.2f, 0.2f, 1.0f};
  95. glLightfv( GL_LIGHT0, GL_AMBIENT, ambientProperties);
  96. glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseProperties);
  97. glLightfv( GL_LIGHT0, GL_SPECULAR, specularProperties);
  98. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0);
  99. //glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
  100. //glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shine);
  101. //glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emissionProperties);
  102. // lighting
  103. glEnable(GL_LIGHT0);
  104. glEnable(GL_LIGHTING);
  105. glEnable(GL_DEPTH_TEST);
  106. m_reRender = true;
  107. return result;
  108. }
  109. BOOL GLView3D::SetImagePixelFormat(HDC hDC)
  110. {
  111. PIXELFORMATDESCRIPTOR pixelDesc;
  112. pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  113. pixelDesc.nVersion = 1;
  114. pixelDesc.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL |
  115. PFD_SUPPORT_GDI;
  116. pixelDesc.iPixelType = PFD_TYPE_RGBA;
  117. pixelDesc.cColorBits = 24;
  118. pixelDesc.cRedBits = 0;
  119. pixelDesc.cRedShift = 0;
  120. pixelDesc.cGreenBits = 0;
  121. pixelDesc.cGreenShift = 0;
  122. pixelDesc.cBlueBits = 0;
  123. pixelDesc.cBlueShift = 0;
  124. pixelDesc.cAlphaBits = 0;
  125. pixelDesc.cAlphaShift = 0;
  126. pixelDesc.cAccumBits = 0;
  127. pixelDesc.cAccumRedBits = 0;
  128. pixelDesc.cAccumGreenBits = 0;
  129. pixelDesc.cAccumBlueBits = 0;
  130. pixelDesc.cAccumAlphaBits = 0;
  131. pixelDesc.cDepthBits = 24;
  132. pixelDesc.cStencilBits = 0;
  133. pixelDesc.cAuxBuffers = 0;
  134. pixelDesc.iLayerType = PFD_MAIN_PLANE;
  135. pixelDesc.bReserved = 0;
  136. pixelDesc.dwLayerMask = 0;
  137. pixelDesc.dwVisibleMask = 0;
  138. pixelDesc.dwDamageMask = 0;
  139. int pix_index = ChoosePixelFormat(hDC,&pixelDesc);
  140. if(!SetPixelFormat(hDC,pix_index,&pixelDesc))
  141. {
  142. DWORD code = GetLastError();
  143. return FALSE;
  144. }
  145. return TRUE;
  146. }
  147. BOOL GLView3D::CreateImageGLContext(HDC hDC)
  148. {
  149. m_hGLContext = wglCreateContext(hDC);
  150. if(m_hGLContext==NULL)
  151. return FALSE;
  152. if(wglMakeCurrent(hDC,m_hGLContext)==FALSE)
  153. return FALSE;
  154. return TRUE;
  155. }
  156. void GLView3D::SetBackgroundColor(double r, double g, double b)
  157. {
  158. m_red_bg = r;
  159. m_green_bg = g;
  160. m_blue_bg = b;
  161. m_reRender = true;
  162. }
  163. void GLView3D::SetOrientation(void)
  164. {
  165.   // Position / translation / scale
  166. glTranslated(m_model_matrix.X(),m_model_matrix.Y(),m_model_matrix.Z());
  167. glRotated(m_model_matrix.XRot(), 1.0, 0.0, 0.0);
  168. glRotated(m_model_matrix.YRot(), 0.0, 1.0, 0.0);
  169. glRotated(m_model_matrix.ZRot(), 0.0, 0.0, 1.0);
  170. glScaled(m_model_matrix.XScale(),m_model_matrix.YScale(),m_model_matrix.ZScale());
  171. }
  172. void GLView3D::PickObject(int x, int y, int& id)
  173. {
  174. m_pickMode = true;
  175. m_pickX = x;
  176. m_pickY = y;
  177. m_pickID = 0;
  178. RenderImage();
  179. m_pickMode = false;
  180. RenderImage();
  181. }
  182. void GLView3D::RenderImage(void)
  183. {
  184. //if (m_reRender)
  185. //{
  186. if (m_IsInitialized && m_dib.IsCreated())
  187. {
  188. glClearColor((float)m_red_bg,(float)m_blue_bg,
  189. (float)m_green_bg,1.0f);
  190. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  191.   // put a working copy on the matrix stack
  192. glPushMatrix();
  193. SetOrientation();
  194.   // build the scene...if necessary
  195. RenderPrimaryImage();
  196. glFlush();
  197.   // pop the working copy of the matrix stack
  198. glPopMatrix();
  199. }
  200. m_reRender = false;
  201. //}
  202. }
  203. void GLView3D::RenderPrimaryImage(void)
  204. {
  205. }
  206. void GLView3D::DrawImage(CWnd * wnd)
  207. {
  208. CClientDC dc(wnd);
  209. m_dib.Draw(&dc,0,0);
  210. }
  211. void NormalVector(GLdouble p1[3], GLdouble p2[3],
  212.   GLdouble p3[3], GLdouble n[3])
  213. {
  214. GLdouble v1[3], v2[3], d;
  215. // calculate two vectors, using the middle point
  216. // as the common origin
  217. v1[0] = p3[0] - p1[0];
  218. v1[1] = p3[1] - p1[1];
  219. v1[2] = p3[2] - p1[2];
  220. v2[0] = p3[0] - p2[0];
  221. v2[1] = p3[1] - p2[1];
  222. v2[2] = p3[2] - p2[2];
  223. // calculate the cross product of the two vectors
  224. n[0] = v1[1] * v2[2] - v2[1] * v1[2];
  225. n[1] = v1[2] * v2[0] - v2[2] * v1[0];
  226. n[2] = v1[0] * v2[1] - v2[0] * v1[1];
  227. // normalize the vector
  228. d = ( n[0] * n[0] + n[1] * n[1] + n[2] * n[2] );
  229. // try to catch very small vectors
  230. if (d < (GLdouble)0.00000001)
  231. {
  232. d = (GLdouble)100000000.0;
  233. }
  234. else
  235. {
  236. d = (GLdouble)1.0 / sqrt(d);
  237. }
  238. n[0] *= d;
  239. n[1] *= d;
  240. n[2] *= d;
  241. }