00001
00002 #if !defined __MYMATHUTILS_H
00003 #define __MYMATHUTILS_H
00004
00005
00006 #include "assert.h"
00007 #include "math.h"
00008 #include "stdlib.h"
00009
00010
00011 #define PI 3.1415926535
00012 #define feps 0.001f //float calculation failure
00013 #define deps 0.000000001f //double calcualtion failure
00014
00015 #define DIV_DEG_TO_RAD 57.29577951 //= 180 / PI
00016 #define DIV_RAD_TO_DEG 0.017453293 //= PI / 180
00017
00018 #define MUL_DEG_TO_RAD DIV_RAD_TO_DEG
00019 #define MUL_RAD_TO_DEG DIV_DEG_TO_RAD
00020
00021
00022
00023 class Vektor3f;
00024 class Vektor4f;
00025
00026
00027 class Matrix4f
00028 {
00029 public:
00030 float m[16];
00031
00032 Matrix4f()
00033 {
00034
00035 }
00036
00037
00038 Matrix4f(Matrix4f &p)
00039 {
00040 *this = p;
00041 }
00042
00043 Matrix4f(float m0, float m4, float m8, float m12,
00044 float m1, float m5, float m9, float m13,
00045 float m2, float m6, float m10, float m14,
00046 float m3, float m7, float m11, float m15)
00047 {
00048 m[0] = m0; m[4] = m4; m[8] = m8; m[12] = m12;
00049 m[1] = m1; m[5] = m5; m[9] = m9; m[13] = m13;
00050 m[2] = m2; m[6] = m6; m[10] = m10; m[14] = m14;
00051 m[3] = m3; m[7] = m7; m[11] = m11; m[15] = m15;
00052 }
00053
00054 inline void set(float m0, float m4, float m8, float m12,
00055 float m1, float m5, float m9, float m13,
00056 float m2, float m6, float m10, float m14,
00057 float m3, float m7, float m11, float m15)
00058 {
00059 m[0] = m0; m[4] = m4; m[8] = m8; m[12] = m12;
00060 m[1] = m1; m[5] = m5; m[9] = m9; m[13] = m13;
00061 m[2] = m2; m[6] = m6; m[10] = m10; m[14] = m14;
00062 m[3] = m3; m[7] = m7; m[11] = m11; m[15] = m15;
00063 }
00064
00065
00066 inline void setIdentity()
00067 {
00068 m[0] = 1; m[4] = 0; m[8] = 0; m[12] = 0;
00069 m[1] = 0; m[5] = 1; m[9] = 0; m[13] = 0;
00070 m[2] = 0; m[6] = 0; m[10] = 1; m[14] = 0;
00071 m[3] = 0; m[7] = 0; m[11] = 0; m[15] = 1;
00072 }
00073
00074
00075 public:
00076
00077 bool Inverse();
00078
00079 private:
00080
00081 void SubMatrix(float *m3, int i, int j);
00082
00083 public:
00084 float getDeterminant();
00085
00086
00087
00088 public:
00089 void RotateX(float angle);
00090
00091 void RotateY(float angle);
00092
00093 void RotateZ(float angle);
00094
00095 void Rotate(float angle, Vektor3f &v);
00096
00097 void Rotate(float angle, float px, float py, float pz);
00098
00099 void Translate(Vektor3f &v);
00100
00101 void Translate(float px, float py, float pz);
00102
00103 void Scale(float sx, float sy, float sz);
00104
00105
00106 void LookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz);
00107
00108 void LookAt(Vektor3f &p_Position, Vektor3f &p_ViewingDirection, Vektor3f &p_UpViewingDirection);
00109
00110 void LookAt(Vektor3f &p_Position, Vektor3f &p_ViewingDirection, Vektor3f &p_UpViewingDirection, Vektor3f &p_NormalViewingDirection);
00111
00112 void LookTo(Vektor3f &p_Position, Vektor3f &p_ViewingDirection, Vektor3f &p_UpViewingDirection);
00113
00114 void LookTo(Vektor3f &p_Position, Vektor3f &p_ViewingDirection, Vektor3f &p_UpViewingDirection, Vektor3f &p_NormalViewingDirection);
00115
00116 public:
00117
00118
00119 void setRotateX(float angle);
00120
00121 void setRotateY(float angle);
00122
00123 void setRotateZ(float angle);
00124
00125 void setRotate(float angle, Vektor3f &v);
00126
00127 void setRotate(float angle, float px, float py, float pz);
00128
00129 void setTranslate(Vektor3f &v);
00130
00131 void setTranslate(float px, float py, float pz);
00132
00133 void setScale(float sx, float sy, float sz);
00134
00135
00136 void setLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz);
00137
00138 void setLookAt(Vektor3f &p_Position, Vektor3f &p_ViewingDirection, Vektor3f &p_UpViewingDirection);
00139
00140 void setLookAt(Vektor3f &p_Position, Vektor3f &p_ViewingDirection, Vektor3f &p_UpViewingDirection, Vektor3f &p_NormalViewingDirection);
00141
00142 void setLookTo(Vektor3f &p_Position, Vektor3f &p_ViewingDirection, Vektor3f &p_UpViewingDirection);
00143
00144 void setLookTo(Vektor3f &p_Position, Vektor3f &p_ViewingDirection, Vektor3f &p_UpViewingDirection, Vektor3f &p_NormalViewingDirection);
00145
00146
00147
00148
00149 Matrix4f operator*(Matrix4f &p);
00150
00151 Matrix4f& operator*=(Matrix4f &p);
00152
00153
00154 Vektor3f operator*(Vektor3f &v);
00155
00156 Vektor4f operator*(Vektor4f &v);
00157 };
00158
00159
00160 typedef Matrix4f Matrix4d;
00161 typedef Matrix4f Matrix;
00162
00163
00164
00165
00167
00169
00170
00171 #define VECTOR Vektor2f
00172 #define SCALAR float
00173
00174
00175 class Vektor2f
00176 {
00177 public:
00178 Vektor2f()
00179 {
00180 setNull();
00181 }
00182
00183 Vektor2f(SCALAR px, SCALAR py)
00184 {
00185 set(px, py);
00186 }
00187
00188 #include "MyVector2x.h"
00189
00190
00191 #undef SCALARUNSIGNED
00192 #undef USEMATRIX
00193 #undef VECTORMATRIX
00194 #undef VECTOR
00195 #undef SCALAR
00196
00197
00198
00199 #define VECTOR Vektor2d
00200 #define SCALAR float
00201
00202 class Vektor2d
00203 {
00204 public:
00205 Vektor2d()
00206 {
00207 setNull();
00208 }
00209
00210 Vektor2d(SCALAR px, SCALAR py)
00211 {
00212 set(px, py);
00213 }
00214
00215 #include "MyVector2x.h"
00216
00217 #undef SCALARUNSIGNED
00218 #undef USEMATRIX
00219 #undef VECTORMATRIX
00220 #undef VECTOR
00221 #undef SCALAR
00222
00223
00224
00225
00226 #define VECTOR Vektor2l
00227 #define SCALAR long
00228
00229 class Vektor2l
00230 {
00231 public:
00232 Vektor2l()
00233 {
00234 setNull();
00235 }
00236
00237 Vektor2l(SCALAR px, SCALAR py)
00238 {
00239 set(px, py);
00240 }
00241
00242 #include "MyVector2x.h"
00243
00244 #undef SCALARUNSIGNED
00245 #undef USEMATRIX
00246 #undef VECTORMATRIX
00247 #undef VECTOR
00248 #undef SCALAR
00249
00250
00251
00252
00253 #define VECTOR Vektor2ul
00254 #define SCALAR unsigned long
00255 #define SCALARUNSIGNED
00256
00257 class Vektor2ul
00258 {
00259 public:
00260 Vektor2ul()
00261 {
00262 setNull();
00263 }
00264
00265 Vektor2ul(SCALAR px, SCALAR py)
00266 {
00267 set(px, py);
00268 }
00269
00270 #include "MyVector2x.h"
00271
00272 #undef SCALARUNSIGNED
00273 #undef USEMATRIX
00274 #undef VECTORMATRIX
00275 #undef VECTOR
00276 #undef SCALAR
00277
00278
00279
00280
00281
00282 #define VECTOR Vektor2i
00283 #define SCALAR int
00284
00285 class Vektor2i
00286 {
00287 public:
00288 Vektor2i()
00289 {
00290 setNull();
00291 }
00292
00293 Vektor2i(SCALAR px, SCALAR py)
00294 {
00295 set(px, py);
00296 }
00297
00298 #include "MyVector2x.h"
00299
00300 #undef SCALARUNSIGNED
00301 #undef USEMATRIX
00302 #undef VECTORMATRIX
00303 #undef VECTOR
00304 #undef SCALAR
00305
00306
00307
00308
00309 #define VECTOR Vektor2ui
00310 #define SCALAR unsigned int
00311 #define SCALARUNSIGNED
00312
00313 class Vektor2ui
00314 {
00315 public:
00316 Vektor2ui()
00317 {
00318 setNull();
00319 }
00320
00321 Vektor2ui(SCALAR px, SCALAR py)
00322 {
00323 set(px, py);
00324 }
00325
00326 #include "MyVector2x.h"
00327
00328 #undef SCALARUNSIGNED
00329 #undef USEMATRIX
00330 #undef VECTORMATRIX
00331 #undef VECTOR
00332 #undef SCALAR
00333
00334
00335
00336
00337
00338 #define VECTOR Vektor2b
00339 #define SCALAR char
00340
00341 class Vektor2b
00342 {
00343 public:
00344 Vektor2b()
00345 {
00346 setNull();
00347 }
00348
00349 Vektor2b(SCALAR px, SCALAR py)
00350 {
00351 set(px, py);
00352 }
00353
00354 #include "MyVector2x.h"
00355
00356 #undef SCALARUNSIGNED
00357 #undef USEMATRIX
00358 #undef VECTORMATRIX
00359 #undef VECTOR
00360 #undef SCALAR
00361
00362
00363
00364
00365
00366 #define VECTOR Vektor2ub
00367 #define SCALAR unsigned char
00368 #define SCALARUNSIGNED
00369
00370 class Vektor2ub
00371 {
00372 public:
00373 Vektor2ub()
00374 {
00375 setNull();
00376 }
00377
00378 Vektor2ub(SCALAR px, SCALAR py)
00379 {
00380 set(px, py);
00381 }
00382
00383 #include "MyVector2x.h"
00384
00385 #undef SCALARUNSIGNED
00386 #undef USEMATRIX
00387 #undef VECTORMATRIX
00388 #undef VECTOR
00389 #undef SCALAR
00390
00391
00392
00393
00395
00397
00398
00399 #define VECTOR Vektor3f
00400 #define SCALAR float
00401 #define USEMATRIX
00402 #define VECTORMATRIX Matrix4f
00403
00404
00405 class Vektor3f
00406 {
00407 public:
00408 Vektor3f()
00409 {
00410 setNull();
00411 }
00412
00413 Vektor3f(SCALAR px, SCALAR py, SCALAR pz)
00414 {
00415 set(px, py, pz);
00416 }
00417
00418 #include "MyVector3x.h"
00419
00420
00421 #undef SCALARUNSIGNED
00422 #undef USEMATRIX
00423 #undef VECTORMATRIX
00424 #undef VECTOR
00425 #undef SCALAR
00426
00427
00428
00429 #define VECTOR Vektor3d
00430 #define SCALAR float
00431
00432 class Vektor3d
00433 {
00434 public:
00435 Vektor3d()
00436 {
00437 setNull();
00438 }
00439
00440 Vektor3d(SCALAR px, SCALAR py, SCALAR pz)
00441 {
00442 set(px, py, pz);
00443 }
00444
00445 #include "MyVector3x.h"
00446
00447 #undef SCALARUNSIGNED
00448 #undef USEMATRIX
00449 #undef VECTORMATRIX
00450 #undef VECTOR
00451 #undef SCALAR
00452
00453
00454
00455
00456 #define VECTOR Vektor3l
00457 #define SCALAR long
00458
00459 class Vektor3l
00460 {
00461 public:
00462 Vektor3l()
00463 {
00464 setNull();
00465 }
00466
00467 Vektor3l(SCALAR px, SCALAR py, SCALAR pz)
00468 {
00469 set(px, py, pz);
00470 }
00471
00472 #include "MyVector3x.h"
00473
00474 #undef SCALARUNSIGNED
00475 #undef USEMATRIX
00476 #undef VECTORMATRIX
00477 #undef VECTOR
00478 #undef SCALAR
00479
00480
00481
00482
00483 #define VECTOR Vektor3ul
00484 #define SCALAR unsigned long
00485 #define SCALARUNSIGNED
00486
00487 class Vektor3ul
00488 {
00489 public:
00490 Vektor3ul()
00491 {
00492 setNull();
00493 }
00494
00495 Vektor3ul(SCALAR px, SCALAR py, SCALAR pz)
00496 {
00497 set(px, py, pz);
00498 }
00499
00500 #include "MyVector3x.h"
00501
00502 #undef SCALARUNSIGNED
00503 #undef USEMATRIX
00504 #undef VECTORMATRIX
00505 #undef VECTOR
00506 #undef SCALAR
00507
00508
00509
00510
00511
00512 #define VECTOR Vektor3i
00513 #define SCALAR int
00514
00515 class Vektor3i
00516 {
00517 public:
00518 Vektor3i()
00519 {
00520 setNull();
00521 }
00522
00523 Vektor3i(SCALAR px, SCALAR py, SCALAR pz)
00524 {
00525 set(px, py, pz);
00526 }
00527
00528 #include "MyVector3x.h"
00529
00530 #undef SCALARUNSIGNED
00531 #undef USEMATRIX
00532 #undef VECTORMATRIX
00533 #undef VECTOR
00534 #undef SCALAR
00535
00536
00537
00538
00539 #define VECTOR Vektor3ui
00540 #define SCALAR unsigned int
00541 #define SCALARUNSIGNED
00542
00543 class Vektor3ui
00544 {
00545 public:
00546 Vektor3ui()
00547 {
00548 setNull();
00549 }
00550
00551 Vektor3ui(SCALAR px, SCALAR py, SCALAR pz)
00552 {
00553 set(px, py, pz);
00554 }
00555
00556 #include "MyVector3x.h"
00557
00558 #undef SCALARUNSIGNED
00559 #undef USEMATRIX
00560 #undef VECTORMATRIX
00561 #undef VECTOR
00562 #undef SCALAR
00563
00564
00565
00566
00567
00568 #define VECTOR Vektor3b
00569 #define SCALAR char
00570
00571 class Vektor3b
00572 {
00573 public:
00574 Vektor3b()
00575 {
00576 setNull();
00577 }
00578
00579 Vektor3b(SCALAR px, SCALAR py, SCALAR pz)
00580 {
00581 set(px, py, pz);
00582 }
00583
00584 #include "MyVector3x.h"
00585
00586 #undef SCALARUNSIGNED
00587 #undef USEMATRIX
00588 #undef VECTORMATRIX
00589 #undef VECTOR
00590 #undef SCALAR
00591
00592
00593
00594
00595
00596 #define VECTOR Vektor3ub
00597 #define SCALAR unsigned char
00598 #define SCALARUNSIGNED
00599
00600 class Vektor3ub
00601 {
00602 public:
00603 Vektor3ub()
00604 {
00605 setNull();
00606 }
00607
00608 Vektor3ub(SCALAR px, SCALAR py, SCALAR pz)
00609 {
00610 set(px, py, pz);
00611 }
00612
00613 #include "MyVector3x.h"
00614
00615 #undef SCALARUNSIGNED
00616 #undef USEMATRIX
00617 #undef VECTORMATRIX
00618 #undef VECTOR
00619 #undef SCALAR
00620
00621
00622
00623
00625
00627
00628
00629 #define VECTOR Vektor4f
00630 #define SCALAR float
00631
00632
00633 class Vektor4f
00634 {
00635 public:
00636 Vektor4f()
00637 {
00638 setNull();
00639 }
00640
00641 Vektor4f(SCALAR px, SCALAR py, SCALAR pz, SCALAR ph)
00642 {
00643 set(px, py, pz, ph);
00644 }
00645
00646 #include "MyVector4x.h"
00647
00648
00649 #undef SCALARUNSIGNED
00650 #undef USEMATRIX
00651 #undef VECTORMATRIX
00652 #undef VECTOR
00653 #undef SCALAR
00654
00655
00656
00657 #define VECTOR Vektor4d
00658 #define SCALAR float
00659
00660 class Vektor4d
00661 {
00662 public:
00663 Vektor4d()
00664 {
00665 setNull();
00666 }
00667
00668 Vektor4d(SCALAR px, SCALAR py, SCALAR pz, SCALAR ph)
00669 {
00670 set(px, py, pz, ph);
00671 }
00672
00673 #include "MyVector4x.h"
00674
00675 #undef SCALARUNSIGNED
00676 #undef USEMATRIX
00677 #undef VECTORMATRIX
00678 #undef VECTOR
00679 #undef SCALAR
00680
00681
00682
00683
00684 #define VECTOR Vektor4l
00685 #define SCALAR long
00686
00687 class Vektor4l
00688 {
00689 public:
00690 Vektor4l()
00691 {
00692 setNull();
00693 }
00694
00695 Vektor4l(SCALAR px, SCALAR py, SCALAR pz, SCALAR ph)
00696 {
00697 set(px, py, pz, ph);
00698 }
00699
00700 #include "MyVector4x.h"
00701
00702 #undef SCALARUNSIGNED
00703 #undef USEMATRIX
00704 #undef VECTORMATRIX
00705 #undef VECTOR
00706 #undef SCALAR
00707
00708
00709
00710
00711 #define VECTOR Vektor4ul
00712 #define SCALAR unsigned long
00713 #define SCALARUNSIGNED
00714
00715 class Vektor4ul
00716 {
00717 public:
00718 Vektor4ul()
00719 {
00720 setNull();
00721 }
00722
00723 Vektor4ul(SCALAR px, SCALAR py, SCALAR pz, SCALAR ph)
00724 {
00725 set(px, py, pz, ph);
00726 }
00727
00728 #include "MyVector4x.h"
00729
00730 #undef SCALARUNSIGNED
00731 #undef USEMATRIX
00732 #undef VECTORMATRIX
00733 #undef VECTOR
00734 #undef SCALAR
00735
00736
00737
00738
00739
00740 #define VECTOR Vektor4i
00741 #define SCALAR int
00742
00743 class Vektor4i
00744 {
00745 public:
00746 Vektor4i()
00747 {
00748 setNull();
00749 }
00750
00751 Vektor4i(SCALAR px, SCALAR py, SCALAR pz, SCALAR ph)
00752 {
00753 set(px, py, pz, ph);
00754 }
00755
00756 #include "MyVector4x.h"
00757
00758 #undef SCALARUNSIGNED
00759 #undef USEMATRIX
00760 #undef VECTORMATRIX
00761 #undef VECTOR
00762 #undef SCALAR
00763
00764
00765
00766
00767 #define VECTOR Vektor4ui
00768 #define SCALAR unsigned int
00769 #define SCALARUNSIGNED
00770
00771 class Vektor4ui
00772 {
00773 public:
00774 Vektor4ui()
00775 {
00776 setNull();
00777 }
00778
00779 Vektor4ui(SCALAR px, SCALAR py, SCALAR pz, SCALAR ph)
00780 {
00781 set(px, py, pz, ph);
00782 }
00783
00784 #include "MyVector4x.h"
00785
00786 #undef SCALARUNSIGNED
00787 #undef USEMATRIX
00788 #undef VECTORMATRIX
00789 #undef VECTOR
00790 #undef SCALAR
00791
00792
00793
00794
00795
00796 #define VECTOR Vektor4b
00797 #define SCALAR char
00798
00799 class Vektor4b
00800 {
00801 public:
00802 Vektor4b()
00803 {
00804 setNull();
00805 }
00806
00807 Vektor4b(SCALAR px, SCALAR py, SCALAR pz, SCALAR ph)
00808 {
00809 set(px, py, pz, ph);
00810 }
00811
00812 #include "MyVector4x.h"
00813
00814 #undef SCALARUNSIGNED
00815 #undef USEMATRIX
00816 #undef VECTORMATRIX
00817 #undef VECTOR
00818 #undef SCALAR
00819
00820
00821
00822
00823
00824 #define VECTOR Vektor4ub
00825 #define SCALAR unsigned char
00826 #define SCALARUNSIGNED
00827
00828 class Vektor4ub
00829 {
00830 public:
00831 Vektor4ub()
00832 {
00833 setNull();
00834 }
00835
00836 Vektor4ub(SCALAR px, SCALAR py, SCALAR pz, SCALAR ph)
00837 {
00838 set(px, py, pz, ph);
00839 }
00840
00841 #include "MyVector4x.h"
00842
00843 #undef SCALARUNSIGNED
00844 #undef USEMATRIX
00845 #undef VECTORMATRIX
00846 #undef VECTOR
00847 #undef SCALAR
00848
00849
00850
00851
00852
00853
00854
00855 typedef Vektor3f Vektor;
00856 typedef Vektor4f Color;
00857
00858
00859
00860 class BoundingBox
00861 {
00862 public:
00863 Vektor3f m_Min, m_Max;
00864 Vektor3f v[8];
00865 Vektor3f m_Center;
00866 Vektor3f m_Extents;
00867 float m_Radius;
00868
00869
00870 BoundingBox()
00871 {
00872 m_Radius = 0;
00873 }
00874
00875
00876
00877 void Init(Vektor3f p_Min, Vektor3f p_Max, float p_Radius);
00878
00879
00880 void DrawSphere(Color p_Color = Color(1.0f, 1.0f, 1.0f, 1.0f));
00881
00882
00883 void DrawBox(Color p_Color = Color(1.0f, 1.0f, 1.0f, 1.0f));
00884
00885
00886
00887 void RotateX(float angle);
00888
00889 void RotateY(float angle);
00890
00891 void RotateZ(float angle);
00892
00893 void Rotate(float angle, Vektor3f &p_v);
00894
00895 void Rotate(float angle, float px, float py, float pz);
00896
00897
00898 void Translate(Vektor3f &p_v);
00899
00900 void Translate(float px, float py, float pz);
00901
00902 void Scale(float s);
00903 };
00904
00905
00906
00907 #endif //#if !defined __MYMATHUTILS_H