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程序就已经完成了。


© 2024 实用范文网 | 联系我们: webmaster# 6400.net.cn