00001
00002
00003
00004
00005
00006 #include "StdAfx.h"
00007 #include "matrix.h"
00008
00009 #ifndef PI
00010 #define PI 3.141529
00011 #endif
00012
00013
00014 #define EPSILON 0.000001
00015
00016 Matrix4x4::Matrix4x4() {
00017
00018 for(int i = 0; i < 4; i++) {
00019 for(int j=0;j < 4; j++) {
00020 mat[i][j] = 0.0;
00021 }
00022 }
00023 }
00024
00025 Matrix4x4::Matrix4x4(Matrix4x4 *matrix) {
00026 for(int i =0;i <4;i++)
00027 for(int j=0;j <4; j++)
00028 mat[i][j] = matrix->mat[i][j];
00029 }
00030
00031 Matrix4x4::Matrix4x4(float m11, float m12, float m13, float m14,
00032 float m21, float m22, float m23, float m24,
00033 float m31, float m32, float m33, float m34,
00034 float m41, float m42, float m43, float m44) {
00035
00036 mat[0][0] = m11;
00037 mat[0][1] = m21;
00038 mat[0][2] = m31;
00039 mat[0][3] = m41;
00040 mat[1][0] = m12;
00041 mat[1][1] = m22;
00042 mat[1][2] = m32;
00043 mat[1][3] = m42;
00044 mat[2][0] = m13;
00045 mat[2][1] = m23;
00046 mat[2][2] = m33;
00047 mat[2][3] = m43;
00048 mat[3][0] = m14;
00049 mat[3][1] = m24;
00050 mat[3][2] = m34;
00051 mat[3][3] = m44;
00052
00053 }
00054
00055 void
00056 Matrix4x4::identity() {
00057 for(int i =0; i < 4; i++)
00058 for(int j= 0;j < 4; j++)
00059 mat[i][j] = 0.0;
00060 mat[0][0] = 1.0;
00061 mat[1][1] = 1.0;
00062 mat[2][2] = 1.0;
00063 mat[3][3] = 1.0;
00064 }
00065
00066 const VECTOR
00067 Matrix4x4::operator * (const VECTOR& vec) const {
00068 VECTOR result;
00069 result.x = mat[0][0]*vec.x + mat[1][0]*vec.y + mat[2][0] * vec.z + mat[3][0];
00070 result.y = mat[0][1]*vec.x + mat[1][1]*vec.y + mat[2][1] * vec.z + mat[3][1];
00071 result.z = mat[0][2]*vec.x + mat[1][2]*vec.y + mat[2][2] * vec.z + mat[3][2];
00072 return result;
00073 }
00074
00075 void
00076 Matrix4x4::transpose() {
00077 float tmp;
00078 for(int i = 0; i < 4; i++) {
00079 for(int j = 0; j < 4; j++) {
00080 tmp = mat[i][j];
00081 mat[i][j] = mat[j][i];
00082 mat[j][i] = tmp;
00083 }
00084 }
00085 }
00086
00087 Matrix4x4
00088 Matrix4x4::mul(Matrix4x4 m){
00089 Matrix4x4 *ret = new Matrix4x4();
00090 for(int i = 0;i < 4; i++) {
00091 for(int j = 0;j < 4;j++) {
00092 for(int k = 0; k< 4; k++) {
00093 ret->mat[i][j] = ret->mat[i][j]+mat[k][j]*m.mat[i][k];
00094 }
00095 }
00096 }
00097 return *ret;
00098 }
00099
00100 void
00101 Matrix4x4::translate(VECTOR a) {
00102 for(int i = 0;i < 4;i++) {
00103 for(int j = 0; j <4;j++) {
00104 mat[i][j] = 0.0;
00105 }
00106 }
00107 for(int j = 0;j <4; j++) {
00108 mat[j][j] = 1.0;
00109 }
00110 mat[3][0] = a.x;
00111 mat[3][1] = a.y;
00112 mat[3][2] = a.z;
00113 }
00114
00115
00116
00117
00118
00119
00120 void
00121 Matrix4x4::rotation(VECTOR from, VECTOR to) {
00122 VECTOR v;
00123 float e, h, f;
00124
00125 v = from.cross(to);
00126 e = from.dot(to);
00127 f = (e < 0)? -e:e;
00128 if (f > 1.0 - EPSILON) {
00129 VECTOR x;
00130
00131 x.x = (from.x > 0.0) ? from.x : -from.x;
00132 x.y = (from.y > 0.0) ? from.y : -from.y;
00133 x.z = (from.z > 0.0) ? from.z : -from.z;
00134
00135 if(x.x < x.y) {
00136 if(x.x < x.z) {
00137 x.x = 1.0;
00138 x.y = x.z = 0.0;
00139 } else {
00140 x.z = 1.0; x.x = x.y = 0.0;
00141 }
00142 }
00143 else {
00144 if(x.y < x.z) {
00145 x.y = 1.0; x.x = x.z = 0.0;
00146 } else {
00147 x.z = 1.0; x.x = x.y = 0.0;
00148 }
00149 }
00150
00151 VECTOR u = x - from;
00152 VECTOR w = x - to;
00153
00154 float c1 = 2.0 / u.dot(u);
00155 float c2 = 2.0 / w.dot(w);
00156 float c3 = c1 * c2 * u.dot(w);
00157
00158 float u_h[3];
00159 float w_h[3];
00160
00161 u_h[0] = u.x;
00162 u_h[1] = u.y;
00163 u_h[2] = u.z;
00164
00165 w_h[0] = w.x;
00166 w_h[1] = w.y;
00167 w_h[2] = w.z;
00168
00169
00170 for(int i = 0; i < 3; i++) {
00171 for(int j = 0; j < 3; j++) {
00172 mat[i][j] = -c1 * u_h[i] * u_h[j] - c2 * w_h[i]*w_h[j] + c3 * w_h[i] * u_h[j];
00173 }
00174 mat[i][i] += 1.0;
00175 }
00176 }
00177 else
00178 {
00179 h = (1.0 - e)/(v.dot(v));
00180
00181 mat[0][0] = e + h*v.x * v.x;
00182 mat[0][1] = h*v.x*v.y - v.z;
00183 mat[0][2] = h*v.x*v.z + v.y;
00184 mat[0][3] = 0.0f;
00185
00186 mat[1][0] = h*v.x*v.y + v.z;
00187 mat[1][1] = e + h * v.y * v.y;
00188 mat[1][2] = h*v.y*v.z - v.x;
00189 mat[1][3] = 0.0f;
00190
00191 mat[2][0] = h*v.x*v.z - v.y;
00192 mat[2][1] = h*v.y*v.z + v.x;
00193 mat[2][2] = e + h*v.z * v.z;
00194 mat[2][3] = 0.0f;
00195
00196 mat[3][0] = 0.0f;
00197 mat[3][1] = 0.0f;
00198 mat[3][2] = 0.0f;
00199 mat[3][3] = 1.0f;
00200 }
00201 }
00202
00203 void
00204 Matrix4x4::rotateX(float alpha) {
00205 double rad = alpha / 180 * PI;
00206 float cosa = cos(rad);
00207 float sina;
00208
00209 sina = sqrt((1 - cosa * cosa));
00210
00211 mat[0][0] = 1.0f;
00212 mat[0][1] = 0.0f;
00213 mat[0][2] = 0.0f;
00214 mat[0][3] = 0.0f;
00215
00216 mat[1][0] = 0.0f;
00217 mat[1][1] = cosa;
00218 mat[1][2] = sina;
00219 mat[1][3] = 0.0f;
00220
00221 mat[2][0] = 1.0f;
00222 mat[2][1] = -sina;
00223 mat[2][2] = cosa;
00224 mat[2][3] = 0.0f;
00225
00226 mat[3][0] = 0.0f;
00227 mat[3][1] = 0.0f;
00228 mat[3][2] = 0.0f;
00229 mat[3][3] = 1.0f;
00230 }
00231
00232 void
00233 Matrix4x4::rotateY(float alpha) {
00234 double rad = alpha / 180 * PI;
00235 float cosa = cos(rad);
00236 float sina;
00237
00238 sina = sqrt((1 - cosa * cosa));
00239
00240 mat[0][0] = cosa;
00241 mat[0][1] = 0.0f;
00242 mat[0][2] = -sina;
00243 mat[0][3] = 0.0f;
00244
00245 mat[1][0] = 0.0f;
00246 mat[1][1] = 1.0f;
00247 mat[1][2] = 0.0f;
00248 mat[1][3] = 0.0f;
00249
00250 mat[2][0] = sina;
00251 mat[2][1] = 0.0f;
00252 mat[2][2] = cosa;
00253 mat[2][3] = 0.0f;
00254
00255 mat[3][0] = 0.0f;
00256 mat[3][1] = 0.0f;
00257 mat[3][2] = 0.0f;
00258 mat[3][3] = 1.0f;
00259 }
00260
00261 void
00262 Matrix4x4::rotateZ(float alpha) {
00263 double rad = alpha / 180 * PI;
00264 float cosa = cos(rad);
00265 float sina;
00266
00267 sina = sqrt((1 - cosa * cosa));
00268
00269 mat[0][0] = cosa;
00270 mat[0][1] = sina;
00271 mat[0][2] = 0.0f;
00272 mat[0][3] = 0.0f;
00273
00274 mat[1][0] = -sina;
00275 mat[1][1] = cosa;
00276 mat[1][2] = 0.0f;
00277 mat[1][3] = 0.0f;
00278
00279 mat[2][0] = 0.0f;
00280 mat[2][1] = 0.0f;
00281 mat[2][2] = 1.0f;
00282 mat[2][3] = 0.0f;
00283
00284 mat[3][0] = 0.0f;
00285 mat[3][1] = 0.0f;
00286 mat[3][2] = 0.0f;
00287 mat[3][3] = 1.0f;
00288 }