1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        mathstuff.h
3 // Purpose:     Some maths used for pyramid sample
4 // Author:      Manuel Martin
5 // Created:     2015/01/31
6 // Copyright:   (c) 2015 Manuel Martin
7 // Licence:     wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9 
10 #ifndef MATHSTUFF_H
11   #define MATHSTUFF_H
12 
13 //NOTE:
14 // glm library is great for handling matrices and vectors in a OpenGL style, see
15 // http://glm.g-truc.net/
16 // But it's too large for this simple sample. I coded on my own the maths needed.
17 
18 // A vector with 3 components
19 class myVec3
20 {
21 public:
myVec3()22     myVec3() { x = y = z = 0.0 ;}
myVec3(double xd,double yd,double zd)23     myVec3(double xd, double yd, double zd) : x(xd), y(yd), z(zd) {}
myVec3(float xd,float yd,float zd)24     myVec3(float xd, float yd, float zd) : x(double(xd)), y(double(yd)), z(double(zd)) {}
~myVec3()25     ~myVec3() {}
26 
27     double x, y, z;
28 };
29 
30 // A vector with 4 components
31 class myVec4
32 {
33 public:
myVec4()34     myVec4() { x = y = z = w = 0.0 ;}
myVec4(double xd,double yd,double zd,double wd)35     myVec4(double xd, double yd, double zd, double wd)
36             : x(xd), y(yd), z(zd), w(wd) {}
37     myVec4(const myVec3& v3, double wd = 0.0)
38             : x(v3.x), y(v3.y), z(v3.z), w(wd) {}
~myVec4()39     ~myVec4() {}
40 
41     double x, y, z, w;
42 };
43 
44 // Overload of "-" operator
45 myVec3 operator- (const myVec3& v1, const myVec3& v2);
46 
47 // Vector normalization
48 myVec3 MyNormalize(const myVec3& v);
49 
50 // Dot product
51 double MyDot(const myVec3& v1, const myVec3& v2);
52 
53 // Cross product
54 myVec3 MyCross(const myVec3& v1, const myVec3& v2);
55 
56 // Distance between two points
57 double MyDistance(const myVec3& v1, const myVec3& v2);
58 
59 // Angle between two normalized vectors, in radians
60 double AngleBetween(const myVec3& v1, const myVec3& v2);
61 
62 // Matrix 4x4 by 4x1 multiplication
63 myVec4 MyMatMul4x1(const double *m1, const myVec4& v);
64 
65 // Matrix 4x4 by 4x4 multiplication
66 void MyMatMul4x4(const double *m1, const double *m2, double* mm);
67 
68 // Matrix inverse. Returns the determinant
69 double MyMatInverse(const double *m, double *minv);
70 
71 // Matrix of rotation around an axis in the origin
72 void MyRotate(const myVec3& axis, double angle, double *mrot);
73 
74 // Matrix for defining the viewing transformation
75 void MyLookAt(const myVec3& camPos, const myVec3& camUp, const myVec3& targ, double *mt);
76 
77 // Matrix for defining the perspective projection with symmetric frustum
78 void MyPerspective(double fov, double aspect, double zNear, double zFar, double *mp);
79 
80 // Matrix for defining the orthogonal projection
81 void MyOrtho(double left, double right, double bottom, double top,
82              double zNear, double zFar, double *mo);
83 
84 #endif // MATHSTUFF_H
85