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!