OpenGL Z 轴向上
我一直在尝试让我的 opengl 引擎使用 z 轴作为上轴,但无论我做什么似乎都没有显示。我这样计算视图矩阵:
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;
之前,当我将 Y 轴向上(工作正常)时,代码如下:
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;
每个对象/顶点首先乘以一个简单的平移矩阵:
1 0 0 xposition
0 1 0 yposition
0 0 1 zposition
0 0 0 1
然后乘以生成的投影矩阵多于 我只是遇到黑屏。我还需要更改其他内容才能使其正常工作吗?
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;
before, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些看起来就像您在投影矩阵中使用的矩阵,如果您旋转该矩阵,就会发生奇怪的事情,因为您扰乱了 OpenGL 对传入数据的期望。
相反,您必须在应用投影之前对传入向量应用旋转;或者换句话说,您从旋转矩阵开始,然后在其上乘以投影。
现在,如果您使用固定函数管道,只需使用工作变体,将其放入投影矩阵中。如果您使用着色器,请将其放入 gl_ProjectionMatrix 制服中。有许多着色器算法需要世界空间顶点位置;将投影和模型视图合并是有问题的。
对于您的模型视图,假设以下内容作为您的单位矩阵:
并继续使用您的工作投影,并将其应用于第二阶段。
然而,您所做的是盲目地交换所有非零 y<->z 条目,这是错误的:
正确的做法是交换 y 和 z 列:
现在,如果您将上面给出的新“身份”相乘,则在您的工作投影你得到的正是这个。
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:
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:
The correct this would have been swapping the y and z column:
Now if you r-multiply the new "identity" given above, on your working projection what you get is exactly this.