手把手教你搭建用MFC进行OpenGL编程的框架第一步:创建项目文件File||New||Project||MFCAppWizard(exe)||输入ProjectName||创建一个基于SDI,View类基于CView的工程文件;第二步:向项目文件中添加OpenGL的绘图函数Project||Settings||在Object/librarymodules:中输入opengl32.lib,glu32.lib,glaux.lib,三者之间用空格隔开,逗号不用输入;第三步:添加一些代码1)在项目工作区的FileView中找到StdAfx.h,添加下面的代码:#includeGL/gl.h#includeGL/glu.h#includeGL/glaux.h2)在项目工作区的ClassView中找到CView类,右击CView类,选择AddMemberVairable,添加一个成员变量HGLRCm_hRC;选择AddMemberFounction,添加一个成员函数voidDrawScene();3)在项目工作区的ClassView中找到CView类,右击CView类,选择AddWindowsMessageHandler,为CView类添加WM_CREATE,WM_DESTORY(方法:在左边的Newwindowsmessage/events中选中并双击,然后点击OK)。在项目工作区的ClassView中找到CView类,找到函数OnCreate(),在该函数中添加如下代码://定义像素存储格式PIXELFORMATDESCRIPTORpfd={sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL,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,};CClientDCdc(this);intpixelFormat=ChoosePixelFormat(dc.m_hDC,&pfd);BOOLsuccess=SetPixelFormat(dc.m_hDC,pixelFormat,&pfd);m_hRC=wglCreateContext(dc.m_hDC);同理找到函数OnDestory(),在该函数中添加下面的代码:wglDeleteContext(m_hRC);4)在类CView中的函数PreCreateWindows()中添加下面的代码:cs.style|=(WS_CLIPSIBLINGS|WS_CLIPCHILDREN);5)在类CView中的函数OnDraw()中添加下面的代码:wglMakeCurrent(pDC-m_hDC,m_hRC);DrawScene();//用户自定义的场景绘制函数wglMakeCurrent(pDC-m_hDC,NULL);这样,一个基于OpenGl标准的程序框架已经构造好了,用户只需在DrawScene()函数中添加程序代码即可。为了验证程序框架的是否正确,请在DrawScene()函数中添加下面的代码:glBegin(GL_TRIANGLE_STRIP);glColor3f(1.0,0.0,0.0);glVertex3f(0.0,0.0,0.0);glColor3f(0.0,1.0,0.0);glVertex3f(-0.5,0.0,0.0);glColor3f(0.0,0.0,1.0);glVertex3f(0.0,0.5,0.0);glEnd();glFlush();然后编译执行直至在MFC窗口中绘制出了一个顶点颜色不同的三角形即可。//MeshView.cpp:implementationoftheCMeshViewclass//#includestdafx.h#includeMesh.h#includeMeshDoc.h#includeMeshView.h#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endif///////////////////////////////////////////////////////////////////////////////CMeshViewIMPLEMENT_DYNCREATE(CMeshView,CView)BEGIN_MESSAGE_MAP(CMeshView,CView)//{{AFX_MSG_MAP(CMeshView)ON_WM_PAINT()ON_WM_DESTROY()ON_WM_SIZE()ON_WM_LBUTTONDOWN()ON_WM_LBUTTONUP()ON_WM_RBUTTONDOWN()ON_WM_RBUTTONUP()ON_WM_MOUSEMOVE()ON_WM_ERASEBKGND()ON_WM_CREATE()ON_COMMAND(ID_EDIT_COPY,OnEditCopy)//}}AFX_MSG_MAP//StandardprintingcommandsON_COMMAND(ID_FILE_PRINT,CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT,CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW,CView::OnFilePrintPreview)END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////////CMeshViewconstruction/destructionCMeshView::CMeshView(){//OpenGLm_hGLContext=NULL;m_GLPixelIndex=0;//Mousem_LeftButtonDown=FALSE;m_RightButtonDown=FALSE;//Colorsm_ClearColorRed=0.0f;m_ClearColorGreen=0.0f;m_ClearColorBlue=0.2f;//Animationm_StepRotationX=0.0f;m_StepRotationY=5.0f;m_StepRotationZ=0.0f;InitGeometry();}CMeshView::~CMeshView(){}//********************************************//InitGeometry//********************************************voidCMeshView::InitGeometry(void){m_xRotation=0.0f;m_yRotation=0.0f;m_zRotation=0.0f;m_xTranslation=0.0f;m_yTranslation=0.0f;m_zTranslation=-5.0f;m_xScaling=1.0f;m_yScaling=1.0f;m_zScaling=1.0f;m_SpeedRotation=1.0f/3.0f;m_SpeedTranslation=1.0f/50.0f;m_xyRotation=1;}BOOLCMeshView::PreCreateWindow(CREATESTRUCT&cs){returnCView::PreCreateWindow(cs);}///////////////////////////////////////////////////////////////////////////////CMeshViewdrawingvoidCMeshView::OnDraw(CDC*pDC){CMeshDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahere}///////////////////////////////////////////////////////////////////////////////CMeshViewprintingBOOLCMeshView::OnPreparePrinting(CPrintInfo*pInfo){//defaultpreparationreturnDoPreparePrinting(pInfo);}voidCMeshView::OnBeginPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/){}voidCMeshView::OnEndPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/){}///////////////////////////////////////////////////////////////////////////////CMeshViewdiagnostics#ifdef_DEBUGvoidCMeshView::AssertValid()const{CView::AssertValid();}voidCMeshView::Dump(CDumpContext&dc)const{CView::Dump(dc);}CMeshDoc*CMeshView::GetDocument()//non-debugversionisinline{ASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CMeshDoc)));return(CMeshDoc*)m_pDocument;}#endif//_DEBUG//////////////////////////////////////////////////////////////////////////////////////////////OPENGL//////////////////////////////////////////////////////////////////////////////////////////////********************************************//OnCreate//CreateOpenGLrenderingcontext//********************************************intCMeshView::OnCreate(LPCREATESTRUCTlpCreateStruct){if(CView::OnCreate(lpCreateStruct)==-1)return-1;HWNDhWnd=GetSafeHwnd();HDChDC=::GetDC(hWnd);if(SetWindowPixelFormat(hDC)==FALSE)return0;if(CreateViewGLContext(hDC)==FALSE)return0;//::ReleaseDC(hWnd,hDC);//DefaultmodeglPolygonMode(GL_FRONT,GL_FILL);glPolygonMode(GL_BACK,GL_FILL);glShadeModel(GL_SMOOTH);glEnable(GL_NORMALIZE);//LightspropertiesfloatambientProperties[]={0.7f,0.7f,0.7f,1.0f};floatdiffuseProperties[]={0.8f,0.8f,0.8f,1.0f};floatspecularProperties[]={1.0f,1.0f,1.