Scissor Test

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值