Nehe的OpenGL框架(MFC版)

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

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;
//绘制环境

<!--<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
}

200772901..jpg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值