我试图在顶点着色器中旋转二维图像的矩阵。
我希望2D图像围绕我认为的z轴旋转。
然而,我所得到的关闭是:
这是我的着色器中的矩阵,我应用了平移和缩放:
mat4 worldPosTrans = mat4(vec4(scale.x * cos(rotateZ), 0, 0, 0),
vec4(0, scale.y, 0, 0),
vec4(0, 0, scale.z, 0),
vec4(translation, 1));
这是一个稍微改变的版本,应该是旋转一切的东西:
mat4 worldPosTrans = mat4(vec4(scale.x * cos(rotateZ), -sin(rotateZ), 0, 0),
vec4(sin(rotateZ), scale.y * cos(rotateZ), 0, 0),
vec4(0, 0, scale.z, 0),
vec4(translation, 1));
这是我的全顶点着色器:
precision mediump float;
attribute vec3 vertPosition;
attribute vec3 vertColor;
attribute vec2 aTextureCoord;
varying vec3 fragColor;
varying highp vec2 vTextureCoord;
varying highp vec2 vTextureCoordBg;
uniform vec2 uvOffsetBg;
uniform vec2 uvScaleBg;
uniform mat4 uPMatrix;
uniform mat4 uMVMatrix;
uniform vec2 uvOffset;
uniform vec2 uvScale;
uniform vec3 translation;
uniform vec3 scale;
uniform float rotateZ;
void main()
{
fragColor = vertColor;
vTextureCoord = (vec4(aTextureCoord, 0, 1)).xy * uvScale + uvOffset;
vTextureCoordBg = (vec4(aTextureCoord, 0, 1)).xy * uvScaleBg + uvOffsetBg;
mat4 worldPosTrans = mat4(
vec4(scale.x * cos(rotateZ), 0, 0, 0),
vec4(0, scale.y, 0, 0),
vec4(0, 0, scale.z, 0),
vec4(translation, 1));
gl_Position = (uPMatrix * worldPosTrans) * vec4(vertPosition.x, vertPosition.y, vertPosition.z, 1.0);
}
修改
我的问题在评论中被Rabbid76解决了。
答案 0 :(得分:0)
GLSL中的4 * 4转换矩阵如下所示:
mat4 m44 = mat4(
vec4( Xx, Xy, Xz, 0.0),
vec4( Yx, Xy, Yz, 0.0),
vec4( Zx Zy Zz, 0.0),
vec4( Tx, Ty, Tz, 1.0) );
通常,围绕Z轴的旋转矩阵设置如下:
float angle;
mat4 rotation = mat4(
vec4( cos(angle), -sin(angle), 0.0, 0.0 ),
vec4( sin(angle), cos(angle), 0.0, 0.0 ),
vec4( 0.0, 0.0, 1.0, 0.0 ),
vec4( 0.0, 0.0, 0.0, 1.0 ) );
这意味着你必须像这样设置worldPosTrans
矩阵:
mat4 worldPosTrans = mat4(
vec4( scale.x * cos(rotateZ), scale.x * -sin(rotateZ), 0.0, 0.0),
vec4( scale.y * sin(rotateZ), scale.y * cos(rotateZ), 0.0, 0.0),
vec4( 0.0, 0.0, scale.z, 0.0),
vec4( translation.xyz, 1.0)
);