Flip Y axis in OpenGL ES?

I'm trying to use spelling mode with OpenGL ES, and the dot (0,0) is in the lower left corner of the screen. However, I want it to be in the upper left corner.

Here, where I configure things 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 to modify the above call in a variety of 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 in 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 tips on how to get the dot (0,0) in the upper left corner of the screen? Thank!

+3
source share
3 answers

What about:

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

glViewport . . glOrtho (gluOrtho2D) .

:

+7

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

+4
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 design matrix

+1
source

Source: https://habr.com/ru/post/1754931/


All Articles