## 2D OpenGL With SDL Rotation + Scaling Problem

Member
Posts: 30
Joined: 2006.05
Post: #1
If there's already a post that answers this question, I'm sorry to bother everyone - but I have looked and haven't been able to find anything.

Essentially, I'm writing a 2D game with SDL and OpenGL - all blitting done with OpenGL texturing and quads. I have bitmaps displaying correctly and am using the screen coordinate system (x = 0-640, y = 0-480, for example).

The problem is, when I rotate or scale an object, the entire screen is rotating or scaling rather than just the object, so the coordinate system is ruined. How can I rotate and scale bitmaps themselves, rather than the entire screen? Snippet below of my current drawing code:

(I need an answer that will work when more than one bitmap is being displayed on the screen at once and will allow me to pass coordinates to the drawing function for each object that are independent of rotation and scaling.)

Code:
```int DrawImage (GLuint& image, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2, long int x3, long int y3, long int w, long int h, GLfloat angle, GLfloat scale) {     glPushMatrix();          glRotatef(angle, 0, 0, 1);     glScalef(scale, scale, 1);          glBindTexture(GL_TEXTURE_2D, image);     glBegin(GL_QUADS);     glTexCoord2f(x1, y1); glVertex2i(x3, y3);     glTexCoord2f(x2, y1); glVertex2i(x3+w, y3);     glTexCoord2f(x2, y2); glVertex2i(x3+w, y3+h);     glTexCoord2f(x1, y2); glVertex2i(x3, y3+h);               glPopMatrix();     glEnd();          return 0; }```
Moderator
Posts: 713
Joined: 2002.04
Post: #2
I'm hardly fluent with OpenGL, but surely the glEnd() should be before the glPopMatrix()?

Mark Bishop
--
Student and freelance OS X & iOS developer
Member
Posts: 30
Joined: 2006.05
Post: #3
You're probably right - but I fixed that and it didn't change anything.
Member
Posts: 338
Joined: 2004.07
Post: #4
When you scale or rotate you're doing that to the entire coordinate system, and when you then do glVertex, the coordinates passed are the distance from the origin in your NEW coordinate system. If you want to scale and rotate one quad around its center, you first want to translate the origin to the center of the quad. So your code would look more like:

Code:
```int DrawImage (GLuint& image, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2, long int x3, long int y3, long int w, long int h, GLfloat angle, GLfloat scale) {     glPushMatrix();          // NEW- add this to move the origin of your coordinate system.     glTranslatef(x3, y3, 0);     // Now you can rotate and scale.     glRotatef(angle, 0, 0, 1);     glScalef(scale, scale, 1);          // Change glVertex to be simply width and height divided by 2 around your new origin.     float half_width = w / 2.0;     float half_height = h / 2.0;     glBindTexture(GL_TEXTURE_2D, image);     glBegin(GL_QUADS);     glTexCoord2f(x1, y1); glVertex2f(-half_width, -half_height);     glTexCoord2f(x2, y1); glVertex2f(half_width, -half_height);     glTexCoord2f(x2, y2); glVertex2f(half_width, half_height);     glTexCoord2f(x1, y2); glVertex2f(-half_width, half_height);     glEnd();     glPopMatrix();     return 0; }```

Justin Ficarrotta
http://www.justinfic.com
"It is better to be The Man than to work for The Man." - Alexander Seropian
Member
Posts: 30
Joined: 2006.05
Post: #5
Exactly what I needed - thanks.

I see that I almost had it right in one test, except that I was trying to translate back and then use the coordinates modified by x3 and y3, and it was just giving me a black screen, probably because of coordinate problems.