OpenGL Display List

Source: Internet
Author: User

The OpenGL display list is made up ofa set of pre-stored OpenGL function statements that are reserved for later invocation, executing the function statements listed in the table in turn when this display list is called. The examples given in the preceding section are instantaneous functions, and OpenGL executes the corresponding command instantaneously, which is called an immediate or instantaneous (Immediate mode) method. This chapter describes in detail the basic introduction to the display list, creation, execution, management, and application of multilevel display lists.

16.1. Introduction to the Display list

Advantages of 16.1.1 Display list

The OpenGL display list is designed to optimize program performance, especially network performance. It is designed to be a command cache rather than a dynamic database cache. In other words, once a display list is established, it cannot be modified. Because if the display list can be modified, the cost of displaying the list's search, memory management execution, and so on will degrade performance.

Drawing in a display list is generally faster than instantaneous mode, especially the display list method can greatly improve the network performance, that is, when the drawing command is issued over the network, because the display list resides in the server, thus minimizing the burden of the network. Also, on a single-user machine, the display list can also improve efficiency. Because once the display list is processed into a format that is appropriate for the graphics hardware, different OpenGL implementations have different optimizations for the commands. For example, the rotation matrix function, glrotate* (), can greatly improve performance if it is placed in the display list. Because the calculation of the rotation matrix is not simple, including the square, trigonometric functions and other complex operations, and in the display list, it is only stored as the final rotation matrix, and then executed as fast as the hardware execution function Glmultmatrix (). In general, the display list can combine many neighboring matrix transformations into a single matrix multiplication, thus speeding up the speed.

16.1.2 Display list for the occasion

It is not possible to optimize program performance by simply invoking the display list. Because the program has some overhead when calling the display list itself, this overhead will outweigh the advantages of the display list if a display list is too small. Here are the scenarios where the display list can be optimized:

    • Matrix operations
      Most matrix operations require OpenGL to compute the inverse matrix, and the matrix and its inverse matrix can be saved in the display list.
       
    • Raster bitmaps and images
      Program-defined raster data is not necessarily the ideal format for hardware processing. When compiling a display list for the organization, OpenGL may convert the data into data that the hardware can accept, which effectively increases the speed of the bitmap.
       
    • Light, material and lighting models
      When you draw a scene with a more complex lighting environment, you can change the material for each object in the scene. However, the material is calculated more, so setting the material may be slow. If you put the material definition in the display list, you do not have to recalculate each time you change the material. Because the results are stored in a table, the lighting scene can be drawn more quickly.
       
    • Texture
      Because the texture format of the hardware may not be the same as the OpenGL format, if the texture definition is placed in the display list, the format can be converted when the display list is compiled, rather than in execution, which can greatly improve efficiency.
       
    • Pattern fill pattern for polygons
      You can put the defined pattern in the display list.

16.2. Create and execute a display list

16.2.1 Creating a display list

OpenGL provides a structure that resembles a draft entity, that is, Glbegin () and Glend () to create a display list whose corresponding functions are:

void Glnewlist (gluint list,glenum mode);

Describes the beginning of a display list, after which the OpenGL function is stored in the display list until the function of the end table is called (see below). The parameter list is a positive integer that flags the unique display list. The possible values for the parameter mode are Gl_compile and Gl_compile_and_execute. To make the subsequent function statements only deposit and not execute, use gl_compile; to make the subsequent function statement into the table and execute it instantaneously once, use Gl_compile_and_execute.

void glendlist (void);

The flag displays the end of the list.

Note : Not all OpenGL functions can be stored in the display list and executed through the display list. In general, function statements that pass parameters or return numeric values cannot be stored in the display list because the table is likely to be called outside the scope of the parameter, and if such a function is called when the display list is defined, they are executed in an instantaneous manner and are not saved in the display list. Sometimes an error occurs when a display list function is called. The following is an OpenGL function that cannot be saved to a display list:

Gldeletelists ()  glisenable () glfeedbackbuffer ()  glislist () glfinish () Glpixelstore (  ) glgenlists ()  Glrendermode () glget* ()  Glselectbuffer ()

16.2.2 performing a display list

After the display list is established, a function that executes the display list can be called to execute it, and the same display list can be executed more than once in the program, as well as in an instantaneous manner with other functions. The display list performs the following functions:

void glcalllist (gluint list);

Executes the display list. The parameter list specifies the display list to be executed. The function statements in the display list are executed sequentially in the order in which they are stored, and nothing happens if the list is not defined. Here's a simple example of an app display list:

1#include <gl/glew.h>2#include <gl/glut.h>3#include <cmath>4 5 voidInit (void)6 {7Glclearcolor (0.2,0.2,0.2,0.0);8 Glmatrixmode (gl_projection);9 glloadidentity ();TenGluortho2d (0.0,800.0,0.0,600.0); One  A } - voidLineSegment (void) - { the glclear (gl_color_buffer_bit); -GLCOLOR3F (1.0,0.0,0.0); -     Const DoubleTWO_PI =6.2831853; - Gluint Reghex; +  - gldouble Theta; + Glint x, y, K; AReghex = Glgenlists (1);//Perform a display table at glnewlist (Reghex, gl_compile); - Glbegin (Gl_polygon); -      for(k =0; K! =6; k++) { -theta = Two_pi * k/6.0; -x = $+ Max*cos (theta); -y = $+ Max*sin (theta); in glvertex2i (x, y); -     } to glend (); + glendlist (); -Glcalllist (Reghex);//to display the hexagon the Glflush (); * } $ Panax Notoginseng intMainintargcChar*argv[]) - { theGlutinit (&argc, argv); +Glutinitdisplaymode (Glut_single |Glut_rgb);  AGlutinitwindowposition ( -, -); theGlutinitwindowsize ( -, -); +Glutcreatewindow ("An Example OpenGL Program"); - Init (); $ Glutdisplayfunc (linesegment); $ Glutmainloop (); -     return 0; -}

16.3. Manage Display list

In the previous example, we used a positive integer as the index of the display list. In practice, however, this is generally not the case, especially if multiple display lists are created. If you do this, it is possible to select an index that is being occupied and overwrite the existing display list, which is harmful to the program's operation. To avoid accidental deletion, you can call the function glgenlist () to produce an unused display list, or call Glislist () to determine whether the specified display list is occupied. In addition, you can also call function gldeletelists () to delete a list of displays in a range or scope during the management of the display list. These functions are described separately below:

Gluint glgenlist (Glsizei range);

Assigns a range of contiguous, unoccupied display list indexes. This function returns a positive integer index value, which is the first value of a contiguous set of empty indexes. The returned index is both empty and occupied, and is no longer returned when this function is called later. The function returns 0 if the specified number of requested indexes is not met or range is 0.

Glboolean glislist (gluint list);

Ask if the display list is already occupied. If the index list is already occupied, the function returns ture, and conversely, returns FAULSE.

void gldeletelists (gluint List,glsizei range);

Deletes a contiguous set of display lists, starting with the display list indicated by the parameter list, deleting the range display list, and the deleted indexes are re-valid. If you delete a display list that is not established, the delete operation is ignored.

OpenGL automatically deletes old tables when you create a display list that is the same as the Display list index that already exists. This section, for example, changes the display list created in the previous section of program/***.c*/to the following code:

Listindex=glgenlists (1); if (listindex!=0) {  glnewlist (listindex,gl_compile);  ...  Glendlist ();}

Well, this program will be more optimized and practical. You can try it yourself and use it to create a few more display lists, or delete one to see how it works.

16.4. Multilevel Display list

The creation of a multilevel display list is to invoke another display list in one display list, that is, call Glcalllist () between the function glnewlist () and Glendlist (). Multilevel display lists are useful for constructing objects made up of multiple components, especially when some components need to be reused. But to avoid infinite recursion, the nesting depth of the display list is up to 64 (perhaps higher, depending on the OpenGL implementation), and of course the function Glgetintegerv () can be called to get the maximum nesting depth value.

OpenGL allows the invocation of a table that has not been established in the established display list, and no action occurs when the first display list calls a table that is not defined by the second one. In addition, a display list is allowed to contain several low-level display lists to simulate the creation of an editable display list. The following code:

1Glnewlist (1, gl_compile);2 3 GLVERTEX3FV (v1);4 5 glendlist ();6 7Glnewlist (2, gl_compile);8 9 GLVERTEX3FV (v2);Ten  One glendlist (); A  -Glnewlist (3, gl_compile); -  the GLVERTEX3FV (v3); -  - glendlist (); -  +Glnewlist (4, gl_compile); -  + Glbegin (Gl_polygon); A  atGlcalllist (1); -  -Glcalllist (2); -  -Glcalllist (3); -  in glend (); -  toGlendlist ();

In this way, to draw the triangle you can call the display list 4, that is, call Glcalllist (4); To edit a vertex, simply re-establish the corresponding vertex display list.

Reference to the Http://www.cnblogs.com/yxnchinahlj/archive/2010/11/19/1881426.html

OpenGL Display List

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.