Using a matrix. Turn in OpenGL ES 2.0

Edit - added more code

You have a lot of problems trying to rotate my square correctly using OpenGL ES 2.0.

It seems to rotate around the center of the screen coordinate. I am trying to make it rotate around its own center (for 2d, therefore only for the z axis).

I experimented with Matrix.translate as shown below. However, changing the x or y pos here simply draws the ATV elsewhere on the screen, but when it rotates, it again rotates around the center of the screen. Please can someone explain how to make it rotate around its own z axis (like wheels)?

Thank you, here are the relevant lines of code - if you need more, ask and I will send a message. (Note that I looked at a lot of similar questions about SO and the wider Internet, but so far I have not been able to find the answer).

Thank.

//Set rotation
Matrix.setRotateM(mRotationMatrix, 0, -angle, 0, 0, 1.0f);

//Testing translation
Matrix.translateM(mRotationMatrix, 0, -.5f, .5f, 0f);

// Combine the rotation matrix with the projection and camera view
Matrix.multiplyMM(mvpMatrix, 0, mRotationMatrix, 0, mvpMatrix, 0);

My shaders (declared at class level)

private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +

"attribute vec4 vPosition;" +
"void main() {" +
" gl_Position = uMVPMatrix * vPosition;" +
"}";

private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";

From onSurfaceChanged

float ratio = (float) width / height;
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);

In my onDrawFrame method

// Set the camera position (View matrix)
Matrix.setLookAtM(mVMatrix, 0, 0, 0, 3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

//Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
+5
source share
4 answers

I ran into the same problems (strange distortions and everything else were noticed), my solution is based on Android Training> Displaying Graphics with OpenGL ES> Adding Motion Below.

(Go to my detailed post for if necessary: Matrix transformations of OpenGL ES Android .

  • mModelMatrix

    Matrix.setIdentityM(mModelMatrix, 0); // initialize to identity matrix
    
  • mModelMatrix

    Matrix.translateM(mModelMatrix, 0, -0.5f, 0, 0); // translation to the left
    
  • mRotationMatrix ( )

    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);
    
  • Matrix.multiplyMM

    mTempMatrix = mModelMatrix.clone();
    Matrix.multiplyMM(mModelMatrix, 0, mTempMatrix, 0, mRotationMatrix, 0);
    
  • mTempMatrix = mMVPMatrix.clone();
    Matrix.multiplyMM(mMVPMatrix, 0, mTempMatrix, 0, mModelMatrix, 0);
    
+5

. , ... . :

pre rotate and translate

, , , ...

, , + run - ...

rotated

:

rotated and translated

, , , , , , . Matrix.setIdentityM(mRotationMatrix, 0); . .

, , .

    Matrix.setIdentityM( mRotationMatrix,0); 
    <as needed movement to center>

    Matrix.rotate(mRotationMatrix, 0, -angle, 0, 0, 1.0f);
    <any other translation you want>

, , , .

+3

, , . , , .

, Matrix, , . , .

, , .

0

:

1st-Matrix.translateM(mRotationMatrix, 0, -.5f,.5f, 0f);

2nd-Matrix.setRotateM(mRotationMatrix, 0, -angle, 0, 0, 1.0f);

.

0

All Articles