Source code of Version 7 of opengl programming guide includes bug Page35 lines. c hongbao book, page35lines. c
Problem 1: When I typed in according to the source code, I found that the running results were incorrect. What's wrong? The source code does not contain glPushAttrib (GL_LINE_STIPPLE) glPopAttrib (). Therefore, each time you update a vertex draw line, the next draw will overwrite the previously drawn dot line with the last set information. Therefore, we need to implement State Pressure stack processing and stack processing. In this way, frame repainting will not affect the rendering results of other frames. (I don't know what the cause is. It is estimated that it is a problem of state machine management. You must understand the internal Drawing Mechanism .) However, it is too difficult to manually add the pressure stack and stack, so I will implement the macro definition form of C_style.
Problem 2: After the reshape operation is blocked, it is found that the window parameters can still be properly drawn as long as the window parameters are correctly set in the display .. The problem arises. Shouldn't the reshape be triggered only when I stretch the window? However, the changes can still be displayed normally. Please advise me !!
Modify source code 1 as follows:
# Include "stdafx. h "# include <GL/freeglut. h> # define drawOneLine (x1, y1, x2, y2) glBegin (GL_LINES); \ glVertex2f (x1), (y1); glVertex2f (x2 ), (y2); glEnd (); void init (void) {glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);} void display (void) {// In the initial initialization definition window, I intentionally moved this section out to test the call of the reshape function. Question 2: int w = 400, h = 150; glViewport, static_cast <GLsizei> (w), static_cast <GLsizei> (h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluOrtho2D (0.0, static_cast <GLfloat> (w), 0.0, static_cast <GLfloat> (h); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); // int I; glClear (GL_COLOR_BUFFER_BIT); glPushMatrix (); glColor3f (1.0, 1.0, 1.0); glable (GL_LINE_STIPPLE); // 1st rowglPushAttrib (GL_LINE_STIPPLE); // read in binary format and draw from low position. The first parameter is the tensile parameter glLineStipple (0101 x ); drawOneLine (50.0, 125.0, 150,125.0); glPopAttrib (); glPushAttrib (GL_LINE_STIPPLE); glLineStipple (1, 0x00FF); drawOneLine (150.0, 125.0, 250.0, 125.0); glPopAttrib (); glPushAttrib (callback); glLineStipple (1, 0x1c47); drawOneLine (250.0, 125.0, 350.0, 125.0); glPopAttrib (); // 2nd rowglLineWidth (5.0); glPushAttrib (callback ); // read in binary format and draw from low position. The first parameter is the tensile parameter glLineStipple (0101x50.0); drawOneLine (100.0, 150.0, 100.0,); glPopAttrib (); glPushAttrib (rows); glLineStipple (1, 0x00ff); drawOneLine (150.0, 100.0, 250.0, 100.0); glPopAttrib (); glPushAttrib (rows); glLineStipple (1, 0x1c47 ); drawOneLine (250.0, 100.0, 350.0, 100.0); glPopAttrib (); glLineWidth (1.0); // 3rd rowglPushAttrib (rows); glLineStipple (1, 0x1c47); glBegin (GL_LINE_STRIP ); for (int I = 0; I <7; I ++) {glVertex2f (50.0 + static_cast <GLfloat> (I * 50.0), 75.0);} glEnd (); glPopAttrib (); glDisable (GL_LINE_STIPPLE); glPopMatrix (); glFlush ();} void reshape (int w, int h) {} int _ tmain (int argc, char * argv []) {gluinit (& argc, argv); gluinitdisplaymode (glu_single | glu_rgb); gluinitwindowsize (400,150); gluinitwindowposition (0, 0); glucreatewindow (argv [0]); init (); fig (NULL); fig (); return 0 ;}
Example of correct source code:
// Lines_P35.cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <GL/freeglut. h> # define drawOneLine (scale, stipple, x1, y1, x2, y2) \ glPushAttrib (GL_LINE_STIPPLE); \ glLineStipple (scale), (stipple )); \ glBegin (GL_LINES); \ glVertex2f (x1), (y1); glVertex2f (x2), (y2); \ glEnd (); \ glPopAttrib (); void init (void) {glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);} void display (void) {glClear (GL_COLOR_BUFFER_BIT); glPushMatrix (); glColor3f (1.0, 1.0, 1.0); glable (GL_LINE_STIPPLE); // 1st row // read in binary format and draw from low position, the first parameter is the tensile parameter drawOneLine (0101x50.0, 125.0, 150,125.0, 150.0); drawOneLine (1, 0x00FF, 125.0, 250.0, 125.0,); drawOneLine (1, 0x1c47, 250.0, 125.0, 350.0, 125.0); // 2nd rowglLineWidth (5.0); // read in binary format and draw from low position. The first parameter is the drawing parameter drawOneLine (0101 x, 50.0, 100.0, 150.0, 100.0); drawOneLine (1, 0x00ff, 150.0, 100.0, 250.0, 100.0); drawOneLine (1, 0x1c47, 250.0, 100.0, 350.0, 100.0 ); glLineWidth (1.0); // 3rd rowglPushAttrib (GL_LINE_STIPPLE); glLineStipple (1, 0x1c47); glBegin (GL_LINE_STRIP); for (int I = 0; I <7; I ++) {glVertex2f (50.0 + static_cast <GLfloat> (I * 50.0), 75.0);} glEnd (); glPopAttrib (); // 4 thfor (int I = 0; I <6; I ++) {drawOneLine (1, 0x1c47, 50.0 + static_cast <GLfloat> (I * 50.0), 50.0, 50.0 + static_cast <GLfloat> (I + 1) * 50.0, 50.0);} // 5 thdrawOneLine (5, 0x1c47, 50.0, 25.0, 350.0, 25.0); glDisable (GL_LINE_STIPPLE); glPopMatrix (); glFlush ();} void reshape (int w, int h) {glViewport (0, 0, static_cast <GLsizei> (w), static_cast <GLsizei> (h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluOrtho2D (0.0, static_cast <GLfloat> (w), 0.0, static_cast <GLfloat> (h); glMatrixMode (GL_MODELVIEW); glLoadIdentity ();} int _ tmain (int argc, char * argv []) {gluinit (& argc, argv); gluinitdisplaymode (glu_single | glu_rgb); gluinitwindowsize (400,150); gluinitwindowposition (0, 0); glucreatewindow (argv [0]); init (); gludisplayfunc (display); glureshapefunc (reshape); glumainloop (); return 0 ;}