OpenGL Rotating axis-aligned image by 90 degrees

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

Recommended Posts

I'm using SFML, but it just wraps OpenGL fairly closely in this area, so this is really an OpenGL question.

My textures are axis-aligned. If I wanted to rotate them 90 degrees, without using a the typical OpenGL fixed-function rotation functions and without using shaders or manipulating matrices, would it make more sense to do it by rotating the position of the vertices or by rotating the texture coordinates?

My code currently looks like this:
//The pre-transform position and size:
float widthBeforeTransform = static_cast<float>(subRect.width);
float heightBeforeTransform = static_cast<float>(subRect.height);
vertices[0].position = sf::Vector2f(0, 0); //<< Should I rotate these?
vertices[1].position = sf::Vector2f(0, heightBeforeTransform);
vertices[2].position = sf::Vector2f(widthBeforeTransform, heightBeforeTransform);
vertices[3].position = sf::Vector2f(widthBeforeTransform, 0);

//Calculate the texture coordinates:
float left   = static_cast<float>(subRect.left);
float right  = left + subRect.width;
float top    = static_cast<float>(subRect.top);
float bottom = top + subRect.height;

//If we're mirroring, swap the texture coordinates vertically and/or horizontally.
if(flipVertically) std::swap(top, bottom);
if(flipHorizontally) std::swap(left, right);

//Set the texture coordinates:
vertices[0].texCoords = sf::Vector2f(left, top); [font='Helvetica Neue', Arial, Verdana, sans-serif]//<< Or should I rotate these?[/font]
vertices[1].texCoords = sf::Vector2f(left, bottom);
vertices[2].texCoords = sf::Vector2f(right, bottom);
vertices[3].texCoords = sf::Vector2f(right, top);

Share on other sites

Change the positions, that will work correctly if the texture is not square.

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11