I have a class Matrix and another class Camera that I want to utilize the Matrix in. My Matrix class looks like this:
class Matrix4f {
public:
Matrix4f() {
this->setMatrix(EMPTY);
}
Matrix4f(Matrix4f &m2) {
this->setMatrix(m2.matrix);
}
static Matrix4f& Matrix4f::identity() {
Matrix4f& identity = Matrix4f() ;
identity.setMatrix(IDENTITY);
return identity;
}
void setMatrix(float f[4][4]) {
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
this->matrix[r][c] = f[r][c];
}
}
}
Matrix4f& operator=(const Matrix4f &m2) {
this->setMatrix(m2.matrix);
}
private:
float matrix[4][4];
static float EMPTY[4][4] = {
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 }
}; // initialize an empty array (all zeros);
static float IDENTIY[4][4] = {
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
}; // initialize a identity (array)
}
And I have this in my camera class:
class Camera {
public:
Camera() {
this->calculateProjection();
}
Matrix4f* getProjection() {
return this->projection;
}
private:
Matrix4f* projection;
Matrix4f* calculateProjection() {
this->projection = &Matrix4f::identity();
// modify this->projection...
return this->projection;
}
}
When I try to create a Camera instance and then get its projection I get something that looks like a corrupted object (the matrix is filled entirely to large negative numbers).
I am really confused what is causing my code to misbehave like this.
I am fairly certain it deals with a reference being automatically deleted by the compiler, and I think it deals with the identity matrix but it doesn't really make sense.
Shouldn't the identity matrix be copied into the projection matrix, so it wouldn't even matter if the identity matrix gets garbage collected?
I found that I can actually make this code work by either
making the identity matrix create a new Matrix4f() and returning that OR making getProjection() return the calculateProjection().
Problem is, I really don't want to do either of those.
I don't want Identity to construct a new Matrix4f because then I have to deal with destroying it, and I don't want getProjection() to call calculateProjection() because that method is expensive,
and should really only be called once as the Projection matrix never changes.