Using the display list, we can reduce the duplication of labor. We can get the experience from Example 5.
Example 5: Draw six colored triangles, as shown in Figure six.
#include <windows.h> #include <GL/glut.h> Gluint listname = 1; void Myinit (void) { Glclearcolor (0.0,0.0,0.0,0.0);/Set Background to Black Glnewlist (listname,gl_compile);//Create Display list /* Draw a color polygon * * Glbegin (Gl_polygon); glcolor3f (1.0,0.0,0.0); GLVERTEX2F (1.0,1.0); glcolor3f (0.0,1.0,0.0); GLVERTEX2F (2.0,2.0); glcolor3f (0.0,0.0,1.0); GLVERTEX2F (1.5,2.5); Gltranslatef (0.5,-0.5,0.0);//Coordinate conversion Glend (); Glendlist ()//End Display list Glshademodel (Gl_smooth); } void Mydisplay (void) { Gluint i; Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit); for (i = 0;i<6;i++) Glcalllist (listname); Glflush (); } void Myreshape (Glsizei w,glsizei h) { Glviewport (0,0,W,H); Glmatrixmode (gl_projection); Glloadidentity (); if (w<=h) Glortho ( -4.0,4.0,-4.0* (glfloat) h/(glfloat) w,4.0* glfloat (h/) glfloat); Else Glortho ( -4.0,4.0* (glfloat) w/(glfloat) h,-4.0,4.0,-8.0,8.0); Glmatrixmode (Gl_modelview); Glloadidentity (); Gltranslatef ( -4.0,0.0,-3.0); } int main (int argc,char * * argv) { /* Initialize * * Glutinit (&ARGC,ARGV); Glutinitdisplaymode (glut_single| glut_rgb| Glut_depth); Glutinitwindowsize (500,500); Glutinitwindowposition (100,200); /* Create window * * Glutcreatewindow ("Display list"); /* Draw and display * * Myinit (); Glutreshapefunc (Myreshape); Glutdisplayfunc (Mydisplay); /* Enter GLUT event handling cycle * * Glutmainloop (); return (0); } |
void Glnewlist (gluint list,glenum mode) and glendlist (void) Create or replace a display list. List is the names of the lists. mode specifies the compilation mode, this example is gl_compile. Gl_compile represents just compiling. Gl_compile_and_execute represents execution when the command is compiled into a display list. When the list is displayed, a set of OpenGL commands that are stored up for future execution, use the Glnewlist function to create a display list, and all the commands that need to be executed are placed in the display list in the order in which they are issued, until the Glendlist function is called to end the display list. The command to be executed in this example is to draw a polygon.
void Glcalllist (Gluint list) performs a display list. The parameter list is the name of the display list that you want to perform, and the type is cosmetic. In this example, six identical triangles are drawn in different places.
Figure VI: Six colored triangles |