28 #define PI 3.1415926535897932384626433832795f 29 #define TO_DEGREE 57.2958 67 Vec2(
const float fX,
const float fY)
105 ret.
x_ = x_ + rhs.
x_;
106 ret.
y_ = y_ + rhs.
y_;
113 ret.
x_ = x_ - rhs.
x_;
114 ret.
y_ = y_ - rhs.
y_;
149 return Vec2(rhs) *= -1;
155 ret.
x_ = lhs * rhs.
x_;
156 ret.
y_ = lhs * rhs.
y_;
163 ret.
x_ = lhs / rhs.
x_;
164 ret.
y_ = lhs / rhs.
y_;
171 return (x_ < v.
x_) || ((!(v.
x_ <
x_)) && (y_ < v.
y_));
208 if (x_ != rhs.
x_ || y_ != rhs.
y_)
223 return sqrtf(x_ * x_ + y_ * y_);
236 return x_ * rhs.
x_ + y_ * rhs.
y_;
241 return sqrtf(((x_ - rhs.
x_) * (x_ - rhs.
x_)) + ((y_ - rhs.
y_) * (y_ - rhs.
y_)));
246 if (isnan(x_) || isnan(y_))
259 printf(
"Vec2 %f %f", x_, y_);
282 Vec3(
const float fX,
const float fY,
const float fZ)
313 return (x_ < v.
x_) && (y_ < v.
y_) && (z_ < v.
z_);
320 ret.
x_ = x_ * rhs.
x_;
321 ret.
y_ = y_ * rhs.
y_;
322 ret.
z_ = z_ * rhs.
z_;
329 ret.
x_ = x_ / rhs.
x_;
330 ret.
y_ = y_ / rhs.
y_;
331 ret.
z_ = z_ / rhs.
z_;
338 ret.
x_ = x_ + rhs.
x_;
339 ret.
y_ = y_ + rhs.
y_;
340 ret.
z_ = z_ + rhs.
z_;
347 ret.
x_ = x_ - rhs.
x_;
348 ret.
y_ = y_ - rhs.
y_;
349 ret.
z_ = z_ - rhs.
z_;
388 return Vec3(rhs) *= -1;
394 ret.
x_ = lhs * rhs.
x_;
395 ret.
y_ = lhs * rhs.
y_;
396 ret.
z_ = lhs * rhs.
z_;
403 ret.
x_ = lhs / rhs.
x_;
404 ret.
y_ = lhs / rhs.
y_;
405 ret.
z_ = lhs / rhs.
z_;
447 if (x_ != rhs.
x_ || y_ != rhs.
y_ || z_ != rhs.
z_)
462 return sqrtf(x_ * x_ + y_ * y_ + z_ * z_);
476 return x_ * rhs.
x_ + y_ * rhs.
y_ + z_ * rhs.
z_;
481 return sqrtf(((x_ - rhs.
x_) * (x_ - rhs.
x_)) + ((y_ - rhs.
y_) * (y_ - rhs.
y_)) + ((z_ - rhs.
z_) * (z_ - rhs.
z_)));
487 ret.
x_ = y_ * rhs.
z_ - z_ * rhs.
y_;
488 ret.
y_ = z_ * rhs.
x_ - x_ * rhs.
z_;
489 ret.
z_ = x_ * rhs.
y_ - y_ * rhs.
x_;
495 if (isnan(x_) || isnan(y_) || isnan(z_))
500 void Value(
float& fX,
float& fY,
float& fZ)
509 printf(
"Vec3 %f %f %f\n", x_, y_, z_);
529 x_ = y_ = z_ = w_ = 0.f;
532 Vec4(
const float fX,
const float fY,
const float fZ,
const float fW)
568 ret.
x_ = x_ * rhs.
x_;
569 ret.
y_ = y_ * rhs.
y_;
570 ret.
z_ = z_ * rhs.
z_;
571 ret.
w_ = z_ * rhs.
w_;
578 ret.
x_ = x_ / rhs.
x_;
579 ret.
y_ = y_ / rhs.
y_;
580 ret.
z_ = z_ / rhs.
z_;
581 ret.
w_ = z_ / rhs.
w_;
588 ret.
x_ = x_ + rhs.
x_;
589 ret.
y_ = y_ + rhs.
y_;
590 ret.
z_ = z_ + rhs.
z_;
591 ret.
w_ = z_ + rhs.
w_;
598 ret.
x_ = x_ - rhs.
x_;
599 ret.
y_ = y_ - rhs.
y_;
600 ret.
z_ = z_ - rhs.
z_;
601 ret.
w_ = z_ - rhs.
w_;
644 return Vec4(rhs) *= -1;
650 ret.
x_ = lhs * rhs.
x_;
651 ret.
y_ = lhs * rhs.
y_;
652 ret.
z_ = lhs * rhs.
z_;
653 ret.
w_ = lhs * rhs.
w_;
660 ret.
x_ = lhs / rhs.
x_;
661 ret.
y_ = lhs / rhs.
y_;
662 ret.
z_ = lhs / rhs.
z_;
663 ret.
w_ = lhs / rhs.
w_;
709 if (x_ != rhs.
x_ || y_ != rhs.
y_ || z_ != rhs.
z_ || w_ != rhs.
w_)
724 return sqrtf(x_ * x_ + y_ * y_ + z_ * z_ + w_ * w_);
739 return x_ * rhs.
x_ + y_ * rhs.
y_ + z_ * rhs.
z_;
745 ret.
x_ = y_ * rhs.
z_ - z_ * rhs.
y_;
746 ret.
y_ = z_ * rhs.
x_ - x_ * rhs.
z_;
747 ret.
z_ = x_ * rhs.
y_ - y_ * rhs.
x_;
753 if (isnan(x_) || isnan(y_) || isnan(z_) || isnan(w_))
758 void Value(
float& fX,
float& fY,
float& fZ,
float& fW)
780 for (
int i = 0; i < 9; i++) {
786 for (
int i = 0; i < 9; i++) {
794 ret.f_[0] = f_[0] * rhs.f_[0] + f_[3] * rhs.f_[1] + f_[6] * rhs.f_[2];
795 ret.f_[1] = f_[1] * rhs.f_[0] + f_[4] * rhs.f_[1] + f_[7] * rhs.f_[2];
796 ret.f_[2] = f_[2] * rhs.f_[0] + f_[5] * rhs.f_[1] + f_[8] * rhs.f_[2];
798 ret.f_[3] = f_[0] * rhs.f_[3] + f_[3] * rhs.f_[4] + f_[6] * rhs.f_[5];
799 ret.f_[4] = f_[1] * rhs.f_[3] + f_[4] * rhs.f_[4] + f_[7] * rhs.f_[5];
800 ret.f_[5] = f_[2] * rhs.f_[3] + f_[5] * rhs.f_[4] + f_[8] * rhs.f_[5];
802 ret.f_[6] = f_[0] * rhs.f_[6] + f_[3] * rhs.f_[7] + f_[6] * rhs.f_[8];
803 ret.f_[7] = f_[1] * rhs.f_[6] + f_[4] * rhs.f_[7] + f_[7] * rhs.f_[8];
804 ret.f_[8] = f_[2] * rhs.f_[6] + f_[5] * rhs.f_[7] + f_[8] * rhs.f_[8];
811 ret.
x_ = rhs.
x_ * f_[0] + rhs.
y_ * f_[3] + rhs.
z_ * f_[6];
812 ret.
y_ = rhs.
x_ * f_[1] + rhs.
y_ * f_[4] + rhs.
z_ * f_[7];
813 ret.
z_ = rhs.
x_ * f_[2] + rhs.
y_ * f_[5] + rhs.
z_ * f_[8];
819 for (
int i = 0; i < 9; ++i) {
820 ret.f_[i] = f_[i] + rhs.f_[i];
827 for (
int i = 0; i < 9; ++i) {
828 ret.f_[i] = f_[i] - rhs.f_[i];
834 for (
int i = 0; i < 9; ++i) {
841 for (
int i = 0; i < 9; ++i) {
850 ret.f_[0] = f_[0] * rhs.f_[0] + f_[3] * rhs.f_[1] + f_[6] * rhs.f_[2];
851 ret.f_[1] = f_[1] * rhs.f_[0] + f_[4] * rhs.f_[1] + f_[7] * rhs.f_[2];
852 ret.f_[2] = f_[2] * rhs.f_[0] + f_[5] * rhs.f_[1] + f_[8] * rhs.f_[2];
854 ret.f_[3] = f_[0] * rhs.f_[3] + f_[3] * rhs.f_[4] + f_[6] * rhs.f_[5];
855 ret.f_[4] = f_[1] * rhs.f_[3] + f_[4] * rhs.f_[4] + f_[7] * rhs.f_[5];
856 ret.f_[5] = f_[2] * rhs.f_[3] + f_[5] * rhs.f_[4] + f_[8] * rhs.f_[5];
858 ret.f_[6] = f_[0] * rhs.f_[6] + f_[3] * rhs.f_[7] + f_[6] * rhs.f_[8];
859 ret.f_[7] = f_[1] * rhs.f_[6] + f_[4] * rhs.f_[7] + f_[7] * rhs.f_[8];
860 ret.f_[8] = f_[2] * rhs.f_[6] + f_[5] * rhs.f_[7] + f_[8] * rhs.f_[8];
868 for (
int i = 0; i < 9; ++i) {
869 ret.f_[i] = f_[i] * rhs;
875 for (
int i = 0; i < 9; ++i) {
882 for (
int i = 0; i < 9; ++i) {
892 f_[0] * f_[4] * f_[8] +
893 f_[1] * f_[5] * f_[6] +
894 f_[2] * f_[3] * f_[7] -
895 f_[0] * f_[5] * f_[7] -
896 f_[2] * f_[4] * f_[6] -
897 f_[1] * f_[3] * f_[8];
903 ret.f_[0] = (f_[4] * f_[8] - f_[7] * f_[5]) / det;
904 ret.f_[1] = (f_[7] * f_[2] - f_[1] * f_[8]) / det;
905 ret.f_[2] = (f_[1] * f_[5] - f_[4] * f_[2]) / det;
907 ret.f_[3] = (f_[6] * f_[5] - f_[3] * f_[8]) / det;
908 ret.f_[4] = (f_[0] * f_[8] - f_[6] * f_[2]) / det;
909 ret.f_[5] = (f_[3] * f_[2] - f_[0] * f_[5]) / det;
911 ret.f_[6] = (f_[3] * f_[7] - f_[6] * f_[4]) / det;
912 ret.f_[7] = (f_[6] * f_[1] - f_[0] * f_[7]) / det;
913 ret.f_[8] = (f_[0] * f_[4] - f_[3] * f_[1]) / det;
992 for (
int i = 0; i < 16; ++i)
998 for (
int i = 0; i < 16; ++i)
1005 ret.f_[0] = f_[0] * rhs.f_[0] + f_[4] * rhs.f_[1] + f_[8] * rhs.f_[2]
1006 + f_[12] * rhs.f_[3];
1007 ret.f_[1] = f_[1] * rhs.f_[0] + f_[5] * rhs.f_[1] + f_[9] * rhs.f_[2]
1008 + f_[13] * rhs.f_[3];
1009 ret.f_[2] = f_[2] * rhs.f_[0] + f_[6] * rhs.f_[1] + f_[10] * rhs.f_[2]
1010 + f_[14] * rhs.f_[3];
1011 ret.f_[3] = f_[3] * rhs.f_[0] + f_[7] * rhs.f_[1] + f_[11] * rhs.f_[2]
1012 + f_[15] * rhs.f_[3];
1014 ret.f_[4] = f_[0] * rhs.f_[4] + f_[4] * rhs.f_[5] + f_[8] * rhs.f_[6]
1015 + f_[12] * rhs.f_[7];
1016 ret.f_[5] = f_[1] * rhs.f_[4] + f_[5] * rhs.f_[5] + f_[9] * rhs.f_[6]
1017 + f_[13] * rhs.f_[7];
1018 ret.f_[6] = f_[2] * rhs.f_[4] + f_[6] * rhs.f_[5] + f_[10] * rhs.f_[6]
1019 + f_[14] * rhs.f_[7];
1020 ret.f_[7] = f_[3] * rhs.f_[4] + f_[7] * rhs.f_[5] + f_[11] * rhs.f_[6]
1021 + f_[15] * rhs.f_[7];
1023 ret.f_[8] = f_[0] * rhs.f_[8] + f_[4] * rhs.f_[9] + f_[8] * rhs.f_[10]
1024 + f_[12] * rhs.f_[11];
1025 ret.f_[9] = f_[1] * rhs.f_[8] + f_[5] * rhs.f_[9] + f_[9] * rhs.f_[10]
1026 + f_[13] * rhs.f_[11];
1027 ret.f_[10] = f_[2] * rhs.f_[8] + f_[6] * rhs.f_[9] + f_[10] * rhs.f_[10]
1028 + f_[14] * rhs.f_[11];
1029 ret.f_[11] = f_[3] * rhs.f_[8] + f_[7] * rhs.f_[9] + f_[11] * rhs.f_[10]
1030 + f_[15] * rhs.f_[11];
1032 ret.f_[12] = f_[0] * rhs.f_[12] + f_[4] * rhs.f_[13] + f_[8] * rhs.f_[14]
1033 + f_[12] * rhs.f_[15];
1034 ret.f_[13] = f_[1] * rhs.f_[12] + f_[5] * rhs.f_[13] + f_[9] * rhs.f_[14]
1035 + f_[13] * rhs.f_[15];
1036 ret.f_[14] = f_[2] * rhs.f_[12] + f_[6] * rhs.f_[13] + f_[10] * rhs.f_[14]
1037 + f_[14] * rhs.f_[15];
1038 ret.f_[15] = f_[3] * rhs.f_[12] + f_[7] * rhs.f_[13] + f_[11] * rhs.f_[14]
1039 + f_[15] * rhs.f_[15];
1047 ret.
x_ = rhs.
x_ * f_[0] + rhs.
y_ * f_[4] + rhs.
z_ * f_[8] + rhs.
w_ * f_[12];
1048 ret.
y_ = rhs.
x_ * f_[1] + rhs.
y_ * f_[5] + rhs.
z_ * f_[9] + rhs.
w_ * f_[13];
1049 ret.
z_ = rhs.
x_ * f_[2] + rhs.
y_ * f_[6] + rhs.
z_ * f_[10] + rhs.
w_ * f_[14];
1050 ret.
w_ = rhs.
x_ * f_[3] + rhs.
y_ * f_[7] + rhs.
z_ * f_[11] + rhs.
w_ * f_[15];
1057 for (
int i = 0; i < 16; ++i)
1059 ret.f_[i] = f_[i] + rhs.f_[i];
1067 for (
int i = 0; i < 16; ++i)
1069 ret.f_[i] = f_[i] - rhs.f_[i];
1076 for (
int i = 0; i < 16; ++i)
1085 for (
int i = 0; i < 16; ++i)
1095 ret.f_[0] = f_[0] * rhs.f_[0] + f_[4] * rhs.f_[1] + f_[8] * rhs.f_[2]
1096 + f_[12] * rhs.f_[3];
1097 ret.f_[1] = f_[1] * rhs.f_[0] + f_[5] * rhs.f_[1] + f_[9] * rhs.f_[2]
1098 + f_[13] * rhs.f_[3];
1099 ret.f_[2] = f_[2] * rhs.f_[0] + f_[6] * rhs.f_[1] + f_[10] * rhs.f_[2]
1100 + f_[14] * rhs.f_[3];
1101 ret.f_[3] = f_[3] * rhs.f_[0] + f_[7] * rhs.f_[1] + f_[11] * rhs.f_[2]
1102 + f_[15] * rhs.f_[3];
1104 ret.f_[4] = f_[0] * rhs.f_[4] + f_[4] * rhs.f_[5] + f_[8] * rhs.f_[6]
1105 + f_[12] * rhs.f_[7];
1106 ret.f_[5] = f_[1] * rhs.f_[4] + f_[5] * rhs.f_[5] + f_[9] * rhs.f_[6]
1107 + f_[13] * rhs.f_[7];
1108 ret.f_[6] = f_[2] * rhs.f_[4] + f_[6] * rhs.f_[5] + f_[10] * rhs.f_[6]
1109 + f_[14] * rhs.f_[7];
1110 ret.f_[7] = f_[3] * rhs.f_[4] + f_[7] * rhs.f_[5] + f_[11] * rhs.f_[6]
1111 + f_[15] * rhs.f_[7];
1113 ret.f_[8] = f_[0] * rhs.f_[8] + f_[4] * rhs.f_[9] + f_[8] * rhs.f_[10]
1114 + f_[12] * rhs.f_[11];
1115 ret.f_[9] = f_[1] * rhs.f_[8] + f_[5] * rhs.f_[9] + f_[9] * rhs.f_[10]
1116 + f_[13] * rhs.f_[11];
1117 ret.f_[10] = f_[2] * rhs.f_[8] + f_[6] * rhs.f_[9] + f_[10] * rhs.f_[10]
1118 + f_[14] * rhs.f_[11];
1119 ret.f_[11] = f_[3] * rhs.f_[8] + f_[7] * rhs.f_[9] + f_[11] * rhs.f_[10]
1120 + f_[15] * rhs.f_[11];
1122 ret.f_[12] = f_[0] * rhs.f_[12] + f_[4] * rhs.f_[13] + f_[8] * rhs.f_[14]
1123 + f_[12] * rhs.f_[15];
1124 ret.f_[13] = f_[1] * rhs.f_[12] + f_[5] * rhs.f_[13] + f_[9] * rhs.f_[14]
1125 + f_[13] * rhs.f_[15];
1126 ret.f_[14] = f_[2] * rhs.f_[12] + f_[6] * rhs.f_[13] + f_[10] * rhs.f_[14]
1127 + f_[14] * rhs.f_[15];
1128 ret.f_[15] = f_[3] * rhs.f_[12] + f_[7] * rhs.f_[13] + f_[11] * rhs.f_[14]
1129 + f_[15] * rhs.f_[15];
1138 for (
int i = 0; i < 16; ++i)
1140 ret.f_[i] = f_[i] * rhs;
1147 for (
int i = 0; i < 16; ++i)
1156 for (
int i = 0; i < 16; ++i)
1171 temp = f_[0] * f_[5] * f_[10];
1176 temp = f_[4] * f_[9] * f_[2];
1181 temp = f_[8] * f_[1] * f_[6];
1186 temp = -f_[8] * f_[5] * f_[2];
1191 temp = -f_[4] * f_[1] * f_[10];
1196 temp = -f_[0] * f_[9] * f_[6];
1209 det_1 = 1.0f / det_1;
1210 ret.f_[0] = (f_[5] * f_[10] - f_[9] * f_[6]) * det_1;
1211 ret.f_[1] = -(f_[1] * f_[10] - f_[9] * f_[2]) * det_1;
1212 ret.f_[2] = (f_[1] * f_[6] - f_[5] * f_[2]) * det_1;
1213 ret.f_[4] = -(f_[4] * f_[10] - f_[8] * f_[6]) * det_1;
1214 ret.f_[5] = (f_[0] * f_[10] - f_[8] * f_[2]) * det_1;
1215 ret.f_[6] = -(f_[0] * f_[6] - f_[4] * f_[2]) * det_1;
1216 ret.f_[8] = (f_[4] * f_[9] - f_[8] * f_[5]) * det_1;
1217 ret.f_[9] = -(f_[0] * f_[9] - f_[8] * f_[1]) * det_1;
1218 ret.f_[10] = (f_[0] * f_[5] - f_[4] * f_[1]) * det_1;
1221 ret.f_[12] = -(f_[12] * ret.f_[0] + f_[13] * ret.f_[4] + f_[14] * ret.f_[8]);
1222 ret.f_[13] = -(f_[12] * ret.f_[1] + f_[13] * ret.f_[5] + f_[14] * ret.f_[9]);
1223 ret.f_[14] = -(f_[12] * ret.f_[2] + f_[13] * ret.f_[6] + f_[14] * ret.f_[10]);
1248 ret.f_[10] = f_[10];
1249 ret.f_[11] = f_[14];
1252 ret.f_[14] = f_[11];
1253 ret.f_[15] = f_[15];
1260 f_[12] += (tx * f_[0]) + (ty * f_[4]) + (tz * f_[8]);
1261 f_[13] += (tx * f_[1]) + (ty * f_[5]) + (tz * f_[9]);
1262 f_[14] += (tx * f_[2]) + (ty * f_[6]) + (tz * f_[10]);
1263 f_[15] += (tx * f_[3]) + (ty * f_[7]) + (tz * f_[11]);
1269 float sinAngle, cosAngle;
1270 float mag = (float)sqrtf(rx * rx + ry * ry + rz * rz);
1272 sinAngle = sinf(angle *
PI / 180.0f);
1273 cosAngle = cosf(angle *
PI / 180.0f);
1276 float xx, yy, zz, xy, yz, zx, xs, ys, zs;
1294 oneMinusCos = 1.0f - cosAngle;
1295 rotMat.
Ptr()[0] = (oneMinusCos * xx) + cosAngle;
1296 rotMat.
Ptr()[1] = (oneMinusCos * xy) - zs;
1297 rotMat.
Ptr()[2] = (oneMinusCos * zx) + ys;
1298 rotMat.
Ptr()[3] = 0.0F;
1299 rotMat.
Ptr()[4] = (oneMinusCos * xy) + zs;
1300 rotMat.
Ptr()[5] = (oneMinusCos * yy) + cosAngle;
1301 rotMat.
Ptr()[6] = (oneMinusCos * yz) - xs;
1302 rotMat.
Ptr()[7] = 0.0F;
1303 rotMat.
Ptr()[8] = (oneMinusCos * zx) - ys;
1304 rotMat.
Ptr()[9] = (oneMinusCos * yz) + xs;
1305 rotMat.
Ptr()[10] = (oneMinusCos * zz) + cosAngle;
1306 rotMat.
Ptr()[11] = 0.0F;
1307 rotMat.
Ptr()[12] = 0.0F;
1308 rotMat.
Ptr()[13] = 0.0F;
1309 rotMat.
Ptr()[14] = 0.0F;
1310 rotMat.
Ptr()[15] = 1.0F;
1347 float y = width / height;
1349 result.
Ptr()[0] = 2.0f * x;
1350 result.
Ptr()[1] = 0.0f;
1351 result.
Ptr()[2] = 0.0f;
1352 result.
Ptr()[3] = 0.0f;
1353 result.
Ptr()[4] = 0.0f;
1354 result.
Ptr()[5] = 2.0f * -y;
1355 result.
Ptr()[6] = 0.0f;
1356 result.
Ptr()[7] = 0.0f;
1357 result.
Ptr()[8] = 0.0f;
1358 result.
Ptr()[9] = 0.0f;
1359 result.
Ptr()[10] = (farPlane + nearPlane) / (farPlane - nearPlane);
1360 result.
Ptr()[11] = 1.0f;
1361 result.
Ptr()[12] = 0.0f;
1362 result.
Ptr()[13] = 0.0f;
1363 result.
Ptr()[14] = -(2.0f * farPlane * nearPlane) / (farPlane - nearPlane);
1364 result.
Ptr()[15] = 0.0f;
1366 float n2 = 2.0f * nearPlane;
1367 float rcpnmf = 1.f / (nearPlane - farPlane);
1370 result.f_[0] = n2 / width;
1375 result.f_[5] = n2 / height;
1380 result.f_[10] = (farPlane + nearPlane) * rcpnmf;
1381 result.f_[14] = farPlane * rcpnmf * n2;
1384 result.f_[11] = -1.0;
1394 float aspect = (float)width / (
float)height;
1395 float f = 1.0f / (float)tan(fovy * (
PI / 360.0f));
1396 float rangeReciprocal = 1.0f / (zNear - zFar);
1398 result.f_[0] = f / aspect;
1399 result.f_[1] = 0.0f;
1400 result.f_[2] = 0.0f;
1401 result.f_[3] = 0.0f;
1403 result.f_[4] = 0.0f;
1405 result.f_[6] = 0.0f;
1406 result.f_[7] = 0.0f;
1408 result.f_[8] = 0.0f;
1409 result.f_[9] = 0.0f;
1410 result.f_[10] = -(zFar + zNear) * rangeReciprocal;
1411 result.f_[11] = 1.0f;
1413 result.f_[12] = 0.0f;
1414 result.f_[13] = 0.0f;
1415 result.f_[14] = 2.0f * zFar * zNear * rangeReciprocal;
1416 result.f_[15] = 0.0f;
1421 static Mat4 Perspective(
float fx,
float fy,
float cx,
float cy,
int w,
int h,
float n,
float f)
1425 result.f_[0] = 2.0f * fx / w;
1426 result.f_[1] = 0.0f;
1427 result.f_[2] = 0.0f;
1428 result.f_[3] = 0.0f;
1430 result.f_[4] = 0.0f;
1431 result.f_[5] = -2.0f * fy / h;
1432 result.f_[6] = 0.0f;
1433 result.f_[7] = 0.0f;
1435 result.f_[8] = 1.0f - 2.0f * cx / w;
1436 result.f_[9] = 2.0f * cy / h - 1.0f;
1437 result.f_[10] = -(f + n) / (n - f);
1438 result.f_[11] = 1.0f;
1440 result.f_[12] = 0.0f;
1441 result.f_[13] = 0.0f;
1442 result.f_[14] = 2.0f * f * n / (n - f);
1443 result.f_[15] = 0.0f;
1446 m[0][0] = 2.0 * fx / width;
1452 m[1][1] = -2.0 * fy / height;
1456 m[2][0] = 1.0 - 2.0 * cx / width;
1457 m[2][1] = 2.0 * cy / height - 1.0;
1458 m[2][2] = (zfar + znear) / (znear - zfar);
1463 m[3][2] = 2.0 * zfar * znear / (znear - zfar);
1470 static Mat4 Ortho(
float left,
float right,
float bottom,
float top,
float nearPlane,
float farPlane)
1472 float r_l = right - left;
1473 float t_b = top - bottom;
1474 float f_n = farPlane - nearPlane;
1476 float tx = -(right + left) / (right - left);
1477 float ty = -(top + bottom) / (top - bottom);
1478 float tz = -(farPlane + nearPlane) / (farPlane - nearPlane);
1481 result.f_[0] = 2.0f / r_l;
1482 result.f_[1] = 0.0f;
1483 result.f_[2] = 0.0f;
1486 result.f_[4] = 0.0f;
1487 result.f_[5] = 2.0f / t_b;
1488 result.f_[6] = 0.0f;
1491 result.f_[8] = 0.0f;
1492 result.f_[9] = 0.0f;
1493 result.f_[10] = 2.0f / f_n;
1496 result.f_[12] = 0.0f;
1497 result.f_[13] = 0.0f;
1498 result.f_[14] = 0.0f;
1499 result.f_[15] = 1.0f;
1506 Vec3 vec_forward, vec_up_norm, vec_side;
1509 vec_forward.
x_ = vec_eye.
x_ - vec_at.
x_;
1510 vec_forward.
y_ = vec_eye.
y_ - vec_at.
y_;
1511 vec_forward.
z_ = vec_eye.
z_ - vec_at.
z_;
1514 vec_up_norm = vec_up;
1516 vec_side = vec_up_norm.
Cross(vec_forward);
1517 vec_up_norm = vec_forward.
Cross(vec_side);
1519 result.f_[0] = vec_side.
x_;
1520 result.f_[4] = vec_side.
y_;
1521 result.f_[8] = vec_side.
z_;
1523 result.f_[1] = vec_up_norm.
x_;
1524 result.f_[5] = vec_up_norm.
y_;
1525 result.f_[9] = vec_up_norm.
z_;
1527 result.f_[2] = vec_forward.
x_;
1528 result.f_[6] = vec_forward.
y_;
1529 result.f_[10] = vec_forward.
z_;
1534 result.f_[15] = 1.0;
1568 ret.f_[12] = vec.
x_;
1572 ret.f_[13] = vec.
y_;
1576 ret.f_[14] = vec.
z_;
1587 float fCosine, fSine;
1589 fCosine = cosf(fAngle);
1590 fSine = sinf(fAngle);
1597 ret.f_[5] = fCosine;
1602 ret.f_[10] = fCosine;
1614 float fCosine, fSine;
1616 fCosine = cosf(fAngle);
1617 fSine = sinf(fAngle);
1619 ret.f_[0] = fCosine;
1629 ret.f_[10] = fCosine;
1641 float fCosine, fSine;
1643 fCosine = cosf(fAngle);
1644 fSine = sinf(fAngle);
1646 ret.f_[0] = fCosine;
1651 ret.f_[5] = fCosine;
1665 static Mat4 Scale(
const float fX,
const float fY,
const float fZ)
1734 printf(
"%f %f %f %f\n", f_[0], f_[1], f_[2], f_[3]);
1735 printf(
"%f %f %f %f\n", f_[4], f_[5], f_[6], f_[7]);
1736 printf(
"%f %f %f %f\n", f_[8], f_[9], f_[10], f_[11]);
1737 printf(
"%f %f %f %f\n", f_[12], f_[13], f_[14], f_[15]);
1748 float x_,
y_, z_, w_;
1763 Quaternion(
const float fX,
const float fY,
const float fZ,
const float fW)
1790 ret.x_ = x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_ + w_ * rhs.x_;
1791 ret.y_ = -x_ * rhs.z_ + y_ * rhs.w_ + z_ * rhs.x_ + w_ * rhs.y_;
1792 ret.z_ = x_ * rhs.y_ - y_ * rhs.x_ + z_ * rhs.w_ + w_ * rhs.z_;
1793 ret.w_ = -x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_ + w_ * rhs.w_;
1800 ret.x_ = x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_ + w_ * rhs.x_;
1801 ret.y_ = -x_ * rhs.z_ + y_ * rhs.w_ + z_ * rhs.x_ + w_ * rhs.y_;
1802 ret.z_ = x_ * rhs.y_ - y_ * rhs.x_ + z_ * rhs.w_ + w_ * rhs.z_;
1803 ret.w_ = -x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_ + w_ * rhs.w_;
1829 float x2 = x_ * x_ * 2.0f;
1830 float y2 = y_ * y_ * 2.0f;
1831 float z2 = z_ * z_ * 2.0f;
1832 float xy = x_ * y_ * 2.0f;
1833 float yz = y_ * z_ * 2.0f;
1834 float zx = z_ * x_ * 2.0f;
1835 float xw = x_ * w_ * 2.0f;
1836 float yw = y_ * w_ * 2.0f;
1837 float zw = z_ * w_ * 2.0f;
1839 mat.f_[0] = 1.0f - y2 - z2;
1840 mat.f_[1] = xy + zw;
1841 mat.f_[2] = zx - yw;
1842 mat.f_[4] = xy - zw;
1843 mat.f_[5] = 1.0f - z2 - x2;
1844 mat.f_[6] = yz + xw;
1845 mat.f_[8] = zx + yw;
1846 mat.f_[9] = yz - xw;
1847 mat.f_[10] = 1.0f - x2 - y2;
1849 mat.f_[3] = mat.f_[7] = mat.f_[11] = mat.f_[12] = mat.f_[13] = mat.f_[14] = 0.0f;
1855 float x2 = x_ * x_ * 2.0f;
1856 float y2 = y_ * y_ * 2.0f;
1857 float z2 = z_ * z_ * 2.0f;
1858 float xy = x_ * y_ * 2.0f;
1859 float yz = y_ * z_ * 2.0f;
1860 float zx = z_ * x_ * 2.0f;
1861 float xw = x_ * w_ * 2.0f;
1862 float yw = y_ * w_ * 2.0f;
1863 float zw = z_ * w_ * 2.0f;
1865 mat.f_[0] = 1.0f - y2 - z2;
1866 mat.f_[1] = xy + zw;
1867 mat.f_[2] = zx - yw;
1868 mat.f_[4] = xy - zw;
1869 mat.f_[5] = 1.0f - z2 - x2;
1870 mat.f_[6] = yz + xw;
1871 mat.f_[8] = zx + yw;
1872 mat.f_[9] = yz - xw;
1873 mat.f_[10] = 1.0f - x2 - y2;
1875 mat.f_[3] = mat.f_[7] = mat.f_[11] = 0.0f;
1882 float s = sinf(angle / 2);
1883 ret.x_ = s * axis.
x_;
1884 ret.y_ = s * axis.
y_;
1885 ret.z_ = s * axis.
z_;
1886 ret.w_ = cosf(angle / 2);
1890 void Value(
float& fX,
float& fY,
float& fZ,
float& fW)
Vec4 & operator*=(const float &rhs)
Definition: vecmath.h:678
void ToMatrixPreserveTranslate(Mat4 &mat)
Definition: vecmath.h:1853
Quaternion(const Vec3 vec, const float fW)
Definition: vecmath.h:1771
Vec3(const Vec2 &vec, float f)
Definition: vecmath.h:303
static Mat4 RotationX(const float fAngle)
Definition: vecmath.h:1584
Vec4 operator+(const Vec4 &rhs) const
Definition: vecmath.h:585
float Dot(const Vec3 &rhs)
Definition: vecmath.h:737
Mat3()
Definition: vecmath.h:779
void Value(float &fX, float &fY, float &fZ)
Definition: vecmath.h:500
Quaternion(const float fX, const float fY, const float fZ, const float fW)
Definition: vecmath.h:1763
Vec2 & operator+=(const Vec2 &rhs)
Definition: vecmath.h:118
Quaternion(const float *p)
Definition: vecmath.h:1779
Vec3 & operator/=(const Vec3 &rhs)
Definition: vecmath.h:377
Mat3 & operator-=(const Mat3 &rhs)
Definition: vecmath.h:840
Vec4 & operator+=(const Vec4 &rhs)
Definition: vecmath.h:605
Vec4(const float *pVec)
Definition: vecmath.h:556
Vec3 operator*(const float &rhs) const
Definition: vecmath.h:410
Mat3 & operator*=(const Mat3 &rhs)
Definition: vecmath.h:847
Mat3 & operator*=(const float rhs)
Definition: vecmath.h:874
Vec4 & operator-=(const Vec4 &rhs)
Definition: vecmath.h:614
static Mat4 Perspective(float fx, float fy, float cx, float cy, int w, int h, float n, float f)
Definition: vecmath.h:1421
Vec3 operator*(const Vec3 &rhs) const
Definition: vecmath.h:317
Mat3 & operator+=(const Mat3 &rhs)
Definition: vecmath.h:833
void Dump()
Definition: vecmath.h:257
void Dump()
Definition: vecmath.h:1732
Vec2 operator*(const Vec2 &rhs) const
Definition: vecmath.h:86
static Mat4 Identity()
Definition: vecmath.h:1687
Vec3 & operator*=(const float &rhs)
Definition: vecmath.h:419
#define PI
Definition: vecmath.h:28
Vec4 operator-(const Vec4 &rhs) const
Definition: vecmath.h:595
float w_
Definition: vecmath.h:520
static Mat4 Translation(const Vec3 vec)
Definition: vecmath.h:1562
Vec2 operator-(const Vec2 &rhs) const
Definition: vecmath.h:110
static Mat4 Perspective(float width, float height, float nearPlane, float farPlane)
Definition: vecmath.h:1342
Mat4 & PostTranslate(float tx, float ty, float tz)
Definition: vecmath.h:1258
Quaternion & operator*=(const Quaternion rhs)
Definition: vecmath.h:1797
Vec3 operator+(const Vec3 &rhs) const
Definition: vecmath.h:335
friend class Vec4
Definition: vecmath.h:58
Vec3(const float fX, const float fY, const float fZ)
Definition: vecmath.h:282
Vec3 Normalize()
Definition: vecmath.h:465
Mat4 operator-(const Mat4 &rhs) const
Definition: vecmath.h:1064
Quaternion Conjugate()
Definition: vecmath.h:1808
Vec3 & operator/=(const float &rhs)
Definition: vecmath.h:436
Quaternion()
Definition: vecmath.h:1755
Vec2 operator/(const float &rhs) const
Definition: vecmath.h:190
float z_
Definition: vecmath.h:520
Vec4 operator/(const Vec4 &rhs) const
Definition: vecmath.h:575
static Mat3 Identity()
Definition: vecmath.h:943
friend Vec4 operator/(const float lhs, const Vec4 &rhs)
Definition: vecmath.h:657
friend Vec2 operator*(const float lhs, const Vec2 &rhs)
Definition: vecmath.h:152
Mat4 & operator-=(const Mat4 &rhs)
Definition: vecmath.h:1083
Definition: vecmath.h:267
float Dot(const Vec3 &rhs)
Definition: vecmath.h:474
Mat4 & operator*=(const Mat4 &rhs)
Definition: vecmath.h:1092
static Mat4 RotationZ(const float fAngle)
Definition: vecmath.h:1638
bool operator==(const Vec2 &rhs) const
Definition: vecmath.h:206
bool operator!=(const Vec3 &rhs) const
Definition: vecmath.h:452
Vec3 & operator+=(const Vec3 &rhs)
Definition: vecmath.h:353
bool Validate()
Definition: vecmath.h:751
void ToMatrix(Mat4 &mat)
Definition: vecmath.h:1827
Vec3(const float *pVec)
Definition: vecmath.h:296
void Dump()
Definition: vecmath.h:507
Mat4 & operator+=(const Mat4 &rhs)
Definition: vecmath.h:1074
Vec4 & operator*=(const Vec4 &rhs)
Definition: vecmath.h:623
Vec4(const Vec3 &vec, const float fW)
Definition: vecmath.h:548
Mat4 & PostRotate(float angle, float rx, float ry, float rz)
Definition: vecmath.h:1267
Vec3 Cross(const Vec3 &rhs)
Definition: vecmath.h:484
Vec4 & operator/=(const float &rhs)
Definition: vecmath.h:697
float Length() const
Definition: vecmath.h:460
Mat4 operator+(const Mat4 &rhs) const
Definition: vecmath.h:1054
float x_
Definition: vecmath.h:520
float y_
Definition: vecmath.h:275
Vec2(const float fX, const float fY)
Definition: vecmath.h:67
friend Vec2 operator/(const float lhs, const Vec2 &rhs)
Definition: vecmath.h:160
Mat3 operator+(const Mat3 &rhs) const
Definition: vecmath.h:817
float Distance(const Vec2 &rhs)
Definition: vecmath.h:239
Mat3 operator-(const Mat3 &rhs) const
Definition: vecmath.h:825
static Mat3 Zero()
Definition: vecmath.h:959
Vec4(const float fX, const float fY, const float fZ, const float fW)
Definition: vecmath.h:532
Vec3(const Vec3 &vec)
Definition: vecmath.h:289
Vec3 & operator-=(const Vec3 &rhs)
Definition: vecmath.h:361
Vec2(const Vec2 &vec)
Definition: vecmath.h:73
Definition: vecmath.h:517
friend class Vec3
Definition: vecmath.h:57
Mat4 operator*(const float rhs)
Definition: vecmath.h:1135
static Mat4 RotationY(const float fAngle)
Definition: vecmath.h:1611
Vec3 operator/(const Vec3 &rhs) const
Definition: vecmath.h:326
static Mat4 Translation(const float fX, const float fY, const float fZ)
Definition: vecmath.h:1540
void Value(float &fX, float &fY)
Definition: vecmath.h:251
Mat3 Inverse()
Definition: vecmath.h:888
bool Validate()
Definition: vecmath.h:244
float Dot(const Vec2 &rhs)
Definition: vecmath.h:234
Mat3 Transpose()
Definition: vecmath.h:920
float Distance(const Vec3 &rhs)
Definition: vecmath.h:479
static Mat4 Perspective(float fovy, int width, int height, float zNear, float zFar)
Definition: vecmath.h:1390
Vec4 operator*(const Vec4 &rhs) const
Definition: vecmath.h:565
static Quaternion RotationAxis(const Vec3 axis, const float angle)
Definition: vecmath.h:1879
Vec2 & operator*=(const float &rhs)
Definition: vecmath.h:183
Vec4 operator*(const Vec4 &rhs) const
Definition: vecmath.h:1044
Vec2 operator+(const Vec2 &rhs) const
Definition: vecmath.h:102
friend Vec3 operator*(const float lhs, const Vec3 &rhs)
Definition: vecmath.h:391
Vec2 operator*(const float &rhs) const
Definition: vecmath.h:175
bool operator==(const Vec4 &rhs) const
Definition: vecmath.h:707
Vec4(const Vec4 &vec)
Definition: vecmath.h:540
Mat3(const float *mIn)
Definition: vecmath.h:785
Vec4 Normalize()
Definition: vecmath.h:727
friend Vec2 operator-(const Vec2 &rhs)
Definition: vecmath.h:147
Vec4 operator*(const float &rhs) const
Definition: vecmath.h:668
Vec2()
Definition: vecmath.h:62
Mat3 operator*(const float rhs)
Definition: vecmath.h:866
Vec2 operator/(const Vec2 &rhs) const
Definition: vecmath.h:94
Quaternion operator*(const Quaternion rhs)
Definition: vecmath.h:1787
Mat4()
Definition: vecmath.h:990
bool operator!=(const Vec4 &rhs) const
Definition: vecmath.h:714
static Mat4 Zero()
Definition: vecmath.h:1709
Vec4()
Definition: vecmath.h:527
Mat4 Transpose()
Definition: vecmath.h:1235
Definition: vecmath.h:1745
void Value(float &fX, float &fY, float &fZ, float &fW)
Definition: vecmath.h:758
Vec2 Normalize()
Definition: vecmath.h:226
float x_
Definition: vecmath.h:275
friend Vec4 operator-(const Vec4 &rhs)
Definition: vecmath.h:642
Definition: vecmath.h:771
Vec3()
Definition: vecmath.h:277
float y_
Definition: vecmath.h:54
float Length() const
Definition: vecmath.h:722
Vec4 & operator/=(const Vec4 &rhs)
Definition: vecmath.h:632
Vec3 operator/(const float &rhs) const
Definition: vecmath.h:427
Mat3 operator*(const Mat3 &rhs) const
Definition: vecmath.h:791
float y_
Definition: vecmath.h:520
Vec3 operator-(const Vec3 &rhs) const
Definition: vecmath.h:344
static Mat4 Scale(const float fX, const float fY, const float fZ)
Definition: vecmath.h:1665
static Mat4 Ortho(float left, float right, float bottom, float top, float nearPlane, float farPlane)
Definition: vecmath.h:1470
bool operator!=(const Vec2 &rhs) const
Definition: vecmath.h:213
float * Ptr()
Definition: vecmath.h:938
Vec4 operator/(const float &rhs) const
Definition: vecmath.h:687
bool Validate()
Definition: vecmath.h:493
void Value(float &fX, float &fY, float &fZ, float &fW)
Definition: vecmath.h:1890
Vec2 & operator*=(const Vec2 &rhs)
Definition: vecmath.h:132
Quaternion Conjugated()
Definition: vecmath.h:1817
Vec3 Cross(const Vec3 &rhs)
Definition: vecmath.h:742
Mat4 & operator*=(const float rhs)
Definition: vecmath.h:1145
Mat4 & PostScale(float sx, float sy, float sz)
Definition: vecmath.h:1317
Vec3 operator*(const Vec3 &rhs) const
Definition: vecmath.h:809
static Mat4 LookAt(const Vec3 &vec_eye, const Vec3 &vec_at, const Vec3 &vec_up)
Definition: vecmath.h:1504
Vec2 & operator-=(const Vec2 &rhs)
Definition: vecmath.h:125
friend Vec3 operator/(const float lhs, const Vec3 &rhs)
Definition: vecmath.h:400
Vec2 & operator/=(const Vec2 &rhs)
Definition: vecmath.h:139
Mat4 operator*(const Mat4 &rhs) const
Definition: vecmath.h:1002
bool operator==(const Vec3 &rhs) const
Definition: vecmath.h:445
friend Vec3 operator-(const Vec3 &rhs)
Definition: vecmath.h:386
Vec2 & operator/=(const float &rhs)
Definition: vecmath.h:198
Mat4 & operator=(const Mat4 &rhs)
Definition: vecmath.h:1154
float * Ptr()
Definition: vecmath.h:1334
float x_
Definition: vecmath.h:53
Mat3 & operator=(const Mat3 &rhs)
Definition: vecmath.h:881
Definition: vecmath.h:980
Mat4(const float *mIn)
Definition: vecmath.h:996
float Length() const
Definition: vecmath.h:221
Mat4 Inverse()
Definition: vecmath.h:1163
bool operator<(const Vec2 &v) const
Definition: vecmath.h:169
friend Vec4 operator*(const float lhs, const Vec4 &rhs)
Definition: vecmath.h:647
Vec3 & operator*=(const Vec3 &rhs)
Definition: vecmath.h:369
Vec2(const float *pVec)
Definition: vecmath.h:79
float z_
Definition: vecmath.h:275