Win32 OpenGL programming (14) display list

Source: Internet
Author: User
Tags mercurial
Document directory
  • Display list
  • Efficiency Comparison
  • Use
  • References

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie


Discuss newsgroups and documents

Technorati label: display list
, Display list
, OpenGL

Some time ago, because I had just changed my job, many things were not on the right track. for work reasons, I learned many other things, such as irrlicht, and now I am slowly slowing down, we should finish learning OpenGL. Haha, in fact, this series is mainly based on their own learning, but I chose a relatively tired writing method. In fact, while learning the Win32 code of the nehe tutorial, I can complete it, however, I chose to follow the OpenGL programming guide.
", This will be more detailed than the nehe learning, but because you need to play a lot of extra words, so it is also a lot tired. Continue to learn.

 

Display list

Displaying a list can improve the performance. In fact, just like storing a pair of functions in OpenGL cache and then calling them quickly, in fact, the advantage of displaying a list is that it is faster, but it cannot be changed. Even the included parameters cannot be changed ....... It is equivalent to sacrificing flexibility in exchange for certain performance ..... Of course, in the pursuit of the performance of graphics programs, the sacrifice is more than flexibility ...... Readability, maintainability, and scalability ..... Have sacrificed

 

Efficiency Comparison

Let's take a look at the example below. For more specific performance display list optimization, there is no way to draw one or two balls. I decided to draw 100 balls on the screen, the source code uses 2009-11-9/lightsimple in the original article to increase the calculation of dynamic illumination and further increase the calculation workload. You can use a common function to do this. The Code is as follows:

void DrawAll(){    glPushMatrix();    glTranslatef(-0.9, 0.9, 0.0);    for(int i = 0; i < 10; ++i)    {        for(int j = 0; j < 10; ++j)        {            glutSolidSphere(0.1, 30, 16); // r = 1 / 20            glTranslatef(0.2, 0.0, 0.0);        }        glTranslatef(-2.0, -0.2, 0.0);    }    glPopMatrix();}

The meaning of the Code should not be explained clearly. First, move the code to the upper left corner of the screen, draw the distance from each ball, draw the next one, and change the line after every 10 balls, and move to the beginning to continue painting.

Then let go of the FPS limit and add a code for calculating the FPS:

// called every frameint CalculateFPS(DWORD now){    static int frameCounted = 0;    static int startTime = GetTickCount();    static int fps = 0;    ++frameCounted;    int elapsed = now - startTime;    if (elapsed >= 1500 )    {        fps = ( 1000 * frameCounted ) / elapsed;        startTime = now;        frameCounted = 0;    }    return fps;}


For the sake of special writing, if you have any questions, the meaning is also very simple. Every 1500 milliseconds (one second and a half) calculates the FPS, why is it 1500 milliseconds, it seems that the error will be a little bigger if it is set to 1000. In fact, the longer the time used here, the smaller the error, but the longer the calculation interval, the less responsive change.

By the way, for better display efficiency, I used the off-screen rendering (dual buffering) to look at the first section.

Shows the effect and approximate running efficiency:


Basically between 150 and ~ About 200 frames. (Because the instant capture of the screen will cause instant FPS drops)

Let's take a look at the version of the List:

Use the following statements to generate a new display list:

giTorus = glGenLists(2);glNewList(giTorus, GL_COMPILE);    glutSolidSphere(0.1, 30, 16); // r = 1 / 20glEndList();glNewList(giTorus+1, GL_COMPILE);glPushMatrix();glTranslatef(-0.9, 0.9, 0.0);for(int i = 0; i < 10; ++i){    for(int j = 0; j < 10; ++j)    {        glCallList(giTorus);        glTranslatef(0.2, 0.0, 0.0);    }    glTranslatef(-2.0, -0.2, 0.0);}glPopMatrix();glEndList();

Then call:

Glcalllist (gitorus + 1 );

Basically, unlike the general OpenGL feature, the display list is very simple to use, but it requires some knowledge and experience to know when to use it, in OpenGL programming guide
", There is even a special section" design philosophy of display list "to elaborate on this.

Let's look at the effect:

Basically between 370 and ~ Between 450 (similarly, the instant capture will cause instant FPS drops)

From the perspective of FPS, the efficiency has increased exponentially. This is the function of displaying the list. Such a small program cannot mask its power ......

 

Use

The list display involves not too many APIs, and it is easy to understand:

OpenGL Reference Manual
":

Gluint glgenlists (glsizei range );

Parameters

Range

Specifies the number of contiguous empty display lists

To be generated.

Glgenlists returns a starting flag of the display list with the specified length of range, which is equivalent to the memory allocated for normal operations. The first memory pointer is returned, and glgenlists returns the first display list flag, it is expressed by an integer.

Then, the glnewlist and gldlist function pairs indicate the start and end of a display list. The display list is determined by the first parameter of glnewlist, just like defining the "{" and "}" of a function.

Then, glcalllist is used to call the display list. Just like calling a function, you can use parameters to specify which display list to call.

giTorus = glGenLists(2);glNewList(giTorus, GL_COMPILE);    glutSolidSphere(0.1, 30, 16); // r = 1 / 20glEndList();


The preceding four sentences have defined a complete display list, allocation, and definition. The gl_compile parameter is defined but not executed. (Just like when a real function is defined for the first time)

glNewList(giTorus+1, GL_COMPILE);glPushMatrix();glTranslatef(-0.9, 0.9, 0.0);for(int i = 0; i < 10; ++i){    for(int j = 0; j < 10; ++j)    {        glCallList(giTorus);        glTranslatef(0.2, 0.0, 0.0);    }    glTranslatef(-2.0, -0.2, 0.0);}glPopMatrix();glEndList();


In the preceding statements, a complete display list operation is displayed, which defines a complete display list (that is, the ball on the screen above) in addition, "glcalllist (gitorus);" is also used to call the display list just defined. This also shows the nesting of the display list.

To save space, only key clips are posted. For the complete source code, see the 2009-12-21/displaylisttest directory of the source code of my blog. For more information about obtaining the complete source code of my blog, see the article.

One program in the complete program implements the above two methods, using the gbisusedisplaylist global variable control, you need to note that, naturally, there is a lightsimple code that contains the code to change the illumination position.

 

References

1. OpenGL Reference Manual
, OpenGL Reference Manual

2. OpenGL
OpenGL programming guide
), Dave shreiner, Mason Woo, Jackie neider, Tom Davis
Xu Bo, Mechanical Industry Press

3. nehe OpenGL tutorials, In the http://nehe.gamedev.net/
You can find the tutorials and related code to download. (The PDF version of the tutorials is available) nehe also developed an object-oriented framework. As a demo program, this framework is very suitable. There are also Chinese Versions
Take all the necessary information.

 

Complete source code retrieval instructions

Due to space limitations, this article generally only posts the main focus of the Code, the full version of the Code with a project (or makefile) (if any) can be downloaded in Google code using mercurial. The article is stored in different directories on the date published by the blog post. Use mercurial to clone the following database:

Https://blog-sample-code.jtianling.googlecode.com/hg/

For how to use mercurial, see Introduction and brief introduction to the distributed and next-generation version control system mercurial.
"

If you only want to browse all the code, you can go to Google Code to view it. the following URL:

Http://code.google.com/p/jtianling/source/browse? Repo = blog-Sample-code

 

The author of the original article retains the copyright reprinted. Please indicate the original author and give a link

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

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.