VC6.0中基于MFC 和OpenGL的图形编程 第一步:选择一个开发工具
在Windows系统中,我们选择VC++ 6.0作为学习OpenGL的环境。
第二步:安装GLUT工具包
GLUT不是OpenGL所必须的,但它会给我们的学习带来一定的方便,推荐安装。 Windows环境下的GLUT下载地址:(大小约为150k)
http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip Windows环境下安装GLUT的步骤: 1、将下载的压缩包解开,将得到5个文件 2、以我的安装目录为例:
(1)“c:\\Program Files\\Microsoft Visual Studio\\VC98\\include\\GL文件夹”。把解压得到的glut.h放到这个GL文件夹里。没有GL文件夹可以自己建一个,一般都有的。
(2)“c:\\Program Files\\Microsoft Visual Studio\\VC98\\lib文件夹”。把解压得到的glut.lib和glut32.lib放到静态函数库所在文件夹,即lib文件夹。
(3)把解压得到的glut.dll和glut32.dll放到操作系统目录下面的system32文件夹内。(典型的位置为:C:\\Windows\\System32)这是非常重要的动态链接库设置!
第三步,创建并设置工程
(1)在VC++ 6.0中创建一个MFC AppWizard[exe]的单文档应用程序,例如工程名为MFCOpenGL。
(2)链接OpenGL libraries:在Visual C++中先单击Project,再单击Settings,再找到Link单击,最后在Object/library modules 的最前面加上opengl32.lib Glut32.lib Glaux.lib glu32.lib 。
第四步,初始化OpenGL绘图环境 1, 在stdafx.h中加入下列语句:
//OpenGL Headers #include
WM_ERASEBACKGROUND (for OnEraseBkground).
3,在窗口创建之前我们必须设置窗口风格包含WS_CLIPCHILDREN和
WS_CLIPSIBLINGS,从而避免OpenGL绘制到其他窗口中去。这些应该放在PreCreateWindow()中。
BOOL CMFCOpenGLView::PreCreateWindow(CREATESTRUCT& cs) {
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs //
An OpenGL Window must be created with the following flags
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN; return CView::PreCreateWindow(cs); }
4, 在CMFCOpenGLView.h中加入如下语句:
HGLRC m_hRC; //Rendering Context
CDC* m_pDC; //Device Context
BOOL InitializeOpenGL(); //Initialize OpenGL BOOL SetupPixelFormat(); //Set up the Pixel Format
void RenderScene(); //Render the Scene
5, 在OnCreate()中我们将通过建立像素格式和绘制上下文来初始化OpenGL. 在InitializeOpenGL()中会创建一个设备上下文(DC),为这个DC选择一个像素格式,创建和这个DC相关的绘制上下文(RC),然后选择这个RC.这个函数会调用SetupPixelFormat()来建立像素格式。
int CMFCOpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct) {
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//Initialize OpenGL Here InitializeOpenGL(); return 0; }
BOOL CMFCOpenGLView::InitializeOpenGL() {
//Get a DC for the Client Area m_pDC = new CClientDC(this); //Failure to Get DC if(m_pDC == NULL) {
MessageBox(\Obtaining DC\ return FALSE; }
//Failure to set the pixel format if(!SetupPixelFormat()) {
return FALSE; }
//Create Rendering Context
m_hRC = ::wglCreateContext (m_pDC->GetSafeHdc ()); //Failure to Create Rendering Context if(m_hRC == 0) {
MessageBox(\Creating RC\ return FALSE; }
//Make the RC Current
if(::wglMakeCurrent (m_pDC->GetSafeHdc (), m_hRC)==FALSE) {
MessageBox(\making RC Current\ return FALSE; }
//Specify Black as the clear color ::glClearColor(0.0f,0.0f,0.0f,0.0f);
//Specify the back of the buffer as clear depth ::glClearDepth(1.0f); //Enable Depth Testing ::glEnable(GL_DEPTH_TEST); return TRUE; }
//Setup Pixel Format
/////////////////////////////////////////////////////////////////////////////
BOOL CMFCOpenGLView::SetupPixelFormat() {
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
16, // 16-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 m_nPixelFormat = ::ChoosePixelFormat(m_pDC->GetSafeHdc
(), &pfd);
if ( m_nPixelFormat == 0 ) {
return FALSE; }
if ( ::SetPixelFormat(m_pDC->GetSafeHdc(), m_nPixelFormat, &pfd) == FALSE) {
return FALSE; }
return TRUE; }
6, 在OnSize()中一般用来设置视口和视锥,因为这些是和窗口大小相关的。基本操作包括设置视口,选择投影矩阵,设置模型视图矩阵。 void { }
CMFCOpenGLView::OnSize(UINT nType, int cx, int cy)
CView::OnSize(nType, cx, cy);
GLdouble aspect_ratio; // width/height ratio
if ( cx <= 0 || cy <= 0 ) {
return; }
// select the full client area ::glViewport(0, 0, cx, cy); // compute the aspect ratio
// this will keep all dimension scales equal aspect_ratio = (GLdouble)cx/(GLdouble)cy;
// select the projection matrix and clear it ::glMatrixMode(GL_PROJECTION); ::glLoadIdentity();
// select the viewing volume
::gluPerspective(45.0f, aspect_ratio, .01f, 200.0f);
// switch back to the modelview matrix and clear it ::glMatrixMode(GL_MODELVIEW); ::glLoadIdentity();
7,在绘制场景时,一般包括如下步骤: 1)清空缓存。 2)绘制场景。