Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flip Y-axis in OpenGL ES?

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!

like image 228
Matt Huggins Avatar asked Dec 10 '22 14:12

Matt Huggins


1 Answers

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:

  • http://pyopengl.sourceforge.net/documentation/manual/gluOrtho2D.3G.html
  • http://www.opengl.org/sdk/docs/man/xhtml/glViewport.xml
like image 105
rioki Avatar answered Feb 08 '23 20:02

rioki