OpenGL display list

Source: Internet
Author: User
OpenGL display list

  Contents
16.1Overview
16.2Create and execute a display list
16.3Manage the display list
16.4Multi-level display list

 

The OpenGL display list is composed of a group of pre-stored OpenGL function statements that will be called later, when this display list is called, the function statements listed in the table are executed in sequence. The examples mentioned above are instantaneous function commands, and OpenGL instantly executes the corresponding commands. This drawing method is called immediate or instantaneous mode ). This chapter describes in detail the basic introduction, creation, execution, management, and multi-level display list applications.

16.1 overview of display list

  16.1.1 advantages of displaying a list
The OpenGL display list design can optimize program running performance, especially network performance. It is designed as a command cache rather than a dynamic database cache. That is to say, once a display list is created, it cannot be modified. If the display list can be modified, the overhead of the display list search and memory management will be reduced.
The display list method is generally faster than the instantaneous method, especially the display list method can greatly improve the network performance, that is, when a drawing command is sent through the network, as the display List resides on the server, the network burden is minimized. In addition, displaying a list on a single-user machine also improves the efficiency. Once the display list is processed into a format suitable for graphic hardware, different OpenGL implementations have different levels of Optimization for commands. For example, if the rotating Matrix Function glrotate * () is placed in the display list, the performance can be greatly improved. Since the calculation of the rotation matrix is not simple, it contains complex operations such as square and trigonometric functions, and in the display list, it is only stored as the final rotation matrix, the execution speed is as fast as the hardware execution function glmultmatrix. In general, the display list can combine many adjacent matrix transformations into a single matrix multiplication to speed up the process.

  16.1.2 applicable scenarios for displaying a list
The program performance is not optimized by calling the display list. Because the program also has some overhead when calling the display list itself. If a display list is too small, this overhead will exceed the superiority of the display list. The following shows the scenarios where the list can be optimized to the maximum extent:
 

  • Matrix Operations
    Most matrix operations require OpenGL to calculate the inverse matrix. Both the matrix and its inverse matrix can be saved in the display list.

     

  • Raster bitmap and Image
    The grating data defined by the program is not necessarily an ideal format suitable for hardware processing. When compiling and organizing a display list, OpenGL may convert the data into data acceptable to the hardware, which can effectively improve the speed of Bitmap painting.

     

  • Light, material, and lighting model
    When you use a complex lighting environment to draw a scenario, you can change the material of each object in the scenario. However, the material calculation is large, so it may be slow to set the material. If you place the material definition in the display list, you do not have to re-calculate each time you change the material. Because the computing results are stored in tables, you can draw illumination scenarios faster.

     

  • Texture
    Because the hardware texture format may be different from the OpenGL format, if the texture definition is placed in the display list, the format can be converted during compilation and display of the list, rather than during execution, this greatly improves the efficiency.

     

  • Pattern filling mode of polygon
    You can put the defined pattern in the display list.

16.2 create and execute a display list

  16.2.1 create a display list
OpenGL allows you to create a display list in the format of glbegin () and glend () similar to the structure of the element to be drawn. The corresponding functions are as follows:

 

void glNewList(GLuint list,GLenum mode);

This section describes the beginning of a display list. The OpenGL function is stored in the display list until the function of the end table is called (see the following ). The list parameter is a positive integer that uniquely displays a list. The possible values of the mode parameter are gl_compile and gl_compile_and_execute. If you want to save the following function statements only without executing them, use gl_compile. If you want to save the following function statements into the table and execute them once instantaneously, use gl_compile_and_execute.
 

void glEndList(void);

Indicates the end of the display list.
  Note:: Not all OpenGL functions can be stored in the display list and executed through the display list. Generally, function statements used to pass parameters or return values cannot be stored in the display list, because this table may be called outside the scope of the parameter; if such functions are called when the display list is defined, they are executed in an instantaneous manner and not saved in the display list. Sometimes an error occurs when you call the display list function. The following lists OpenGL functions that cannot be saved to the display list:
 

 glDeleteLists()  glIsEnable()glFeedbackBuffer()  glIsList()glFinish()  glPixelStore()glGenLists()  glRenderMode()glGet*()  glSelectBuffer()

  16.2.2 execution display list
After the display list is created, you can call the function that executes the display list to execute it. The function allows multiple times to run the same display list in the program. It can also be used in combination with other functions in an instantaneous manner. The list of executed functions is displayed as follows:

 

void glCallList(GLuint list);

Execution display list. The list parameter specifies the display list to be executed. The function statements in the list are executed sequentially according to the order in which they are stored. If the list is not defined, nothing will happen. The following is a simple example of an application display list:

  Example 16-1 list display routine(Displist. c)
 

#include "glos.h"#include <GL/gl.h>#include <GL/glu.h>#include <GL/glaux.h> void myinit(void);void drawLine(void);void CALLBACK display(void);void CALLBACK myReshape(GLsizei w, GLsizei h); GLuint listName = 1; void myinit (void){  glNewList (listName, GL_COMPILE);  glColor3f (1.0, 0.0, 0.0);  glBegin (GL_TRIANGLES);  glVertex2f (0.0, 0.0);  glVertex2f (1.0, 0.0);  glVertex2f (0.0, 1.0);  glEnd ();  glTranslatef (1.5, 0.0, 0.0);  glEndList ();  glShadeModel (GL_FLAT);} void drawLine (void){  glColor3f(1.0,1.0,0.0);  glBegin (GL_LINES);  glVertex2f (0.0, 0.5);  glVertex2f (5.0, 0.5);  glEnd ();} void CALLBACK display(void){  GLuint i;  glClear (GL_COLOR_BUFFER_BIT);  glColor3f (0.0, 1.0, 0.0);  glPushMatrix();  for (i = 0; i <5; i++)    glCallList (listName);  drawLine ();  glPopMatrix();  glFlush ();} void CALLBACK myReshape(GLsizei w, GLsizei h){  glViewport(0, 0, w, h);  glMatrixMode(GL_PROJECTION);  glLoadIdentity();  if (w <= h)    gluOrtho2D (0.0, 2.0, -0.5 * (GLfloat) h/(GLfloat) w, 1.5 * (GLfloat) h/(GLfloat) w);  else    gluOrtho2D (0.0, 2.0 * (GLfloat) w/(GLfloat) h, -0.5, 1.5); glMatrixMode(GL_MODELVIEW);  glLoadIdentity();} void main(void){  auxInitDisplayMode (AUX_SINGLE | AUX_RGBA);  auxInitPosition (10, 200, 400, 50);  auxInitWindow ("Display List");  myinit ();  auxReshapeFunc (myReshape);  auxMainLoop(display);}

The above program running result shows the red triangles defined in the five display lists, and then draws a non-Table yellow line segment.

 

Figure 16-1 List


16.3 manage the display list
In the previous example, we used a positive integer as the index for displaying the list. However, this method is generally not used in practical applications, especially when multiple display lists are created. If you do this, you may select an index that is being occupied and overwrite the existing display list, which may cause harm to the program running. To avoid accidental deletion, you can call the glgenlist () function to generate a list of unused displays, or call glislist () to determine whether the specified display list is occupied. In addition, you can also use the gldeletelists () function to delete a display list in one or more ranges. These functions are described as follows:

GLuint glGenList(GLsizei range);

Assign a range of adjacent unused display list indexes. This function returns a positive integer index value, which is the first value of a group of consecutive null indexes. The returned indexes are empty and occupied. These indexes will not be returned when you call this function later. If the specified number of indexes cannot be met or the range is 0, the function returns 0.
 

GLboolean glIsList(GLuint list);

Check whether the list is in use. If the index list is occupied, the function returns true; otherwise, the function returns faulse.

void glDeleteLists(GLuint list,GLsizei range);

Delete a set of continuous display lists, that is, from the display list indicated by the Parameter List, delete the range display lists, and the deleted indexes are valid again. If you delete an uncreated display list, the delete operation is ignored.
When an existing display list index is created, OpenGL automatically deletes the old table. For example, if you change the display list created in the previous program/***. c **/to the following code:
 

 listIndex=glGenLists(1);if(listIndex!=0){  glNewList(listIndex,GL_COMPILE);  ...  glEndList();}

Then, this program will be more optimized and practical. You may try it on your own. You can also use it to create several more display lists, or delete another one to see how it works?

16.4 multi-level display list
The establishment of a multi-level display list is to call another display list in one display list, that is, to call glcalllist () between the glnewlist () function and gldlist (). Multi-Level Display lists are useful for constructing objects composed of multiple components, especially when some components need to be reused. To avoid infinite recursion, the nested depth of the display list is up to 64 (maybe higher, depending on different OpenGL implementations). Of course, you can also call the glgetintegerv () function () to obtain the maximum nested depth value.
OpenGL allows you to call tables that have not been created in the created display list. When the first display list calls the second table that is not defined, no operation is performed. In addition, you can use a display list that contains several low-level display lists to simulate the creation of an editable display list. The following code:

 

glNewList(1,GL_COMPILE);glVertex3fv(v1);glEndList(); glNewList(2,GL_COMPILE);glVertex3fv(v2);glEndList(); glNewList(3,GL_COMPILE);glVertex3fv(v3);glEndList(); glNewList(4,GL_COMPILE);glBegin(GL_POLYGON);glCallList(1);glCallList(2);glCallList(3);glEnd();glEndList();

In this way, you can call display list 4 to draw a triangle, that is, call glcalllist (4). to edit a vertex, you only need to re-create the corresponding vertex display list.

Original article: http://www.cppblog.com/zmj/archive/2006/01/17/2820.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.