Sign in to follow this  
asafdav2

glRotate misunderstanding

Recommended Posts

there's a certain phenomena i don't understand let's say i want to move a cube across the negative z axis in code, this looks something like that
vector3 position(0, 0,  0);
        velocity(0, 0, -1);

void Update(double dt)
{
	position += velocity * dt;
}

void Display(void)
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glPushMatrix();
		
		// move the camera up a bit
                glTranslatef(0.0f, -1.0f, 0.0f);	
		
		glTranslatef(pos.x, pos.y, pos.z);
		glutSolidCube(1.0f);
	glPopMatrix();
	glFlush();
}

void Idle() 
{
	// update the frame time
	SortFrameTimer();
	// update the game, use time amount since last query
	Update(FrameTime());
	// redraw the screen
	glutPostRedisplay();
}



the rest of the functions are normal. this works and looks well. now i want to do something a bit different. i want to spin the cube abit on some axis (lets say the x axis), and move it again along the negative z axis. in code the only change is
void Display(void)
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glPushMatrix();
		
		
                glRotatef(45.0f, 1.0f, 0.0f, 0.0f)  // NEW
                // move the camera up a bit
                glTranslatef(0.0f, -1.0f, 0.0f);	
		
		glTranslatef(pos.x, pos.y, pos.z);
		glutSolidCube(1.0f);
	glPopMatrix();
	glFlush();
}


however i dont get the effect i want - instead of going along the negative z axis, it goes along some other line (looks like (1, 1, 0)), even though i rotated before translating. what is the proper way of doing this?

Share this post


Link to post
Share on other sites
If I get you, you have to put glRotate after grTranslate,because you have to think the multiplications as occuring in the opposite order(in a grand coordinate system) thus in order to rotate the object relative to an axis and then move it away you have to put glRotate after glTranslate in your code.

[Edited by - Lord_Vader on March 5, 2007 4:15:17 AM]

Share this post


Link to post
Share on other sites
In OpenGL, the transformations happen in object space, not in the world space. Thus, if you rotate first and then translate, that translation happens along the new local axes that differ from the global coordinate axes after the rotation. It's completely intuitive if you think about it that way. And, as has been said, if you want to think in world coordinates, just reverse the order of transformations.

Share this post


Link to post
Share on other sites
Think of it this way:

On OpenGL, each operation pre-pends to the previous operation. The vertex follows the operation from last to first; it flows "backwards" through your code.

One reason for this is so that you can set up the camera transform once, and then render each object using glPushMatrix()/glPopMatrix() go get back to the camera transform (without object-specific transform).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this