1
\$\begingroup\$

I need help with a little problem in my project, in which I have a scene graph with the intented camera like this:

  • MovementNode
    • Camera Node
    • Mesh Object

My problem is that I have the propagation of the world_space_matrix from the parent to the child done like this:

void BaseNode::update_matrix_if_needed() {
        if (_updated_position) {
            BaseNodeInstance* parent = instance.get_parent();

            global_space_matrix = glm::mat4(1.0f); // construct identity matrix

            //scale, rotate, move
                
                global_space_matrix = glm::scale(global_space_matrix, scl);
        
                rotation_matrix = glm::rotate(rot.x, glm::vec3(1, 0, 0));
                rotation_matrix *= glm::rotate(rot.y, glm::vec3(0, 1, 0));
                rotation_matrix *= glm::rotate(rot.z, glm::vec3(0, 0, 1));
                global_space_matrix =  rotation_matrix * global_space_matrix;

                global_space_matrix = glm::translate(local_space_matrix, pos);

            if (parent  != nullptr) {
                global_space_matrix = parent->get_data()->global_space_matrix * global_space_matrix;
            }
        }

        _updated_position = false;
    }

Then, in the Camera Node, I submit the view matrix like this:

void CameraNode::uniform_view_matrix(GLuint location) {
    glUniformMatrix4fv(location, 1, 0, glm::value_ptr(camera_matrix));
}

where camera_matrix is computed as:

void CameraNode::update(float delta) {
    //add_pos(glm::vec3(0, 0, -1) * delta);

    if (_updated_position) {
        BaseNode::update(delta);
        camera_matrix = global_space_matrix;
    }
}

However, when I move the camera in my application, the objects are moving in completely different directions than my camera (although they stick together how they should).

I think the problem is in how I interpret the view_matrix from the cameras local space. What can be my problem?

\$\endgroup\$
0
\$\begingroup\$

The problem was how I updated my Camera.

I used global_space_matrix as my view_matrix but then I tried to use glm::inverse(global_space_matrix) and it is working as intended!

Still I don't completely understand why.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.