Search code examples
javaandroidmatrixopengl-esscale

Scale matrix in place


currently I am scaling a matrix like so:

public void scale(float aw, float ah){
    Matrix.scaleM(modelMatrix, 0, aw, ah, 1f);
    updateMVP();
}

private void updateMVP(){
    Matrix.multiplyMM(mvpMatrix, 0, projectionMatrix, 0, modelMatrix, 0);
}

And using: gl_Position = u_Matrix * a_Position; in my vertex shader, u_Matrix being the mvpMatrix. The camera I am using is the default and the projectionMatrix is created by:

ASPECT_RATIO = (float) height / (float) width;
orthoM(projectionMatrix, 0, -1f, 1f, -ASPECT_RATIO, ASPECT_RATIO, -1f, 1f);

Now I can scale my object properly, but the only problem is that every time I scale the matrix, the object moves a little bit. I was wondering how I could scale the matrix while keeping the center point and not having the object translate. Anyone know how I can do this in OpenGL ES 2.0 on Android? Thanks


Solution

  • Do you have any other matrices (rotation/translation)?

    If so: you might not be multiplying your matrices in the correct order, which can cause issues.

    (proper order multiply right to left) Translate * Rotation * Scale

    Your error sounds like the one explained here:

    You translate the ship by (10,0,0). Its center is now at 10 units of the origin. You scale your ship by 2. Every coordinate is multiplied by 2 relative to the origin, which is far away… So you end up with a big ship, but centered at 2*10 = 20. Which you don’t want.

    http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/