I appear to have coded a class that travels backwards in time. Allow me to explain:
I have a function, `OrthogonalCamera::project()`, that sets a matrix to a certain value. I then print out the value of that matrix, as such.
- Code: Select all
cam.project();
std::cout << "My Projection Matrix: " << std::endl << ProjectionMatrix::getMatrix() << std::endl;
cam.project() pushes a matrix onto ProjectionMatrix's stack (I am using the std::stack<Mat4> container), and ProjectionMatrix::getMatrix() just returns the stack's top element. If I run just this code, I get the following output:
- Code: Select all
2 0 0 0
0 7.7957 0 0
0 0 -0.001 0
-1 -1 -0.998 1
But if I run the code with these to lines
after the `std::cout` call- Code: Select all
float *foo = new float[16];
Mat4 fooMatrix = foo;
Then I get this output:
- Code: Select all
2 0 0 0
0 -2 0 0
0 0 -0.001 0
-1 1 -0.998 1
Running it through the debugger gave me a
third value when I reach the print step:
- Code: Select all
-7.559 0 0 0
0 -2 0 0
0 0 -0.001 0
1 1 -0.998 1
My question is the following: what could I possibly be doing such that code that comes
after I print a value changes the value being printed?
Some of the functions I'm using:
- Code: Select all
static void load(Mat4 &set)
{
if(ProjectionMatrix::matrices.size() > 0)
ProjectionMatrix::matrices.pop();
ProjectionMatrix::matrices.push(set);
}
static Mat4 &getMatrix()
{
return ProjectionMatrix::matrices.top();
}
and
- Code: Select all
void OrthogonalCamera::project()
{
Mat4 orthProjection = { { 2.0f / (this->r - this->l), 0, 0, -1 * ((this->r + this->l) / (this->r - this->l)) },
{ 0, 2.0f / (this->t - this->b), 0, -1 * ((this->t + this->b) / (this->t - this->b)) },
{ 0, 0, -2.0f / (this->farClip - this->nearClip), -1 * ((this->farClip + this->nearClip) / (this->farClip - this->nearClip)) },
{ 0, 0, 0, 1 } }; //this is apparently the projection matrix for an orthographic projection.
orthProjection = orthProjection.transpose();
ProjectionMatrix::load(orthProjection);
}
Here is the function that initializes fooMatrix:
- Code: Select all
typedef Matrix<float, 4, 4> Mat4;
template<typename T, unsigned int rows, unsigned int cols>
Matrix<T, rows, cols>::Matrix(T *set)
{
this->matrixData = new T*[rows];
for (unsigned int i = 0; i < rows; i++)
{
this->matrixData[i] = new T[cols];
}
for (unsigned int i = 0; i < rows; i++)
{
for (unsigned int j = 0; j < cols; j++)
{
this->matrixData[i][j] = set[j * rows + i];
counter++;
}
}
}
This isn't just an output problem. I actually have to use the value of the matrix elsewhere, and it's value aligns with the described behaviours (whether or not I print it).