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

本文对第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类中加入一个变量来保存显示列表名称

GLuint m_sceneList;

2,创建显示列表

void CCY457OpenGLView::CreateSceneList()
{//创建显示列表
  m_sceneList = glGenLists(1);
  glNewList(m_sceneList, GL_COMPILE);
    SetupLighting();
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,m_Texture[0]);
    //Front Face
    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();
    //Back Face
    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]);
    
    //Left Face
    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();
    //Right Face
    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]);
    //Top Face
    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();
    //Botton Face
    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();
}

时间: 2024-10-28 13:10:36

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

《基于MFC的OpenGL编程》Part 19 Creating a Virtual Reality Walkthrough…

<基于MFC的OpenGL编程>Part 19 Creating a Virtual Reality Walkthrough Application 本文是整个系列文章的最后一篇,将创建一个完整的虚拟office应用程序(如图所示)来做为ending. 1,在CCY457OpenGLView类中加入下述变量,用来保存office内各个物体的显示列表 //Display List Names GLuint m_SceneList; GLuint m_ComputerList; GLuint m_

《基于MFC的OpenGL编程》Part 5 Transformations

<基于MFC的OpenGL编程>Part 5 Transformations - Rotations,Translations and Scaling Transformations - Translation, Rotation and Scaling Translation is nothing but moving along an arbitrary axis. Rotation is spinning about an arbitrary axis. Scaling is incre

《基于MFC的OpenGL编程》Part 2 Setting up OpenGL on Windows

WGL – Windows的 OpenGL扩展层 The WGL extension consists of a set of functions (wglCreateContext, wglDeleteContext etc.) and structures (such as PIXELFORMATDESCRIPTOR, GLYPHMETRICSFLOAT) etc. Thus every OpenGL implementation has a platform-specific portio

《基于MFC的OpenGL编程》Part 13 Creating 2D and 3D Text

wglUseFontBitmaps函数 The wglUseFontBitmaps() function creates a set of bitmap display lists based on the glyphs in the currently selected font in the current DC for use in the current OpenGL RC. It basically creates a series of sequential display list

《基于MFC的OpenGL编程》Part 18 Reading objects from the OBJ File Format

本文将介绍如何从Obj文件格式中创建3D对象,我们使用的是Nate Miller的obj格式加载类. This would be very useful to create large Virtual Reality applications as we could make use of the readily available 3D model files or make use of modeling tools to create these models and load them

《基于MFC的OpenGL编程》Part 17 Shadows

Shadows Conceptually drawing a shadow is quite simple. A shadow is produced when an object keeps light from a source from striking some object or surface behind the object, casting the shadow. The area on the shadowed object's surface outlined by the

《基于MFC的OpenGL编程》Part 15 Selection

Selection Selection is a powerful feature of OpenGL that allows you click at some position of the OpenGL window using the mouse and determine which of your objects lie beneath it. The act of selecting a specific object is called Picking. With OpenGL'

《基于MFC的OpenGL编程》Part 14 Quadrics

本文在第11篇文章的基础上,为其加入显示各种二次曲面的代码: Quadrics Every quadric has a few settings associated with it. We have to create a quadric first and then customize its settings to render the shape we want. The gluNewQuadric function creates a state variable that descr

《基于MFC的OpenGL编程》Part 11 Blending, Antialiasing and Fog

Blending and Transparency Blending in OpenGL provides pixel-level control of RGBA color storage in the color buffer. To enable blending we must first call glEnable(GL_BLEND). We have to set up the blending function glBlendFunc with two arguments: the