mardi 27 juin 2017

change value of private variable inside class

I'm learning opengl. I got a camera class, and I change the code for the camera to contain the matrices for the projection and the view.

The projection matrix is set in the constructor. Meanwhile, the view is updated every frame. Everyframe the view matrix is calculated and stored override the previous values.

Here is the code Camera.h

class Camera : public Entity
{
public:
    Camera();
    ~Camera();

    void input(float delta);
    void update();

    const Matrix4& getView() const;
    const Matrix4& getProjection() const;

private:

    void mouseInput();

    void printMatrix(Matrix4& m);

    Matrix4 view;
    Matrix4 projection;

};

Camera.cpp

Camera::Camera()
    : projection(Maths::createProjectionMatrix())
{

}

void Camera::update()
{
    view = Maths::createViewMatrix(*this);
}

const Matrix4& Camera::getView() const
{
    return view;
}

const Matrix4 & Camera::getProjection() const
{
    return projection;
}

I removed code that it works and is not relevant. The view and projection matrices were calculated outside the class and it worked correctly. So, I decided to move inside the camera those two matrices because they belong to the camera.

Every update the view is recalculated with the camera position and rotation. If you print the matrix view at the end of the update function (where is calculated) the matrix show that is changed every frame. But if you print the matrix inside the function "getview" the matrix haven't beeing change and keeps the value from the default initialization.

How can the function "getView" return this matrix that has been updated and changed?

Aucun commentaire:

Enregistrer un commentaire