Introduction to OpenGL - L26b
Scissor Test
Next we need to decide at what point we need the initial scissor box defined and to what values to set it. In this example it will be set to the size of the window whenever the window is resized. Usefully the resize function is also called when the window is first drawn.
So, in the function Transform , we add this code. Note the "adjustments" for the window decorations. Also, we use the function glGetIntegerv to return the current scissor box parameters and print them on the consol.
/* (re)Set the scissor box to the current window */
scissorx = glutGet(GLUT_WINDOW_X)-8; /* -8 for pixel border */
scissory = glutGet(GLUT_WINDOW_Y)-32; /* -32 for top bar ! */
scissorwidth = glutGet(GLUT_WINDOW_WIDTH);
scissorheight = glutGet(GLUT_WINDOW_HEIGHT);
glScissor(scissorx, scissory, scissorwidth, scissorheight);
glGetIntegerv( GL_SCISSOR_BOX,chop);
printf("x/y = %d %d w/h = %d %d/n",chop[0],chop[1],chop[2],chop[3]);
The OpenGL specification states that only the window inside the scissor rectangle is cleared by the function glClear . So, we have to provide some tests to enable and disable the scissor test if we wish to clear the window outside the scissor rectangle. So, in the drawing function DrawGLScene we add code around the glClear function.
if( glIsEnabled(GL_SCISSOR_TEST) )glDisable(GL_SCISSOR_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(!glIsEnabled(GL_SCISSOR_TEST) && scissortest==1 )glEnable(GL_SCISSOR_TEST);
Notice the use of the gl function glIsEnabled(GL_SCISSOR_TEST) to check if scissor testing is enabled. If so, it is disabled for the glClear function call. We now have to use our own state variable to determine whether to enable it again (together with !glIsEnabled(GL_SCISSOR_TEST) , to avoid unnecessary state changes).
Finally it would be nice to see the scissor box and so we add code into the drawing function DrawGLScene to do this. Because the scissor box parameters are in screen coordinates, we need to establish an orthographic projection to draw this data.
/******* Draw the scissor window so we can see what gives ******/
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, glutGet(GLUT_WINDOW_WIDTH),
0.0, glutGet(GLUT_WINDOW_HEIGHT), -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINE_STRIP);
glVertex2f( scissorx+1 , scissory+1);
glVertex2f( scissorwidth+scissorx-1, scissory+1);
glVertex2f( scissorwidth+scissorx-1, scissorheight+scissory-1);
glVertex2f( scissorx+1, scissorheight+scissory-1);
glVertex2f( scissorx+1, scissory+1);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
Notice we save the PROJECTION matrix and establish a new orthographic projection. We then save the MODELVIEW matrix and load the identity matrix for this. Set the colour to white, draw the box, but note that we draw it 1 pixel smaller in x and y, otherwise this box will itself be clipped by the scissor test! Finally we reset the MODELVIEW matrix and the PROJECTION matrix back to their previous states.
Now proceed to the next part of the lesson.
转自:http://www-course.cs.york.ac.uk/cgv/OpenGL/L26b.html