SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
WAIMath.h
Go to the documentation of this file.
1 #ifndef WAI_MATH_H
2 #define WAI_MATH_H
3 
4 #include <math.h>
5 
6 //! WAI : Where Am I: Collection of duplicate structs for vectors
7 namespace WAI
8 {
9 //-----------------------------------------------------------------------------
10 struct V2
11 {
12  union
13  {
14  float e[2];
15  struct
16  {
17  float x, y;
18  };
19  struct
20  {
21  float u, v;
22  };
23  };
24 };
25 //-----------------------------------------------------------------------------
26 struct V3
27 {
28  union
29  {
30  float e[3];
31  struct
32  {
33  float x, y, z;
34  };
35  };
36 };
37 //-----------------------------------------------------------------------------
38 inline V3 v3(float x, float y, float z)
39 {
40  V3 result;
41 
42  result.x = x;
43  result.y = y;
44  result.z = z;
45 
46  return result;
47 }
48 //-----------------------------------------------------------------------------
49 inline V3 subV3(V3 v1, V3 v2)
50 {
51  V3 result;
52 
53  result.x = v1.x - v2.x;
54  result.y = v1.y - v2.y;
55  result.z = v1.z - v2.z;
56 
57  return result;
58 }
59 //-----------------------------------------------------------------------------
60 struct M3x3
61 {
62  float e[3][3];
63 };
64 
66 {
67  M3x3 result;
68 
69  for (int i = 0; i < 3; i++)
70  {
71  for (int j = 0; j < 3; j++)
72  {
73  if (i == j)
74  {
75  result.e[i][j] = 1.0f;
76  }
77  else
78  {
79  result.e[i][j] = 0.0f;
80  }
81  }
82  }
83 
84  return result;
85 }
86 //-----------------------------------------------------------------------------
87 inline M3x3 multM3x3(M3x3 m1, M3x3 m2)
88 {
89  M3x3 result = {};
90 
91  for (int i = 0; i < 3; i++)
92  {
93  for (int j = 0; j < 3; j++)
94  {
95  for (int k = 0; k < 3; k++)
96  {
97  float v1 = m1.e[k][j];
98  float v2 = m2.e[i][k];
99  result.e[i][j] += v1 * v2;
100  }
101  }
102  }
103 
104  return result;
105 }
106 //-----------------------------------------------------------------------------
107 inline V3 multM3x3V3(M3x3 m, V3 v)
108 {
109  V3 result = {};
110 
111  for (int i = 0; i < 3; i++)
112  {
113  for (int j = 0; j < 3; j++)
114  {
115  result.e[i] += m.e[j][i] * v.e[j];
116  }
117  }
118 
119  return result;
120 }
121 //-----------------------------------------------------------------------------
122 inline M3x3 rotateXM3x3(float theta)
123 {
124  M3x3 result = identityM3x3();
125 
126  result.e[1][1] = cos(theta);
127  result.e[1][2] = sin(theta);
128  result.e[2][1] = -1 * sin(theta);
129  result.e[2][2] = cos(theta);
130 
131  return result;
132 }
133 
134 struct M4x4
135 {
136  float e[4][4];
137 };
138 }
139 //-----------------------------------------------------------------------------
140 #endif
WAI : Where Am I: Collection of duplicate structs for vectors.
Definition: WAIMath.h:8
M3x3 multM3x3(M3x3 m1, M3x3 m2)
Definition: WAIMath.h:87
V3 subV3(V3 v1, V3 v2)
Definition: WAIMath.h:49
M3x3 identityM3x3()
Definition: WAIMath.h:65
M3x3 rotateXM3x3(float theta)
Definition: WAIMath.h:122
V3 v3(float x, float y, float z)
Definition: WAIMath.h:38
V3 multM3x3V3(M3x3 m, V3 v)
Definition: WAIMath.h:107
float e[3][3]
Definition: WAIMath.h:62
float e[4][4]
Definition: WAIMath.h:136
float u
Definition: WAIMath.h:21
float y
Definition: WAIMath.h:17
float x
Definition: WAIMath.h:17
float v
Definition: WAIMath.h:21
float e[2]
Definition: WAIMath.h:14
float x
Definition: WAIMath.h:33
float z
Definition: WAIMath.h:33
float e[3]
Definition: WAIMath.h:30
float y
Definition: WAIMath.h:33