简体   繁体   English

如何在像素级的OpenGL ES 2.0中旋转纹理

[英]How to rotate texture in opengl Es 2.0 in pixel-level

I'm working with opengl pixel-level (2D) 我正在使用opengl像素级(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 旋转纹理,但是行为很奇怪,所有内容在屏幕上而不是在中心旋转约0.0

Any idea how to rotate this? 知道如何旋转吗?

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

The texture (Red) rotate over 0,0 pixel no over your center 纹理(红色)在0,0像素上旋转而不在您的中心上

I multiply the MVP matrix by the rotation matrix, So I needed to move backward rotation matrix and move forward the MVP matrix 我将MVP矩阵乘以旋转矩阵,所以我需要向后移动旋转矩阵并向前移动MVP矩阵

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 :) 在工作,在忙 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM