VC(MFC)开发OPENGL程序
作者:李英江
日期: 2006-08-10 19:07:00
网站:
转载请保留作者内容!利用使用VC开发OPENGL程序,运用MFC库,做一个简单例子
我简单介绍一下图形接口,现在流行的两大图形接口Direct3D 和OpenGL,Direct3D主要是针对游戏,像starcaft星际争霸,它支持DX5.0以上
的版本,但是不少游戏同时支持两种图形加速,如CS,魔兽争霸。OpenGL主要是针对专业的图形应用,在军事,医学,GIS地理信息系统,动画
(3dsmax,maya...),电影,等应用广泛,并且可以在不同的平台上应用,DX相对来说就只能限于Windows操作系统了。
1.运用MFC向导,生成一个单文档应用程序,不做任何事的MFC框架程序。
2.视图类中添加窗口样式。
/*********************************************************************/
BOOL CMfc_basicView::PreCreateWindow(CREATESTRUCT& cs)
{
// Add Window styles required for OpenGL before window is created
cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS | CS_OWNDC);
return CView::PreCreateWindow(cs);
}
/*********************************************************************/
3.在afxcmn.h中包含OPENGL头文件。
/*********************************************************************/
#include
// MFC support for Windows Common Controls
// Inserted these files for openGL
#include
#include
#include
#include
#endif // _AFX_NO_AFXCMN_SUPPORT
/*********************************************************************/
4.视图类中 .h文件中加入四个public 成员变量
/*********************************************************************/
HGLRC m_hRC; // Permanent Rendering Context
HDC m_myhDC; // Private GDI Device Context
int m_height; // Stores the height of the View
int m_width; // Stores the width of the view
/*********************************************************************/
5.视图类中右键添加一个BOOL成员函数SetupPixelFormat
/*********************************************************************/
BOOL COglm_demoView::SetupPixelFormat()
{
GLuint PixelFormat;
static PIXELFORMATDESCRIPTOR pfd= {
sizeof(PIXELFORMATDESCRIPTOR),
// Size Of This Pixel Format Descriptor
1,
// Version Number (?)
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
24, // Select A 24Bit Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored (?)
0, // No Alpha Buffer
0, // Shift Bit Ignored (?)
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored (?)
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer (?)
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved (?)
0, 0, 0 // Layer Masks Ignored (?)
};
m_myhDC = ::GetDC(m_hWnd); // Gets A Device Context For The Window
PixelFormat = ChoosePixelFormat(m_myhDC, &pfd); // Finds The Closest Match To The Pixel Format We Set Above
if (!PixelFormat)
{
::MessageBox(0,"Can't Find A Suitable PixelFormat.","Error",MB_OK|MB_ICONERROR);
PostQuitMessage(0);
// This Sends A 'Message' Telling The Program To Quit
return false ; // Prevents The Rest Of The Code From Running
}
if(!SetPixelFormat(m_myhDC,PixelFormat,&pfd))
{
::MessageBox(0,"Can't Set The PixelFormat.","Error",MB_OK|MB_ICONERROR);
PostQuitMessage(0);
return false;
}
m_hRC = wglCreateContext(m_myhDC);
if(!m_hRC)
{
::MessageBox(0,"Can't Create A GL Rendering Context.","Error",MB_OK|MB_ICONERROR);
PostQuitMessage(0);
return false;
}
if(!wglMakeCurrent(m_myhDC, m_hRC))
{
::MessageBox(0,"Can't activate GLRC.","Error",MB_OK|MB_ICONERROR);
PostQuitMessage(0);
return false;
}
// Now that the screen is setup we can
// initialize OpenGL();
InitGL();
return true;
}
/*********************************************************************/
6.关闭WM_ERASEBACKGROUND消息,不关闭的话,画图时会闪动.在视图类中选择“Add Windows Message Handler”加入消息处理函数.
/*********************************************************************/
BOOL CMfc_basicView::OnEraseBkgnd(CDC* pDC)
{
return FALSE;
}
/*********************************************************************/
7.初始化OPENGL 添加InitGL()函数.
/*********************************************************************/
void CMfc_basicView::InitGL()
{
// Enables Depth Testing
glEnable(GL_DEPTH_TEST);
// Enable the point size for selected points
glPointSize(5.0f);
// This Will Clear The Background Color To Black
glClearColor(.4, 0.2, 0.0, 0.0f);
// Reset the current projection matrix
SetProjection();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Enable back face culling, defaults to Clock wise vertices.
glEnable(GL_CULL_FACE);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glShadeModel(GL_SMOOTH);
glPolygonMode(GL_FRONT, GL_FILL);
}
/*********************************************************************/
8. 在视图类中添加调窗口大小的处理函数OnSize() 消息句柄
/*********************************************************************/
void CMfc_basicView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Make the rendering context current
wglMakeCurrent(m_myhDC,m_hRC);
// Reset The Current Viewport And Perspective Transformation
glViewport(0, 0, cx, cy);
m_height= cy;
m_width = cx;
// Calculate The Aspect Ratio Of The Window
gluPerspective(60.0f,
(GLfloat)cx/(GLfloat)cy,
0.1f,
1000.0f);
}
9.添加SetPorjection()函数
void CMfc_basicView::SetProjection()
{
glViewport(0, 0, m_width, m_height);
// Reset The Projection Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// It's a perspective projection
// Calculate The Aspect Ratio Of The Window
gluPerspective(60.0f,(GLfloat)m_width/(GLfloat)m_height, 0.1f,3000.0f);
}
/*********************************************************************/
10.在Project 菜单选择 Settings. 单击 Link 选项页 Object/library Module添加库文件.
opengl32.lib glu32.lib glut.lib glaux.lib
11.在视图类中加入OnCreate消息句柄
/*********************************************************************/
int CMfc_basicView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
SetupPixelFormat();
wglMakeCurrent(NULL,NULL);
return 0;
}
/*********************************************************************/
//现在编译程序的话,会看到一个无法刷新的应用程序了。
12.利用OPENGL函数在视图类OnDraw函数中画三个圆环并各自旋转。
/*********************************************************************/
// CMfc_basicView drawing
// This function draws three toruses on the screen and rotates them
// Replace this with your own drawing code.
//
void CMfc_basicView::OnDraw(CDC* pDC)
{
// Integer declared as static so the value is maintained
static int i=0;
// Increase i, this is the rotation value.
i += 1.11;
// Make the rendering context current as we're about to
// draw into it
wglMakeCurrent(m_myhDC,m_hRC);
// Clear the screen and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset the model matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Translate to a suitable position
glTranslatef(0,0,-100);
// We're going to draw using this color. ( an orange )
glColor3f( 0.89f, 0.36f, 0.0f);
// Rotate to some angle (i)
glRotatef(i, 1,0,0);
glRotatef(i ,0,1,0);
glRotatef(i ,0,0,1);
// Draw the first torus.
auxSolidTorus( 8,50);
// Rotate some more and draw the second.
glRotatef(i ,0,1,0);
glRotatef(i ,0,0,1);
auxSolidTorus( 8,30);
// Rotate some more and draw the third
glRotatef(-i ,0,1,0);
glRotatef(i ,0,0,1);
auxSolidTorus( 8,10);
// Swap the virtual screens
SwapBuffers(m_myhDC);
// Invalidate the window as we're
// ready to draw another frame
Invalidate(FALSE);
}
/*********************************************************************/
//一个完整的opengl程序就已经完成了。
相关文章
- 为何要学编程
- 软件体系结构期末试卷
- 各种软件工程项目用到的编程环境总结
- 学生成绩管理系统报告
- 可视化配电网潮流计算软件设计
- vc++课程设计实验报告
- VC 的DLL应用(含Demo演示) - 一点一滴的Beer - 博客园
- 一个程序员应该具备的基础知识和概念
- c++更改电脑桌面
一.为何要学编程? 每个人的动机不一样.大致有: 1.为了找个好工作:或为了有更好的机会和更好的发展. 2.看到别人超厉害,所以也想学. 3.实际工作中很多场合需要. 4.从小就立志做个程序员,做软件工程师. 5.振兴中国的软件事业. .. ...
北京工业大学2008 – 2009学年 第二学期考试样题 考试课程: 软件体系结构 II 考试日期:2009 年 12 月 日 学 院: 软件学院 专 业: 软件工程 学 号: 姓名: 成绩: 一 填空题 (共 30 空, 每空 1 分) ...
第一部分VC6.0 一. 介绍 面向window的IDE(集成开发环境): 集编辑.编译.连接.调试等功能: 两种编程方式: a) 基于Windows API的C编程方式,特点:代码效率高,但开发难度和工作量也很高: b) 基于MFC的C+ ...
目录 1 引言................................................................................................................. ...
第37卷第3期2009年3月 林业机械与木工设备 FORESTRY MACHINERY &WOODWORKING EQUIPMENT Vo137No.3Mar. 2009 可视化配电网潮流计算软件设计 唐文秀,赵志江 (东北林业大学 ...
VC++课程设计报告 一.设计时间 2010年12月 27日----12月31日 二.设计地点 三.设计目的 <VC++程序设计>是计算机科学与技术专业的必修专业基础课程,其实践性.应用性很强.实践教学环节是必不可少的一个重要环 ...
作者:一点一滴的Beer Email /Gtalk:[email protected] 出处:http://www.cnblogs.com/beer/ VC++的DLL应用(含Demo演示) 作者:一点一滴的Beer http://bee ...
一个程序员应该具备的基础知识和概念 1.计算机是有什么组成的,CPU是什么东西,其工作原理是什么.(对于这些以及下面将要提到的概念我不会告诉你什么答案,你可以看相应的教材,关于教材我会在下一部分详述,记住理解最重要!) 2.机器语言和微指令 ...
网上很难找到一个完整的使用IActiveDesktop更改桌面图片的确实可行的例子,现笔者收集多方资料整理如下,该程序在VC6.0上运行OK. Step1. 在需要使用IActiveDesktop程序所在的C++文档里包含头文件#inclu ...