Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    GLES20.glClearColor(0.0f, 0.0f, 1.0f, 0.5f);
    camWidth=480;camHeight=320;
    // initialize a square
    square = new Square();
}

@Override
public void onDrawFrame(GL10 nope) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    //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);


    square.draw(mMVPMatrix);
}

@Override
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);
    GLES20.glCompileShader(shader);

    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
    bb.order(ByteOrder.nativeOrder());

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

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

    dlb.order(ByteOrder.nativeOrder());

    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    int vertexShader = ChizRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
            vertexShaderCode);
    int fragmentShader = ChizRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
            fragmentShaderCode);

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

}

public void draw(float[] mvpMatrix) {
    // Add program to OpenGL ES environment
    GLES20.glUseProgram(mProgram);

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

    // Enable a handle to the triangle vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // 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
    GLES20.glDisableVertexAttribArray(mPositionHandle);

}
}

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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;
}
share|improve this answer
    
Thank you it worked! I forgot to use it in my vertex shader, now everything makes sense! –  Baruch Jan 30 '13 at 12:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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