OpenGLMFCView.cpp
上传用户:yanjing
上传日期:2022-06-12
资源大小:36k
文件大小:7k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // OpenGLMFCView.cpp : implementation of the COpenGLMFCView class
  2. //
  3. #include "stdafx.h"
  4. #include "OpenGLMFC.h"
  5. #include "OpenGLMFCDoc.h"
  6. #include "OpenGLMFCView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // COpenGLMFCView
  14. IMPLEMENT_DYNCREATE(COpenGLMFCView, CScrollView)
  15. BEGIN_MESSAGE_MAP(COpenGLMFCView, CScrollView)
  16. //{{AFX_MSG_MAP(COpenGLMFCView)
  17. ON_WM_DESTROY() //*******************************************
  18. ON_WM_SIZE()    //*******************************************
  19. ON_WM_CREATE()  //*******************************************
  20. //}}AFX_MSG_MAP
  21. // Standard printing commands
  22. ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
  23. ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // COpenGLMFCView construction/destruction
  28. COpenGLMFCView::COpenGLMFCView()
  29. {
  30. // TODO: add construction code here
  31. }
  32. COpenGLMFCView::~COpenGLMFCView()
  33. {
  34. }
  35. BOOL COpenGLMFCView::PreCreateWindow(CREATESTRUCT& cs)
  36. {
  37. // TODO: Modify the Window class or styles here by modifying
  38. //  the CREATESTRUCT cs
  39. return CScrollView::PreCreateWindow(cs);
  40. }
  41. /////////////////////////////////////////////////////////////////////////////
  42. // COpenGLMFCView drawing
  43. void COpenGLMFCView::OnDraw(CDC* pDC)
  44. {
  45. COpenGLMFCDoc* pDoc = GetDocument();
  46. ASSERT_VALID(pDoc);
  47. // TODO: add draw code for native data here
  48. //*******************************************
  49.     /////////////////////////////////////////////
  50. wglMakeCurrent(pDC->m_hDC,hglrc);
  51. drawwithopengl();
  52. wglMakeCurrent(pDC->m_hDC,NULL);
  53. SwapBuffers(pDC->m_hDC);
  54. /////////////////////////////////////////////
  55. //*******************************************
  56. }
  57. void COpenGLMFCView::OnInitialUpdate()
  58. {
  59. CScrollView::OnInitialUpdate();
  60. CSize sizeTotal;
  61. // TODO: calculate the total size of this view
  62. sizeTotal.cx = sizeTotal.cy = 100;
  63. SetScrollSizes(MM_TEXT, sizeTotal);
  64. }
  65. /////////////////////////////////////////////////////////////////////////////
  66. // COpenGLMFCView printing
  67. BOOL COpenGLMFCView::OnPreparePrinting(CPrintInfo* pInfo)
  68. {
  69. // default preparation
  70. return DoPreparePrinting(pInfo);
  71. }
  72. void COpenGLMFCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  73. {
  74. // TODO: add extra initialization before printing
  75. }
  76. void COpenGLMFCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  77. {
  78. // TODO: add cleanup after printing
  79. }
  80. /////////////////////////////////////////////////////////////////////////////
  81. // COpenGLMFCView diagnostics
  82. #ifdef _DEBUG
  83. void COpenGLMFCView::AssertValid() const
  84. {
  85. CScrollView::AssertValid();
  86. }
  87. void COpenGLMFCView::Dump(CDumpContext& dc) const
  88. {
  89. CScrollView::Dump(dc);
  90. }
  91. COpenGLMFCDoc* COpenGLMFCView::GetDocument() // non-debug version is inline
  92. {
  93. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COpenGLMFCDoc)));
  94. return (COpenGLMFCDoc*)m_pDocument;
  95. }
  96. #endif //_DEBUG
  97. /////////////////////////////////////////////////////////////////////////////
  98. // COpenGLMFCView message handlers
  99. BOOL COpenGLMFCView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
  100. {
  101. // TODO: Add your specialized code here and/or call the base class
  102.     
  103. return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
  104. }
  105. void COpenGLMFCView::OnDestroy() 
  106. {
  107. CScrollView::OnDestroy();
  108. // TODO: Add your message handler code here
  109. //*******************************************
  110. /////////////////////////////////////////////
  111. // delete rending context
  112. wglDeleteContext(hglrc);
  113. /////////////////////////////////////////////
  114. //*******************************************
  115. }
  116. void COpenGLMFCView::OnSize(UINT nType, int cx, int cy) 
  117. {
  118. CScrollView::OnSize(nType, cx, cy);
  119. // TODO: Add your message handler code here
  120. //*******************************************
  121. int w=cx;
  122. int h=cy;
  123. GLfloat nRange = 100.0f;
  124. // prevent divied by aero
  125. if(h==0)
  126. h=1;
  127. // set viewport to windows dimensions
  128. glViewport(0,0,w,h);
  129. // reset coordinate system
  130.     glMatrixMode(GL_PROJECTION);
  131. glLoadIdentity();
  132. // establish cliping volune (left,right,top,near,far)
  133. if(w<=h)
  134. glOrtho(-nRange,nRange,-nRange*h/w,nRange*h/w,-nRange,nRange);
  135. else
  136.     glOrtho(-nRange*w/h,nRange*w/h,-nRange,nRange,-nRange,nRange);
  137. glMatrixMode(GL_MODELVIEW);
  138.     glLoadIdentity();
  139. //*******************************************
  140. }
  141. //*******************************************
  142. /////////////////////////////////////////////
  143. void COpenGLMFCView::drawwithopengl(void)
  144. {
  145. float z,angle,x,y;
  146. // clear the window with current clearing color
  147. glClear(GL_COLOR_BUFFER_BIT);
  148. glClearColor(0.0,1.0,0.0,1.0);
  149. // save matrix state and do the rotation
  150. glPushMatrix();
  151. // call only once for all remaining points
  152. glColor3f(1.0,0.0,0.0);
  153. glBegin(GL_LINES);
  154. z=0.0f;
  155. for(angle=0.0f;angle<=3.14159260f;angle+=0.1f)
  156. {
  157. // top half of the circle
  158. x=50.0f*sin(angle);
  159. y=50.0f*cos(angle);
  160. glVertex3f(x,y,z);
  161. // bottom half of the circle
  162. x=50.0f*sin(angle+3.1415f);
  163. y=50.0f*cos(angle+3.1415f);
  164. glVertex3f(x,y,z);
  165. }
  166. // done drawing points
  167. glEnd();
  168. // restore transformations
  169. glPopMatrix();
  170. /*
  171. // 画填充多边形
  172. glClearColor(1.0,1.0,1.0,1.0);
  173. glPolygonMode(GL_FRONT,GL_FILL);
  174. glTranslatef(90.0,0.0,0.0);
  175. glColor3f(0.0,0.0,1.0);
  176. glBegin(GL_POLYGON);
  177. glVertex2f(10.0,0.0);
  178. glVertex2f(50.0,0.0);
  179. glVertex2f(60.0,3.0);
  180. glVertex2f(0.0,5.0);
  181. glEnd();
  182. */
  183. // flush drawing commands
  184. glFlush();
  185. }
  186. /////////////////////////////////////////////
  187. //*******************************************
  188. int COpenGLMFCView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  189. {
  190. if (CScrollView::OnCreate(lpCreateStruct) == -1)
  191. return -1;
  192. // TODO: Add your specialized creation code here
  193. //*******************************************
  194. //以下代码不能加入到Create()函数中
  195. //*******************************************
  196. /////////////////////////////////////////////
  197. // initializing the pixel format
  198. PIXELFORMATDESCRIPTOR pfd=
  199. {
  200. sizeof(PIXELFORMATDESCRIPTOR),
  201. 1,
  202. PFD_DRAW_TO_WINDOW|
  203. PFD_SUPPORT_OPENGL|
  204. PFD_DOUBLEBUFFER,
  205. PFD_TYPE_RGBA,
  206. 24,
  207. 0,0,0,0,0,0,
  208. 0,
  209. 0,
  210. 0,
  211. 0,0,0,0,
  212. 32,
  213. 0,
  214. 0,
  215. PFD_MAIN_PLANE,
  216. 0,
  217. 0,0,0,
  218. };
  219. // get the dc for the client area
  220. CClientDC clientdc(this);
  221. // return the requested pixel format closely matched
  222. int pf=ChoosePixelFormat(clientdc.m_hDC,&pfd);
  223. // set the pixel format
  224. BOOL rt=SetPixelFormat(clientdc.m_hDC,pf,&pfd);
  225. // create the rending context
  226. hglrc=wglCreateContext(clientdc.m_hDC);
  227. /////////////////////////////////////////////
  228. //*******************************************
  229. return 0;
  230. }