Rotating Texture Problem

Started by
2 comments, last by Sir Sapo 18 years, 11 months ago
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.
My Current Project Angels 22 (4E5)
Advertisement
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.
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...
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.
My Current Project Angels 22 (4E5)

This topic is closed to new replies.

Advertisement