Spot The Bug!
Saturday, November 15th, 2008Lately I have been tinkering with a simple little 3D graphics program in my spare time. To support my various graphics and math projects I have put together a simple C++ math library including matrices, vectors, and the like. My matrices are stored in row-major order, like most matrices in C/C++ programs, but OpenGL uses column-major order. “Simple,” I think, “I just need to transpose my orientation matrices before passing them to OpenGL!”
But it just wouldn’t work. See if you can figure out why:
template<class T, int dim> class SquareMatrix {
...
/** Transpose the matrix. **/
void transpose() {
for (int r = 0; r < dim; r++) {
for (int c = 0; c < dim; c++) {
// Diagonal elements don't need transposing!
if (r == c) continue;
T tmp = getElem(r, c);
setElem(r, c, getElem(c, r));
setElem(c, r, tmp);
}
}
}
...
};
