OpenGL | Show List

1. Introduction to display list

        The OpenGL display list (Display List) is composed of a group of pre-stored OpenGL function statements to be called later. When this display list is called, the function statements listed in the table are executed sequentially.

        Using display lists can improve drawing efficiency and save computer processing resources. But the OpenGL display list is designed as a command cache, not a dynamic database cache. That is, once a display list is created, it cannot be modified. ( Because if the display list can be modified, the overhead of display list search, memory management execution, etc. will reduce performance. ) And because the program itself has some overhead when calling the display list, especially when a display list is too small, it will cause system overhead Over the superiority of the list, so it is not possible to optimize the performance of the program just by calling the display list.

2. Use the display list

        There are generally four steps in using a display list : assigning a display list number, creating a display list, calling a display list, and destroying a display list.

1. Assign display list numbers

        In order to avoid overwriting caused by repeated display lists defined by yourself, use the glGenLists function to automatically assign an unused display list number.

GLuint WINAPI glGenLists(
   GLsizei range
);

        The glGenLists function has a parameter range, indicating that a range of consecutive unused display list numbers is to be allocated. Returns the smallest of several consecutive numbers assigned.

        For example: glGenLists(3); If it returns 20, it means that three consecutive numbers of 20, 21 and 22 are assigned. If the function returns zero, the allocation failed. You can use the glIsList function to determine whether a number has been used as a display list.

2. Create a display list

        Creating a display list is actually loading various OpenGL function calls into the display list. Use glNewList to start loading and glEndList to end loading.

void WINAPI glNewList(
   GLuint list,
   GLenum mode
);

glNewList has two parameters:

  • The first parameter is a positive integer (the list number returned by glGenLists), indicating which display list to load into.
  • The second parameter has two values:
    • If it is GL_COMPILE, it means that the following content is just loaded into the display list, but they are not executed now;
    • If it is GL_COMPILE_AND_EXECUTE, it means execute the loaded content once while loading.

3. Call the display list

(1) Call a display list

        Calling a display list uses the glCallList function, which has only one parameter, that is, the number of the display list to be called, for example, glCallList(10); the display list numbered 10 can be called.

(2) Call a series of display lists

        Using the glCallLists function, there are 3 parameters:

  1. The first parameter indicates how many display lists to call
  2. The second parameter indicates the storage format of these numbers: it can be GL_BYTE (each number is represented by a GLbyte), GL_UNSIGNED_BYTE (each number is represented by a GLubyte), GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT.
  3. The third parameter indicates the position (array) of these display list numbers

        Before using glCallLists, you can use the glListBase function to set an offset. Assuming that the offset is k, and the numbers of the display lists required to be called in glCallLists are l1, l2, l3, ..., the actual call display lists are l1+k, l2+k, l3+k, ....

4. Destroy the display list

void WINAPI glDeleteLists(
   GLuint  list,
   GLsizei range
);

        Use glDeleteLists to destroy a string of consecutively numbered display lists. For example: using glDeleteLists (20, 4);will destroy the four display lists 20, 21, 22, and 23.

Three, give an example

        The Init function in the ListTest class below creates a display list, and loads the work of drawing triangles and textures into it; the Draw function calls this display list.

#pragma once
#include <windows.h>   //根据OpenGL编程指南, 在包含"gl.h"头文件时, 要先包含"windows.h", 因为在gl.h里使用了windows.h中定义的标识符
#include <gl/GL.h>
#include <gl/GLU.h>
#include <gl/glext.h>
#include "texture.h"   //用来加载贴图纹理,可参考前文
class ListTest
{
private:
    int list = 0;
public:
    void Init();
    void Draw();
};
#include "ListTest.h"
void ListTest::Init()
{
   if (list == 0)
   {
       list = glGenLists(1);  //分配显示列表编号

       glNewList(list, GL_COMPILE);   //创建显示列表

       Texture texture;
       texture.Init("C:\\Users\\***\\Downloads\\272.bmp");
       glEnable(GL_TEXTURE_2D);//开启纹理
       glBindTexture(GL_TEXTURE_2D, texture.textureId);

       glBegin(GL_TRIANGLES);      

       glTexCoord2f(0.0f, 0.0f);
       glVertex3f(-0.2f, -0.2f, -1.0f);

       glTexCoord2f(1.0f, 0.0f);
       glVertex3f(0.2f, -0.2f, -1.0f);

       glTexCoord2f(0.5f, 1.0f);
       glVertex3f(0.0f, 0.2f, -1.0f);
       glEnd();

       glEndList();
   }
}

void ListTest::Draw()
{
    glCallList(list);     //调用显示列表
}

Guess you like

Origin blog.csdn.net/weixin_39766005/article/details/129832116