Mel
Mel

Reputation: 2075

Why are my y coordinates in reverse

So I am trying to render my program in landscape mode... however I am coming across something that I am having trouble wrapping my mind around. In order to move down on my map I have to have a negative offset. If I keep the current positive yOffset of 100 then the map scrolls down instead of up. Why is this? I'm not happy having to put negative y coords to see the map. Is there a way I can reverse this so the map scrolls in the opposite direction when yOffset is positive?

        float yOffset = 100;
    float xOffset = 0;
    glOrthof(0 - yOffset,
             320 - yOffset,
             480 + xOffset,
             0 + xOffset,
             -10000.0f,10000.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();       
    glTranslatef(320.0f, 0.0f, 0.0f );
    glRotatef(90.0, 0.0, 0.0, 1.0);

    static const GLfloat squareVertices[] = {
    0.0f,  0.0f,
    1024.0f,  0.0f,
    0.0f,   1024.0f,
    1024.0f,   1024.0f
};



static const GLfloat texCoords[] = {
    0.0, 1.0,
    1.0, 1.0,
    0.0, 0.0,
    1.0, 0.0
};


glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);    

glTexCoordPointer(2, GL_FLOAT, 0, texCoords);   
glVertexPointer(2, GL_FLOAT, 0, squareVertices);        


glBindTexture(GL_TEXTURE_2D, mapTexture);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);

Upvotes: 1

Views: 1722

Answers (4)

easeout
easeout

Reputation: 8734

Your Y coordinates are negative because you are doing a 90 degree rotation of the 320x480 screen, probably because you're playing in landscape and not portrait. A 90 degree rotation in vector math can be expressed as:

(X, Y) becomes (-Y, X)

That's happening in your call to glOrtho() when you supply -yOffset into the X boundaries and +xOffset into the Y boundaries.

As for correcting this, I'm having one of those "how does 3D math work again" moments, so maybe one of these other fine responders will help you.

...

(Old version of this response that I don't think is correct anymore, but may be helpful):

Two ideas:

You might be confused by the fact that when you transform your matrix in OpenGL, you're not moving the camera. You're moving the world. To move the camera up you actually need to move the world down. But, this might not be your problem depending on how you're transforming.

I notice you're rotating 90 degrees around X. I forget which, but that will either turn your +Z into +Y and +Y into -Z, or it will turn your +Z into -Y and your +Y into +Z. This might be the source of your axis inversion. You could try rotating the other way (-90), or you could just not rotate, and use the native coordinate orientation.

Upvotes: 2

jessecurry
jessecurry

Reputation: 22116

Maybe swap these values: -10000.0f,10000.0f

Upvotes: 0

Alex Reynolds
Alex Reynolds

Reputation: 96927

OpenGL inverts y-axis coordinates, relative to UIKit (CGxyz etc.) -- or vice versa, depending on your perspective.

Upvotes: 0

luke
luke

Reputation: 37433

OpenGL uses a "right-handed" coordinate system. To switch your y direction, you can do something like:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef (1.0f, -1.0f, 1.0f);
// multiply view transforms as usual... 
// multiply model transforms as usual...

Upvotes: 1

Related Questions