MFC 中配置OpenGL环境

MFC为编写Windows应用程序提供了极大的方便,程序员在编写Windows应用程序时,只需要几步就可以把一个程序的框架建立起来.该类极大的提高了编程者的开发效率,是个具有极大实用价值的工具.


下面我们编写MFC 的OpenGL 应用程序.

要搭建一个OpenGL基本框架,需要以下几步:

1)新建一个单文档工程“OpenGLMFC”

(2)给工程添加三个库文件“GLAUX.LIB、GLU32.LIB、OpenGL32.LIB”,这三个库文件一般在“C:\Program Files\Microsoft Visual Studio\VC98\Lib\”路径中。

(3)在“stdafx.h”文件中添加包含三个OpenGL的头文件

                                   #include<gl\gl.h>

                                  #include <gl\glu.h>

                                 #include <gl\glaux.h>

(4)通过类向导给视图类添加三个消息响应函数:

      OnInitialUpdate(),OnCreate(),OnSize()。

(5)给视图类添加三个成员变量:



                  CClientDC  *m_pDC;

                   HGLRCm_hRC; // 专门用于OpenGL的数据类型

                   CRectm_rect;

(6)       OnCreate()函数中添加如下代码,其中重点是bSetupPixelFormat()函数,

{	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	m_pDC=new CClientDC(this);
	ASSERT(m_pDC!=NULL);
	if(!bSetupPixelFormat())
		return -1;
   <span style="white-space:pre">	</span>m_hRC=wglCreateContext(m_pDC->GetSafeHdc());
	wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC);
	return 0;  }

bSetupPixelFormat()函数用于设置OpenGL基本图元格式,需要自定义添加,代码如下:

{  static	PIXELFORMATDESCRIPTOR pfd={
		    sizeof(PIXELFORMATDESCRIPTOR),
			1,
			PFD_DRAW_TO_WINDOW|
			PFD_SUPPORT_OPENGL|
			PFD_DOUBLEBUFFER,
			PFD_TYPE_RGBA,
			24,
			0,0,0,0,0,0,
			0,
			0,
			0,	
			0,0,0,0,
			16,
			0,
			0,
			PFD_MAIN_PLANE,
			0,
			0,0,0
			};
	 int pixelformat;
	 if((pixelformat=ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd))==0)
	 {
		 MessageBox("choose failed");
		 return FALSE;
	 }
	 if(SetPixelFormat(m_pDC->GetSafeHdc(),pixelformat,&pfd)==FALSE)
     {
		 MessageBox("set failed");
		 return FALSE;
	 }
	 return TRUE;  }

(7)在OnSize()函数中设置视口和场景大小。

{
	CView::OnSize(nType, cx, cy);
	
	wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC);
	glViewport(0,0,cx,cy);   //设置视口

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0,1.0,1.0,500.0); //设置场景
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();	
	glTranslatef(0.0,0.0,-10.0);
	glRotatef(0.0,0.0,1.0,0.0);

	Invalidate(FALSE);
}

(8) 在OnInitialUpdate()函数中添加初始化光照、材质等特性的代码

{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC);

	float position0[]={-3.0,3.0,3.0,0.0};
	float local_view[]={0.0};

	float position1[]={-3.0f,-3.0f,-3.0f,0.0f};
	float light1_ambient[]={0.2f,0.2f,0.2f};
	float light1_diffuse[]={0.1f,0.2f,0.2f};

	float position2[]={-50.0f,100.0f,-300.0f,0.0f};
	float light2_ambient[]={0.2f,0.2f,0.2f};
	float light2_diffuse[]={0.1f,0.2f,0.2f};

	glLightfv(GL_LIGHT0,GL_POSITION,position0);
	glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER,local_view);

	glLightfv(GL_LIGHT1,GL_DIFFUSE,light1_diffuse);
        glLightfv(GL_LIGHT1,GL_AMBIENT,light1_ambient);
	glLightfv(GL_LIGHT1,GL_POSITION,position1);
	glLightfv(GL_LIGHT2,GL_DIFFUSE,light2_diffuse);
        glLightfv(GL_LIGHT2,GL_AMBIENT,light2_ambient);
	glLightfv(GL_LIGHT2,GL_POSITION,position2);
		
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_LIGHT1);
	glEnable(GL_LIGHT2);
	glEnable(GL_AUTO_NORMAL);
	glEnable(GL_NORMALIZE);

	GetClientRect(&m_rect);
	glClearColor(0.0,1.0,1.0,1.0);
	glClearDepth(1.0f);
        glEnable(GL_DEPTH_TEST);
        glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE);
	glEnable(GL_BLEND);
        glEnable(GL_LINE_SMOOTH);

	wglMakeCurrent(NULL,NULL);
}

(9) 在OnDraw()函数中添加代码如下:


 

 

        wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC);
         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 
         auxSolidBox(1.6,0.6,0.4);
         glTranslatef(0.0,3.0,0.0);
         auxSolidBox(0.6,0.6,0.4);
 
         glFlush();
         SwapBuffers(wglGetCurrentDC());

 

我们用自己构造的来绘制个简单的图形





阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoge132/article/details/50832813
文章标签: OpenGL MFC c++
个人分类: OpenGL
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭