Viewports Viewports

Started by
12 comments, last by Brother Bob 17 years, 11 months ago
Hi, Simplicity!
Things are not ok in your code, so I'll point them:

// Your first exampleglViewport( 0, 0, WinWidth, WinHeight );glClearColor(1.0f, 1.0f, 0, 1);glClear(GL_COLOR_BUFFER_BIT);glViewport( 0, 0, WinWidth * 0.5f, WinHeight * 0.5f );glClearColor(1.0f, 0, 0, 1);glClear(GL_COLOR_BUFFER_BIT);glutSwapBuffers();


What you are doing here is telling your OpenGL program to (supposing WinWidth == 800 and WinHwight == 600):
1- Set a viewport with bottom-left at (0,0) and top-right at (800,600);
2- Clear ALL the screen to yellow (glClear will not distinguish between viewports);
3- Set a viewport with bottom-left at (0,0) and top-right at (400,300);
4- Clear ALL the screen (again?!) to red;
5- Swap the front and back buffers

This is wrong for multiple reasons. You should try something like this simplified example (I can't test it right now, sorry):

////////////////////// FIRST VIEWPORT //////////////////////// Be sure you are handling the projection matrixglMatrixMode( GL_PROJECTION );// Reset the projection matrixglLoadIdentity();// Set the desired viewportglViewport( 0, 0, WinWidth, WinHeight );// Set the type of the projection matrix. glOrtho is for orthogonal// projection (for perspective, use gluPerspective or glFrustum). Remember that the bottom-left coordinate (in this case, "0,0", is RELATIVE to the bottom-left of the viewport)glOrtho( 0, WinWidth, 0, WinHeight, -1, 1 );// From now on, use the modelview matrix to animate my sceneglMatrixMode( GL_MODELVIEW );// Reset the modelview matrixglLoadIdentity();// Draw a square in the (0, 0, 800, 600) viewportsquare.draw();// Remember to clear the depth buffer (if you use it, of course) or strange // things will happen to your scene. DON'T CLEAR the color buffer (only at the // END OF ALL THE DRAWING PROCESS)glClear( GL_DEPTH_BUFFER_BIT );/////////////////////// SECOND VIEWPORT ///////////////////////// Be sure you are handling the projection matrixglMatrixMode( GL_PROJECTION );// Reset the projection matrixglLoadIdentity();// Set the desired viewport (it will be drawn OVER the first viewport. Is this // what you really want? Whathever...glViewport( 0, 0, WinWidth * 0.5f, WinHeight* 0.5f );// Set the type of the projection matrix. glOrtho is for orthogonal// projection (for perspective, use gluPerspective or // glFrustum). Remember that the bottom-left coordinate (in this case, // "0,0", is RELATIVE to the bottom-left of the viewport).// WARNING WARNING: if your viewport has 400x300 pixels, this will PAST the// bounds of itglOrtho( 0, WinWidth, 0, WinHeight, -1, 1 );// From now on, use the modelview matrix to animate my sceneglMatrixMode( GL_MODELVIEW );// Reset the modelview matrixglLoadIdentity();// Draw a diamond in the (0, 0, 400, 300) viewportdiamond.draw();// Remember to clear the depth buffer (if you use it, of course) or strange // things will happen to your scene. DON'T CLEAR the color buffer (only at the // END OF ALL THE DRAWING PROCESS)glClear( GL_DEPTH_BUFFER_BIT );///////////////// FINISHING /////////////////// Clear the color bufferglClear( GL_COLOR_BUFFER_BIT );// Swap the front and back buffersglutSwapBuffers();


Hope it helps! Goodbye!
Advertisement
Quote:Original post by iliak
Try to cheat with glScissor()...


I don't think its a cheat, I'm pretty sure its the right way todo.. infact, I seem to recall a FAQ entry on opengl.org which yelled 'glViewport doesnt clip', and that the scissor test is required to only clear certain areas of the screen.
So the what does glViewport actually do? And interm of comparison to D3D viewport. I'm trying to write an engine that needs to have the same behavior for D3D and OpenGL incase you're wondering.

Thanks for the help by the way.
When the geometry have passed the perspective division, the coordinates are in normalized device coordinates. That is, in the range [-1, 1] along all axes. glViewport only defines the transformation from normalized device coordinates to window coordinates in pixel units so you can decide where on the window you want the normalized device coordinates to go. That's all it does.

If you want to stop drawing outside the viewport completely (it is possible to draw outside it actually), or restrict the area cleared by glClear, you have to use the scissor box.

This topic is closed to new replies.

Advertisement