MFC下使用OpenGL

1.MFC下创建OpenGL程序,必须先创建一个图形操作描述表(Rending Context),然后启动它,按常规方式调用OpenGL函数绘制图形,最后销毁它。windows提供了wgl前缀的函数,其中有5个用于管理图形操作描述表。

  wglCreateContext()

  wglDeleteContext()

  wglGetCurrentContext()

  wglGetCurrentDC()

  wglMakeCurrent()

 具体用法可参见MSDN。

例子如下:

HDC    hdc;
HGLRC  hglrc;

// create a rendering context
hglrc = wglCreateContext (hdc);

// make it the calling thread's current rendering context
wglMakeCurrent (hdc, hglrc);

// call OpenGL APIs as desired ...

// when the rendering context is no longer needed ... 

// make the rendering context not current
wglMakeCurrent (NULL, NULL) ;

// delete the rendering context
wglDeleteContext (hglrc);

 

2.在创建一个Rending Context之前,必须设置设备的像素格式,windows提供了4个函数用于该操作,分别是:

  ChoosePixelFormat()

  DescribePixelFormat()

  GetPixelFormat()

  SetPixelFormat()

 这其中,有个很重要的PIXELFORMATEDISCRIPTOR结构体,需要填充相应的字段。应用例子如下(MSDN):

PIXELFORMATDESCRIPTOR pfd = {     sizeof(PIXELFORMATDESCRIPTOR),   // size of this pfd    
 1,                     // version number  
 PFD_DRAW_TO_WINDOW |   // support window    
 PFD_SUPPORT_OPENGL |   // support OpenGL   
 PFD_DOUBLEBUFFER,      // double buffered     
 PFD_TYPE_RGBA,         // RGBA type     
 24,                    // 24-bit color depth     
 0, 0, 0, 0, 0, 0,      // color bits ignored     
 0,                     // no alpha buffer     
 0,                     // shift bit ignored     
 0,                     // no accumulation buffer     
 0, 0, 0, 0,            // accum bits ignored    
 32,                    // 32-bit z-buffer   
 0,                     // no stencil buffer    
 0,                     // no auxiliary buffer    
 PFD_MAIN_PLANE,        // main layer   
 0,                     // reserved   
 0, 0, 0                // layer masks ignored
 };
 HDC  hdc; 
int  iPixelFormat;  
// get the best available match of pixel format for the device context 
iPixelFormat = ChoosePixelFormat(hdc, &pfd); 
 // make that the pixel format of the device context
 SetPixelFormat(hdc, iPixelFormat, &pfd); 
3.需要响应的消息和重写的函数可能有:
WM_CREATE
WM_DESTROY
WM_SIZE
WM_ERASEBKGND

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(OpenGL)