4

I'm attempting to draw in orthographic mode with OpenGL ES, and the point (0,0) is in the lower-left corner of the screen. However, I want to make it be in the upper-left hand corner.

Here's where I'm setting things up in my Android app:

public void onSurfaceChanged(final GL10 gl, final int width, final int height) {
    assert gl != null;

    // use orthographic projection (no depth perception)
    GLU.gluOrtho2D(gl, 0, width, 0, height);
}

I tried changing the above call in many ways, including:

    GLU.gluOrtho2D(gl, 0, width, 0, height);
    GLU.gluOrtho2D(gl, 0, width, 0, -height);
    GLU.gluOrtho2D(gl, 0, width, height, 0);
    GLU.gluOrtho2D(gl, 0, width, -height, 0);

I also tried playing with the viewport to no avail:

public void onSurfaceChanged(final GL10 gl, final int width, final int height) {
    assert gl != null;

    // define the viewport
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();

    // use orthographic projection (no depth perception)
    GLU.gluOrtho2D(gl, 0, width, 0, height);
}

And again, I tried playing with the viewport settings to no avail:

    gl.glViewport(0, 0, width, height);
    gl.glViewport(0, 0, width, -height);
    gl.glViewport(0, height, width, 0);
    gl.glViewport(0, -height, width, 0);

Any clues on how to get the point (0,0) to the top-left of the screen? Thanks!

3 Answers 3

9

How about:

glViewport(0, 0, width, height);
gluOrtho2D(0, width, height, 0);

The glViewport call only sets the viewport in device coordinates. That is that of your window system. The glOrtho (gluOrtho2D) calls sets the coordinate mapping from world coordinates to device coordinates.

See:

0
4

How about glScalef(1f, -1f, 1f); ?

6
  • glScalef should be used for scaling, not fixing the coordinate projection.
    – rioki
    Jul 16, 2010 at 12:58
  • 3
    How do you think gluOrtho2D works? It adjusts the projection matrix of course, same thing glScale changes.
    – Ben Voigt
    Jul 16, 2010 at 14:25
  • GL ES doesn't provide matrix operators
    – Steven Lu
    Dec 10, 2013 at 1:13
  • @StevenLu: Say what? GLES 1.x provides glScalef and later versions allow you to upload a projection matrix as a uniform, and provide matrix operations in shaders.
    – Ben Voigt
    Dec 10, 2013 at 4:11
  • 1
    Okay, my statement was probably technically wrong. But I was just going at how you actually can't call glScalef with GL ES 2.0, it's not defined, it's expected that you build the matrices to send to your shaders on your own.
    – Steven Lu
    Dec 10, 2013 at 6:18
1
double fov = 60.f * 3.1415f/180.f;
float aspect = (float) width / (float) height;
float zNear = 0.01f;
float zFar = 3000.0f;
// cotangent
float f = (float) (Math.cos(fov*0.5f) / Math.sin(fov*0.5f));
float perspMtx[] = new float[16];

// columns first

for( int i=0; i<16; ++i )
 perspMtx[i] = 0.0f;

perspMtx[0] = f / aspect;
perspMtx[5] = -f; // flip Y? <- THIS IS YOUR ANSWER

perspMtx[10] = (zFar+zNear) / (zNear - zFar);
perspMtx[11] = -1.0f;
perspMtx[14] = 2.0f*(zFar*zNear) / (zNear - zFar);

gl.glMultMatrixf(perspMtx, 0);

this solved the problem for me, should be similar for the orthogonal projection matrix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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