// Class automatically generated by Dev-C++ New Class wizard #include #include #include "myMatrix.h" // class's header file /* From OpenGL Manual (pg 103 of Programers 1.1) If you are programming in C and declare a matrix as m[4][4] then the element m[i][j] is in the ith column and jth row of the OpenGL transformation matrix. This is the reverse of the stadard C convention in which m[i][j] is in row i and column j. To avoid confusion you should declare your matrices as m[16]. So here is how it goes: m1 m5 m9 m13 m2 m6 m10 m14 m3 m7 m11 m15 m4 m8 m12 m16 OR Zero based: _ _ | m0 m4 m8 m12 | | | | m1 m5 m9 m13 | | | | m2 m6 m10 m14 | | | | m3 m7 m11 m15 | - - */ extern int DEBUG_PRINT; // class constructor MyMat4f::MyMat4f() { } MyMat4f::MyMat4f(const MyMat4f &copied) { this->data[0] = copied.data[0]; this->data[1] = copied.data[1]; this->data[2] = copied.data[2]; this->data[3] = copied.data[3]; this->data[4] = copied.data[4]; this->data[5] = copied.data[5]; this->data[6] = copied.data[6]; this->data[7] = copied.data[7]; this->data[8] = copied.data[8]; this->data[9] = copied.data[9]; this->data[10] = copied.data[10]; this->data[11] = copied.data[11]; this->data[12] = copied.data[12]; this->data[13] = copied.data[13]; this->data[14] = copied.data[14]; this->data[15] = copied.data[15]; } // class destructor MyMat4f::~MyMat4f() { } void MyMat4f::print(char *string){printf("%s\n", string); print();} void MyMat4f::print(){ if ( DEBUG_PRINT == 0) return; printf("\t%.2f %.2f %.2f %.2f\n", this->data[0], this->data[4], this->data[8], this->data[12]); printf("\t%.2f %.2f %.2f %.2f\n", this->data[1], this->data[5], this->data[9], this->data[13]); printf("\t%.2f %.2f %.2f %.2f\n", this->data[2], this->data[6], this->data[10], this->data[14]); printf("\t%.2f %.2f %.2f %.2f\n\n", this->data[3], this->data[7], this->data[11], this->data[15]); } void MyMat4f::identity() { this->data[0] = 1; this->data[1] = 0; this->data[2] = 0; this->data[3] = 0; this->data[4] = 0; this->data[5] = 1; this->data[6] = 0; this->data[7] = 0; this->data[8] = 0; this->data[9] = 0; this->data[10] = 1; this->data[11] = 0; this->data[12] = 0; this->data[13] = 0; this->data[14] = 0; this->data[15] = 1; } extern void load4DMatrix(MyMat4f &loadMe); void MyMat4f::turnLeft(float angle){ printf("NEED TO IMPLEMENT: turnLeft\n"); } void MyMat4f::turnRight(float angle){ printf("NEED TO IMPLEMENT: turnRight\n"); } void MyMat4f::rot_x(float theta) { printf("NEED TO IMPLEMENT: Rotate X\n"); this->data[0] = 1; this->data[1] = 0; this->data[2] = 0; this->data[3] = 0; this->data[4] = 0; this->data[5] = 1; this->data[6] = 0; this->data[7] = 0; this->data[8] = 0; this->data[9] = 0; this->data[10] = 1; this->data[11] = 0; this->data[12] = 0; this->data[13] = 0; this->data[14] = 0; this->data[15] = 1; } void MyMat4f::rot_y(float theta) { printf("NEED TO IMPLEMENT Rotate Y\n"); this->data[0] = 1; this->data[1] = 0; this->data[2] = 0; this->data[3] = 0; this->data[4] = 0; this->data[5] = 1; this->data[6] = 0; this->data[7] = 0; this->data[8] = 0; this->data[9] = 0; this->data[10] = 1; this->data[11] = 0; this->data[12] = 0; this->data[13] = 0; this->data[14] = 0; this->data[15] = 1; } void MyMat4f::rot_z(float theta) { if (DEBUG_PRINT) printf("Rotate z: %.2f radians or %.2f degrees\n", theta, theta*180.0/M_PI); printf("NEED TO IMPLEMENT Rotate Z\n"); this->data[0] = 1; this->data[1] = 0; this->data[2] = 0; this->data[3] = 0; this->data[4] = 0; this->data[5] = 1; this->data[6] = 0; this->data[7] = 0; this->data[8] = 0; this->data[9] = 0; this->data[10] = 1; this->data[11] = 0; this->data[12] = 0; this->data[13] = 0; this->data[14] = 0; this->data[15] = 1; } void MyMat4f::setTranslation(const float &a, const float &b, const float &c, const float&d){ printf("Need to IMPLEMENT setTranslation\n"); } void MyMat4f::setTranslation(const MyVec4f &v) { this->data[12] = v.data[0]; this->data[13] = v.data[1]; this->data[14] = v.data[2]; this->data[15] = v.data[3]; } MyVec4f MyMat4f::getTranslation() { MyVec4f v; v.data[0] = this->data[12]; v.data[1] = this->data[13]; v.data[2] = this->data[14]; v.data[3] = this->data[15]; return v; } void MyMat4f::transpose() { float temp; temp = this->data[1]; this->data[1] = this->data[4]; this->data[4] = temp; temp = this->data[2]; this->data[2] = this->data[8]; this->data[8] = temp; temp = this->data[3]; this->data[3] = this->data[12]; this->data[12] = temp; temp = this->data[6]; this->data[6] = this->data[9]; this->data[9] = temp; temp = this->data[7]; this->data[7] = this->data[13]; this->data[13] = temp; temp = this->data[11]; this->data[11] = this->data[14]; this->data[14] = temp; } void MyMat4f::zero() { this->data[0] = 0; this->data[1] = 0; this->data[2] = 0; this->data[3] = 0; this->data[4] = 0; this->data[5] = 0; this->data[6] = 0; this->data[7] = 0; this->data[8] = 0; this->data[9] = 0; this->data[10] = 0; this->data[11] = 0; this->data[12] = 0; this->data[13] = 0; this->data[14] = 0; this->data[15] = 0; } void MyMat4f::column1(const MyVec4f &v) { this->data[0] = v.data[0]; this->data[1] = v.data[1]; this->data[2] = v.data[2]; this->data[3] = v.data[3]; } void MyMat4f::column2(const MyVec4f &v) { this->data[4] = v.data[0]; this->data[5] = v.data[1]; this->data[6] = v.data[2]; this->data[7] = v.data[3]; } void MyMat4f::column3(const MyVec4f &v) { this->data[8] = v.data[0]; this->data[9] = v.data[1]; this->data[10] = v.data[2]; this->data[11] = v.data[3]; } void MyMat4f::column4(const MyVec4f &v) { this->data[12] = v.data[0]; this->data[13] = v.data[1]; this->data[14] = v.data[2]; this->data[15] = v.data[3]; } MyVec4f MyMat4f::column1() { MyVec4f v; v.data[0] = this->data[0]; v.data[1] = this->data[1]; v.data[2] = this->data[2]; v.data[3] = this->data[3]; return v; } MyVec4f MyMat4f::column2() { MyVec4f v; v.data[0] = this->data[4]; v.data[1] = this->data[5]; v.data[2] = this->data[6]; v.data[3] = this->data[7]; return v; } MyVec4f MyMat4f::column3() { MyVec4f v; v.data[0] = this->data[8]; v.data[1] = this->data[9]; v.data[2] = this->data[10]; v.data[3] = this->data[11]; return v; } MyVec4f MyMat4f::column4() { MyVec4f v; v.data[0] = this->data[12]; v.data[1] = this->data[13]; v.data[2] = this->data[14]; v.data[3] = this->data[15]; return v; } void MyMat4f::row1(const MyVec4f &v) { this->data[0] = v.data[0]; this->data[4] = v.data[1]; this->data[8] = v.data[2]; this->data[12] = v.data[3]; } void MyMat4f::row2(const MyVec4f &v) { this->data[1] = v.data[0]; this->data[5] = v.data[1]; this->data[9] = v.data[2]; this->data[13] = v.data[3]; } void MyMat4f::row3(const MyVec4f &v) { this->data[2] = v.data[0]; this->data[6] = v.data[1]; this->data[10] = v.data[2]; this->data[14] = v.data[3]; } void MyMat4f::row4(const MyVec4f &v) { this->data[3] = v.data[0]; this->data[7] = v.data[1]; this->data[11] = v.data[2]; this->data[15] = v.data[3]; } MyVec4f MyMat4f::row1() { MyVec4f v; v.data[0] = this->data[0]; v.data[1] = this->data[4]; v.data[2] = this->data[8]; v.data[3] = this->data[12]; return v; } MyVec4f MyMat4f::row2() { MyVec4f v; v.data[0] = this->data[1]; v.data[1] = this->data[5]; v.data[2] = this->data[9]; v.data[3] = this->data[13]; return v; } MyVec4f MyMat4f::row3() { MyVec4f v; v.data[0] = this->data[2]; v.data[1] = this->data[6]; v.data[2] = this->data[10]; v.data[3] = this->data[14]; return v; } float MyMat4f::operator() (const unsigned char &i, const unsigned char &j) { return this->data[i+4*j-5]; } MyMat4f MyMat4f::operator=(const MyMat4f &other) { if(&other != this) { this->data[0] = other.data[0]; this->data[1] = other.data[1]; this->data[2] = other.data[2]; this->data[3] = other.data[3]; this->data[4] = other.data[4]; this->data[5] = other.data[5]; this->data[6] = other.data[6]; this->data[7] = other.data[7]; this->data[8] = other.data[8]; this->data[9] = other.data[9]; this->data[10] = other.data[10]; this->data[11] = other.data[11]; this->data[12] = other.data[12]; this->data[13] = other.data[13]; this->data[14] = other.data[14]; this->data[15] = other.data[15]; } return *this; } bool MyMat4f::operator==(const MyMat4f &other) const { if(this->data[0] != other.data[0]) return false; if(this->data[1] != other.data[1]) return false; if(this->data[2] != other.data[2]) return false; if(this->data[3] != other.data[3]) return false; if(this->data[4] != other.data[4]) return false; if(this->data[5] != other.data[5]) return false; if(this->data[6] != other.data[6]) return false; if(this->data[7] != other.data[7]) return false; if(this->data[8] != other.data[8]) return false; if(this->data[9] != other.data[9]) return false; if(this->data[10] != other.data[10]) return false; if(this->data[11] != other.data[11]) return false; if(this->data[12] != other.data[12]) return false; if(this->data[13] != other.data[13]) return false; if(this->data[14] != other.data[14]) return false; if(this->data[15] != other.data[15]) return false; return true; } bool MyMat4f::operator!=(const MyMat4f &other) const { if(this->data[0] != other.data[0]) return true; if(this->data[1] != other.data[1]) return true; if(this->data[2] != other.data[2]) return true; if(this->data[3] != other.data[3]) return true; if(this->data[4] != other.data[4]) return true; if(this->data[5] != other.data[5]) return true; if(this->data[6] != other.data[6]) return true; if(this->data[7] != other.data[7]) return true; if(this->data[8] != other.data[8]) return true; if(this->data[9] != other.data[9]) return true; if(this->data[10] != other.data[10]) return true; if(this->data[11] != other.data[11]) return true; if(this->data[12] != other.data[12]) return true; if(this->data[13] != other.data[13]) return true; if(this->data[14] != other.data[14]) return true; if(this->data[15] != other.data[15]) return true; return false; } MyMat4f MyMat4f::operator*=(const float &k) { this->data[0] *= k; this->data[1] *= k; this->data[2] *= k; this->data[3] *= k; this->data[4] *= k; this->data[5] *= k; this->data[6] *= k; this->data[7] *= k; this->data[8] *= k; this->data[9] *= k; this->data[10] *= k; this->data[11] *= k; this->data[12] *= k; this->data[13] *= k; this->data[14] *= k; this->data[15] *= k; return *this; } MyMat4f operator*(const MyMat4f &M, const float &K) { MyMat4f temp; temp.data[0] = M.data[ 0] * K; temp.data[1] = M.data[ 1] * K; temp.data[2] = M.data[ 2] * K; temp.data[3] = M.data[ 3] * K; temp.data[4] = M.data[ 4] * K; temp.data[5] = M.data[ 5] * K; temp.data[6] = M.data[ 6] * K; temp.data[7] = M.data[ 7] * K; temp.data[8] = M.data[ 8] * K; temp.data[9] = M.data[ 9] * K; temp.data[10] = M.data[10] * K; temp.data[11] = M.data[11] * K; temp.data[12] = M.data[12] * K; temp.data[13] = M.data[13] * K; temp.data[14] = M.data[14] * K; temp.data[15] = M.data[15] * K; return temp; } MyMat4f operator*(const float &K, const MyMat4f &M) { MyMat4f temp; temp.data[0] = M.data[ 0] * K; temp.data[1] = M.data[ 1] * K; temp.data[2] = M.data[ 2] * K; temp.data[3] = M.data[ 3] * K; temp.data[4] = M.data[ 4] * K; temp.data[5] = M.data[ 5] * K; temp.data[6] = M.data[ 6] * K; temp.data[7] = M.data[ 7] * K; temp.data[8] = M.data[ 8] * K; temp.data[9] = M.data[ 9] * K; temp.data[10] = M.data[10] * K; temp.data[11] = M.data[11] * K; temp.data[12] = M.data[12] * K; temp.data[13] = M.data[13] * K; temp.data[14] = M.data[14] * K; temp.data[15] = M.data[15] * K; return temp; } MyMat4f operator*(const MyMat4f &lefthm, const MyMat4f &righthm) { //NOTE: 10.07.03: AAG: this code lhm and rhm are backwards... //code was orignally written for row matrix order //hence in my solution to project 2, // all of my matrix ops are M = r * l to reverse to column order //Here I am breaking that code by correcting the problem MyMat4f temp; // collumn 1 temp.data[ 0] = lefthm.data[0]*righthm.data[ 0] + lefthm.data[4]*righthm.data[ 1] + lefthm.data[ 8]*righthm.data[2] + lefthm.data[12]*righthm.data[3]; temp.data[ 1] = lefthm.data[1]*righthm.data[ 0] + lefthm.data[5]*righthm.data[ 1] + lefthm.data[ 9]*righthm.data[2] + lefthm.data[13]*righthm.data[3]; temp.data[ 2] = lefthm.data[2]*righthm.data[ 0] + lefthm.data[6]*righthm.data[ 1] + lefthm.data[10]*righthm.data[2] + lefthm.data[14]*righthm.data[3]; temp.data[ 3] = lefthm.data[3]*righthm.data[ 0] + lefthm.data[7]*righthm.data[ 1] + lefthm.data[11]*righthm.data[2] + lefthm.data[15]*righthm.data[3]; // collumn 2 temp.data[ 4] = lefthm.data[0]*righthm.data[ 4] + lefthm.data[4]*righthm.data[ 5] + lefthm.data[ 8]*righthm.data[6] + lefthm.data[12]*righthm.data[ 7]; temp.data[ 5] = lefthm.data[1]*righthm.data[ 4] + lefthm.data[5]*righthm.data[ 5] + lefthm.data[ 9]*righthm.data[6] + lefthm.data[13]*righthm.data[ 7]; temp.data[ 6] = lefthm.data[2]*righthm.data[ 4] + lefthm.data[6]*righthm.data[ 5] + lefthm.data[10]*righthm.data[6] + lefthm.data[14]*righthm.data[ 7]; temp.data[ 7] = lefthm.data[3]*righthm.data[ 4] + lefthm.data[7]*righthm.data[ 5] + lefthm.data[11]*righthm.data[6] + lefthm.data[15]*righthm.data[ 7]; // collumn 3 temp.data[ 8] = lefthm.data[0]*righthm.data[ 8] + lefthm.data[4]*righthm.data[ 9] + lefthm.data[ 8]*righthm.data[10] + lefthm.data[12]*righthm.data[11]; temp.data[ 9] = lefthm.data[1]*righthm.data[ 8] + lefthm.data[5]*righthm.data[ 9] + lefthm.data[ 9]*righthm.data[10] + lefthm.data[13]*righthm.data[11]; temp.data[10] = lefthm.data[2]*righthm.data[ 8] + lefthm.data[6]*righthm.data[ 9] + lefthm.data[10]*righthm.data[10] + lefthm.data[14]*righthm.data[11]; temp.data[11] = lefthm.data[3]*righthm.data[ 8] + lefthm.data[7]*righthm.data[ 9] + lefthm.data[11]*righthm.data[10] + lefthm.data[15]*righthm.data[11]; // collumn 4 temp.data[12] = lefthm.data[0]*righthm.data[12] + lefthm.data[4]*righthm.data[13] + lefthm.data[ 8]*righthm.data[14] + lefthm.data[12]*righthm.data[15]; temp.data[13] = lefthm.data[1]*righthm.data[12] + lefthm.data[5]*righthm.data[13] + lefthm.data[ 9]*righthm.data[14] + lefthm.data[13]*righthm.data[15]; temp.data[14] = lefthm.data[2]*righthm.data[12] + lefthm.data[6]*righthm.data[13] + lefthm.data[10]*righthm.data[14] + lefthm.data[14]*righthm.data[15]; temp.data[15] = lefthm.data[3]*righthm.data[12] + lefthm.data[7]*righthm.data[13] + lefthm.data[11]*righthm.data[14] + lefthm.data[15]*righthm.data[15]; return temp; }