OpenGL Z Axis Up
Ive been trying to make my opengl engine use the z axis for the up axis, but no matter what I do nothing seems to display. I calculate the view matrix like this:
float width = xymax - xmin;
float height = xymax - ymin;
float depth = zfar - znear;
float q = -(zfar + znear) / depth;
float qn = -2 * (zfar * znear) / depth;
float w = 2 * znear / width;
w = w / aspect;
float h = 2 * znear / height;
m[0] = w;
m[1] = 0;
m[2] = 0;
m[3] = 0;
m[4] = 0;
m[5] = 0;
m[6] = h;
m[7] = qn;
m[8] = 0;
m[9] = q;
m[10] = 0;
m[11] = 0;
m[12] = 0;
m[13] = 0;
m[14] = -1;
m[15] = 0;
befo开发者_StackOverflow社区re, when I had the Y axis as up (which worked fine), the code was like this:
m[0] = w;
m[4] = 0;
m[8] = 0;
m[12] = 0;
m[1] = 0;
m[5] = h;
m[9] = 0;
m[13] = 0;
m[2] = 0;
m[6] = 0;
m[10] = q;
m[14] = -1;
m[3] = 0;
m[7] = 0;
m[11] = qn;
m[15] = 0;
Each object/vertex is first multiplied by a simple translation matrix:
1 0 0 xposition
0 1 0 yposition
0 0 1 zposition
0 0 0 1
and then by the projection matrix generated above I just get a black screen. Is there something else I need to change for this to work?
These are looking like matrices you'd use in the projection matrix, if you rotate that one, strange things happens, since you're messing with OpenGL expectations of the incoming data.
Instead you've to apply a rotation to the incoming vectors before you apply the projection; or in other word you start with a rotated matrix and multiply the projection on top of that.
Now, if you're using the fixed function pipeline, just use the working variant, put that in the projection matrix. And if you're using shaders, put it into the gl_ProjectionMatrix uniform. There are many shader algorithms that require the world space vertex positions; having projection and modelview merged is problematic.
For your modelview assume the following as your identity matrix:
1 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
And keep using your working projection, and apply it in a second stage.
What you did however was mindlessly exchanging all nonzero y<->z entries, which is wrong:
w 0 0 0
0 h 0 0
0 0 q qn
0 0 -1 0
The correct this would have been swapping the y and z column:
w 0 0 0
0 q 0 0
0 0 h -1
0 0 qn 0
Now if you r-multiply the new "identity" given above, on your working projection what you get is exactly this.
精彩评论