投影和裁剪问题?
我目前正在使用 C# 开发 Direct3D 11 渲染引擎。
我已经成功地绘制了三角形、应用了颜色和纹理等。 我现在尝试显示的对象是一个边长为 1 的旋转立方体,放置在世界空间的原点。 但在应用视图,尤其是投影矩阵时,我得到的结果并不令人满意。由于很难解释,我只是向您展示不同视图/投影组合得到的结果:
来自我的顶点着色器的 HLSL 片段:
float4x4 wvpMatrix = mul(World, mul(View, Projection));
output.Position = mul(input.Position, wvpMatrix);
我使用的世界矩阵:
// Static rotation around all three axes
matrices.World = Matrix.Identity * Matrix.CreateFromYawPitchRoll(0.5f, 0.6f, 1.0f);
测试 A
来自原点的简单视图向量z 正方向。没有使用投影。 您可以看到立方体的背面被修剪。
matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.Identity;
测试 B
视图矩阵保持不变,但现在我使用的是正交投影矩阵。 这次立方体的正面消失了。您可以看到的部分是在测试 A 中被剪裁的部分。
matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.CreateOrthographic(2, 2, 0.01f, 100.0f);
测试 C
,就像测试 A 中一样。这次是从负 z 方向三个单位的点查看场景。 当相机位置未设置为系统原点时,立方体看起来会变形。我认为还有一些剪辑。
matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, -3), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.Identity;
测试 D
最后,我使用透视投影。到目前为止,我还没有找到可以看到立方体任何部分的相机位置。我得到的只是全屏矢车菊蓝色。
matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, -3), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.CreatePerspectiveFieldOfView(1, 1, 0.1f, 100);
我想做的第一件事是绘制完整的立方体 - 没有剪切。我尝试了近/远平面值的不同组合,但找不到可接受的解决方案。 我的最终目标是进行透视投影。但正如你所见,我什至无法获得场景的良好正交视图。
I am currently developing a Direct3D 11 rendering engine using C#.
I have already succeded in drawing triangles, applying color and textures, etc.
The object I am trying to display now is a rotated cube with a side length of 1 placed at the origin of the world space.
But I am getting unsatisfying results when applying the view and especially the projection matrix. As it is difficult to explain, I just show you what result I get for different view/projection combinations:
HLSL snippet from my vertex shader:
float4x4 wvpMatrix = mul(World, mul(View, Projection));
output.Position = mul(input.Position, wvpMatrix);
The world matrix I use:
// Static rotation around all three axes
matrices.World = Matrix.Identity * Matrix.CreateFromYawPitchRoll(0.5f, 0.6f, 1.0f);
Test A
Simple view vector from origin in the positive z direction. No projection used.
You can see that the back of the cube is being clipped.
matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.Identity;
Test B
View matrix stays the same, but now I am using an orthographic projection matrix.
This time the front of the cube vanishes. The parts you can see are those which were clipped in Test A.
matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.CreateOrthographic(2, 2, 0.01f, 100.0f);
Test C
Again no projection, just like in Test A. This time the scene is being viewed from a point three units in the negative z direction.
The cube looks distorted when the camera position is not set to the origin of the system. I think there is also some clipping.
matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, -3), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.Identity;
Test D
Finally, I use a perspective projection. Up to now I have not found a camera position from which any part of the cube is visible. All I get is fullscreen cornflower blue.
matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, -3), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.CreatePerspectiveFieldOfView(1, 1, 0.1f, 100);
First thing I want to do is drawing the full cube - no clipping. I tried different combinations of near/far plane values but I could not find an acceptable solution.
My final goal would be to have a perspective projection. But as you see I can't even get a good orthagonal view of the scene.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
矩阵必须在某个时刻进行转置。
原因:
Microsoft.Xna.Framework.Matrix
结构的布局是基于行。 HLSL 中的矩阵是基于列的。来自 XNA 文档:
来自关于矩阵顺序的维基百科文章:
解决方案 A:
在将所有矩阵发送到着色器之前转置它们:
解决方案 B:
将此行放入您的着色器代码中:
The matrices must be transposed at some point.
Reason:
The layout of the
Microsoft.Xna.Framework.Matrix
struct is row-based. Matrices in HLSL are column-based.From the XNA Documentation:
From the Wikipedia article on matrix order:
Solution A:
Transpose all matrices before they are sent to the shader:
Solution B:
Put this line in your shader code: