首页 > 解决方案 > Python OpenGL - 如何在转换后找到一个点的当前世界坐标?

问题描述

在 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 = ?

标签: pythonopenglpyopengl

解决方案


您可以使用 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 数学库的语法略有不同。

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

推荐阅读