OpenGL: display list

Source: Internet
Author: User

1. Advantages of displaying a list
Optimized OpenGL display list DesignProgramRunning 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.

2. Create and execute a display list

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 ()

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

 

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 followingCode:

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.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/StFairy/archive/2007/07/27/1711681.aspx

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.