开发者

Issues with Z-axis rotation matrix in glsl shader

开发者 https://www.devze.com 2023-03-14 19:34 出处:网络
I\'ve recently began putting together an OpenGL ES 1.1/2.0 2D pipeline from the ground up (iPhone only). This pipeline is intended to be used by engineers with no 3D math experience.

I've recently began putting together an OpenGL ES 1.1/2.0 2D pipeline from the ground up (iPhone only). This pipeline is intended to be used by engineers with no 3D math experience.

Commented out are the X and Y axis rotation matrices that produce the exact results they should. The Z rotation matrix seems to do nothing.

VERTEX SHADER

//THESE WORK
/*
highp mat4 rotationMatrix = mat4(1.0, 0.0, 0.0, 0.0,
                                 0.0, cos(angle), -sin(angle), 0.0,
                                 0.0, sin(angle), cos(angle), 0.0,
                                 0.0, 0.0, 0.0, 1.0); 

highp mat4 rotationMatrix = mat4(cos(angle), 0.0, sin(angle), 0.0,
                                 0.0, 1.0, 0.0, 0.0,
                                 -sin(angle), 0.0, cos(angle), 0.0,
                                 0.0, 0.0, 0.0, 1.0); 
*/

//THIS DOESN'T WORK >:(
highp mat4 rotationMatrix = mat4(cos(angle), -sin(angle), 0.0, 0.0,
                                     sin(angle), cos(angle), 0.0, 0.0,
                                     0.0, 0.0, 1.0, 0.0,
                                     0.0, 0.0, 0.0, 1.0);  

gl_Position = a_position;
gl_Position *= rotationMatrix;

Since this will be for 2D rendering and handed to engineers without 3D experience, I would prefer to stay away from passing in a MVP matrix and just push the basic scale, rotation and translation variables (and skip writing a partial matrix lib for the 10th time).

It's been a while since I've tangled with matrix math and shaders, so I'm hoping its a small error.

Thanks for your help!

EDIT/UPDATE:

I found at that a post-processing pass was clobbering the angle.

I now find that Z-rotation seems to scale the quad. I remember this being a n00b problem and am looking into it...

I forgot to mention that I have a cheap temp projection Matrix

//s_scalefactor is for retina vs non-retina display

highp mat4 projectionMatrix = mat4( 2.0/(320.0 * s_scalefactor), 0.0, 0开发者_如何学Python.0, -1.0,
                                    0.0, 2.0/(480.0 * s_scalefactor), 0.0, -1.0,
                                    0.0, 0.0, 1.0, 0.0,
                                    0.0, 0.0, 0.0, 1.0);  

gl_Position *= projectionMatrix;

Its cheap hack, but I'm not certain it would stiff a Z rotation.

EDIT #2:

I've also gotten nowhere attempting to use a frustum instead of ortho and calculating the matrix outside of the shader.


I had the same problem and it was very, very strange. I was able to resolve it by populating the mat4 structure by using the accessor operators as opposed to using the constructor:

mat4 rotateZ;
rotateZ[0].x    = cosAngle;
rotateZ[0].y    = negSinAngle;
rotateZ[0].z    = 0.0;
rotateZ[0].w    = 0.0;
rotateZ[1].x    = sinAngle;
rotateZ[1].y    = cosAngle;
rotateZ[1].z    = 0.0;
rotateZ[1].w    = 0.0;
rotateZ[2].x    = 0.0;
rotateZ[2].y    = 0.0;
rotateZ[2].z    = 1.0;
rotateZ[2].w    = 0.0;
rotateZ[3].x    = 0.0;
rotateZ[3].y    = 0.0;
rotateZ[3].z    = 0.0;
rotateZ[3].w    = 1.0;
0

精彩评论

暂无评论...
验证码 换一张
取 消