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;
}
Quote this message in a reply
Moderator
Posts: 704
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
Quote this message in a reply
Member
Posts: 30
Joined: 2006.05
Post: #3
You're probably right - but I fixed that and it didn't change anything.
Quote this message in a reply
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
Quote this message in a reply
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Alpha Channel Problem Moganza 1 3,299 Jan 19, 2013 08:25 AM
Last Post: sealfin
  iPad, OpenGL ES, and XCode Instruments problem! Bandit 0 4,106 Dec 13, 2010 01:21 PM
Last Post: Bandit
  OpenGL global and local coordinate problem. mikey 4 4,133 May 26, 2009 08:48 AM
Last Post: mikey
  Simple OpenGL ES problem soulstorm 3 4,370 May 14, 2009 03:53 PM
Last Post: AnotherJake
  Opengl picking problem (zip file) papillon68 1 4,594 Mar 1, 2009 08:49 PM
Last Post: chronus