Multiple rotating objects on one window

Hello there.
I want to make an application having half of the window showing the front profile of an object whereas the other half showing the top profile. Now I have done this by creating two viewports. I get my objects in the correct orientation but as soon as i rotate them i don’t see them both rotate together. I tried to push matrix and pop it
just to give you the idea this is what I am doing
in my rendering function.

clear
setViewport1()
push matrix
rotate()
drawObject()
pop matrix

setViewport2()
push matrix
rotate()
drawObject()
pop matrix

Help me please. I tried to replace the push pop matrix calls with loadIdentity but still get the same result. Thanx in advance.

Does your setViewport function set the camera transformations (for a top view for example).

what do you mean you don’t see them rotate together? only one rotates?

I don’t see anything obviously wrong with your code there, it’s probably elsewhere.

Check your glMatrixMode() calls carefully, perhaps you are doing everything on top of the projection matrix (which usually has a stack limit of two).

Originally posted by Aeluned:
[b]Does your setViewport function set the camera transformations (for a top view for example).

what do you mean you don’t see them rotate together? only one rotates?

I don’t see anything obviously wrong with your code there, it’s probably elsewhere.[/b]
My setViewport is nothing but
glViewport(0,0,300,300);
and the second one is
glViewport(300,0,300,300);
Thus I have two 300x300 regions.

By not seeing them together, I mean that when I place glRotate call before first DrawObject call then both of the objects rotate however when I place two glRotate calls (one before each drawObject function) then nothing rotates everything is static.

Originally posted by <Honk>:
Check your glMatrixMode() calls carefully, perhaps you are doing everything on top of the projection matrix (which usually has a stack limit of two).
Well, I have setup the projection in my reshape function. I would check to see if I have changed the matrix to modelview in or before my render function. Thanx for that.

your pseudocode looks fine though…
hhmm…could you post the actual code?

Originally posted by Aeluned:
your pseudocode looks fine though…
hhmm…could you post the actual code?

Sure here you go.

/////////////////////////////////////////////////
//Main.cpp
#include <GL/glut.h>


struct point2D{
	GLfloat x,y;
};



void Init()
{
   glClearColor(1.0,1.0,1.0,0.0);

   glMatrixMode(GL_PROJECTION);
   gluOrtho2D(-100,100,-100,100);

   glMatrixMode(GL_MODELVIEW);
}

void DrawTriangle(point2D *pts)
{
   GLint i;

   glBegin(GL_TRIANGLES);
   for(i=0;i<3;i++)
	   glVertex2f(pts[i].x, pts[i].y);
   glEnd();
}

void Render()
{
   point2D pts[3]={{-50.0,-25.0 },{50.0,-25.0},{0.0,50.0}};

   glClear(GL_COLOR_BUFFER_BIT);
   
   glColor3f(0, 0 , 1);				//Change color to blue
   glViewport(0,0,300,300);         //SetViewport1
   glPushMatrix();					//Push matrix
   glRotatef(2.0f,0.0f,0.0f,1.0f);	//Rotate in z axis
   DrawTriangle(pts);				//DrawObject
   glPopMatrix();                   //Pop matrix 

   glColor3f(1, 0 , 0);			   //Change color to red
   glViewport(300,0,300,300);      //SetViewport2
   glPushMatrix();				   //Push matrix
   glRotatef(2.0f,1.0f,0.0f,0.0f); //Rotate in x axis
   DrawTriangle(pts);			   //Pop matrix
   glPopMatrix();
     
   glFlush();

}

void Idle()
{		
   glutSwapBuffers();
   glutPostRedisplay();
}

void main(int args, char **argv)
{
   glutInit(&args,argv);
   glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
   glutInitWindowSize(600,300);
   glutCreateWindow("Test vieports in GLUT");
   Init();
   glutDisplayFunc(Render);  
   glutIdleFunc(Idle);
   glutMainLoop();
}

/////////////////////////////////////////////////

Let me know your comments.Thanx for putting in your time.

Try this: Remove the glutSwapBuffers from the idle loop and replace the glFlush in the Render function with it.
In general, do not rely on backbuffer contents after SwapBuffers.

Originally posted by Relic:
Try this: Remove the glutSwapBuffers from the idle loop and replace the glFlush in the Render function with it.
In general, do not rely on backbuffer contents after SwapBuffers.

Tried what you said but now I don’t see my triangles. When the program runs now, it sort of takes a snapshot of my screen and displays it. Now I do not see my triangles. What is wrong now???

Hi MMMovania,

I changed the gluOrho2D call to

glOrtho(-100,100,-100,100,-100,100),

and now I see 2 happy triangles.

I think you were seeing the right triangle clipped by the far-plane.

Originally posted by graham:
[b]Hi MMMovania,

I changed the gluOrho2D call to

glOrtho(-100,100,-100,100,-100,100),

and now I see 2 happy triangles.

I think you were seeing the right triangle clipped by the far-plane.[/b]
Thanx Graham for that but are the triangles rotating alright they are not rotating on my PC even though I see clipped tringles?

If you mean rotated, yes.

If you mean animating, no. To do that, update an angle variable each frame and pass that to Rotatef.

For example, add something like this to the very top of your Render function:

static float angle = 0;
if( (angle += 0.5) >= 360 )
    angle -= 360;
...
glRotatef( angle, 1,0,0 );
...

The angle update should be based on time, but I hope you get the idea.