PDA

View Full Version : Multiple viewports



clintCJM
10-04-2006, 01:45 PM
I've got a 3D openGL window in my application. I've created two viewports. The first viewport is the leftmost 85% of the openGL window. The second viewport is the rightmost 15% of the window. I've done this successfully. I then succesfully draw my objects in the left viewport.

NOW FOR THE HELP I NEED. The object that I'm drawing the second viewport needs to be perfectly centered, and rotate around it's middle axis, like a person spinning around a pole. However, when I rotate the objects in the window, the second viewport's object isn't rotating TIGHTLY. Instead, it's rotating simlarly to the model in the left veiwport.

Any thought?

Thanks very much.

ZbuffeR
10-04-2006, 02:57 PM
I am confused by your sparse explanation :-)

Anyway, you may want to swap your glTranslate and glRotate calls, it is a common error.

darkrappey
10-04-2006, 05:56 PM
Wat do you mean by TIGHTLY and why caps? Is that a constant?(...)

I imagine wat you mean is that the object is not rotating along it its own axes, but along the global center axes. To fix this first call glLoadIdentity(); Then call glTranslatef(x,y,z);

x,y,z being the x,y,x of the objects location or position.

Then call:

glRotatef(x,1,0,0);
glRotatef(y,0,1,0);
glRotatef(z,0,0,1);

x,y,z being the rotation in that axes (in degrees)

Ok hopefully that is the correct answer to the correct question.

Post back.

clintCJM
10-05-2006, 06:42 AM
Here's my code:

SetProjection();

// clear background to white and clear depth buffer
glClearColorfv( m_fvClearColor );
glClearDepth( 1.0 );
glEnable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glFlush();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(x,y,z);
glRotated(dRotation, 0.0, 0.0, 1.0);

// draw object in the first viewport
glPushAttrib( GL_ALL_ATTRIB_BITS );
glEnable( GL_CULL_FACE );
glCullFace( GL_BACK );
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

glPushMatrix();

//here's drawing code for the object itself, in the first view port (it's simply a disk)

quadObj = gluNewQuadric();
gluQuadricOrientation( quadObj, GLU_INSIDE );
gluQuadricDrawStyle( quadObj, GLU_FILL );
gluQuadricNormals( quadObj, GLU_SMOOTH );
gluDisk( quadObj, 0.0, dLength, 16, 8);
gluDeleteQuadric( quadObj );

glPopMatrix();

glPopAttrib();

glPopMatrix();

//draw object in the second viewport
//(this is the one I'm having trouble with)

glViewport(....);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective( m_dFOV, dAspectRatioOfSecondViewport, 1.0, 15.0);
glClear (GL_DEPTH_BUFFER_BIT);

//draw the object in the second viewport
.....

glPopMatrix();

SwapBuffers(wglGetCurrentDC());


Everything in my first viewport draws perfectly. It's only the second viewport that's giving me problems. I don't even see the object in my second viewport, with the above code. Perhaps my openGL function calls are out of sequence? I've tinkered around with changing the order, but to no avail.

clintCJM
10-05-2006, 11:09 AM
Ok, I modified my code and now my object in my second viewport draws, but it does NOT rotate. Viewport #2 needs rotate simultaneously with Viewport #1.


Here's my code:

SetProjection();

// clear background to white and clear depth buffer
glClearColorfv( m_fvClearColor );
glClearDepth( 1.0 );
glEnable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glFlush();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(x,y,z);
glRotated(dRotation, 0.0, 0.0, 1.0);

// draw object in the first viewport
glPushAttrib( GL_ALL_ATTRIB_BITS );
glEnable( GL_CULL_FACE );
glCullFace( GL_BACK );
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

glPushMatrix();

//here's drawing code for the object itself, in the first view port (it's simply a disk)

quadObj = gluNewQuadric();
gluQuadricOrientation( quadObj, GLU_INSIDE );
gluQuadricDrawStyle( quadObj, GLU_FILL );
gluQuadricNormals( quadObj, GLU_SMOOTH );
gluDisk( quadObj, 0.0, dLength, 16, 8);
gluDeleteQuadric( quadObj );

glPopMatrix();

glPopAttrib();

glPopMatrix();


//NOW I HANDLE THE VIEWPORT #2

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glViewport();//set viewport #2

gluPerspective( m_dFOV, dAspectRatio, .1, 6.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//draw the object in viewport #2
......
.....

glClear (GL_DEPTH_BUFFER_BIT);
SwapBuffers(wglGetCurrentDC());

Again, viewport #1 is perfect, rotates correctly, etc. Viewport #2 is ok, except that the object does not rotate whatsoever. It should rotate when the object in viewport #1 is rotated by the user.

Thanks again.

clintCJM
10-09-2006, 11:40 AM
I figured out the issue(s). Mainly, the problem was not moving (glTranslate) to the proper location in viewport #2 before drawing that viewport's model. When calling gluPerspective(...), you've got to be sure that the 'near Z' and 'far Z' are set such that the Z of the drawing itself falls between them.

Thanks for your help, darkrappey and ZbuffeR.