有的人说,学习要按部就班,学了几年的编程稍有点感悟,个人觉得面对技术的不断变化,以及需求的不断变更,如果按部就班的来搞,人生0.5的时间就浪费了,很多时候问自己,why I do need to learn from the beginner?ask myself why? 群里朋友说,先把MFC学好吧,多练练glut吧,shit,等我学好MFC再从Opengl+MFC了解起,黄花菜都凉了。下面是我查阅了别人的资料,自己加以修改实现的MFC下跑Opengl的程序,总结一下。
It is a good idea to isolate opengl functions from MFC Framework. SO I recommend you to create GLRender class with the fllowing picture showing:

新建MFC时候,选项不同,下图的结构也不同

注解:BOOL GLRender::PrepareScene(HDC hDC)//该函数放在OpenGL2View.cpp----OnCreate事件中,设置像素格式;创建opengl上下文
void GLRender::DrawScene()//该函数放在OpenGL2View.cpp----OnPaint事件中,Opengl绘画的内容都放于此
void GLRender::Reshape(int cx, int cy)//该函数放在OpenGL2View.cpp----OnSize事件中,MFC窗体变化时重绘内容。
BOOL GLRender::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER|PFD_SUPPORT_GDI,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
this->m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);
if(this->m_GLPixelIndex==0)
{
this->m_GLPixelIndex = 1;
if(DescribePixelFormat(hDC,this->m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
{
return FALSE;
}
}
if(SetPixelFormat(hDC,this->m_GLPixelIndex,&pixelDesc)==FALSE)
{
return FALSE;
}
return TRUE;
}
BOOL GLRender::CreateGLContext(HDC hDC)
{
this->m_hGLContext = wglCreateContext(hDC);
if(this->m_hGLContext==NULL)
{//创建失败
return FALSE;
}
if(wglMakeCurrent(hDC,this->m_hGLContext)==FALSE)
{//选为当前RC失败
return FALSE;
}
return TRUE;
}
BOOL GLRender::PrepareScene(HDC hDC)
{
if(this->SetWindowPixelFormat(hDC)==FALSE)
{
return 0;
}
if(this->CreateGLContext(hDC)==FALSE)
{
return 0;
}
}
void GLRender::Reshape(int cx, int cy)
{
GLsizei width,height;
GLdouble aspect;
width = cx;
height = cy;
if(cy==0)
{
aspect = (GLdouble)width;
}
else
{
aspect = (GLdouble)width/(GLdouble)height;
}
glViewport(-100,0,width+100,height+100);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0*aspect,0.0,500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
x_width=width;
y_height=height;
}
void GLRender::DrawScene()
{
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
// Set up the camera
//
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Determine the screen size so we can determine the aspect ratio
int width, height;
width=x_width;
height=y_height;
GLdouble aspectratio = GLdouble(width) / GLdouble(height);
// Set the camera parameters
gluPerspective(25., // Vertical FOV degrees.
aspectratio, // The aspect ratio.
10., // Near clipping 40/130
200.); // Far clipping
// Set the camera location
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(20., 10., 50., // eye x,y,z
0., 0., 0., // center x,y,z
0., 1., 0.); // Up direction
//
// Some standard parameters
//
// Enable depth test
glEnable(GL_DEPTH_TEST);
// Cull backfacing polygons
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
// Draw a coordinate axis
glColor3d(0., 1., 1.);
glBegin(GL_LINES);
glVertex3d(0., 0., 0.);
glVertex3d(12., 0., 0.);
glVertex3d(0., 0., 0.);
glVertex3d(0., 12., 0.);
glVertex3d(0., 0., 0.);
glVertex3d(0., 0., 12.);
glEnd();
//
// INSERT DRAWING CODE HERE
//
glFlush();
}
DestorySecene函数没有写。简单点就是:准备创建场景;绘制场景;窗口变化时重新绘制场景,虽然简单还是费了不少功夫。通过该框架只需要修改DrawScene的内容,就可以实现不同内容绘制,如果要用glut,把与winds相关的内容删除掉,将glutDisplayFunc那个函数与DrawScene绑定即可,别的文件就不需要修改嘞。如下图

http://download.csdn.net/detail/wuxiaoyan_1988/5828815全部代码这里下载。