Jump to content
  • Advertisement
Sign in to follow this  
Arjan B

OpenGL Rotation and Translation

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Goal: I'm trying to have a square drawn with it's center at (square.X(), square.Y()), rotated by square.Orientation(). ( So no matter what it's orientation is, it's center will be at (x, y). ) Problem: I thought I would be able to do that by first rotating and then translating, it does rotate the square but it looks like it also rotates the direction in which it translates.. So then I thought I'd try first translating and then rotating, but that didn't do much good either.. Code: OpenGL initialization:
// Set up OpenGL
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, -1.0, 1.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glClearColor( 0.0, 0.0, 0.0, 0.0 );
if ( glGetError() != GL_NO_ERROR ) {
	ShowMsg( "Could not set up OpenGL." );
	return false;
}



Drawing square:
void Draw( Square& square )
{
        glClear( GL_COLOR_BUFFER_BIT );

	// Draw quad
        glLoadIdentity();
	glTranslatef( square.X(), square.Y(), 0.0 );
	glRotatef( square.Orientation(), 0.0, 0.0, 1.0 );
	glBegin( GL_QUADS );
		glColor4f( 1.0, 1.0, 1.0, 1.0 );
		glVertex2f( square.Left(),  square.Top() );
		glVertex2f( square.Right(), square.Top() );
		glVertex2f( square.Right(), square.Bottom() );
		glVertex2f( square.Left(),  square.Bottom() );
	glEnd();

	// Update screen
	SDL_GL_SwapBuffers();
}



Question: Could anyone help me out with the goal I'm achieving and point out what I'm doing wrong at the moment? Thank you in advance, Arjan

Share this post


Link to post
Share on other sites
Advertisement
You'll have to draw the square with it's center at [0,0] (in local space), and do rotation first, then have a translation that transforms that center to the correct position.


Share this post


Link to post
Share on other sites
Thanks for your reply!

Quote:
Original post by quasar3d
(1) draw the square with it's center at [0,0] (in local space), (2) and do rotation first, (3) then have a translation that transforms that center to the correct position.


Isn't that what this does?

(1) glLoadIdentity();
(3) glTranslatef( x, y, 0 );
(2) glRotatef( angle, 0, 0, 1 );



Switching (2) and (3) didn't help either ^^.
By the way: before these calls, the matrix mode is GL_MODELVIEW ( in case that matters ).

Share this post


Link to post
Share on other sites
No, what it means is that the squares center must be located at the origin for the rotation to be as you describe. For example, if your square ranges from 0 to 1 along both axes, the center is at (0.5, 0.5) which is not the origin. For your square to be located at the center, square.top() == -square.bottom() and square.right() == -square.left() must be true.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!