在多文档应用程序中使用OpenGL绘图

 
1、在视图类的头文件中添加两个成员变量:

   1: public:
   2:     int m_GLPixelIndex;
   3:     HGLRC m_hGLContext;

2、在视图类的构造函数中对这两个成员进行初始化:

   1: m_GLPixelIndex = 0;
   2: m_hGLContext = NULL;

3、在视图类声明中添加两个成员函数:

   1: BOOL SetWindowPixelFormat(HDC hDC);
   2: BOOL CreateViewGLContext(HDC hDC);

4、在视图类的实现文件中添加这两个成员函数的实现:

   1: BOOL CXXXView::CreateViewGLContext(HDC hDC)
   2: {
   3:     m_hGLContext = wglCreateContext(hDC);
   4:  
   5:     if(m_hGLContext==NULL)
   6:         return FALSE;
   7:  
   8:     if(wglMakeCurrent(hDC,m_hGLContext)==FALSE)
   9:         return FALSE;
  10:  
  11:     return TRUE;    
  12: }

以及

   1: BOOL CXXXView::SetWindowPixelFormat(HDC hDC)
   2: {
   3:     PIXELFORMATDESCRIPTOR pixelDesc;
   4:  
   5:     pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
   6:     pixelDesc.nVersion = 1;
   7:  
   8:     pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
   9:         PFD_DOUBLEBUFFER | PFD_STEREO_DONTCARE;
  10:  
  11:     pixelDesc.iPixelType = PFD_TYPE_RGBA;
  12:     pixelDesc.cColorBits = 32;
  13:     pixelDesc.cRedBits = 8;
  14:     pixelDesc.cRedShift = 16;
  15:     pixelDesc.cGreenBits = 8;
  16:     pixelDesc.cGreenShift = 8;
  17:     pixelDesc.cBlueBits = 8;
  18:     pixelDesc.cBlueShift = 0;
  19:     pixelDesc.cAlphaBits = 0;
  20:     pixelDesc.cAlphaShift = 0;
  21:     pixelDesc.cAccumBits = 64;
  22:     pixelDesc.cAccumRedBits = 16;
  23:     pixelDesc.cAccumGreenBits = 16;
  24:     pixelDesc.cAccumBlueBits = 16;
  25:     pixelDesc.cAccumAlphaBits = 0;
  26:     pixelDesc.cDepthBits = 32;
  27:     pixelDesc.cStencilBits = 8;
  28:     pixelDesc.cAuxBuffers = 0;
  29:     pixelDesc.iLayerType = PFD_MAIN_PLANE;
  30:     pixelDesc.bReserved = 0;
  31:     pixelDesc.dwLayerMask = 0;
  32:     pixelDesc.dwVisibleMask = 0;
  33:     pixelDesc.dwDamageMask = 0;
  34:  
  35:     m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);
  36:     if(m_GLPixelIndex == 0) // Choose default
  37:     {
  38:         m_GLPixelIndex = 1;
  39:         if(DescribePixelFormat(hDC,m_GLPixelIndex,
  40:             sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
  41:             return FALSE;
  42:     }
  43:  
  44:     if(!SetPixelFormat(hDC,m_GLPixelIndex,&pixelDesc))
  45:         return FALSE;
  46:  
  47:     return TRUE;
  48: }

在视图类的OnCreate成员函数中添加:

   1: HWND hWnd = GetSafeHwnd();
   2: HDC hDC = ::GetDC(hWnd);
   3:  
   4: if(SetWindowPixelFormat(hDC)==FALSE)
   5:     return -1;
   6:  
   7: if(CreateViewGLContext(hDC)==FALSE)
   8:     return -1;

添加OnPaint消息响应函数,在函数中加入:

   1: HWND hWnd = GetSafeHwnd();
   2: HDC hDC = ::GetDC(hWnd);
   3: wglMakeCurrent(dc.m_ps.hdc,m_hGLContext);
   4:  
   5: ....
   6:  
   7: // Double buffer
   8: SwapBuffers(dc.m_ps.hdc);
   9: glFlush();
  10:  
  11: // release
  12: ::ReleaseDC(hWnd,hDC);

添加OnSize消息响应函数。这里的内容可以根据需要进行修改:

   1: glViewport(0,0,(GLsizei)cx,(GLsizei)cy);
   2: glMatrixMode(GL_PROJECTION);
   3: glLoadIdentity();
   4: gluPerspective(60.0,(GLfloat)cx/(GLfloat)cy,0.1,100.0);
   5: glMatrixMode(GL_MODELVIEW);
   6: glLoadIdentity();

添加虚函数OnActivateView,在其中添加:

   1: HWND hWnd = GetSafeHwnd();
   2: HDC hDC = ::GetDC(hWnd);
   3: wglMakeCurrent(hDC,m_hGLContext);
   4: ::ReleaseDC(hWnd,hDC);

添加WM_DESTROY消息响应函数,在其中添加:

   1: if(wglGetCurrentContext() != NULL)
   2:     wglMakeCurrent(NULL,NULL);
   3:  
   4: if(m_hGLContext != NULL)
   5: {
   6:     wglDeleteContext(m_hGLContext);
   7:     m_hGLContext = NULL;
   8: }

Over。

你可能感兴趣的:(OpenGL)