# draw two models changing the MVP matrix (android opengl)

I'm newbie with opengl2 in android, and I'm making an app in which I'm testing some things. Now I was trying to duplicate a sphere object with a texture, so I thought it was enough to change my MVP matrix applying a small translation to the Model matrix, using same View Matrix and same projection matrix.

int[] numIndices = balon.getNumIndices();
ShortBuffer[] indices = balon.getIndices();

GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
for (int j = 0; j < numIndices.length; j++) {
GLES20.glDrawElements(GLES20.GL_TRIANGLES,
numIndices[j], GLES20.GL_UNSIGNED_SHORT,
indices[j]);
}

GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix2, 0);
for (int j = 0; j < numIndices.length; j++) {
GLES20.glDrawElements(GLES20.GL_TRIANGLES,
numIndices[j], GLES20.GL_UNSIGNED_SHORT,
indices[j]);
}


balon is an object of type Sphere. mMVPMatrix and mMVPMatrix2 are the MVP matrices I was talking about. I simply changed the MVP matrix and did the drawing again. The result is that I have the two spheres, but one of them is behaving strangely, appears distorted, the position in which appears is right, the two spheres move along, but one of them changes it's shape.

What I'm missing?, I guess I have to duplicate some things in my code before I do this, but what?, thought that being the same model, this was enough...

This is how I changed the MVP matrix:

Matrix.setIdentityM(mModelMatrix, 0);
Matrix.setIdentityM(mModelMatrix2, 0);
Matrix.translateM(mModelMatrix, 0, posX, posY, posZ);
Matrix.translateM(mModelMatrix2, 0, posX+0.5f, posY+0.5f, posZ);

Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
Matrix.multiplyMM(mMVPMatrix2, 0, mViewMatrix, 0, mModelMatrix2, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
Matrix.multiplyMM(mMVPMatrix2, 0, mProjectionMatrix, 0, mMVPMatrix2, 0);


There probably nothing wrong with your transforms. Your use of glDrawElements is definitely wrong. This code draws the model numIndices times with the wrong arguments passed to count and indices:

### glDrawElements

glDrawElements — render primitives from array data

### C Specification

void glDrawElements(GLenum mode,
GLsizei count,
GLenum type,
const GLvoid * indices);


### Parameters

count Specifies the number of elements to be rendered.

...

indices Specifies a pointer to the location where the indices are stored.

So you may want to call it like:

int[] numIndices = balon.getNumIndices();
ShortBuffer[] indices = balon.getIndices();

GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES,
indices.length,
GLES20.GL_UNSIGNED_SHORT,
null);

GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix2, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES,
indices.length,
GLES20.GL_UNSIGNED_SHORT,
null);


I don't know why, but cloning mModelMatrix2 into mTempMatrix2 and using that as the operand in Matrix.multiplyMM instead of directly using mModelMatrix2, solved the problem

    float[] mTempMatrix2 = mModelMatrix2.clone();
Matrix.multiplyMM(mModelMatrix2, 0, mTempMatrix2, 0, mRotationMatrix, 0);

• Learn java before doing OpenGL with it. Java stores objects by reference, if you have 2 variables referencing 1 memory place (e.g. you did Object a = b), then if you edit 1 of them, you edit both. – Bálint May 6 '16 at 12:52