How to rotate model and view matrix with same angle?

20 Views Asked by At

I'm writing a OpenGL program, Model is sphere, and the camera is at the center of the sphere, now I want to rotate the sphere around the center by a specified euler angle, and also rotate the camera with the same angle, As I expected the image in the viewport should be the same before the rotation, but it‘s not, please help me check my code:

@Override
public void onSurfaceChanged(GL10 gl10, int w, int h) {
    GLES30.glViewport(0, 0, w, h);

    mWidth = w;
    mHeight = h;

    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.setIdentityM(mViewMatrix, 0);

    MDQuaternion quaternion = new MDQuaternion();
    quaternion.setEulerAngles(60, 20, 10);
    quaternion.toMatrix(mModelMatrix);
    quaternion.toMatrix(mViewMatrix);
    
    Matrix.setIdentityM(mViewModelMatrix, 0);

    final float ratio = (float) mWidth / mHeight;
    Matrix.frustumM(mProjectMatrix,0, -ratio, ratio,-1f,1f, 0.5f,333f);
}

@Override
public void onDrawFrame(GL10 gl10) {
    GLES30.glFrontFace(GLES30.GL_CW);
    GLES30.glCullFace(GLES30.GL_BACK);
    GLES30.glEnable(GLES30.GL_CULL_FACE);

    GLES30.glEnable(GLES30.GL_DEPTH_TEST);
    GLES30.glDepthFunc(GLES30.GL_LESS);

    GLES30.glClearColor(0.f, 0.f, 0.2f, .5f);
    GLES30.glClearDepthf(1.0f);
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT | GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_STENCIL_BUFFER_BIT);

    GLES30.glUseProgram(mProgramId);

    Matrix.multiplyMM(mViewModelMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
    Matrix.multiplyMM(mMVPMatrix,0, mProjectMatrix,0, mViewModelMatrix,0);

    int uMatrixLocation = GLES30.glGetUniformLocation(mProgramId,"vMatrix");
    GLES30.glUniformMatrix4fv(uMatrixLocation,1,false, mMVPMatrix,0);

    GLES30.glEnableVertexAttribArray(mVertexAttrib);
    GLES30.glVertexAttribPointer (mVertexAttrib, 3, GLES30.GL_FLOAT, false, 0, mVertexBuffer);

    GLES30.glEnableVertexAttribArray(mTexCoordsAttrib);
    GLES30.glVertexAttribPointer (mTexCoordsAttrib, 2, GLES30.GL_FLOAT, false, 0, mTexCoordsBuffer);

    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mTextureId);
    GLES30.glUniform1i(GLES30.glGetUniformLocation(mProgramId, "tex_yuv"), 0);
    
    int numIndices = 2 * SegmentsW * (SegmentsH - 1) * 3;
    GLES30.glDrawElements(GLES30.GL_TRIANGLES, numIndices, GLES30.GL_UNSIGNED_SHORT, mIndicesBuffer);
    
    GLES30.glBindVertexArray(0);
    GLES30.glDisableVertexAttribArray(mVertexAttrib);
    GLES30.glDisableVertexAttribArray(mTexCoordsAttrib);
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
}
0

There are 0 best solutions below