简体   繁体   English

顶点和片段着色器中旋转矩阵的差异结果

[英]Difference result of rotation matrix in Vertex and Fragment shaders

I have a problem with rotation matrix.It gives different results in Vertex and fragment shaders. 我的旋转矩阵有问题,它在顶点着色器和片段着色器中给出不同的结果。

There are two objects. 有两个对象。 First is sphere.Sphere needs its texture to be rotated in the fragment shader. 首先是球体。球体需要在片段着色器中旋转其纹理。 It works fine and matches the GUI algorithm. 它工作正常并且与GUI算法匹配。 Used exact same algorithm. 使用完全相同的算法。

Second object is points (sprites). 第二个对象是点(子画面)。 It should rotated around sphere by same angles. 它应该围绕球体旋转相同的角度。

I use next algorithm in fragment and vertex shader: 我在片段和顶点着色器中使用next算法:

mat3 rotateX = mat3(
   1.0, 0, 0,
   0, cos(angles.x), -sin(angles.x),
   0, sin(angles.x), cos(angles.x)
   );

   mat3 rotateY = mat3(
   cos(angles.y), 0, sin(angles.y),
   0, 1, 0,
   -sin(angles.y), 0, cos(angles.y)
   );

   mat3 rotateZ = mat3(
   cos(angles.z), -sin(angles.z), 0,
   sin(angles.z), cos(angles.z), 0,
   0, 0, 1
   );

   float Fi = (-current.x) * M_PI;
   float Te = current.y * M_PI;

   vec3 coord;
   coord.x = sin(Te) * cos(Fi);
   coord.y = sin(Te) * sin(Fi);
   coord.z = cos(Te);

   coord = vec3(coord.x, coord.y, coord.z) * rotateX * rotateY * rotateZ;

I have the following problem. 我有以下问题。 If making rotation from one any Axis, all is ok. 如果从任一轴旋转,则一切正常。 If making rotation by two or three Axis then sphere and points rotate in difference directions. 如果旋转两个或三个轴,则球体和点将沿不同方向旋转。

Its normal behavior (start position and one axis rotation): http://joxi.ru/JqkaVP3JTJA_Xelvbds 其正常行为(开始位置和一个轴旋转): http : //joxi.ru/JqkaVP3JTJA_Xelvbds

Its after begining rotation: http://joxi.ru/qqoaVIwyTJBWeTcvy4A 开始轮播之后: http//joxi.ru/qqoaVIwyTJBWeTcvy4A

What am I doing wrong? 我究竟做错了什么?

Using WebGL (OpenGL ES2). 使用WebGL(OpenGL ES2)。

I solved the problem. 我解决了问题。 Just need to multiply rotation matrix in reverse order. 只需要以相反的顺序乘以旋转矩阵即可。

Shader for fragment rotation 片段旋转着色器

   mat3 rotateX = mat3(
   1.0, 0, 0,
   0, cos(angles.x), -sin(angles.x),
   0, sin(angles.x), cos(angles.x)
   );

   mat3 rotateY = mat3(
   cos(angles.y), 0, sin(angles.y),
   0, 1, 0,
   -sin(angles.y), 0, cos(angles.y)
   );

   mat3 rotateZ = mat3(
   cos(angles.z), -sin(angles.z), 0,
   sin(angles.z), cos(angles.z), 0,
   0, 0, 1
   );

   float Fi = (-cUv.x + 0.25) * 2.0 * M_PI;
   float Te = cUv.y * M_PI;  

   vec3 coord;
   coord.x = sin(Te) * cos(Fi);
   coord.y = sin(Te) * sin(Fi);
   coord.z = cos(Te);

   coord = coord * rotateX * rotateY * rotateZ;

Shader for vertex rotation 顶点旋转着色器

angles = -angles;

mat3 rotateX = mat3(
    1.0, 0, 0,
    0, cos(angles.x), -sin(angles.x),
    0, sin(angles.x), cos(angles.x)
);

mat3 rotateY = mat3(
    cos(angles.y), 0, sin(angles.y),
    0, 1, 0,
    -sin(angles.y), 0, cos(angles.y)
);

mat3 rotateZ = mat3(
    cos(angles.z), -sin(angles.z), 0,
    sin(angles.z), cos(angles.z), 0,
    0, 0, 1
);

float Fi = (-curPosition.x) * M_PI; //hor
float Te = (curPosition.y) * M_PI; //vert

vec3 coord;
coord.x = sin(Te) * cos(Fi);
coord.y = sin(Te) * sin(Fi);
coord.z = cos(Te);

return coord * rotateZ * rotateY * rotateX;

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

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