Search code examples
c++openglvectortransformationquaternions

Transformation Hierarchy - applying a parent's orientation to its children


My goal:

  • When being rendered, a child object needs to render relative to its parent; thus its own position and orientation need to be relative to its parent

My problem:

  • Translations work, but the parent's rotation/orientation isn't correctly being added to a child's transformation calculation; the child rotates on its own orientation fine, but when the parent's orientation is added the calculated vertex for the child is rotated twice as much

My approach:

  • When calculating gl_Position in my vertex shader, I pass the shader my mvpMatrix, object vertex, object position, object orientation, parent position, and parent orientation.
  • Within the shader I've defined structs for holding a quaternion, as well as methods for rotating a vector by a quaternion, which work on their own when I rotate an object by itself.

    gl_Position = mvpMatrix * (vec4(parentPosition,1) + rotVertexByQuaternion(parentOrientation, vec4(objectPosition,1) + rotVertexByQuaternion(objectOrientation,vertex) ) );
    

Here's a picture depicting what I aim, and what I get. I omit adding the parents position in this example, because it is 0,0,0, but it would be done last anyways. I've labeled 1 vertex green for the child object and traced it all throughout, so that the rotations can be traced. Also, the current step in my calculation is displayed beneath each panel. Example of the issue


Solution

  • Apparently I still had some leftover old code applying manual orientation changes to all children of a parent, outside of the shader, as suggested by @ratchet freak I thought it was just a coincidence resulting from bad math, but it turned out to actually be a full double rotation of the parent orientation.

    So, the problem was fixed by removing those lines of code so that the code I've posted in the opening post actually works.

    Further, apparently my implementation is less than ideal, instead I should learn how to create a proper model matrix, but for now the problem is technically closed.