Search code examples
pythonopenglpyopengl

Python OpenGL - How to find the current world coordinate of a point after transformation?


In C++, I can find the current position of a point like this:

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);

How can I do the same calculation in Python to get curretPointPosition? Can I use Python's pyrr?

In PyOpenGL, I have the following code:

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

Solution

  • You can use the OpenGL Mathematics (GLM) library for Python (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)
    

    The syntax with the Pyrr Maths Library is slightly different.

    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