Sign in to follow this  
Sir Sapo

Rotating Texture Problem

Recommended Posts

Sir Sapo    769
Hey guys, My program used to run on BMPs, but I recently converted it to use PNGs which brings me to my problem. I assume PNGs are read from bottom to top, not top to bottom because when I run my app, all my textures are upside down. No big deal I thought , I'll just switch to the texture matrix and rotate them all 180 degrees right? But I found that when you rotate a texture using that method, the texture rotates around the bottom left hand corner, messing up my texture mapping. My question is, how can I make the textures rotate around the center of the textures and not the corner. Thx in advance.

Share this post


Link to post
Share on other sites
mrrolf810p    122
When you rotate, it is always around the orgin. To rotate about an arbitrary point ( the middle of the texture), you need to move the origin to that point, rotate, then move the origin back. So instead of just rotating, do something like this:

translate by 0.5,0.5
rotate 180 degrees
translate by -0.5,-0.5

I've had this problem before also. I found it much easier to read the file 'upside down' instead of messing with the texture matrix. when you read in the image data, read the rows in reverse order. So have something like this:

for(h=0;h<imageHeight;h++){
for(w=0;w<imageWidth;w++){
imageBuffer[imageHeight-h-1][w] = GetPixelFromFile(w,h);
}
}




Hope that helps.

Share this post


Link to post
Share on other sites
dotproduct    180
I really recommend flipping the images before you give them to OpenGL instead of using the texture matrix to rotate them.
How are you loading your images? If you're using some sort of library for that, I'm sure that library also has a flip() function (or something similar). If you're loading them with your own code, you can quite easily write a flip function as well...

Share this post


Link to post
Share on other sites
Sir Sapo    769
Thanks mrrolf, I got it working, here's how if anyone else is having this problem right now.

glTranslatef(0.5 , 0.5, 0);
glScalef(-1 , 1 , 1);
glRotatef(180 , 0 , 0 , 1);
glTranslatef(-0.5 , -0.5, 0);

I am using pngLIB to load and bind my pngs to textures, and I dont know if it has a flip function , but I will look. BTW will doing this to the matrix stack give me any hit on performance or is it a pretty low cost operation?

EDIT: yeah pngLIB has a flip function, turns out libPNG uses the top left corner for (0 , 0) not the bottom left , thats why the textures were upside down.

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