problem using glViewport to display an object twice

Started by
3 comments, last by radom 13 years, 12 months ago
Hi guys. I was trying to draw glutWireCube twice in my window using glViewport() command to divide the window into two parts. The thing is, I'm getting it drawn only once. If I comment out either of the glViewport() commands, I see that they are working as they should, displaying the glutWireCube in the specified window. But, when I combine them, only the latter gets displayed. Here is the code, thanks for any help. #include <windows.h> #include <glut.h> #include <stdlib.h> #include <stdio.h> #include <math.h> void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); } void display(void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glLoadIdentity (); /* clear the matrix */ /* viewing transformation */ gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef (1.0, 2.0, 1.0); /* modeling transformation */ glutWireCube (1.0); glFlush (); } void reshape (int w, int h) { glViewport (0, 0, ((GLsizei) w)/2, ((GLsizei) h)/2); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); glMatrixMode (GL_MODELVIEW); // This is the second window where the glutWireCube is redrawn glLoadIdentity(); glViewport ((GLsizei) w/2, 0, ((GLsizei)w)/2, ((GLsizei) h)/2); glMatrixMode (GL_PROJECTION); glLoadIdentity (); //glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); gluPerspective(55, 1, 1.5, 20); glMatrixMode (GL_MODELVIEW); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (600, 600); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; }
Advertisement
Use [ source ] tags (no spaces) when posting code.

It looks to me like you might be trying to set up both viewports 'at once' in your reshape function. OpenGL doesn't work this way. OpenGL is a state machine; at any one time there is one active viewport, one modelview matrix state (assuming FFP), etc. When you load a new transform or set the viewport, you're not setting up an additional transform or viewport, you're overwriting the previous one. The way your code is now, only the second 'setup' has any effect (the values established in the first are overwritten).

Assuming I'm correct about what's wrong with your code, what you'll need to do to get the effect you're after is to set up the first viewport/projection/etc., render, then set up the second viewport/projection/etc. and render.
Thanks jyk. That's very helpful. can you make it more clear by writing a snippet of code because I don't exactly know how to render multiple times from a single code..

Radom
Please follow jyk's request to wrap your code by [source] and [/source] tags. It would make reading the code easier for us.

The scheme would look something like the following (untested code copied-&-pasted from the OP):
// global variables used here for demonstration purposes; not a good idea in generalstatic int windowWidth = 600, windowHeight = 600;void reshape (int w, int h) {    windowWidth = w;    windowHeight = h;}void display () {   // 1st rendering ...   glViewport (0, 0, ((GLsizei) windowWidth)/2, ((GLsizei) windowHeight)/2);   glMatrixMode (GL_PROJECTION);   glLoadIdentity ();   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);   glMatrixMode (GL_MODELVIEW);   draw ();   // 2nd rendering ...   glViewport ((GLsizei) windowWidth/2, 0, ((GLsizei) windowWidth)/2, ((GLsizei) windowHeight)/2);   glMatrixMode (GL_PROJECTION);   glLoadIdentity ();   gluPerspective(55, 1, 1.5, 20);   glMatrixMode (GL_MODELVIEW);   draw ();   // reworking   glFlush ();}void draw () {   glClear (GL_COLOR_BUFFER_BIT);   glColor3f (1.0, 1.0, 1.0);   glLoadIdentity ();   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);   glScalef (1.0, 2.0, 1.0);   glutWireCube (1.0);}

You will probably have to set glScissor everywhere where you set glViewport, too.
Thanks a lot haegarr. I added glScissor() and it now works just fine..
Sure I'll use
 
from now onwards,...

This topic is closed to new replies.

Advertisement