PDA

View Full Version : Multiple rotating objects on one window



MMMovania
02-17-2005, 04:50 AM
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.

Aeluned
02-17-2005, 07:02 AM
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.

02-17-2005, 07:34 AM
Check your glMatrixMode() calls carefully, perhaps you are doing everything on top of the projection matrix (which usually has a stack limit of two).

MMMovania
02-17-2005, 07:12 PM
Originally posted by Aeluned:
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.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.

MMMovania
02-17-2005, 07:19 PM
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.

Aeluned
02-18-2005, 05:49 AM
your pseudocode looks fine though...
hhmm...could you post the actual code?

MMMovania
02-18-2005, 06:49 AM
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(&amp;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.

Relic
02-18-2005, 08:12 AM
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.

MMMovania
02-19-2005, 12:45 AM
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???

plasmonster
02-19-2005, 03:36 AM
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.

MMMovania
02-19-2005, 05:23 AM
Originally posted by graham:
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.Thanx Graham for that but are the triangles rotating alright they are not rotating on my PC even though I see clipped tringles?

plasmonster
02-19-2005, 05:59 AM
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.