tags:

views:

129

answers:

1

Hi,
I'm pretty new to OpenGL. I was playing around with some code but I can't figure out why the following will not produce two viewports with the same object view. Here's the code:

glViewport(0, windowHeight/2, windowWidth/2, windowHeight);
glScissor(0, windowHeight/2, windowWidth/2, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 45.0, (GLfloat)(windowWidth/2)/(GLfloat)(windowHeight/2), 0.1f,  
                500.0 ); 
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
drawParticleView();

glViewport(windowWidth/2, 0, windowWidth, windowHeight/2);
glScissor(windowWidth/2, 0, windowWidth, windowHeight/2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 45.0, (GLfloat)(windowWidth/2)/(GLfloat)(windowHeight/2), 0.1f, 
                500.0 ); 
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
drawParticleView();

drawParticleView() just draws an array of rectangles. The problem is that the second viewport is a squashed representation of the first. My window width is 1280 and height 960. I'm obviously doing something wrong but what? Thanks

+1  A: 

The parameters to glViewport are the lower left corner of your viewport as x and y, then width and height.

For a window of 100 pixels square, your two viewports are specified as:

x1 = 0, y1 = 50, width1 = 50, height1 = 100.

x2 = 50, y2 = 0, width2 = 100, height2 = 50.

These placements and sizes put the first viewport in the upper left quadrant of your window, hanging half out the top of your window, and the second in the lower left quadrant of your window, hanging half out the side of your window.

For side by side viewports I think you want:

glViewport(0, 0, windowWidth/2, windowHeight);
// drawing code
glViewport(windowWidth/2, 0, windowWidth/2, windowHeight); 
// repeat drawing code

Or top and bottom viewports I think you want:

glViewport(0, 0, windowWidth, windowHeight/2);
// drawing code
glViewport(0, windowHeight/2, windowWidth, windowHeight/2); 
// repeat drawing code

The reason your second viewport is squashed is because it's aspect ratio is inverted, and therefore the parameter to gluPerspective is wrong. The aspect ratio parameter should be (windowWidth/2)/windowHeight for the first option above, and windowWidth/(windowHeigh/2) in the second option above.

Mr. Berna
Great! I see where I was going wrong. However, I think for what I was trying to achieve, which I didn't make clear, the aspect ratio was correct. I was trying to display the view in two diagonal viewports, hence I required space for 4 viewports in total. Therefore, I think that my original aspect ratio was correct (albeit pointlessly divided by 2). Thanks
Brett