How to rotate a texture in a shader, Android

So, I have a texture that I want to rotate taking into account the angle of rotation . Here are my UV coordinates

 float[] landscapeVerts = { // X, Y, Z, U, V -ratio, -1.0f, z_0, 1.0f, 0.0f, ratio, -1.0f, z_0, 0.0f, 0.0f, -ratio, 1.0f, z_0, 1.0f, 1.0f, ratio, 1.0f, z_0, 0.0f, 1.0f, }; 

I want to rotate only part of the UV. So I built the following matrix to execute this in the shader.

  Matrix.setIdentityM(mProjMatrix, 0); //start with identity matrix Matrix.translateM(mProjMatrix,0,-0.5f,-0.5f,0.0f); // move center to center [0 1] : [-.5 .5] Matrix.rotateM(mProjMatrix, 0, rotation, 0.0f,0.0f,-1.0f); // rotate about z axis Matrix.translateM(mProjMatrix,0,0.5f,0.5f,0.0f); // move back to [0 1] range 

Then I load the matrix into the shader as uSTMatrix and multiply its texture coordinates

 "uniform mat4 uMVPMatrix;\n" + "uniform mat4 uSTMatrix;\n" + "attribute vec4 aPosition;\n" + "attribute vec4 aTextureCoord;\n" + "varying vec2 vTextureCoord;\n" + "varying vec2 calcTexCoord;\n" + "void main() {\n" + " gl_Position = uMVPMatrix * aPosition;\n" + " vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n" + " calcTexCoord = aTextureCoord.xy;\n" + "}\n"; 

But that does not do what I want. This should be easy to do in the shader, what am I missing? or how it can be done easily.

0
source share
1 answer

I get it. Just need to change the order of work.

 float[] identityMatrix = new float[16]; Matrix.setIdentityM(identityMatrix, 0); //start with identity matrix Matrix.translateM(mProjMatrix,0,identityMatrix, 0, 0.5f,0.5f,0.0f); // move back to [0 1] range Matrix.rotateM(identityMatrix, 0, mProjMatrix, 0, rotation, 0.0f,0.0f,1.0f); // rotate about z axis Matrix.translateM(mProjMatrix,0, identityMatrix, 0, -0.5f,-0.5f,0.0f); // move center to center [0 1] : [-.5 .5] 

Instead of moving to the center, spinning and moving backward; we build the matrix in the opposite direction → move backward, rotate, and then go to the center.

0
source

Source: https://habr.com/ru/post/981364/


All Articles