kick
|
00001 // 00002 // math.cpp 00003 // KickCPP 00004 // 00005 // Created by morten on 8/13/13. 00006 // Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved. 00007 // 00008 00009 #include "kick/math/glm_ext.h" 00010 00011 00012 00013 using namespace glm; 00014 00015 namespace kick { 00016 glm::mat4 TRS(glm::vec3 translate, glm::quat rotateQuat, glm::vec3 scale){ 00017 return glm::translate(translate) * mat4_cast(rotateQuat) * glm::scale(scale); 00018 /*glm::mat4 res; 00019 float *out = &(res[0][0]); 00020 // Quaternion math 00021 float scaleX = scale[0], scaleY = scale[1], scaleZ = scale[2], 00022 x = rotateQuat[1], y = rotateQuat[2], z = rotateQuat[3], w = rotateQuat[0], 00023 x2 = x + x, 00024 y2 = y + y, 00025 z2 = z + z, 00026 00027 xx = x * x2, 00028 xy = x * y2, 00029 xz = x * z2, 00030 yy = y * y2, 00031 yz = y * z2, 00032 zz = z * z2, 00033 wx = w * x2, 00034 wy = w * y2, 00035 wz = w * z2; 00036 00037 out[0] = (1 - (yy + zz)) * scaleX; 00038 out[1] = (xy + wz) * scaleX; 00039 out[2] = (xz - wy) * scaleX; 00040 out[3] = 0; 00041 out[4] = (xy - wz) * scaleY; 00042 out[5] = (1 - (xx + zz)) * scaleY; 00043 out[6] = (yz + wx) * scaleY; 00044 out[7] = 0; 00045 out[8] = (xz + wy) * scaleZ; 00046 out[9] = (yz - wx) * scaleZ; 00047 out[10] = (1 - (xx + yy)) * scaleZ; 00048 out[11] = 0; 00049 out[12] = translate[0]; 00050 out[13] = translate[1]; 00051 out[14] = translate[2]; 00052 out[15] = 1; 00053 00054 return res; */ 00055 } 00056 00057 glm::mat4 TRSInverse(glm::vec3 translate, glm::quat rotateQuat, glm::vec3 scale){ 00058 return inverse(TRS(translate, rotateQuat, scale)); 00059 /* 00060 // Quaternion math 00061 float scaleX = scale.x, scaleY = scale.y, scaleZ = scale.z, 00062 x = rotateQuat.x, y = rotateQuat.y, z = rotateQuat.z, w = rotateQuat.w, 00063 x2 = x + x, 00064 y2 = y + y, 00065 z2 = z + z, 00066 00067 xx = x * x2, 00068 xy = x * y2, 00069 xz = x * z2, 00070 yy = y * y2, 00071 yz = y * z2, 00072 zz = z * z2, 00073 wx = w * x2, 00074 wy = w * y2, 00075 wz = w * z2, 00076 00077 // compute trs 00078 a00 = (1 - (yy + zz)) * scaleX, 00079 a01 = (xy + wz) * scaleX, 00080 a02 = (xz - wy) * scaleX, 00081 a10 = (xy - wz) * scaleY, 00082 a11 = (1 - (xx + zz)) * scaleY, 00083 a12 = (yz + wx) * scaleY, 00084 a20 = (xz + wy) * scaleZ, 00085 a21 = (yz - wx) * scaleZ, 00086 a22 = (1 - (xx + yy)) * scaleZ, 00087 a30 = translate[0], 00088 a31 = translate[1], 00089 a32 = translate[2], 00090 a33 = 1, 00091 // compute inverse 00092 b00 = a00 * a11 - a01 * a10, 00093 b01 = a00 * a12 - a02 * a10, 00094 b03 = a01 * a12 - a02 * a11, 00095 b06 = a20 * a31 - a21 * a30, 00096 b07 = a20 * a32 - a22 * a30, 00097 b08 = a20 * a33, 00098 b09 = a21 * a32 - a22 * a31, 00099 b10 = a21 * a33, 00100 b11 = a22 * a33, 00101 00102 d = (b00 * b11 - b01 * b10 + b03 * b08), 00103 invDet; 00104 00105 mat4 res; 00106 float *out = &(res[0][0]); 00107 // Calculate the determinant 00108 if (!d) { return res; } 00109 invDet = 1 / d; 00110 00111 out[0] = (a11 * b11 - a12 * b10) * invDet; 00112 out[1] = (-a01 * b11 + a02 * b10) * invDet; 00113 out[2] = (a33 * b03) * invDet; 00114 out[3] = 0; 00115 out[4] = (-a10 * b11 + a12 * b08) * invDet; 00116 out[5] = (a00 * b11 - a02 * b08) * invDet; 00117 out[6] = (-a33 * b01) * invDet; 00118 out[7] = 0; 00119 out[8] = (a10 * b10 - a11 * b08) * invDet; 00120 out[9] = (-a00 * b10 + a01 * b08) * invDet; 00121 out[10] = (a33 * b00) * invDet; 00122 out[11] = 0; 00123 out[12] = (-a10 * b09 + a11 * b07 - a12 * b06) * invDet; 00124 out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * invDet; 00125 out[14] = (-a30 * b03 + a31 * b01 - a32 * b00) * invDet; 00126 out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * invDet; 00127 00128 return res; */ 00129 } 00130 00131 glm::quat lookAt(glm::vec3 eyePosition, glm::vec3 lookAtPos, glm::vec3 up){ 00132 return quat_cast(glm::lookAt(eyePosition, lookAtPos, up)); 00133 } 00134 }