在软件光栅器中实现 az 缓冲区
作为家庭作业,我们正在编写一个软件光栅器。我注意到我的 z 缓冲没有正常工作,所以我尝试通过将其输出到屏幕来调试它。 (黑色为近,白色为远)。
然而,我得到了每个顶点的 z 值。这就是我用来变换点的方法:
float Camera::GetZToPoint(Vec3 a_Point)
{
Vec3 camera_new = (m_MatRotation * a_Point) - m_Position;
return (HALFSCREEN / tanf(_RadToDeg(60.f * 0.5f)) / camera_new.z);
}
m_MatRotation
是一个 3x3 矩阵。将其乘以向量将返回变换后的向量。
我得到 0 和 x 之间的最大值和最小值,其中 x 是一个看似随机的数字。
我这个转变做对了吗?如果是这样,我如何标准化我的 Z 值,使其位于两个设定点之间?
提前致谢。
as a homework assignment, we're writing a software rasterizer. I've noticed my z buffering is not working as well as it should, so I'm trying to debug it by outputting it to the screen. (Black is near, white is far away).
However, I'm getting peculiar values for the z per vertex. This is what I use to transform the points:
float Camera::GetZToPoint(Vec3 a_Point)
{
Vec3 camera_new = (m_MatRotation * a_Point) - m_Position;
return (HALFSCREEN / tanf(_RadToDeg(60.f * 0.5f)) / camera_new.z);
}
m_MatRotation
is a 3x3 matrix. Multiplying it by a vector returns a transformed vector.
I get maximum and minimum values between 0 and x, where x is a seemingly random number.
Am I doing this transformation right? If so, how can I normalize my Z values so it lies between two set points?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要标准化 Z 值,您必须定义近剪裁平面和远剪裁平面。然后对 Z 进行归一化,使其在近平面处为 0,在远平面处为 1。
但是,您通常会在投影后执行此操作。看起来你的最后一行是投影发生的地方。
许多其他事情:
To normalize the Z values you have to define a near clipping plane and a far clipping plane. Then you normalize Z such that its 0 at the near plane and 1 at the far plane.
However, you would usually do that after projection. It looks like your last line is where projection occurs.
A number of other things:
假设您想知道某个顶点处的 z,即 a_Point:
首先,您希望在旋转之前执行平移,以便您围绕相机而不是空间原点(可能在其他地方)执行旋转。其次,camera_new这个名字选得不太好,因为它代表了由相机位置设置的新参考中a_Point的坐标。相反,请执行以下操作:
如果这不起作用,您将不得不通过创建一个真实的投影矩阵来完成此操作,该矩阵在一次乘法中执行平移、旋转和投影。这里有一些教程可以帮助我更好地理解如何做到这一点。
http://www.songho.ca/opengl/gl_projectionmatrix.html
codeguru.com /cpp/misc/misc/math/article.php/c10123/
一旦你成功地以透视正确的方式在屏幕上投影顶点,你就必须找到一种方法来填充它们之间的空间,并找到,对于每个填充的像素,z 是多少。这是一个全新的故事,维基百科关于纹理映射的文章帮助我做到了这一点。
en.wikipedia.org/wiki/Texture_mapping#Perspective_ Correctness
抱歉,我无法给您更多链接,Stackoverflow 不会让我这样做,因为我是新用户......
Assuming you want to know z at a vertez, which would be a_Point:
First of all, you want to perform the translation before the rotation so you perform the rotation around your camera and not the origin of your space, wchich may be somewhere else. Second, camera_new is not very well chosen a name, as it represents the coordinates of a_Point in the new referential set by the position of your camera. Instead, do the following:
If that does not work, you'll have to do it the hard way by creating a real projection matrix that performs the translation, rotation and projection all in one multiplication. Here are some tutorials that helped me a lot understanding how to do that.
http://www.songho.ca/opengl/gl_projectionmatrix.html
codeguru.com/cpp/misc/misc/math/article.php/c10123/
Once you have managed to project vertices on the screen in a perspective-correct way, you'll have to find a way to fill the space between them, and find, for each pixel filled, what is z. This is a whole new story and the Wikipedia article about Texture mapping helped me do it.
en.wikipedia.org/wiki/Texture_mapping#Perspective_correctness
Sorry I couldn't give you more links, Stackoverflow would not let me because I'm a new user...