为相机投影屏幕
我试图通过编写一个简单的光线追踪器来了解更多关于矢量数学的知识,并且我一直在阅读它,但我无法找到如何确定主光线的方向。 这听起来像是一个简单的问题,也可能是,但以我目前的知识,我还无法弄清楚。
我认为我需要一台相机(只不过是作为矢量的位置和方向),并且从相机将主光线发射到相机前面的屏幕上,该屏幕代表最终图像。 我无法弄清楚的是屏幕的角坐标。 如果我知道屏幕,找到主光线的方向就很容易。
我希望只用不需要任何旋转矩阵的简单数学就可以计算出屏幕。 我最好的猜测是:
我将相机的方向作为矢量,该方向等于投影屏幕平面的法线。 所以我有了屏幕的法线,从那里我可以轻松计算出屏幕的中心,即:
camera_location + (normal * distance)
其中距离是屏幕和相机之间的距离。 然而,这就是我迷失的地方,我无法找到一种方法来计算出相机任意方向的平面角坐标。
你们有人能帮我吗? 如果我的方法行不通,那又怎样呢?
I'm trying to learn a little more on vectormath through writing a simple ray tracer and I've been doing some reading on it, but what I haven't been able to find is how to determine the direction of the primary rays. This sounds like a simple problem and probably is, but with my current knowledge I haven't been able to figure it out.
I figured that I need a camera (nothing more than a location and a direction as vectors) and from the camera I fire the primary rays onto a screen in front of the camera which represents the final image. What I can't figure out are the corner coordinates of the screen. If I know the screen, finding the direction the primary rays is easy.
I'm hoping the screen can be figured out using nothing but simple math that doesn't require any rotation matrices. My best guess is this:
I have the direction of the camera as a vector, this direction is equal to the normal of the plane of the projection screen. So I have the normal of the screen, and from there I can easily calculate the center of the screen which is:
camera_location + (normal * distance)
Where distance is the distance between the screen and the camera. However, that's where I get lost and I can't find a way to figure out the corner coordinates of the plane for any arbitrary direction of the camera.
Can any of you help me out here? And if my method can't possibly work, what does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:这里有一些代码,与最初发布的代码相比大大减少了,因为它省略了 viewMatrix 的创建。 这仅是一些扫描线渲染所需要的,并不用于光线追踪。
困难的工作是lookat() 函数,特别是“向上”和“向右”向量的创建。 它们必须彼此垂直,并且也必须垂直于眼睛和图片中心之间的矢量。 这些向量的创建依赖于叉积向量函数。
投射光线的实际函数假设屏幕视口在 Y 方向上从 -0.5 到 +0.5 运行。 该函数非常简单,只需将“视图”、“向上”和“右”向量的正确比例相加即可。
EDIT: Here's some code, which is substantially reduced from that originally posted because it omits the creation of a viewMatrix. That was only needed for some some scanline rendering and wasn't used for ray-tracing.
The hard work is the lookat() function, particularly the creation of the 'up' and 'right' vectors. These have to be perpendicular to each other, and also to the vector running between the eye and the center of the picture. Creation of those vectors relies on the cross-product vector function.
The actual function for casting rays assumes that the screen viewport runs from -0.5 to +0.5 in the Y direction. That function is pretty simple, and just consists of adding the correct proportions of the 'view', 'up' and 'right' vectors together.
感谢您的信息。 就我个人而言,我对矩阵变换不太满意,并且希望尽可能避免它们。 但是,从您的帖子中我了解到转换是做我想做的事情的唯一方法? 那太糟糕了,因为我认为我非常接近(我得到了屏幕的法线和中心点)并且会喜欢纯向量数学解决方案。
我将尝试采用您的代码,但不幸的是,我在某些时候并不确切知道您的对象内部发生了什么,主要是在 Matrix4d 对象中。
PS:如果您想以问题创建者的身份回复 StackOverflow 上的答案,您应该对该答案发表评论,还是可以创建一个新的“答案”?
Thank you for the information. Personally, I'm not too comfortable with matrix transformations and would like to avoid them as much as possible. However, from your post I understand that transformations are the only way to do what I want to do? That would too bad, since I thought I was pretty close (I got the normal and centerpoint of the screen) and would've liked a pure vector math solution.
I'll try to adopt your code, but unfortunately I don't know exactly what is going on internally in your objects at some points, mainly in the Matrix4d objects.
PS: If you want to reply to an answer on StackOverflow as the creator of the question, are you supposed to make a comment to that answer, or is it OK to create a new 'answer'?