How to rotate texture around its center ????

Started by
4 comments, last by mmakrzem 14 years, 10 months ago
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.
http://www.gaanza.com/bloghttp://gaanza.com/games
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.

------------------------------------------------------------- neglected projects Lore and The KeepersRandom artwork
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();
}
http://www.gaanza.com/bloghttp://gaanza.com/games
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.
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?

------------------------------------------------------------- neglected projects Lore and The KeepersRandom artwork
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.

This topic is closed to new replies.

Advertisement