Why can't I display same object in multiple viewports?
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开发者_如何学C.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
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.
精彩评论