Jump to content
  • Advertisement
Sign in to follow this  
padam

OpenGL How to rotate texture around its center ????

This topic is 3445 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

Hello everyone. I am new to opengl.I am rendering 2D Sprite using Opengl with SDL. I am trying to rotate the texture but it rotates and revolves(translates) also in a orbit around top-left region of a texture. I don't know why its happening cuz i haven't used the glTranslatef(). I want to rotate my texture around its center.And want to know why my texture is revolving. Here is the code: void drawImage() { // Clear the screen before drawing glClear( GL_COLOR_BUFFER_BIT ); glRotatef( spin, 0.0, 0.0, 1.0 ); // Bind the texture to which subsequent calls refer to glBindTexture( GL_TEXTURE_2D, texture ); glBegin( GL_QUADS ); // Top-left vertex (corner) glTexCoord2i( 0, 0 ); glVertex3f( 100, 100, 0 ); // Bottom-left vertex (corner) glTexCoord2i( 1, 0 ); glVertex3f( 228, 100, 0 ); // Bottom-right vertex (corner) glTexCoord2i( 1, 1 ); glVertex3f( 228, 228, 0 ); // Top-right vertex (corner) glTexCoord2i( 0, 1 ); glVertex3f( 100, 228, 0 ); glEnd(); glLoadIdentity(); SDL_GL_SwapBuffers(); } Why i am having this kinda output?? How to rotate it around its center ? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
I'm in a hurry so I'm going to assume I know the answer without looking at your code. Based on your "I'm new" and it's rotating around its corner I'd say its the way you are drawing the quad.

When you draw your quad you are probably drawing the first point of the new quad at 0,0 which is what you are rotating around. Lets say you have a 10x10 quad for your texture you would actually want to draw it as -5,-5 to 5,5 instead of 0,0 to 10,10. That way you create the quad around the rotation point instead of starting at it.

Share this post


Link to post
Share on other sites
Thanks a lot. yeah it worked. As u suggested, my quad size is 128*128 so i drew it as (-64,-64) to (64,64)...and it worked. Thanks again. But could u explain the concept that why i had to do that. Thanks

and here is the updated code:

void drawImage()
{
// Clear the screen before drawing
glClear( GL_COLOR_BUFFER_BIT );

glTranslatef(300.0,200.0,0.0);
glRotatef( spin, 0.0, 0.0, 1.0 );

// Bind the texture to which subsequent calls refer to
glBindTexture( GL_TEXTURE_2D, texture );

glBegin( GL_QUADS );
// Top-left vertex (corner)
glTexCoord2i( 0, 0 );
glVertex3f( -64, -64, 0 );

// Bottom-left vertex (corner)
glTexCoord2i( 1, 0 );
glVertex3f( 64, -64, 0 );

// Bottom-right vertex (corner)
glTexCoord2i( 1, 1 );
glVertex3f( 64, 64, 0 );

// Top-right vertex (corner)
glTexCoord2i( 0, 1 );
glVertex3f( -64, 64, 0 );
glEnd();
glLoadIdentity();

SDL_GL_SwapBuffers();
}

Share this post


Link to post
Share on other sites
Rotations happen about 0,0. So since you draw your quad at 0,0 with a height of 128 and width of 128 it was rotating around 0,0 (the top left corner). However, by translating the quad to -64,-64. 0,0 is now directly at the centre of the quad, thus when you do the rotation, it rotates about the centre of the quad.

Share this post


Link to post
Share on other sites
You might try a little real world test just for kicks. Take a sheet of paper and cut it into a square. Then take a pin of some sort. Imagine that pin is and will always be 0,0. Where do you have to put the pin to get it the paper to rotate around the center? What happens when you put your pin in the corner of the paper?

If you don't put the pin in the center, or perhaps, Put the paper around the pin, then it won't spin in the center. If you put the corner of the paper where the pin is at then it will spin on its corner. Just like your textured quad was doing. Make any more sense?

Share this post


Link to post
Share on other sites
As you've seen, the render code is always wrt a specific point in space. If you didn't want to change your render code, you can still specify an arbitray rotation centre by doing the following.

1) translate the object by a set offset to locate your origin at the desired "centre" spot

2) rotate

3) translate by the -ve amount you did in step 1

4) Now render

By choosing the offset in step 1 so that you move your render origin to any where you like, you gain the ability to rotate about any point in space without having to change your rendering coordinates.

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!