Samuel Liz
Samuel Liz

Reputation: 59

How to rotate texture in opengl Es 2.0 in pixel-level

I'm working with opengl pixel-level (2D)

Everything was fine until I came across the rotation

I have tried different ways and nothing

This is my matrix to work at the level of pixels

float[] uScreen =
      {
         2f/width, 0f, 0f, 0f,
         0f, -2f/height, 0f, 0f,
         0f, 0f, 0f, 0f,
        -1f, 1f, 0f, 1f
      };

Vertex

String vertexSrc =
        "uniform mat4 uScreen;\n" +
        "attribute vec2 aPosition;\n" +
        "void main() {\n" +
        " gl_Position = uScreen * vec4(aPosition.xy, 0.0, 1.0);\n" +
        "}";

Fragment shader

String fragmentSrc =

    "precision mediump float;\n"+
    "void main(void)\n" +
    "{\n" +
    " gl_FragColor = vec4(1, 0, 0, 1);\n" +
    "}";

This works well and I can draw on screen pixel-level without problems. But I want to rotate the texture and do not know how

Now try adding the following

long time = SystemClock.uptimeMillis() % 4000L;
float angleX = 0.090f * ((int) time);

float[] axis = { 1.0f,0.0f,0.0f};

Matrix.rotateM(uScreen,0,angleX,axis[0],axis[1],axis[2]);



GLES20.glUniformMatrix4fv(uScreenPos, 1, false, uScreen, 0);

The texture is rotated, but with a strange behavior, everything rotates around 0.0 on the screen not on its center

Any idea how to rotate this?

enter image description here

enter image description here

enter image description here

enter image description here

The texture (Red) rotate over 0,0 pixel no over your center

Upvotes: 0

Views: 1958

Answers (1)

Samuel Liz
Samuel Liz

Reputation: 59

I multiply the MVP matrix by the rotation matrix, So I needed to move backward rotation matrix and move forward the MVP matrix

This is the solution

Matrix.setIdentityM(rotationMat, 0);
Matrix.setRotateM(rotationMat, 0,angleX,1,0,0);
Matrix.translateM(uScreen, 0,sprite.x, sprite.y, 0);
Matrix.translateM(rotationMat, 0,-sprite.x, -sprite.y, 0);

Work now :)

Upvotes: 1

Related Questions