0
votes

I have a projection in OpenGL ES 2.0 but unfortunately I want the Y to be positive so its easier for me to use TouchEvents.

Right now this is the Projection Matrix:

private void setOrthographicMatrix(int w, int h)
{
    orthographicMatrix[0] = (float) (2.0f / w ); //a
    orthographicMatrix[3] = -1f;                 //tx
    orthographicMatrix[5] = (float) (2.0f / h);  //b
    orthographicMatrix[7] = 1f;                  //ty
    orthographicMatrix[10] = -1f;                //c
    orthographicMatrix[15] = 1f;                 //1

//  a, 0, 0, tx,
//  0, b, 0, ty,
//  0, 0, c, tz,
//  0, 0, 0, 1
}

Vertex Shader:

attribute vec4 a_position;
attribute vec2 a_texCoord;

varying vec2 v_texCoord;

uniform mat4 u_MVPMatrix;

void main()
{
    //Pass Values to Fragment Shader
    v_texCoord = a_texCoord; 

    //Set Position
    gl_Position = a_position * u_MVPMatrix;
}

Fragment Shader:

precision mediump float;

varying vec2 v_texCoord;

uniform vec4 u_color;
uniform sampler2D u_s_texture;

void main()
{
    gl_FragColor = texture2D(u_s_texture, v_texCoord) * u_color;
}

And the result is: (0,0) - topleft corner

0,0----------> X++
|
|
|
↓
Y--

And I want the projection to map so Y becomes positive down the screen.

EDIT - Solution:

I've replaced the setup of the OrthographicMatrix function to use the built in Android Matrix library:

Matrix.orthoM(orthographicMatrix, 0, 0, w, h, 0, -10f, 10f);

And changed my Vertex Shader to this:

gl_Position = u_MVPMatrix * a_position;
1

1 Answers

1
votes

I would recommend to just use OpenGL's standard orthographic projection function, which allows you to define the top, bottom, left, right instead of just width and height, and as such you can flip the Y axis any way you want:

This function doesn't exist in OpenGLES 2.0, but you can copy the equations into your own orthographic function:

http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml

Also, if this is for Android (your other posts are for android so I will assume so), there already exists an ortho matrix function you can use (android.opengl.Matrix.orthoM)