View Full Version : glfrustum and glrotatef

Fooly1Cooly

05-06-2004, 04:07 AM

here is the short of it:

with no projections turned on etc.

rotate x axis : rotates cube on x axis

rotate y axis : rotates cube on y axis

rotate z axis : rotates cube on z axis

with projections turned one etc.

rotate x axis : translates cube on +y axis

rotate y axis : translates cube on -x axis

rotate z axis : rotates cube on z axis

I'm really not sure what is wrong, b/c whenever I comment out any rotations while leaving the projections on, everything runs and looks right, and whenever i comment out the projections(depth test, gl_projection, frustum) and adjust my object's z back to 0 and uncomment the rotations, it rotates and behaves correctly on each axis.

Thanks for any help, FLCL

-NiCo-

05-06-2004, 05:08 AM

Looks like your projection matrix is also messing with the rotations.

Don't forget to load the identity matrix before using glFrustum, otherwise the matrix on top of the stack will be multiplied by the matrix you specify.

So if there happened to be a rotation matrix on the stack, this could explain the result.

N.

Fooly1Cooly

05-06-2004, 06:07 AM

That does sound right, but I am loading the identity? Here is my scene code:

{

glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glFrustum(-2,2,-2,2,30,120);

glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glObjects trial;

trial.color(0,1,0);

trial.location(0,0,-35);

trial.scale(0.5,0.5,0.5);

trial.rotation(2,0,1,1);

trial.drawCube();

glFinish();

}

-NiCo-

05-06-2004, 06:40 AM

Everything looks just fine, but I don't know what the glObject functions do:

glObjects trial;

trial.color(0,1,0);

trial.location(0,0,-35);

trial.scale(0.5,0.5,0.5);

trial.rotation(2,0,1,1);

trial.drawCube();

Can you also post the functions called for rotating?

N.

Fooly1Cooly

05-06-2004, 08:12 AM

Here is the draw and rotation method summary :

The drawCube() method just sets the shape to cube and calls the draw function.

draw method

{

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glColor3f(red,green,blue);

glRotatef(objRotation,onX,onY,onZ);

glTranslatef(xLocation,yLocation,zLocation);

glScalef(xScale,yScale,zScale);

glEnable(GL_POLYGON_SMOOTH);

glEnable(GL_LINE_SMOOTH);

glShadeModel(GL_SMOOTH);

< switch statement to correct shape >

}

rotation, scale, location functions just edit the values before it is drawn.

rotate method(float r, float x, float z)

{

objRotation = r; onX = x; onY = y; onZ = z;

}

rotation, scale, location functions just edit the values before it is drawn.

rotate method(float r, float x, float z)

{

objRotation = r; onX = x; onY = y; onZ = z;

}

Fooly1Cooly

05-06-2004, 08:19 AM

just in case, here is the switch statement selection for a triangle :

switch(currentShape)

{

...

case triangle:

glPushMatrix();

glBegin(GL_TRIANGLES);

glVertex3f(-1,0,0); //left

glVertex3f(1,0,0); //right

glVertex3f(0,1,0); //top

glEnd();

glPopMatrix();

break;

...

}

-NiCo-

05-06-2004, 09:00 AM

If the translation is defined relative to the camera coordinate frame, try:

draw method

{

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glColor3f(red,green,blue);

glTranslatef(xLocation,yLocation,zLocation);

glRotatef(objRotation,onX,onY,onZ);

glScalef(xScale,yScale,zScale);

glEnable(GL_POLYGON_SMOOTH);

glEnable(GL_LINE_SMOOTH);

glShadeModel(GL_SMOOTH);

< switch statement to correct shape >

} notice that the translation function has changed place. The reason for this, is that al the matrix transforms perform a postmultiplication.

N.

Fooly1Cooly

05-06-2004, 09:16 AM

Thanks a lot, I actually changed the arrangements to what I've shown you to achieve the same affect. I thought, rotate the shape, then scale the shape, then move the shape. I'm glad you set it right though, thanks a lot really.

So, as long as my translation etc. is relative to the camera, I should maintain this oder? move, rotate, scale?

Thanks, FLCL

-NiCo-

05-06-2004, 09:26 AM

As long as your scalefactor is the same for all three axes, the rotation and the scale can be interchanged.

Typically, what you want to do is:

scale, rotate and then translate

To calculate the final position of a vertex, the modelview is applied to the original vertex.

This modelview matrix is a concatenation of the three operations you perform.

Because of the postmultiplication nature of OpenGL operations on matrices, the operation you specify last will be executed first.

So the desired result is: M*vertices = T*R*S*vertices

glLoadidentity();

//M=I

glTranslatef(.,.,.);

//M=T

glRotatef(.,.,.,.);

//M=T*R

glScalef(.,.,.);

//M=T*R*S

N.

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.