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

来源:互联网 发布:网易股票分时数据 编辑:程序博客网 时间:2024/05/01 13:31

《基于MFC的OpenGL编程》Part 11 Creating and Using Display Lists
原文链接
源码链接
本文对第10篇文章进行修改,使用显示列表来存储渲染命令。

注:添加显示列表虽然对界面没有特别大的影响,但是有一个明显的效果,程序运行时的内存占用量变小了,特别是加载带纹理的图像时,内存占用会急剧飙升,添加了显示列表后,内存占用相对平稳,暂无卡死现象。

显示列表
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,CMy2OpenGLView类中加入一个变量来保存显示列表名称:

GLuint m_sceneList;

2,创建显示列表:

void CMy2OpenGLView::CreateSceneList(){    //创建显示列表    m_sceneList = glGenLists(1);    glNewList(m_sceneList,GL_COMPILE);        SetupLighting();        glEnable(GL_TEXTURE_2D);        glShadeModel(GL_SMOOTH);        LoadGLTextures();        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();        //Bottom 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();//this is important}

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

int CMy2OpenGLView::InitializeOpenGL(){    //Get a DC for the Client Area    m_pDC = new CClientDC(this);    //Failure to Get DC    if(m_pDC == NULL)    {        MessageBox("Error Obtaining DC");        return FALSE;    }    //Failure to set the pixel format    if(!SetupPixelFormat())    {        return FALSE;    }    //Create Rendering Context    m_hRC = ::wglCreateContext (m_pDC->GetSafeHdc ());    //Failure to Create Rendering Context    if(m_hRC == 0)    {        MessageBox("Error Creating RC");        return FALSE;    }    //Make the RC Current    if(::wglMakeCurrent (m_pDC->GetSafeHdc (), m_hRC)==FALSE)    {        MessageBox("Error making RC Current");        return FALSE;    }    //Specify Black as the clear color    ::glClearColor(0.5,0.0,0.5,1.0);/////此时为紫色//////////////此处设置背景颜色值1.0,1.0,1.0,1.0(白色)|0.0f,0.0f,0.0f,0.0f(黑色)    //Specify the back of the buffer as clear depth    ::glClearDepth(1.0f);    //Enable Depth Testing    ::glEnable(GL_DEPTH_TEST);    ::glEnable(GL_SMOOTH);    //set effect of Fog    ::glFogi(GL_FOG_MODE,GL_EXP);    GLfloat fog_color[4] = {0.2f,0.2f,0.2f,0.0f};    ::glFogfv(GL_FOG_COLOR,fog_color);    ::glFogf(GL_FOG_DENSITY,0.25);    //Load Texture//  LoadGLTextures();    //Setup Lighting//  SetupLighting();    //创建显示列表    ::glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_DST_ALPHA);     CreateSceneList();    return TRUE;}

4、修改RenderScene3D的绘制代码:

    if (m_bCubeTexture)//画一个纹理自旋立方体    {        glLoadIdentity();//立方体中心移至坐标系原点        glTranslatef(1.0f,-0.5f,-5.0f);        glRotatef(m_xRot,1.0f,0.0f,0.0f);        glRotatef(m_yRot,0.0f,1.0f,0.0f);        glCallList(m_sceneList);    }

运行结果:
这里写图片描述

0 0
原创粉丝点击