相机(LootkAt +正交投影)不想工作 [SlimDX / D3D9]
我不太明白“相机”如何与 D3D9 配合使用
首先,我如何设置相机:
public Camera()
{
this.eye = new Vector3(0.0f, 0.0f, 0.0f);
this.lookAt = new Vector3(0.0f, 0.0f, 1.0f);
this.up = new Vector3(0.0f, 1.0f, 0.0f);
viewMatrix = Matrix.LookAtLH(eye, lookAt, up);
projectionMatrix = Matrix.OrthoLH(1 * zoomLevel, 1 * zoomLevel, 0.0f, 1.0f);
}
以及我的顶点:
vertices = new VertexTexture[]
{
new VertexTexture()
{
Position = new Vector4(0.0f, 0.0f, 0.0f, 1.0f),
TextureCoord = new Vector2(0.0f, 1.0f)
},
new VertexTexture()
{
Position = new Vector4(0.0f, model.Height, 0.0f, 1.0f),
TextureCoord = new Vector2(0.0f, 0.0f)
},
new VertexTexture()
{
Position = new Vector4(model.Width, model.Height, 0.0f, 1.0f),
TextureCoord = new Vector2(1.0f, 0.0f)
},
new VertexTexture()
{
Position = new Vector4(model.Width, 0.0f, 0.0f, 1.0f),
TextureCoord = new Vector2(1.0f, 1.0f)
}
};
它可以工作。我可以移动相机、变焦等。
但相机的属性对我来说似乎很奇怪!我认为它会是这样的:
public Camera()
{
this.eye = new Vector3(0.0f, 0.0f, 1.0f);
this.lookAt = new Vector3(0.0f, 0.0f, 0.0f);
this.up = new Vector3(0.0f, 1.0f, 0.0f);
viewMatrix = Matrix.LookAtLH(eye, lookAt, up);
projectionMatrix = Matrix.OrthoLH(1 * zoomLevel, 1 * zoomLevel, 0.1f, 100.0f);
}
但使用该参数它不起作用。如果我更改计划的 Z 坐标(需要设置为 0 才能工作),则相同。
现在,我尝试渲染其他对象。我生成一个球体的顶点(它在 D3D 10 上工作正常,在 (0;0;0) 周围生成一个 1 半径的球体),但屏幕上没有出现任何内容
我用眼睛和注视参数玩,但我可以弄清楚如何制作它工作,那么,我做错了什么?
I don't really understand how "cameras" work with D3D9
First, how i set my camera up:
public Camera()
{
this.eye = new Vector3(0.0f, 0.0f, 0.0f);
this.lookAt = new Vector3(0.0f, 0.0f, 1.0f);
this.up = new Vector3(0.0f, 1.0f, 0.0f);
viewMatrix = Matrix.LookAtLH(eye, lookAt, up);
projectionMatrix = Matrix.OrthoLH(1 * zoomLevel, 1 * zoomLevel, 0.0f, 1.0f);
}
And my vertices :
vertices = new VertexTexture[]
{
new VertexTexture()
{
Position = new Vector4(0.0f, 0.0f, 0.0f, 1.0f),
TextureCoord = new Vector2(0.0f, 1.0f)
},
new VertexTexture()
{
Position = new Vector4(0.0f, model.Height, 0.0f, 1.0f),
TextureCoord = new Vector2(0.0f, 0.0f)
},
new VertexTexture()
{
Position = new Vector4(model.Width, model.Height, 0.0f, 1.0f),
TextureCoord = new Vector2(1.0f, 0.0f)
},
new VertexTexture()
{
Position = new Vector4(model.Width, 0.0f, 0.0f, 1.0f),
TextureCoord = new Vector2(1.0f, 1.0f)
}
};
It works. I can move the camera, zoom, etc.
But the cameras properties seems weird to me ! I thought it would be something like:
public Camera()
{
this.eye = new Vector3(0.0f, 0.0f, 1.0f);
this.lookAt = new Vector3(0.0f, 0.0f, 0.0f);
this.up = new Vector3(0.0f, 1.0f, 0.0f);
viewMatrix = Matrix.LookAtLH(eye, lookAt, up);
projectionMatrix = Matrix.OrthoLH(1 * zoomLevel, 1 * zoomLevel, 0.1f, 100.0f);
}
but with that parameters it doesn't work. Same if i change the Z coordinate for my plan (Which need to be set at 0 to work).
Now, i try to render other objects. I generate vertices for a sphere (it works fine on D3D 10, a 1 radius sphere generated around (0;0;0) ) but nothing appear on the screen
I played with the eye and lookat parameters but i can figure how to make it work, so, what i m doing wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,一些背景知识。在正交投影中,我们技术上不需要 Z 坐标变换,但实际上 D3D 系统并不关心您使用哪种投影 - 对于设备来说,它只是一个变换矩阵,仅此而已。因此,无论投影类型如何,Z 坐标总是进行变换。另一方面,相机坐标不通过投影矩阵进行转换(否则定位将是一场噩梦)。现在让我们回到您的情况,看看发生了什么。
在 OrthoLH 中指定 zn=0.1f 即可声明这是最小深度。因此,每个顶点的深度将移动 -0.1f。顶点布局变换后,Z 将为 0.9f,但您记得相机不受影响吗?所以,它的深度仍然是 1.0f,这就是为什么你看不到任何东西:相机后面没有顶点。
在此处添加浮点计算错误,您就会明白为什么将相机设置为 0.9f 有时有帮助,有时没有。不过,将位置设置为 0.8f 对于任何情况都适用。
First, some background. In orthogonal projection we technically don't need Z coordinate transforming but in fact D3D system doesn't care what kind of projection you use - for device it's just a transformation matrix and nothing more. So, Z coordinate is always transformed regardless to the projection type. On the other hand, camera coordinates are not transformed by a projection matrix (otherwise positioning would be a nightmare). Now let's get back to your situation and see what's happening.
Specifying zn=0.1f in
OrthoLH
you declare that this is your minimum depth. Hence, every vertex will be shifted in depth by -0.1f. With your vertex layout transformed Z will be 0.9f but you remember camera is not affected? So, it's still at depth 1.0f and this why you see nothing: there are no vertexes behind the camera.Add here float computation error and you'll understand why setting camera at 0.9f sometimes helps and sometimes not. Though, setting position to 0.8f will work for any case.
首先,感谢您的回答。
我设法通过设置我的相机的属性来避免这个问题:
我会再次阅读你所说的,并会考虑一个最好的修复:)
我认为我的问题与我的顶点有关......例如,我生成一个半径为 20 的球体,然后我通过根据我的屏幕坐标进行缩放,将该顶点转换为对应于 20px 宽的球体。
我的 X / Y 缩放是可以的。但是如何缩放Z坐标呢? 2.0 / ((控制高度 + 控制宽度) / 2 ) ?
我会再读一遍你的答案,以理解一切:)
First, thank's for the answer.
I managed to avoid the problem by setting my camera with that properties:
I'll read again what you said, and will think about a nicest fix :)
I think my problem is relative to my vertices ... for exemple, I generate a sphere with a radius of 20, then I transform that vertices to correspond to a 20px wide sphere, by scaling according to my screen coordinates
My X / Y scaling is ok. But how to scale the Z coordinate ? 2.0 / ((ControlHeight + ControlWidth ) / 2 ) ?
I'll read your anszer again, to understand everything :)