views:

229

answers:

4

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);
+1  A: 

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...
luke
Please don't switch your y-direction. Just live with the adjusted coordinate system.
Stefan Kendall
I wouldn't recommend it either- but it does answer his question.
luke
If you do this, and you're back face culling, be sure to reverse the order of your polygon points. Instead of counterclockwise, they will have to go clockwise.
Kevin Conner
Good call Kevin.
luke
Oh yeah, if you don't want to reverse your polygon points, another option is to use front face culling instead of back face culling. To me that feels like kind of a hack, because you're saying the back is actually the front. But all graphics programming has some degree of finagling in it. :) Just write some good comments about it.
Kevin Conner
A: 

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

Alex Reynolds
A: 

Maybe swap these values: -10000.0f,10000.0f

jessecurry
+2  A: 

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.

Kevin Conner