Skip to main content
1 of 5

How to compute matrix transform local/world on OpenGL

I'm developing an OpenGL program where each object / entity contains m_WorldPosition, m_WorldOrientation, m_WorldScale and m_WorldTransform (Vector3, Quaternion, Vector3 and Matrix4f).

The problem is that these transformations are global. So I move, rotate and scale an object / entity I need to use a function that I did.

// C++ void HEntity::translate(const HVec3f& pos, const bool local) { if(local) { m_WorldPosition += m_WorldOrientation.toMatrix3() * pos; } else { m_WorldPosition += pos; } m_NeedTransformUpdate = true; } 

I wanted to do the same as other software such as Blender or Unity3D 3D where each object / entity contains the variables position, localPosition, rotation, localRotation... But I do not know how.

Below is the function to compute the matrix.

// C++ void HEntity::updateTransform() { m_WorldTransform.identity(); m_WorldTransform.setRotationAxis(m_WorldOrientation.getAngle(), m_WorldOrientation.getAxis()); m_WorldTransform.scale(m_WorldScaling); m_WorldTransform.translate(m_WorldPosition); if(m_pParent) { m_WorldTransform = m_pParent->m_WorldTransform * m_WorldTransform; } }