- 2
- 0
- 约4.8千字
- 约 5页
- 2015-10-16 发布于江苏
- 举报
如何在MFC中调用OpenGL.doc
如何在MFC中调用OpenGL
第一步:
在CView类cpp中添加头文件支持:
#include gl/GL.h
#include gl/glut.h
第二步:
更改窗口样式使其支持OpenGL.
在CView类成员函数PreCreateWindow中添加代码
cs.style|=WS_CLIPSIBLINGS|WS_CLIPCHILDREN;
第三步:
重写CView中的Create函数,在函数中添加代码
CString ClassName=AfxRegisterWndClass(CS_OWNDC);
第四步:
重新设置像素格式,并将其设置为当前像素格式
在CView类中添加public型成员函数
BOOL SetupPixelFormat(HDC hDC);
函数体如下:
//step 3 重新设置像素格式
BOOL CXXXMFCGLView::SetupPixelFormat(HDC hDC)
{
//重新设置画图窗口的像素格式
static 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
};
int pixelformat;
//分配一个像素格式号
if ( (pixelformat = ChoosePixelFormat(hdc, pfd)) == 0 )
{
MessageBox(LChoosePixelFormat failed);
return FALSE;
}
//设置为当前的像素格式
if (SetPixelFormat(hdc, pixelformat, pfd) == FALSE)
{
MessageBox(LSetPixelFormat failed);
return FALSE;
}
return TRUE;
}
第五步:
在CView类中添加消息WM_CREATE,在OnCreate函数中添加代码
HDC hDC;
HGLRC hRC;
hDC=::GetDC(GetSafeHwnd());
SetupPixelFormat(hDC);
hRC=wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
第六步:
初始化OpenGL
重写CView类中的OnInitialUpdate,根据需要在OnInitialUpdate中添加代码
这里OnInitialUpdate的作用与常用的OpenGL控制台程序中init()相
原创力文档

文档评论(0)