我如何在这个库中使用透视投影

发布于 2024-08-07 18:59:23 字数 1760 浏览 4 评论 0原文

我找到了一个名为 pyeuclid 的库,它似乎在 3D 数学方面做了我想做的事情。 它包含一个 3D 矢量类和一个能够进行旋转、平移和缩放等变换的 4X4 矩阵类。

矩阵创建很简单,只需传递参数即可创建矩阵。

>>> m = Matrix4()
>>> m.translate(50,50,50)
Matrix4([    1.00     0.00     0.00    50.00
             0.00     1.00     0.00    50.00
             0.00     0.00     1.00    50.00
             0.00     0.00     0.00     1.00])

该库附带一个 3D 矢量类,如果我想用这个矩阵平移 3D 点,我需要首先创建 向量类,就像这样。

>>> v = Vector3(100,200,300)
>>> v
Vector3(100.00, 200.00, 300.00)
>>> v.x
100
>>> v.y
200
>>> v.z
300

现在 Matrix4() 类带有一个转换坐标的方法,其使用方式如下。

>>> m.transform(v)
Point3(150.00, 250.00, 350.00)

这很棒,但是,没有应用透视投影,该库附带了一种名为 Matrix4.new_perspective(fov_y,aspect,near,far) 的方法。 这就是问题所在,我不知道如何正确使用这个函数,(m.transform(vector3) 不会产生任何可用的东西)文档没有显示它如何与向量或其他矩阵一起使用,它说“相当于OpenGL调用gluPerspective.fov_y”,但我从未使用过opengl,所以不会帮助。

这是模块(来自 euclid import Vector3 、 Matrix4)

我不想要的,是对某些游戏引擎/可视化库的推荐,或者是一些想要在屏幕上绘制某些内容的东西,我只是想知道我是否可以使用这个库将 3D 矢量坐标转换为 2D 屏幕坐标,如果不能,什么库可以我用???

所以快去那里帮助陷入困境的菜鸟吧! :-) 感谢您的帮助。

i found a library called pyeuclid and it seems to do what i want in respect to 3D math.
it contins a 3D vector class and a 4X4 matrix class capable of transformations like rotate,translate and scale.

matrix creation is simple, simply pass along the arguments and the matrix is created.

>>> m = Matrix4()
>>> m.translate(50,50,50)
Matrix4([    1.00     0.00     0.00    50.00
             0.00     1.00     0.00    50.00
             0.00     0.00     1.00    50.00
             0.00     0.00     0.00     1.00])

the lbrary comes with a 3D vector class, if i wanted to translate a 3D point with this matrix i would need to first create the vector class, like this.

>>> v = Vector3(100,200,300)
>>> v
Vector3(100.00, 200.00, 300.00)
>>> v.x
100
>>> v.y
200
>>> v.z
300

Now the Matrix4() class comes with a method to transform coordinates, and its used like so.

>>> m.transform(v)
Point3(150.00, 250.00, 350.00)

this is great, however, there is no perspective projection applied, the library comes with a method called Matrix4.new_perspective(fov_y, aspect, near, far).
Thats where the problem lies, i have no idea how to use this function properly, (m.transform(vector3) doesent produce anything usable )the documentation doesent show how its used with vectors or other matrices, it says something about being 'equivalent to the OpenGL call gluPerspective.fov_y', but ive never used opengl so that wont help.

this is the module (from euclid import Vector3 , Matrix4)

what i dont want, is recomendtions for some game engine/visulization library, or something thats bent on drawing something to the screen, i just want to know if i can use this library to to turn 3D vector coordnates into 2D screen coordinates, and if not what library can i use???

so get in there and help out noob in distress! :-)
thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

酒解孤独 2024-08-14 18:59:23

本教程解释了gluPerspective(),并且应该转移,因为您的库是用它作为模型编写的。

我希望 new_perspective() 方法像构造函数一样工作,即它返回一个设置为透视变换的 Matrix。然后,您应该能够通过将向量与该矩阵相乘,将世界空间坐标转换为透视投影坐标。

This tutorial explains the arguments to gluPerspective(), and should transfer over since your library is written with that as a model.

I would expect the new_perspective() method to work like a constructor, i.e. it returns a Matrix set up as a perspective transformation. You should then be able to transform world-space coordinates into perspective-projected coordinates by multiplying vectors with that matrix.

祁梦 2024-08-14 18:59:23

我不知道这是否相关,因为您可能已经知道这一点,但您也需要一个视图矩阵。视图矩阵表示相机在世界中的变换的逆矩阵。您可能会发现矩阵类还具有用于创建视图矩阵的辅助方法,有时称为类似的lookAt os。或者,只需手动创建一个矩阵来定位和定向您的(假想的)相机,然后反转它。

如果您将投影矩阵应用于世界坐标,您将隐含假设您的视图矩阵是恒等式,因此您的相机位于世界的原点并指向一个轴。哪个轴取决于投影矩阵,但如果它像大多数其他系统一样,它将是 z 轴,因为惯例是沿 z 测量相机深度。它可能是正向或负向,因此请再次检查文档以了解详细信息。

因此,总而言之,您需要将模型矩阵与视图矩阵相乘,然后再与投影矩阵相乘。投影矩阵被设计为在相机坐标而不是世界坐标中工作。

I don't know if this is relevant as you may already know this but you'll need a view matrix too. The view matrix represents the inverse of the transform of your camera in the world. You may find the matrix class also has helper methods for creating view matrices, sometimes called lookAt os similar. Alternatively just create a matrix manually that positions and orientates your (imaginary) camera and then invert it.

If you apply the projection matrix to world co-ords you're making the implicit assumption that your view matrix is the identity and so your camera is at the origin of the world and pointing down an axis. Which axis that is will depend on the projection matrix but if it's like most other systems it'll be the z-axis since convention has camera depth measured along z. It could be in either the positive or negative direction so again check the documentation for details.

So, to summarise, you need to multiply your model matrix with the view matrix and then by the projection matrix. The projection matrix is designed to work in camera coordinates rather than world coordinates.

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