您的位置:首页 > 运维架构

[转]如何设置opengl结合vc开发绚丽的3D图形---操作详解

2009-10-21 16:44 447 查看
1)系统环境设置:

1.将开发库中的.h文件拷贝到Visual C++ 6.0的/Include/GL目录中 在2008中如没有这个文件目录,可自行创建

2.将.lib文件拷贝到Visual C++ 6.0的/lib目录中

3.将.dll文件拷贝到操作系统的system32目录中

以上文件可上网搜索opengl开发库函数下载,在 工程/设置/连接 中 物体或库模块中添加 opengl32.lib glu32.lib glut32.lib glaux.lib

2)在文件 StdAfx.h 中添加OpenGL函数定义的头文件,这样就可以在工程的任何位置调用其各种函数

#include<gl/gl.h>

#include<gl/glu.h>

#include<gl/glaux.h>

#include<gl/glut.h>

3)在View类中添加如下成员变量:

CDC *m_pDC; //创建绘图设备

HGLRC m_hRC; //OpenGL绘制描述表

HPALETLE m_hPalette; //OpenGL调色板,简单图形绘制可省略

void DrawScene(CDC *cDC); //绘制图形函数,绘图的语法和实现具体参考opengl书籍,材质、光照、渲染等内容为进一步美化绘图

void SetLogicalPalette(void); //设置逻辑调色板

int InitOpenGL(); //初始化opengl相关数值

void DestroyOpenGL(); //销毁绘图设备和绘图描述表

BOOL SetPixelFormat(void); //设置像素格式

4)以下是各函数内容的实现,具体可参考opengl书籍

BOOL CPointView::PreCreateWindow(CREATESTRUCT& cs)

{

// TODO: Modify the Window class or styles here by modifying

// the CREATESTRUCT cs

cs.style|=WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

return CView::PreCreateWindow(cs);

}

BOOL CPointView::bSetupPixelFormat()

{

static PIXELFORMATDESCRIPTOR pfd=

{

sizeof(PIXELFORMATDESCRIPTOR),

1,

PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |PFD_DOUBLEBUFFER,

24,

0,0,0,0,0,0,

0,

0,

0,

0,0,0,0,

32,

0,

0,

PFD_MAIN_PLANE,

0,

0,0,0

};

int pixelformat;

if((pixelformat=ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd))==0)

{

MessageBox("choose pixel format failure!");

return FALSE;

}

if(SetPixelFormat(m_pDC->GetSafeHdc(),pixelformat,&pfd)==FALSE)

{

MessageBox("set pixel format failure!");

return FALSE;

}

return TRUE;

}

void CPointView::InitOpenGL()

{

PIXELFORMATDESCRIPTOR pfd;

int n;

HGLRC hrc;

m_pDC=new CClientDC(this);

ASSERT(m_pDC!=NULL);

if(!bSetupPixelFormat())

return;

n=::GetPixelFormat(m_pDC->GetSafeHdc());

::DescribePixelFormat(m_pDC->GetSafeHdc(),n,sizeof(pfd),&pfd);

hrc=wglCreateContext(m_pDC->GetSafeHdc());

wglMakeCurrent(m_pDC->GetSafeHdc(),hrc);

glClearDepth(1.0f);

glEnable(GL_DEPTH_TEST);

}

void CPointView::OnSize(UINT nType, int cx, int cy) //当窗口尺寸发生改变,调用此函数,其内容主要是设置投影视图范围

{

CView::OnSize(nType, cx, cy);



// TODO: Add your message handler code here

int w=cx;

int h=cy;

GLfloat nRange=1.0f;

if(h==0)h=1;

glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

if(w<h)

glOrtho(-nRange,nRange,-nRange*h/w,nRange*h/w,-nRange,nRange);

else

glOrtho(-nRange*h/w,nRange*h/w,-nRange,nRange,-nRange,nRange);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

}

void CPointView::OnDestroy()

{

CView::OnDestroy();



// TODO: Add your message handler code here

HGLRC hrc;

hrc=::wglGetCurrentContext();

::wglMakeCurrent(NULL,NULL);

if(hrc)

::wglDeleteContext(hrc);

if(m_pDC)

delete m_pDC;

}

图形操作描述表在实例中的应用:

HDC hdc;

HGLRC hglrc;

hglrc=wglCreateContext(hdc);

wglMakeCurrent(hdc,hglrc);

DrawScene();

wglMakeCurrent(NULL,NULL);

wglDeleteContext(hglrc);

DeleteObject(hdc);

在编程中必须映射的消息函数:

PreCreateWindow

OnCreate

OnDestroy

OnSize

OnEraseBkgnd

OnInitialUpdate

OnDraw

OnIdle
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: