2D OpenGL With SDL Rotation + Scaling Problem
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.)
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;
}My Game Dev Page:
http://whindgames.50webs.com/
StressFracture: http://www.sourceforge.net/projects/stressfracture
I'm hardly fluent with OpenGL, but surely the glEnd() should be before the glPopMatrix()?
Mark Bishop
You're probably right - but I fixed that and it didn't change anything.
My Game Dev Page:
http://whindgames.50webs.com/
StressFracture: http://www.sourceforge.net/projects/stressfracture
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
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.
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.
My Game Dev Page:
http://whindgames.50webs.com/
StressFracture: http://www.sourceforge.net/projects/stressfracture
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| OpenGL Alpha Channel Problem | Moganza | 1 | 1,421 |
Jan 19, 2013 08:25 AM Last Post: sealfin |
|
| Simple OpenGL ES problem | soulstorm | 3 | 3,343 |
May 14, 2009 03:53 PM Last Post: AnotherJake |
|
| Opengl picking problem (zip file) | papillon68 | 1 | 3,743 |
Mar 1, 2009 08:49 PM Last Post: chronus |
|
| OpenGL Center of rotation? | 3DTOPO | 7 | 7,742 |
Sep 26, 2008 01:30 PM Last Post: haegarr |
|
| Problem with SOIL in OpenGL | RingoEST | 12 | 7,917 |
Aug 12, 2008 09:30 PM Last Post: RingoEST |
|

