Search code examples
c++opengltransformationglm-mathscenegraph

Order of transformations for rendering a scenegraph


In my 2D Game Engine I'm struggling with correctly rendering sprites if those objects are in a parent-child relationship. The picture explains the problem. I use a scenegraph for rendering and use the visitor pattern for traversal. I want the rotation of the parent to only rotate the child in place.

Image showing problem

//spriterenderer.cpp

// sprites are positioned & rotated around the center
    GLfloat vertices[] = {
        // Pos      // Tex
        -0.5f, 0.5f, 0.0f, 1.0f,
        0.5f, -0.5f, 1.0f, 0.0f,
        -0.5f, -0.5f, 0.0f, 0.0f,

        -0.5f, 0.5f, 0.0f, 1.0f,
        0.5f, 0.5f, 1.0f, 1.0f,
        0.5f, -0.5f, 1.0f, 0.0f
    };

// this gets called if a GameObject has children
bool SpriteRenderer::Enter(GameObject & node)
{
...
RenderSprite(...);

// save the current modelMatrix on the stack
m_matrixStack.push_back(m_modelMatrix);

// apply transformation. I assume this is where the mistake is made
m_modelMatrix = glm::translate(m_modelMatrix, glm::vec3(node.GetLocalPosition(), 0.0f));
m_modelMatrix = glm::rotate(m_modelMatrix, node.GetLocalRotation(), glm::vec3(0.0f, 0.0f, 1.0f));
m_modelMatrix = glm::scale(m_modelMatrix, glm::vec3(node.GetLocalScale(), 1.0f));
return true;

}

// after drawing all children of a node restore the previous model matrix
bool SpriteRenderer::Leave(GameObject & node)
{
    m_modelMatrix = m_matrixStack.back();
    m_matrixStack.pop_back();
    return true;
}

// if a node doesn't have children
bool SpriteRenderer::Visit(GameObject & node)
{
    RenderSprite(...);
}

void SpriteRenderer::RenderSprite(...)
{
// save the current transformation
    m_matrixStack.push_back(m_modelMatrix);

    // apply model transform
    m_modelMatrix = glm::translate(m_modelMatrix, glm::vec3(gameObject.GetLocalPosition(), 0.0f));
    m_modelMatrix = glm::rotate(m_modelMatrix, gameObject.GetLocalRotation(), glm::vec3(0.0f, 0.0f, 1.0f));
    m_modelMatrix = glm::scale(m_modelMatrix, glm::vec3(textureSize, 1.0f));

....
//restore previous transform
    m_modelMatrix = m_matrixStack.back();
    m_matrixStack.pop_back();
}

Solution

  • I've found a working solution by keeping track of the rotation in an additive way.

    So, instead of

    // in SpriteRenderer::Enter
    m_modelMatrix = glm::rotate(m_modelMatrix, node.GetLocalRotation(), glm::vec3(0.0f, 0.0f, 1.0f));
    

    I use m_additiveRotation += node.GetLocalRotation(); and in SpriteRenderer::Leave I substract the amount again.

    Finally, in SpriteRenderer::RenderSprite it changes to

    m_modelMatrix = glm::translate(m_modelMatrix, glm::vec3(gameObject.GetLocalPosition(), 0.0f));
    m_modelMatrix = glm::rotate(m_modelMatrix, m_additiveRotation, glm::vec3(0.0f, 0.0f, 1.0f));
    m_modelMatrix = glm::rotate(m_modelMatrix, gameObject.GetLocalRotation(), glm::vec3(0.0f, 0.0f, 1.0f));
    m_modelMatrix = glm::scale(m_modelMatrix, glm::vec3(textureSize, 1.0f));