Group 12 Display List
Translator: Sakura
This is a screenshot of this lesson.
This lesson I will teach you how to use the list of display. Use the display list not only speeds up the running speed of the code, but also greatly shorten the length of the code.
for example. When you make a small aster game scene, there are at least two asteroids every layer. So you are sitting next to your drawings, calculating how to make 3D small planets. When you understand everything, you will use polygons or surfaces in OpenGL to build asteroids. Suppose the asteroids are octahedron. If you are smart enough, you will build a loop and draw your asteroid over a loop. You will use approximately 18 lines of code or more to complete the production of asteroids. Each time you create them when you are drawn to the window (the translator: that is, the overhead is particularly large). You will understand what I mean when you come into contact with more composite objects.
So how do you solve it? Use the display list! With the list of display, you will only create an object. You can make textured mappings, dye it, and all you want to do. You have to give a list of a list, such as we call the list of display of this asteroid called "Asteroid". Then no matter when you want to draw this textured mapping or dyed asteroid to the window, you just need to call the function GlcallList ("Planet"). The asteroids we previously created will appear on the window. This is because the asteroids have been created in the display list. OpenGL does not have to create how to create him, it has been created in memory before. This will save a lot of CPU times and allow your program to run faster!
So are you ready? In this example we will call this display list as Q-BERT. We will complete 15 cubes with Q-BERT. Each cube is made up of a top and a box. Top is a separate display list, which is to give them a brunette shadow. The box is not top.
The code is based on the sixth lesson. I will rewrite the most places of the program, so you easily see which places have been modified. The following lines of code are standard code in all our courses.
#include
#include
#include
#include
#include
HDC HDC = NULL; // Private GDI Device Context
Hglrc hrc = null; // permanent rendering context
HWND HWND = NULL; // Holds Our Window Handle
Hinstance Hinstance; // Holds The Instance of the Application
Bool keys [256]; // array buy for the keyboard routineBool activity = true; // window activ flag set to true by default
Bool Fullscreen = true; // Fullscreen Flag Set to Fullscreen Mode by Default
Next, set the variable. First establish a variable of the storage texture. Then we build 2 new variables to save the display list. These two variables are like a pointer, pointing out the location where the list is stored in memory. They are called Box and TOP. Then we create two variables called Xloop and YLOOP, they will be used to indicate the position of the cube on the screen, and the other 2 variables XROT and YROT will represent the angle of the cube in the X and Y axis.
Gluint Texture [1]; // Storage for One Texture
Gluint Box; // Storage for the Display List
Gluint Top; // Storage for the Second Display List
Gluint xloop; // loop for x axis
Gluint YLOOP; // loop for y axis
GLFLOAT XROT; // Rotates Cube on The x Axis
GLFLOAT YROT; // Rotates Cube on the y y
Next we create 2 color arrays. The first is the color of the box, stores bright red, orange, yellow, green and blue. Each color contains RGB components in {}. The second set of colors is dark red, dark orange, dark yellow, dark green and dark blue. These dark colors will be used to create the top of the box. We hope that the lid is more deeper than the other parts of the box.
Static Glfloat Boxcol [5] [3] = // Array for Box Colors
{
// Bright: Red, Orange, Yellow, Green, Blue
{1.0F, 0.0F, 0.0F}, {1.0F, 1.0F, 0.0F}, {0.0F, 1.0F, 0.0F}, {0.0F, 1.0F , 1.0F}
}
Static GLFLOAT TOPCOL [5] [3] = // Array for Top Colors
{
// Dark: Red, Orange, Yellow, Green, Blue
{.5F, 0.0F, 0.0F}, {0.5F, 0.25F, 0.0F}, {0.5F, 0.5F, 0.0F}, {0.0F, 0.5F, 0.0F}, {0.0F, 0.5F , 0.5f}
}
LResult Callback WndProc (HWND, UINT, WPARAM, LPARAM); // Declaration for WndProc
This is now a list of display. If you noticed all the code to create a box in the first list, and the code used to build the top is another, I will explain these details. GLVOID BUILDLIST () // Build Box Display List
{
When we started, we told OpenGL we have to build two listings. Glgenlists (2) establishes two spaces for displaying lists and returns the first pointer to the first display list. "Box" points to the first display list, which calls "Box" first display list will be displayed.
Box = Glgenlists (2); // Building Two Lists
The first display list is now created. We have applied for the space for two display lists, and you already know that Box points to the first display list. So now we should tell OpenGL to establish what type of display list. We use the command Glnewlist () to do this. You must notice that Box is the first parameter, which means that OpenGL will store the list to the memory space pointed to by Box. The second parameter GL_Compile tells OpenGL we want to build this list in memory so that you don't have to calculate how to create an object every time you draw. GL_Compile is similar to programming. When you write a program, load it into the compiler, you need to recompile each runner. And if he has compiled into a .exe file, then you only need to double-click the .exe file, you don't need to compile. When OpenGL compiles the display list, you don't need to recompile it every time you are displayed. This is why the display list can be accelerated.
Glnewlist (Box, GL_Compile); // New Compiled Box Display List
The code below draws a box without top, which does not appear on the screen, will only be stored in the list. You can add any code you want to add in Glnewlist () and GLENGLIST (). You can set the color, you can change the texture map, and the like. The only code that cannot be added is to change the code of the display list. Once the list is created, you cannot change it. If you add GLCOLOR3UB (Rand ()% 255, Rand ()% 255), this is to have different colors for each painting object. But because the list will only be established once, the color does not change each time the object is used. The object will remain the color of the first established. If you want to change the color of the display list, you only change the color before calling the display list. This will explain this later.
Glbegin (GL_QUADS); // Start Drawing Quads
// Bottom Face
GLTEXCOORD2F (1.0F, 1.0F); GlvertEX3F (-1.0F, -1.0F, -1.0F); // Top Right of The Texture and Quad
GLTEXCOORD2F (0.0F, 1.0F); GlvertEX3F (1.0F, -1.0F, -1.0F); // Top Left of the texture and quad
GLTEXCOORD2F (0.0F, 0.0F); Glvertex3f (1.0F, -1.0F, 1.0F); // bottom Left of the texture and quad
GLTEXCOORD2F (1.0F, 0.0F); GlvertEX3F (-1.0F, -1.0F, 1.0F); // bottom Right of the Texture and Quad
// Front Face
GLTEXCOORD2F (0.0F, 0.0F); GlvertEX3F (-1.0F, -1.0F, 1.0F); // bottom left of the texture and quadgltexcoord2f (1.0F, 0.0F); GlvertEX3F (1.0F, -1.0F, 1.0 f); // bottom right of the texture and quad
GLTEXCOORD2F (1.0F, 1.0F); GLVERTEX3F (1.0F, 1.0F, 1.0F); // Top Right of The Texture and Quad
GLTEXCOORD2F (0.0F, 1.0F); Glvertex3f (-1.0F, 1.0F, 1.0F); // Top Left of the texture and quad
// Back Face
GLTEXCOORD2F (1.0F, 0.0F); GlvertEX3F (-1.0F, -1.0F, -1.0F); // bottom Right of the Texture and Quad
GLTEXCOORD2F (1.0F, 1.0F); GlvertEX3F (-1.0F, 1.0F, -1.0F); // Top Right of The Texture and Quad
GLTEXCOORD2F (0.0F, 1.0F); GlvertEX3F (1.0F, 1.0F, -1.0F); // Top Left of the Texture and Quad
GLTEXCOORD2F (0.0F, 0.0F); GlvertEX3F (1.0F, -1.0F, -1.0F); // bottom left of the texture and quad
// Right Face
GLTEXCOORD2F (1.0F, 0.0F); GlvertEX3F (1.0F, -1.0F, -1.0F); // bottom right of the texture and quad
GLTEXCOORD2F (1.0F, 1.0F); Glvertex3f (1.0F, 1.0F, -1.0F); // Top Right of The Texture and Quad
GLTEXCOORD2F (0.0F, 1.0F); GLVERTEX3F (1.0F, 1.0F, 1.0F); // Top Left of the Texture and Quad
GLTEXCOORD2F (0.0F, 0.0F); Glvertex3f (1.0F, -1.0F, 1.0F); // bottom Left of the texture and quad
// LEFT FACE
GLTEXCOORD2F (0.0F, 0.0F); GlvertEX3F (-1.0F, -1.0F, -1.0F); // bottom left of the texture and quad
GLTEXCOORD2F (1.0F, 0.0F); GlvertEX3F (-1.0F, -1.0F, 1.0F); // bottom Right of the Texture and Quad
GLTEXCOORD2F (1.0F, 1.0F); GLVERTEX3F (-1.0F, 1.0F, 1.0F); // Top Right of The Texture and Quad
GLTEXCOORD2F (0.0F, 1.0F); GlvertEX3F (-1.0F, 1.0F, -1.0F); // Top Left of the texture and quad
glend (); // done drawing quads
We use glendlist () to tell OpenGL, we have completed the list of display. Then the part between glbeginlist () and glendlist () is the content of the display list. The part outside them is not the content of the currently displayed list. glendlist ();
Then let's create the next display list. We increment above the old list Box to get the second display list in memory. The following code will create a list of 'TOP'.
TOP = Box 1;
We already know that the second display list is stored, we can build it. We will establish a second in accordance with the method of establishing the first display list. However, we will tell OpenGL to store the list in top instead of Box.
Glnewlist (Top, GL_Compile);
The following code segment is drawing the top of the box. It is a very simple graphic on the z plane.
Glbegin (GL_QUADS); // Start Drawing Quad
// Top Face
GLTEXCOORD2F (0.0F, 1.0F); GlvertEX3F (-1.0F, 1.0F, -1.0F); // Top Left of the texture and quad
GLTEXCOORD2F (0.0F, 0.0F); GLVERTEX3F (-1.0F, 1.0F, 1.0F); // bottom Left of the texture and quad
GLTEXCOORD2F (1.0F, 0.0F); GLVERTEX3F (1.0F, 1.0F, 1.0F); // bottom Right of the Texture and Quad
GLTEXCOORD2F (1.0F, 1.0F); Glvertex3f (1.0F, 1.0F, -1.0F); // Top Right of The Texture and Quad
glend (); // Done Drawing Quad
Let's tell the OpenGL list again. Of course, use command glendlist (). We have successfully established 2 lists.
Glendlist (); // done building the top display list
The code used to load the bitmap established texture and the same in previous courses. We hope that we can use the texture to render all 6 faces of the cube. So I decided to use "multiped mapping (translator: that is, MipMapping" to make the texture look smooth. I hate to see pixels. The loaded texture is called cube.bmp. It is stored in the Data folder. Find the loadbmp function and change the corresponding code row.
INT INITGL (GLVOID) // All setup for OpenGL Goes Here
{
If (! loadinggltextures ()) // jump to texture loading routine
{
Return false; // if texture Didn't load return false
}
BuildLists (); // jump to the code That Creates Our Display Lists
Glenable (GL_Texture_2D); // enable texture mapping
Glshademodel (GL_SMOOTH); // enable smooth shading
GlclearColor (0.0F, 0.0F, 0.0F, 0.5F); // black backgroundglcleardepth (1.0f); // depth buffer setup
Glenable (GL_DEPTH_TEST); // Enables Depth Testing
GLDEPTHFUNC (GL_LEQUAL); // the Type of Depth Testing to do
The 3 line code is activated in the following line. Light0 is predefined in most graphics cards, so we can avoid excessive discussion of the light source. After we activate Light0, we will activate illumination. If Light0 can't work on your graphics card (you will see a dark), turn off the light.
The following line GL_COLOR_MATERIAL allows us to join the texture map. If the material color is not activated, the texture will keep it the color. Functions GLCOLOR3F (R, G, B) will not have any effects of changing color. So activating him is very important.
Glenable (GL_LIGHT0);
Glenable (GL_Lighting);
GLENABLE (GL_COLOR_MATERIAL);
Finally we set up a perspective and make it look more beautiful. And return True, let our program know that initialization is successful.
GLHINT (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice Perspective Correction
Return True;
Now we write a drawing code. As usual, I have a little crazy about mathematics. No sin, no COS, but still have a little strange. We still start like a clear screen and depth cache as usual.
Then we build textures for cubes. I can add this line code to the display list code. But after leaving it outside the list, I can change the texture at any time. If I join GlbindTexture (GL_Texture_2D, Texture [0]) in the list, the display list will be permanently maintained for the texture I have chosen for him.
INT Drawglscene (Glvoid) // Here's Where We do all the drawing
{
// Clear the screen and the defth buffer
GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GlbindTexture (GL_Texture_2d, Texture [0]); // Select The Texture
The next is an interesting object. We call this loop for YLOOP. This loop is used to define the position of the cube on the Y-axis. We hope that there will be 5 cubes up and down, so we circulate from 1 to 5.
For (YLOOP = 1; YLOOP <6; YLOOP ) // Loop THROUGH THE Y PLANE
{
We also have a loop called xloop. It is used to indicate the position of the cube on the X axis. The number of cubes depends on which line we are. If we are in the first line, Xloop will only range from 0 to 0, that is, just draw a cube. Downstream will draw 2 cubes and so on.
For (xloop = 0; xloop { Reset the scene with GLLoadIndentity (). GLLoadIndentity (); The following code translands the object on the screen. It looks confused, but in fact, it is not the case. On the x-axis, the following events occur: In order to make the pyramid appear on the center of the screen, we move 1.4 units to the right. Then the XLOOP is multiplied on 2.4 and plus 1.4 (multiplying 2.8 to make these cubes are not on the top of other cubes, when we rotate 45 degrees, 2.8 is a rough approximate to the cubic width). Finally minus YLOOP * 1.4. This will move the cube left and depend on the current line number. If we don't have left shift, the pyramid will be straight back to the left (it looks like a pyramid). On the Y-axis we subtract YLOOP 6 otherwise the pyramid will be built above. Then we multiply the structure in 2.4. Otherwise, the cubic will be in contact with the top of other cubes (2.4 is an approximate cube height). Then subtract 7, so the pyramid can be established upward on the bottom of the screen. Finally, we move the object along the Z axial screen. This way our lovely pyramid is perfectly matched. GLTRANSLATEF (1.4f * 2.8F) - (Float (YLOOP) * 1.4F) ((6.0F-float (YLOOP)) * 2.4F) -7.0F, -20.0F); Now we turn around the X axis. We will enable the cube to tilt (45-2 * YLOOP) degrees. Since the perspective tilt of the cube is automatically carried out, I subtract it to offset the tilt effect. Although it is not the best way, it can do very well :) Finally, we have accumulated XROT. It provides our interface to control the angle of rotation with a keyboard (it will be very interesting). After we rotate around the x-axis, we turn 45 degrees on the Y-axis, and add YROT to control the rotation of the Y axis. GLROTATEF (45.0F- (2.0F * YLOOP) XROT, 1.0F, 0.0F, 0.0F); // Tilt The Cubes Up and Down GLROTATEF (45.0F YROT, 0.0F, 1.0F, 0.0F); Before the cube box (excluding the top), we must first select color (bright) first. Note We use Glcolor3FV (). This command will load the RGB color from the braces {} disposable and set it. 3FV indicates 3 values, floating points, and vector mode. The index of the color we chose is YLOOP-1, which will give each of the boxes different colors. If you use XLOOP-1, then the color of each column box will not be the same. Glcolor3FV (Boxcol [YLOOP-1]); The color has been set, we should start painting our box. Everything we do is just call the display list, rather than writing a drawing function of all boxes. We use the command GlcallList (box) to call the display list. Box tells OpenGL to select a Box display list instead of others. The Box display list draws a cube without top. The box will draw with our choice and move to our expectant location. GlcallList (box); Before drawing the top of the box, first choose the top color (dark color). This color will depend on the line (YLOOP-1). Glcolor3FV (Topcol [YLOOP-1]); Finally, the only thing we have is to draw the top of the box. This will add a dark cover to the box. This is all. It's really simple! GlcallList (TOP); } } Return True; } The remaining changes are in WinMain (). Some code will be added later after SwapBuffer (HDC), if you press the left, right, upper or down keys, the cube will move accordingly! SwapBuffers (HDC); if (Keys [VK_LEFT]) { YROT- = 0.2F; } IF (Keys [vk_right]) { YROT = 0.2F; } IF (Keys [vk_up]) { XROT- = 0.2F; } IF (Keys [vk_down]) { XROT = 0.2F; } Just like the previous tutorial, make sure that the window title is correct. IF (Keys [vk_f1]) { Keys [vk_f1] = false; Killglwindow (); Fullscreen =! fullscreen; IF (! "" Nehe's Display List Tutorial, 640, 480, 16, Fullscreen)) { Return 0; } } } } At this end, you should have a good understanding of the list of display. For example, how to create them, how to display them on the screen. The list is really great! They not only make the encoding of complex objects simplify, but also improve some frame rates. I hope you can like this tutorial. If you have any questions or something you don't understand, please write let me know. Translator: Old rules, announce my contact information ... Email: sakura@china.com MSN: autsak@hotmail.com This fuel tank does not accept Website: http://www.autsak.com