Nehe的几个OpenGL框架都是Win32 sdk版本的,我现在需要在MFC下学习OpenGL,今天看了他写的第一个OpenGL框架,就在MFC中实现了下。为了简单起见,把全屏那部分就抛弃掉了,毕竟重点不在这上面,而且MFC要实现这个全屏的功能也不像sdk那么容易。。。
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->//openGLDemoView.h:interfaceoftheCOpenGLDemoViewclass
//
/////////////////////////////////////////////////////////////////////////////
protected:
BOOLSetWindowPixelFormat(HDChDC);//设置像素格式
BOOLCreateViewGLContext(HDChDC);//创建绘制环境(RC)并使之成为当前绘制环境
BOOLInitGL(GLvoid);//初始化openGL
intDrawGLScene(GLvoid);//绘图代码区
intm_GLPixelIndex;
HGLRCm_hGLContext;//绘制环境
//
/////////////////////////////////////////////////////////////////////////////
protected:
BOOLSetWindowPixelFormat(HDChDC);//设置像素格式
BOOLCreateViewGLContext(HDChDC);//创建绘制环境(RC)并使之成为当前绘制环境
BOOLInitGL(GLvoid);//初始化openGL
intDrawGLScene(GLvoid);//绘图代码区
intm_GLPixelIndex;
HGLRCm_hGLContext;//绘制环境
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->//openGLDemoView.cpp:implementationoftheCOpenGLDemoViewclass
//
COpenGLDemoView::COpenGLDemoView()
{
//TODO:addconstructioncodehere
this->m_GLPixelIndex=0;
this->m_hGLContext=NULL;
}
BOOLCOpenGLDemoView::PreCreateWindow(CREATESTRUCT&cs)
{
//TODO:ModifytheWindowclassorstylesherebymodifying
//theCREATESTRUCTcs
cs.style|=(WS_CLIPCHILDREN|WS_CLIPSIBLINGS);//openGL必需的
returnCView::PreCreateWindow(cs);
}
BOOLCOpenGLDemoView::SetWindowPixelFormat(HDChDC)
{//定义窗口的像素格式
PIXELFORMATDESCRIPTORpixelDesc=
{
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)
{//用默认的像素格式进行设置
returnFALSE;
}
}
if(SetPixelFormat(hDC,this->m_GLPixelIndex,&pixelDesc)==FALSE)
{
returnFALSE;
}
returnTRUE;
}
BOOLCOpenGLDemoView::InitGL(GLvoid)//AllSetupForOpenGLGoesHere
{
glShadeModel(GL_SMOOTH);//EnableSmoothShading
glClearColor(0.0,0.0,0.0,0.0);//BlackBackground
glClearDepth(1.0f);//DepthBufferSetup
glEnable(GL_DEPTH_TEST);//EnablesDepthTesting
glDepthFunc(GL_LEQUAL);//TheTypeOfDepthTestingToDo
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ReallyNicePerspectiveCalculations
returnTRUE;//InitializationWentOK
}
BOOLCOpenGLDemoView::CreateViewGLContext(HDChDC)
{
this->m_hGLContext=wglCreateContext(hDC);//创建RC
if(this->m_hGLContext==NULL)
{//创建失败
returnFALSE;
}
if(wglMakeCurrent(hDC,this->m_hGLContext)==FALSE)
{//选为当前RC失败
returnFALSE;
}
returnTRUE;
}
intCOpenGLDemoView::OnCreate(LPCREATESTRUCTlpCreateStruct)
{
if(CView::OnCreate(lpCreateStruct)==-1)
return-1;
//TODO:Addyourspecializedcreationcodehere
HWNDhWnd=this->GetSafeHwnd();
HDChDC=::GetDC(hWnd);
if(this->SetWindowPixelFormat(hDC)==FALSE)
{//设置像素格式
return0;
}
if(this->CreateViewGLContext(hDC)==FALSE)
{//创建RC并选为所用
return0;
}
if(!this->InitGL())
{//初始化openGL
return0;
}
return0;
}
voidCOpenGLDemoView::OnDestroy()
{
CView::OnDestroy();
//TODO:Addyourmessagehandlercodehere
if(wglGetCurrentContext()!=NULL)
{
wglMakeCurrent(NULL,NULL);
}
if(this->m_hGLContext!=NULL)
{
wglDeleteContext(this->m_hGLContext);
this->m_hGLContext=NULL;
}
}
voidCOpenGLDemoView::OnSize(UINTnType,intcx,intcy)
{
CView::OnSize(nType,cx,cy);
//TODO:Addyourmessagehandlercodehere
GLsizeiwidth,height;
width=cx;
height=cy;
if(height==0)//PreventADivideByZeroBy
{
height=1;//MakingHeightEqualOne
}
glViewport(0,0,width,height);//ResetTheCurrentViewport
glMatrixMode(GL_PROJECTION);//SelectTheProjectionMatrix
glLoadIdentity();//ResetTheProjectionMatrix
//CalculateTheAspectRatioOfTheWindow
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);//透视投影
glMatrixMode(GL_MODELVIEW);//SelectTheModelviewMatrix
glLoadIdentity();//ResetTheModelviewMatrix
}
voidCOpenGLDemoView::OnPaint()
{
CPaintDCdc(this);//devicecontextforpainting
this->DrawGLScene();
}
intCOpenGLDemoView::DrawGLScene(GLvoid)
{//Here'sWhereWeDoAllTheDrawing
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//ClearScreenAndDepthBuffer
glLoadIdentity();//ResetTheCurrentModelviewMatrix
glTranslatef(-1.5f,0.0f,-6.0f);//物体左移1.5,向内移6,相当于移动镜头一样,让物体进入镜头中
glBegin(GL_TRIANGLES);//绘制三角形
glColor3f(255.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);//上顶点
glColor3f(0.0f,255.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);//左下
glColor3f(0.0f,0.0f,255.0f);
glVertex3f(1.0f,-1.0f,0.0f);//右下
glEnd();//三角形绘制结束
glTranslatef(3.0f,0.0f,0.0f);
glBegin(GL_QUADS);//绘制正方形
glColor3f(255.0f,0.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);//左上
glColor3f(0.0f,255.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);//右上
glColor3f(0.0f,0.0f,255.0f);
glVertex3f(1.0f,-1.0f,0.0f);//左下
glColor3f(255.255f,255.0f,255.0f);
glVertex3f(-1.0f,-1.0f,0.0f);//右下
glEnd();//正方形绘制结束
glFlush();
returnTRUE;//EverythingWentOK
}
//
COpenGLDemoView::COpenGLDemoView()
{
//TODO:addconstructioncodehere
this->m_GLPixelIndex=0;
this->m_hGLContext=NULL;
}
BOOLCOpenGLDemoView::PreCreateWindow(CREATESTRUCT&cs)
{
//TODO:ModifytheWindowclassorstylesherebymodifying
//theCREATESTRUCTcs
cs.style|=(WS_CLIPCHILDREN|WS_CLIPSIBLINGS);//openGL必需的
returnCView::PreCreateWindow(cs);
}
BOOLCOpenGLDemoView::SetWindowPixelFormat(HDChDC)
{//定义窗口的像素格式
PIXELFORMATDESCRIPTORpixelDesc=
{
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)
{//用默认的像素格式进行设置
returnFALSE;
}
}
if(SetPixelFormat(hDC,this->m_GLPixelIndex,&pixelDesc)==FALSE)
{
returnFALSE;
}
returnTRUE;
}
BOOLCOpenGLDemoView::InitGL(GLvoid)//AllSetupForOpenGLGoesHere
{
glShadeModel(GL_SMOOTH);//EnableSmoothShading
glClearColor(0.0,0.0,0.0,0.0);//BlackBackground
glClearDepth(1.0f);//DepthBufferSetup
glEnable(GL_DEPTH_TEST);//EnablesDepthTesting
glDepthFunc(GL_LEQUAL);//TheTypeOfDepthTestingToDo
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ReallyNicePerspectiveCalculations
returnTRUE;//InitializationWentOK
}
BOOLCOpenGLDemoView::CreateViewGLContext(HDChDC)
{
this->m_hGLContext=wglCreateContext(hDC);//创建RC
if(this->m_hGLContext==NULL)
{//创建失败
returnFALSE;
}
if(wglMakeCurrent(hDC,this->m_hGLContext)==FALSE)
{//选为当前RC失败
returnFALSE;
}
returnTRUE;
}
intCOpenGLDemoView::OnCreate(LPCREATESTRUCTlpCreateStruct)
{
if(CView::OnCreate(lpCreateStruct)==-1)
return-1;
//TODO:Addyourspecializedcreationcodehere
HWNDhWnd=this->GetSafeHwnd();
HDChDC=::GetDC(hWnd);
if(this->SetWindowPixelFormat(hDC)==FALSE)
{//设置像素格式
return0;
}
if(this->CreateViewGLContext(hDC)==FALSE)
{//创建RC并选为所用
return0;
}
if(!this->InitGL())
{//初始化openGL
return0;
}
return0;
}
voidCOpenGLDemoView::OnDestroy()
{
CView::OnDestroy();
//TODO:Addyourmessagehandlercodehere
if(wglGetCurrentContext()!=NULL)
{
wglMakeCurrent(NULL,NULL);
}
if(this->m_hGLContext!=NULL)
{
wglDeleteContext(this->m_hGLContext);
this->m_hGLContext=NULL;
}
}
voidCOpenGLDemoView::OnSize(UINTnType,intcx,intcy)
{
CView::OnSize(nType,cx,cy);
//TODO:Addyourmessagehandlercodehere
GLsizeiwidth,height;
width=cx;
height=cy;
if(height==0)//PreventADivideByZeroBy
{
height=1;//MakingHeightEqualOne
}
glViewport(0,0,width,height);//ResetTheCurrentViewport
glMatrixMode(GL_PROJECTION);//SelectTheProjectionMatrix
glLoadIdentity();//ResetTheProjectionMatrix
//CalculateTheAspectRatioOfTheWindow
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);//透视投影
glMatrixMode(GL_MODELVIEW);//SelectTheModelviewMatrix
glLoadIdentity();//ResetTheModelviewMatrix
}
voidCOpenGLDemoView::OnPaint()
{
CPaintDCdc(this);//devicecontextforpainting
this->DrawGLScene();
}
intCOpenGLDemoView::DrawGLScene(GLvoid)
{//Here'sWhereWeDoAllTheDrawing
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//ClearScreenAndDepthBuffer
glLoadIdentity();//ResetTheCurrentModelviewMatrix
glTranslatef(-1.5f,0.0f,-6.0f);//物体左移1.5,向内移6,相当于移动镜头一样,让物体进入镜头中
glBegin(GL_TRIANGLES);//绘制三角形
glColor3f(255.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);//上顶点
glColor3f(0.0f,255.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);//左下
glColor3f(0.0f,0.0f,255.0f);
glVertex3f(1.0f,-1.0f,0.0f);//右下
glEnd();//三角形绘制结束
glTranslatef(3.0f,0.0f,0.0f);
glBegin(GL_QUADS);//绘制正方形
glColor3f(255.0f,0.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);//左上
glColor3f(0.0f,255.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);//右上
glColor3f(0.0f,0.0f,255.0f);
glVertex3f(1.0f,-1.0f,0.0f);//左下
glColor3f(255.255f,255.0f,255.0f);
glVertex3f(-1.0f,-1.0f,0.0f);//右下
glEnd();//正方形绘制结束
glFlush();
returnTRUE;//EverythingWentOK
}

本文介绍如何在MFC中实现OpenGL编程,包括设置像素格式、创建OpenGL环境、初始化OpenGL及绘图等关键步骤。
1750

被折叠的 条评论
为什么被折叠?



