在软件光栅器中实现 az 缓冲区

发布于 2024-08-15 11:14:47 字数 495 浏览 4 评论 0原文

作为家庭作业,我们正在编写一个软件光栅器。我注意到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯沦落人 2024-08-22 11:14:47

要标准化 Z 值,您必须定义近剪裁平面和远剪裁平面。然后对 Z 进行归一化,使其在近平面处为 0,在远平面处为 1。

但是,您通常会在投影后执行此操作。看起来你的最后一行是投影发生的地方。

许多其他事情:

  • 您计算完整的矩阵向量乘法,但只保留 Z,这是浪费的。您应该考虑变换点并保留它们所有的 X、Y、Z 坐标;
  • 您在每个顶点重新计算 tanf(),但它是常数;
  • 我建议您使用投影矩阵而不是 tanf 计算;
  • 从简单的正交投影开始,调试起来会更容易。

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:

  • You compute the full matrix-vector multiplication but keep only the Z, this is wasteful. You should consider transforming the points and keeping all their X, Y, Z coordinates ;
  • You recompute tanf() at every vertex, but its constant ;
  • I would suggest you use a projection matrix rather than the tanf computation ;
  • Start with a simple orthogonal projection, it will be easier to debug.
路弥 2024-08-22 11:14:47

假设您想知道某个顶点处的 z,即 a_Point:

首先,您希望在旋转之前执行平移,以便您围绕相机而不是空间原点(可能在其他地方)执行旋转。其次,camera_new这个名字选得不太好,因为它代表了由相机位置设置的新参考中a_Point的坐标。相反,请执行以下操作:

Vec3 point_new = (m_MatRotation * (a_Point-m_Position));

如果这不起作用,您将不得不通过创建一个真实的投影矩阵来完成此操作,该矩阵在一次乘法中执行平移、旋转和投影。这里有一些教程可以帮助我更好地理解如何做到这一点。

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:

Vec3 point_new = (m_MatRotation * (a_Point-m_Position));

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文