I am trying to make a projection matrix scaling the screen and making a coordination system. For some reason I don't think any of my matrix calling is working... the 3 function I am using are

Matrix.orthoM(mProjMatrix, 0, 0, 1520, 0,  1000, -1, 10);
Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1.0f, 0.0f, 0f, 0f, 0f, 1.0f, 0.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

Are they canceling each other out? anything wrong with it? (the full rendering class code is at the end)

My main goal in doing this is eventually getting to a situation where when I make a square, I could provide coordinates such as (200, 100,0) //x, y, z which are not only between -1 and 1.

Here is my full rendering class:

public class MyRenderer implements Renderer {

private static final String TAG = "MyRenderer";

Square square;

private final float[] mMVPMatrix = new float[16];
private final float[] mProjMatrix = new float[16];
private final float[] mVMatrix = new float[16];
private final float[] mRotationMatrix = new float[16];

private int camWidth,camHeight;

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    GLES20.glClearColor(0.0f, 0.0f, 1.0f, 0.5f);
    // initialize a square
    square = new Square();

public void onDrawFrame(GL10 nope) {

    //set camera position

    GLES20.glViewport(0, 0, camWidth, camHeight);

    Matrix.orthoM(mProjMatrix, 0, 0, 1520, 0,  1000, -10, 999999);
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1.0f, 0.0f, 0f, 0f, 0f, 1.0f, 0.0f);
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);


public void onSurfaceChanged(GL10 nope, int width, int height) {

    GLES20.glViewport(0, 0, camWidth, camHeight);

    Matrix.orthoM(mProjMatrix, 0, 0, 1520, 0,  1000, -10, 999999);
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1.0f, 0.0f, 0f, 0f, 0f, 1.0f, 0.0f);

public static int loadShader(int type, String shaderCode) {

    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);

    return shader;


My Square class (wasn't sure if needed or not, but just to be safe :) ) -

public class Square {
private final String vertexShaderCode =
    "attribute vec4 vPosition;" +
    "void main() {" +
    "  gl_Position = vPosition;" +

private final String fragmentShaderCode = "precision mediump float;"
        + "uniform vec4 vColor;" + "void main() {"
        + "  gl_FragColor = vColor;" + "}";

static final int COORDS_PER_VERTEX = 3;

static float triangleCoords[] = { // in counterclockwise order:
-0.5f, 0.5f, 0.0f, // top left
        -0.5f, -0.5f, 0.0f, // bottom left
        0.5f, -0.5f, 0.0f, // bottom right
        0.5f, 0.5f, 0.0f

private short drawOrder[] = { 0, 1, 2, 0, 2, 3 };
private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;

private final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex

// red-green-blue-alpha
float color[] = { 0.63f, 0.76f, 0.22f, 1.0f };

private final int mProgram;
private int mPositionHandle;
private int mColorHandle;
private int mMVPMatrixHandle;
private FloatBuffer vertexBuffer;
private ShortBuffer drawListBuffer;

public Square() {
    ByteBuffer bb = ByteBuffer.allocateDirect(
    // # of coords values * 4 bytes per float
            triangleCoords.length * 4);

    // use native byte order

    // create a floating point buffer from the ByteBuffer
    vertexBuffer = bb.asFloatBuffer();
    // add coordination to FloatBuffer
    // set the buffer to read first coordinate

    ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);


    drawListBuffer = dlb.asShortBuffer();

    int vertexShader = ChizRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
    int fragmentShader = ChizRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,

    mProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgram, vertexShader);
    GLES20.glAttachShader(mProgram, fragmentShader);


public void draw(float[] mvpMatrix) {
    // Add program to OpenGL ES environment

    // get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

    // Enable a handle to the triangle vertices

    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
            GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

    // get handle to fragment shader's vColor member
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

    // set color for drawing the triangle
    GLES20.glUniform4fv(mColorHandle, 1, color, 0);

    // get handle to shape's transformation matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

    // apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

    // Draw the triangle
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length,
            GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    // dispable vertex array


And lastly, just so you could have some visuals:

with and without metrix functions

This is how it looks on my phone with and without the three metrix functions mentioned before, it also seems that the only thing that did make any change with the width and height was GLES20.glViewport(0, 0, camWidth, camHeight);

It seems as if the metrix is doing nothing.


1 Answer 1


In OpenGLES 2, your matrices are not multiplied for you by the fixed pipeline as in OpenGLES 1.1. You have to do that by yourself, and the best place to do that is in the vertex shader.

It seems like you're already passing the matrix to the vertex shader as a uniform called uMVPMatrix, so all you have to do is use it.

To do this, change your vertex shader code to look like this:

attribute vec4 vPosition;
uniform mat4 uMVPMatrix;

void main()
    gl_Position = uMVPMatrix * vPosition;
  • \$\begingroup\$ Thank you it worked! I forgot to use it in my vertex shader, now everything makes sense! \$\endgroup\$
    – Baruch
    Commented Jan 30, 2013 at 12:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .