3DGrapher.cpp
Upload User: qzhyjz
Upload Date: 2018-02-25
Package Size: 165k
Code Size: 6k
Category:

Algorithm

Development Platform:

Matlab

  1. // 3DGrapher.cpp: implementation of the C3DGrapher class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "3DGrapher.h"
  6. #include "glglaux.h"
  7. #include <cmath>
  8. using namespace std;
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14. //////////////////////////////////////////////////////////////////////
  15. // Construction/Destruction
  16. //////////////////////////////////////////////////////////////////////
  17. C3DGrapher::C3DGrapher()
  18. {
  19. m_nProjectionType = PERSPECTIVE;
  20. m_dXrange = 100.0f;
  21. m_dXrange = 100.0f;
  22. m_dXrange = 100.0f;
  23. m_bDepthTest = true;
  24. m_bCullFace = true;
  25. m_nShadeModel = SMOOTH;
  26. m_nPolygonMode = FILL;
  27. m_dFOV = 45.0f;
  28. m_dXrot = 20.0f;
  29. m_dYrot = -20.0f;
  30. m_dZrot = 0.0f;
  31. m_dXtran = 0.0f;
  32. m_dYtran = 0.0f;
  33. m_dZtran = 0.0f;
  34. m_strTitle = _T("Untitled");
  35. m_strXtitle = _T("X");
  36. m_strYtitle = _T("Y");
  37. m_strZtitle = _T("Z");
  38. m_nFontBase = 0;
  39. m_colBkGnd = RGB(127, 127, 0);
  40. m_nSize = 0;
  41. }
  42. C3DGrapher::~C3DGrapher()
  43. {
  44. Clear();
  45. }
  46. void C3DGrapher::Init(HWND hWnd) throw()
  47. {
  48. ASSERT(::IsWindow(hWnd));
  49. m_hWnd = hWnd;
  50. m_hDC = ::GetDC(m_hWnd);
  51. try {
  52. SetWindowPixelFormat();
  53. CreateGLContext();
  54. } catch(CGrapherException e) {
  55. ::MessageBox(NULL, e.what(), _T("GrapherException"), MB_OK);
  56. }
  57. CreateFont();
  58. Initialize();
  59. }
  60. void C3DGrapher::Clear() throw()
  61. {
  62. list<C3DData*>::iterator iter;
  63. C3DData* pdata = NULL;
  64. for(iter=m_listData.begin(); iter!=m_listData.end(); ++iter) {
  65. pdata = *iter;
  66. delete pdata;
  67. }
  68. m_listData.clear();
  69. }
  70. void C3DGrapher::Destroy() throw()
  71. {
  72. if(::wglGetCurrentContext())
  73. ::wglMakeCurrent(NULL, NULL);
  74. if(m_hRC) {
  75. ::wglDeleteContext(m_hRC);
  76. m_hRC = NULL;
  77. }
  78. ::ReleaseDC(m_hWnd, m_hDC);
  79. DeleteFont();
  80. }
  81. void C3DGrapher::Resize(int cx, int cy) throw()
  82. {
  83. BEGIN_GL
  84. if(cy==0)
  85. cx = 1;
  86. ::glViewport(0, 0, cx, cy);
  87. ::glMatrixMode(GL_PROJECTION);
  88. ::glLoadIdentity();
  89. if(m_nProjectionType == PERSPECTIVE) {
  90. GLdouble fAspect;
  91. fAspect = (GLdouble)cx/(GLdouble)cy;
  92. ::gluPerspective(m_dFOV, fAspect, 1.0, m_dZrange*300);
  93. } else if(m_nProjectionType == ORTHOGRAPHIC) {
  94. if(cx <= cy)
  95. ::glOrtho(-m_dXrange*1.2, m_dXrange*1.2, -m_dYrange*cy/cx*1.2, m_dYrange*cy/cx*1.2, m_dZrange*300.0, -300*m_dZrange);
  96. else
  97. ::glOrtho(-m_dXrange*cx/cy*1.2, m_dXrange*cx/cy*1.2, -m_dYrange*1.2, m_dYrange*1.2, 300.0*m_dZrange, -300*m_dZrange);
  98. }
  99. ::glMatrixMode(GL_MODELVIEW);
  100. END_GL
  101. }
  102. void C3DGrapher::SetAxisRange(double xRange, double yRange, double zRange) throw()
  103. {
  104. m_dXrange = xRange;
  105. m_dYrange = yRange;
  106. m_dZrange = zRange;
  107. }
  108. void C3DGrapher::SetBkGndColor(COLORREF col) throw()
  109. {
  110. m_colBkGnd = col;
  111. BEGIN_GL
  112. ::glClearColor(GetRValue(col)/255.0f, GetGValue(col)/255.0f, GetBValue(col)/255.0f, 1.0);
  113. END_GL
  114. }
  115. void C3DGrapher::Add3DData(C3DData* pdata) throw()
  116. {
  117. m_listData.push_back(pdata);
  118. m_nSize++;
  119. }
  120. void C3DGrapher::SetTitles(string title, string xTitle, string yTitle, string zTitle) throw()
  121. {
  122. m_strTitle = title;
  123. m_strXtitle = xTitle;
  124. m_strYtitle = yTitle;
  125. m_strZtitle = zTitle;
  126. }
  127. void C3DGrapher::SetWindowPixelFormat() throw(CGrapherException)
  128. {
  129. PIXELFORMATDESCRIPTOR pfd = {
  130. sizeof(PIXELFORMATDESCRIPTOR),
  131. 1,
  132. PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA,
  133. 24, 
  134. 0, 0, 0, 0, 0, 0,
  135. 0, 0,
  136. 0, 0, 0, 0, 0,
  137. 32,
  138. 0,
  139. 0,
  140. PFD_MAIN_PLANE,
  141. 0,
  142. 0, 0, 0
  143. };
  144. int nPixelFormat = ChoosePixelFormat(m_hDC, &pfd);
  145. if(!SetPixelFormat(m_hDC, nPixelFormat, &pfd))
  146. throw CGrapherException(_T("Failed to set pixel format"));
  147. }
  148. void C3DGrapher::CreateGLContext() throw(CGrapherException)
  149. {
  150. m_hRC = ::wglCreateContext(m_hDC);
  151. if(!m_hRC)
  152. throw CGrapherException(_T("Failed to Create RC"));
  153. if(!::wglMakeCurrent(m_hDC, m_hRC))
  154. throw CGrapherException(_T("Failed to set current RC"));
  155. ::wglMakeCurrent(m_hDC, NULL);
  156. }
  157. void C3DGrapher::CreateFont(string fontName, int height) throw(CGrapherException)
  158. {
  159. BEGIN_GL
  160. CFont font;
  161. if((m_nFontBase = ::glGenLists(96)) == 0)
  162. throw CGrapherException(_T("Can't create font"));;
  163. if(fontName == _T("symbol"))
  164. font.CreateFont(height, 0, 0, 0, FW_NORMAL, 0, FALSE, FALSE, SYMBOL_CHARSET, OUT_TT_PRECIS,
  165. CLIP_DEFAULT_PRECIS, DRAFT_QUALITY, DEFAULT_PITCH, fontName.c_str());
  166. else
  167. font.CreateFont(height, 0, 0, 0, FW_NORMAL, 0, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS,
  168. CLIP_DEFAULT_PRECIS, DRAFT_QUALITY, DEFAULT_PITCH, fontName.c_str());
  169. ::SelectObject(m_hDC, font);
  170. ::wglUseFontBitmaps(m_hDC, 32, 96, m_nFontBase);
  171. END_GL
  172. }
  173. void C3DGrapher::DeleteFont()
  174. {
  175. BEGIN_GL
  176. if(m_nFontBase == 0)
  177. return;
  178. ::glDeleteLists(m_nFontBase, 96);
  179. END_GL
  180. }
  181. void C3DGrapher::PrintString(string str, double x, double y, double z) const throw()
  182. {
  183. BEGIN_GL
  184. ::glRasterPos3f(x, y, z);
  185. ::glPushAttrib(GL_LIST_BIT);
  186. ::glListBase(m_nFontBase-32);
  187. ::glCallLists(str.length(), GL_UNSIGNED_BYTE, str.c_str());
  188. ::glPopAttrib();
  189. END_GL
  190. }
  191. void C3DGrapher::Initialize()
  192. {
  193. BEGIN_GL
  194. ::glClearDepth(1.0);
  195. if(m_bDepthTest)
  196. ::glEnable(GL_DEPTH_TEST);
  197. else
  198. ::glDisable(GL_DEPTH_TEST);
  199. if(m_bCullFace)
  200. ::glEnable(GL_CULL_FACE);
  201. else
  202. ::glDisable(GL_CULL_FACE);
  203. if(m_nShadeModel == FLAT)
  204. ::glShadeModel(GL_FLAT);
  205. else if(m_nShadeModel == SMOOTH)
  206. ::glShadeModel(GL_SMOOTH);
  207. if(m_nPolygonMode == FILL)
  208. ::glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  209. else if(m_nPolygonMode == OUTLINE)
  210. ::glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  211. ::glClearColor(GetRValue(m_colBkGnd)/255.0f, GetGValue(m_colBkGnd)/255.0f, GetBValue(m_colBkGnd)/255.0f, 1.0);
  212. ::glLineWidth(2.0); // required
  213. ::glPointSize(2.0);
  214. END_GL
  215. }