// // matrix.cc // // Handle matrices // // Copyright (C) J. Belson 1998.01.21 // #include #include "matrix.h" /** * Constructor * Create a matrix using data in the form used by OpenGL, which * is unfortunately different to our format: * * 0 4 8 12 * 1 5 9 13 * 2 6 10 14 * 3 7 11 15 */ matrix::matrix(float *m) { for (int i=0; i= 0 && index <= NUM_ELEMENTS) { return elem[index]; } else { std::cerr << "operator[] index out of range!" << std::endl; return elem[0]; } } /** * Reference an element of this matrix */ const float &matrix::operator[](int index) const { if (index >= 0 && index <= NUM_ELEMENTS) { return elem[index]; } else { std::cerr << "operator[] index out of range!" << std::endl; return elem[0]; } } /** * Make this matrix a rotation matrix */ void matrix::make_rotation(float a, float b, float c) { make_identity(); if (a != 0.0) { matrix m(IDENTITY); m[5] = cos(a); m[6] = -sin(a); m[9] = sin(a); m[10] = cos(a); *this = m * *this; } if (b != 0.0) { matrix m(IDENTITY); m[0] = cos(b); m[2] = sin(b); m[8] = -sin(b); m[10] = cos(b); *this = m * *this; } if (c != 0.0) { matrix m(IDENTITY); m[0] = cos(c); m[1] = -sin(c); m[4] = sin(c); m[5] = cos(c); *this = m * *this; } } /** * Make this matrix a translation matrix */ void matrix::make_translation(float x, float y, float z) { make_identity(); elem[3] = x; elem[7] = y; elem[11] = z; } /** * Make this matrix a perspective projection matrix */ void matrix::make_projection(float depth) { make_identity(); elem[14] = -1.0/depth; elem[15] = 0.0; } /** * Make this matrix a scaling matrix */ void matrix::make_scaling(float a, float b, float c) { make_identity(); elem[0] = a; elem[5] = b; elem[10] = c; } /** * Transpose this matrix */ matrix matrix::transpose(void) { matrix m; for (int i=0; i