【问题标题】:Python OpenGL - How to find the current world coordinate of a point after transformation?Python OpenGL - 如何在转换后找到一个点的当前世界坐标?
【发布时间】:2021-11-01 08:16:47
【问题描述】:

在 C++ 中,我可以像这样找到一个点的当前位置:

glm::vec3 somePoint(x,y,z); //x,y,z are some float values
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(xTrans, yTrans, zTrans));

glm::vec4 currentPointPosition = translationMatrix*glm::vec4(somePoint,1);

如何在 Python 中进行相同的计算以得到 curretPointPosition?我可以使用 Python 的 pyrr 吗?

在 PyOpenGL 中,我有以下代码:

somePoint = [x,y,z]
translationMatrix= pyrr.matrix44.create_from_translation(pyrr.Vector3([xTrans, yTrans, zTrans]))
currentPointPosition = ?

【问题讨论】:

  • 可以通过将 (x,y,z,1) 点转换为 NP 数组并将 translationMatrix 保持为 pyrr 来完成。然后像这样的点积:currentPointPosition = currentPointPosition.dot(translationMatrix)。我更喜欢@Rabbid76 的答案,因为我可以将 GLM 用于 C++,将 PyGLM 用于 Python。

标签: python opengl pyopengl


【解决方案1】:

您可以使用 Python 的 OpenGL 数学 (GLM) 库 (PyGLM)

somePoint = glm.vec3(x, y, z)
tranaltionVec = glm.vec3(xTrans, yTrans, zTrans)
translationMatrix = glm.translate(glm.mat4(1), tranaltionVec)
currentPointPosition = translationMatrix * glm.vec4(somePoint, 1)

Pyrr Maths Library 的语法略有不同。

somePoint = pyrr.Vector3((x, y, z))
tranaltionVec = pyrr.Vector3((xTrans, yTrans, zTrans))
translationMatrix = pyrr.matrix44.create_from_translation(tranaltionVec)
currentPointPosition = pyrr.Vector4.from_vector3(somePoint, 1) @ translationMatrix

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2013-04-14
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多