Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm having an issue here with rotation in OpenGL. Before I changed my rendering function the rotation I had for my object worked fine, but now it seems to be messed up.

I changed my rendering function because I wanted to be able to clip the image (render frames of the image), whereas before I was just rendering things on a 1 object, 1 image basis.

The rendering is fine, just the rotation to clarify that. Here's my current rendering code:

void DrawRotateAdv(int x, int y, int width, int height, float sourceX, float sourceY, float imageWidth, float imageHeight,
    GLuint texture, float angle, bool blendFlag)
{
    glTranslatef((GLfloat) x + (width / 2),  (GLfloat) y + (height / 2), 0.0);
    glRotatef(angle, 0.0, 0.0, 1.0);

    if (blendFlag)
    {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }

    glBindTexture(GL_TEXTURE_2D, texture);
    float texscaleX = 1.0f / (float)(imageWidth / width);
    float texscaleY = 1.0f / (float)(imageHeight / height);
    sourceX = sourceX / (float)imageWidth;
    sourceY = sourceY / (float)imageHeight;

    glBegin(GL_QUADS);

    // Top-left vertex (corner)
    glTexCoord2f(sourceX, sourceY);
    glVertex2i( (-width / 2), (-height / 2));

    //Bottom-left vertex (corner)
    glTexCoord2f(sourceX, sourceY + texscaleY);
    glVertex2i( (width / 2), (-height / 2));

    //Bottom-right vertex (corner)
    glTexCoord2f(sourceX + texscaleX, sourceY + texscaleY);
    glVertex2i( (width / 2), (height / 2));

    //Top-right vertex (corner)
    glTexCoord2f( sourceX + texscaleX, sourceY);
    glVertex2i( (-width / 2) , (height / 2));

    glEnd();
    glLoadIdentity();
}

I think that perhaps my translation is wrong? Unsure though, guess that's a stab in the dark on my part. As for the getting the angle:

     angle = atan2((cEnemy->position.y - (float)position.y), cEnemy->position.x -        (float)position.x) / 3.14159265f * 180;

But I don't think that's the issue, like I said it worked before-hand perfectly. Screenshot of the output:

http://imageshack.us/photo/my-images/190/rotation.png/

share|improve this question
3  
Describing things as "messed up" doesn't really help narrow down what the problem is. Can you add a screenshot of the bad rendering output? –  Nathan Reed Oct 26 '11 at 23:06
    
Yeah, sorry about that. Its hard to describe... –  danran Oct 26 '11 at 23:37
    
Added, to say that the object should be facing the enemy (green virus) –  danran Oct 26 '11 at 23:45
1  
Sorry, it's still not clear what the problem is. Can you add an arrow or something to point it out, or a picture of desired result vs actual result? –  Nathan Reed Oct 27 '11 at 0:33
    

1 Answer 1

up vote 1 down vote accepted

So it looks like the rotation is off by 90 degrees. This could be caused by your tex coords / vert coords being mismatched, which it looks like they might be. As a test, set the angle to zero; the object should point along the positive X axis (to the right, ordinarily).

Looking at just the first couple of verts,

// Top-left vertex (corner)
glTexCoord2f(sourceX, sourceY);
glVertex2i( (-width / 2), (-height / 2));

//Bottom-left vertex (corner)
glTexCoord2f(sourceX, sourceY + texscaleY);
glVertex2i( (width / 2), (-height / 2));

It doesn't make sense that when you go from top-left to bottom-left, you're changing the Y part of the texture coordinates but the X part of the vertex coordinates. Shouldn't you change the Y part of both? And similarly for the other verts.

share|improve this answer
    
Thanks man :), got it ;) –  danran Oct 27 '11 at 20:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.