Enriquillo
Enriquillo

Reputation: 37

How to rotate on Android (java) and OpenGL ES 2.0

Actually Im doing this into the draw method of my renderer class:

    Matrix.setIdentityM(mMMatrix, 0);
//      Rotate view

long time = SystemClock.uptimeMillis()*4000L;
float angle = .09F*(int)time;
Matrix.setRotateM(mMMatrix, 0, angle, 1, 0, 0);
Matrix.rotateM(mMMatrix, 0, -2f, 1, 0, 0);
//Matrix.scaleM(mMMatrix, 0, 0.01f, 0.01f, 0.01f);

// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);

// Draw Shapes      
obj.draw(mMVPMatrix);

But it doesn't work... i'v tried some other but no luck, the objects stays the same, no rotation.

Update: I change the code to this and no luck.

        // Clear Screen
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);


        Matrix.setIdentityM(mMMatrix, 0);

        // Rotate view
        Matrix.setRotateM(mMMatrix, 0, angle, 1, 0, 0);
        //Matrix.scaleM(mMMatrix, 0, 0.01f, 0.01f, 0.01f);

        // Calculate the projection and view transformation
        Matrix.multiplyMM(mMVMatrix, 0, mVMatrix, 0, mMMatrix, 0);
        Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVMatrix, 0);

        // Draw Shapes      
        obj.draw(mMVPMatrix);

Upvotes: 0

Views: 4273

Answers (1)

chrisendymion
chrisendymion

Reputation: 175

Why are you Matrix.rotateM after Matrix.setRotateM ? The second disabled the first.

Here the pseudo code I'm using in my engine :

Matrix.setIdentityM(_model_matrix, 0);

Matrix.setRotateM(_rotation_x_matrix, 0, _angles.getX(), 1, 0, 0);
Matrix.setRotateM(_rotation_y_matrix, 0, _angles.getY(), 0, 1, 0);
Matrix.setRotateM(_rotation_z_matrix, 0, _angles.getZ(), 0, 0, 1);
Matrix.multiplyMM(_model_matrix, 0, _rotation_x_matrix, 0, _model_matrix, 0);
Matrix.multiplyMM(_model_matrix, 0, _rotation_y_matrix, 0, _model_matrix, 0);
Matrix.multiplyMM(_model_matrix, 0, _rotation_z_matrix, 0, _model_matrix, 0);

Matrix.multiplyMM(_modelview_matrix, 0, _view_matrix, 0, _model_matrix, 0);
Matrix.multiplyMM(_modelviewprojection_matrix, 0, _projection_matrix, 0, _modelview_matrix, 0);

Hope it helps ;-)

Upvotes: 3

Related Questions