《基于MFC的OpenGL编程》Part 12 Creating and Using Display Lists

OpenGL显示列表优化
本文介绍如何使用OpenGL显示列表存储预处理的渲染命令,通过创建显示列表实现场景渲染的优化,特别是对于那些在每一帧中不发生变化的几何体。

本文对第11篇文章进行修改,使用显示列表来存储渲染命令。

显示列表

OpenGL provides a facility to create a preprocessed set of OpenGL commands called a display list. Creating a display list is a straight forward process. We just have to delimit the display list code with glNewList and glEndList. The display list is named by an integer and this name is used to call the list to be executed later on. Display lists are very useful for scenes which have lot of geometry that don't change in from frame to frame. If we have to rerender something that doesn't change it is not worth going through all the calculations required once again - it is better to store them somewhere in memory and reuse it. This is exactly what the display list lets us achieve. Thus if we are going to repeatedly execute the same sequence of OpenGL commands we can create and store a display list and then have this cached sequence of calls repeated with minimal overhead, since all the vertices, lighting calculations, textures and matrix operations are calculated only when the list is created and not when it is replayed. Only the results of the calculations end up being stored in display lists. This means we cannot modify the list once we create it.

1,CY457OpenGLView类中加入一个变量来保存显示列表名称

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> GLuintm_sceneList;

2,创建显示列表

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> void CCY457OpenGLView::CreateSceneList()
{
// 创建显示列表
m_sceneList = glGenLists( 1 );
glNewList(m_sceneList,GL_COMPILE);
SetupLighting();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,m_Texture[
0 ]);
// FrontFace
glBegin(GL_POLYGON);
glTexCoord2f(
0 , 0 );
glVertex3f(
- 1.0f , - 1.0f , 0.0f );
glTexCoord2f(
1 , 0 );
glVertex3f(
1.0f , - 1.0f , 0.0f );
glTexCoord2f(
1 , 1 );
glVertex3f(
1.0f , 1.0f , 0.0f );
glTexCoord2f(
0 , 1 );
glVertex3f(
- 1.0f , 1.0f , 0.0f );
glEnd();
// BackFace
glBegin(GL_POLYGON);
glTexCoord2f(
1 , 0 );
glVertex3f(
- 1.0f , - 1.0f , - 1.0f );
glTexCoord2f(
1 , 1 );
glVertex3f(
- 1.0f , 1.0f , - 1.0f );
glTexCoord2f(
0 , 1 );
glVertex3f(
1.0f , 1.0f , - 1.0f );
glTexCoord2f(
0 , 0 );
glVertex3f(
1.0f , - 1.0f , - 1.0f );
glEnd();
glBindTexture(GL_TEXTURE_2D,m_Texture[
1 ]);

// LeftFace
glBegin(GL_POLYGON);
glTexCoord2f(
1 , 0 );
glVertex3f(
- 1.0f , - 1.0f , 0.0f );
glTexCoord2f(
1 , 1 );
glVertex3f(
- 1.0f , 1.0f , 0.0f );
glTexCoord2f(
0 , 1 );
glVertex3f(
- 1.0f , 1.0f , - 1.0f );
glTexCoord2f(
0 , 0 );
glVertex3f(
- 1.0f , - 1.0f , - 1.0f );
glEnd();
// RightFace
glBegin(GL_POLYGON);
glTexCoord2f(
0 , 0 );
glVertex3f(
1.0f , - 1.0f , 0.0f );
glTexCoord2f(
1 , 0 );
glVertex3f(
1.0f , - 1.0f , - 1.0f );
glTexCoord2f(
1 , 1 );
glVertex3f(
1.0f , 1.0f , - 1.0f );
glTexCoord2f(
0 , 1 );
glVertex3f(
1.0f , 1.0f , 0.0f );
glEnd();
glBindTexture(GL_TEXTURE_2D,m_Texture[
2 ]);
// TopFace
glBegin(GL_POLYGON);
glTexCoord2f(
0 , 0 );
glVertex3f(
- 1.0f , 1.0f , 0.0f );
glTexCoord2f(
0 , 1 );
glVertex3f(
1.0f , 1.0f , 0.0f );
glTexCoord2f(
1 , 1 );
glVertex3f(
1.0f , 1.0f , - 1.0f );
glTexCoord2f(
1 , 0 );
glVertex3f(
- 1.0f , 1.0f , - 1.0f );
glEnd();
// BottonFace
glBegin(GL_POLYGON);
glTexCoord2f(
0 , 1 );
glVertex3f(
- 1.0f , - 1.0f , 0.0f );
glTexCoord2f(
0 , 0 );
glVertex3f(
- 1.0f , - 1.0f , - 1.0f );
glTexCoord2f(
1 , 0 );
glVertex3f(
1.0f , - 1.0f , - 1.0f );
glTexCoord2f(
1 , 1 );
glVertex3f(
1.0f , - 1.0f , 0.0f );
glEnd();
glDisable(GL_TEXTURE_2D);
glEndList();
}

3,在InitializeOpenGL函数中加入对上述函数的调用:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> // 创建显示列表
CreateSceneList();

4,修改RenderScene的绘制代码

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> void CCY457OpenGLView::RenderScene()
{
// 绘制函数
glTranslatef( 0.0f , 0.0f , - 5.0f );
glRotatef(m_xRot,
1.0f , 0.0f , 0.0f );
glRotatef(m_yRot,
0.0f , 1.0f , 0.0f );
glCallList(m_sceneList);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值