Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
Frm_3DMath.h
Go to the documentation of this file.
1 // Frm_3DMath.h
3 // Declares vector, matrix, rectangles and defines special
4 // array elements
6 
7 #ifndef FRM_MATH_H
8 #define FRM_MATH_H
9 
10 #include <mem.h>
11 
12 namespace Frm {
13 
14 float ACos(float pValue);
15 
16 //Vector description/////////////////////////
17 template <typename T>
18 class vector {
19  public:
20  T x, y, z;
21  vector(void): x(0), y(0), z(0) {};
22  vector(T px, T py, T pz): x(px), y(py), z(pz) {};
23  vector(const vector<T> &pv):x (pv.x), y(pv.y), z(pv.z){};
24  vector(const T* &pv){x = pv[0]; y = pv[1]; z = pv[2];};
25 
27  {x = pv.x; y = pv.y; z = pv.z; return *this;};
28  inline void Set(T px, T py, T pz){x=px; y=py; z=pz;};
29  bool operator>(const vector<T> &pv){return (x+y)>(pv.x+pv.y);};
30 
31  T operator[](int index)const{if (index== 1) return y; if (index== 2)return z; return x;};
32  T& operator[](int index){if (index== 1) return y; if (index== 2)return z; return x;};
33 
34  vector<T> operator+(vector<T> pv)const {return vector<T>((T)(x + pv.x), (T)(y + pv.y), (T)(z + pv.z));};
35  vector<T> operator-(vector<T> pv)const{return vector<T>((T)(x - pv.x), (T)(y - pv.y),(T)(z - pv.z));};
36  vector<T> operator*(T pT)const{return vector<T>((T)(pT * x), (T)(pT * y), (T)(pT * z));};
37 
38  void Normalize(void){vector<T> pv((T)(x*x),(T)(y*y), (T)(z*z));T fLength = (T)(1.0f/(float)(pv.x + pv.y + pv.z)); if (fLength < 1e-08) return; x = (T)(pv.x * fLength); y = (T)(pv.y * fLength); z = (T)(pv.z * fLength);};
39  T Dot(vector<T> pV)const{return (T)(x*pV.x + y*pV.y + z*pV.z);};
40  vector<T> Cross(vector<T> pV)const{return vector<T>((T)(y * pV.z - z * pV.y), (T)(z * pV.x - x * pV.z), (T)(x * pV.y - y * pV.x));};
41  vector<T> UnitCross(vector<T> pV)const{vector<T> pR((T)(y * pV.z - z * pV.y), (T)(z * pV.x - x * pV.z), (T)(x * pV.y - y * pV.x)); pR.Normalize(); return pR;};
42 };
43 
44 template<typename T, int size>
45 struct Array {
46  T data[size];
47  Array(void){memset(data, 0, size * sizeof(T));};
48  Array(const Array<T, size> &pA){memcpy(data, pA.data, size * sizeof(T));};
49  Array(const T* pT){memcpy(data, pT, size * sizeof(T));};
50 
52  {memcpy(data, pA.data, size * sizeof(T)); return *this;};
54  Array<T, size> operator*(const T pScalar);
56  T operator[](int index)const{return data[index];};
57  T& operator[](int index){return data[index];};
58 };
59 
60 typedef Array<unsigned short, 3> Face;
61 typedef Array<float, 3> Vertex;
62 typedef Array<float, 2> TCoord;
63 typedef Array<float, 4> Color4;
64 typedef Array<float, 3> Color3;
65 
66 template<typename T>
67 Array<T, 4> Set4(T px, T py, T pz, T pw){T data[4]; data[0]=px; data[1]=py; data[2]=pz; data[3]=pw;return Array<T, 4>(data);};
68 
69 template<typename T>
70 Array<T, 3> Set3(T px, T py, T pz){T data[3]; data[0]=px; data[1]=py; data[2]=pz;return Array<T, 3>(data);};
71 
72 template<typename T>
73 Array<T, 2> Set2(T pu, T pv){T data[2]; data[0]=pu; data[1]=pv;return Array<T, 2>(data);};
74 
75 /***********************************************
76 NEW-NEW- NEW- NEW- NEW- NEW- NEW- NEW- NEW- NEW*/
77 
78 template <typename T>
79 class matrix {
80  public:
81  T data[16];
82  matrix(void){memset(data, 0, 16 * sizeof(T));};
83  matrix(const matrix<T> &pm){memcpy(data, pm.data, 16 * sizeof(T));};
84  matrix(const T* pT){memcpy(data, pT, 16 * sizeof(T));};
85  void Zero(void){memset(data, 0, 16 * sizeof(T));};
86  void Identity(void){memset(data, 0, 16 * sizeof(T)); data[0]=(T)1; data[5]=(T)1; data[10]=(T)1; data[15]=(T)1;};
87  matrix<T>& operator=(const matrix<T> &pm){memcpy(data, pm.data, 16 * sizeof(T));return *this;};
95 
96  T operator[](int index)const{return data[index];};
97  T& operator[](int index){return data[index];};
98 
99  T _fastcall Index(int pRow, int pCol)const{return data[(pRow<<2) + pCol];};
100  T operator()(int pRow, int pCol)const{return data[(pRow<<2) + pCol];};
101  T& operator()(int pRow, int pCol){return data[(pRow<<2) + pCol];};
102  vector<T> GetRow(int index){index <<= 2;return vector<T>(data[index++], data[index++], data[index]);};
104 
105  private:
106  int k, l, row, col;
107 };
108 
110 /***END*****************************************/
111 
112 //Rectangle description///////////////////////
113 template <typename T>
114 class trect {
115  public:
116  T xs, ys, xe, ye;
117  trect(void): xs(0), ys(0), xe(0), ye(0){};
118  trect(const trect<T> &prect)
119  {xs = prect.xs; ys = prect.ys; xe = prect.xe; ye = prect.ye;};
120  trect<T>& operator=(const trect<T> &prect)
121  {xs = prect.xs; ys = prect.ys; xe = prect.xe; ye = prect.ye; return *this;};
123  {xs += pV.x; ys += pV.y; xe += pV.x; ye += pV.y; return *this;};
124 /* trect<T>& operator+=(vector<T> &pV)
125  {xs += pV[VX]; ys += pV[VY]; xe += pV[VX]; ye += pV[VY]; return *this;};/**/
126  trect(const T pxs, const T pys, const T pxe, const T pye)
127  {xs = pxs; ys = pys; xe = pxe; ye = pye;};
128  inline void Set(const T pxs, const T pys, const T pxe, const T pye)
129  {xs = pxs; ys = pys; xe = pxe; ye = pye;};
130  bool _fastcall InRect(const T &px, const T &py)
131  { if (px < xs) return false;
132  if (px > xe) return false;
133  if (py < ys) return false;
134  if (py > ye) return false;
135  return true;};
136  bool _fastcall InRect(const vector<T> &pv)
137  { if (pv.x < xs) return false;
138  if (pv.x > xe) return false;
139  if (pv.y < ys) return false;
140  if (pv.y > ye) return false;
141  return true;};
142 /* bool _fastcall InRect( vector<T> &pv)
143  { if (pv[VX] < xs) return false;
144  if (pv[VX] > xe) return false;
145  if (pv[VY] < ys) return false;
146  if (pv[VY] > ye) return false;
147  return true;};/**/
148  bool _fastcall InRect(const trect<T> &prect)
149  { if ((prect.xe <= xe) &&
150  (prect.ye <= ye) &&
151  (prect.xs >= xs) &&
152  (prect.ys >= ys))
153  return true;
154  return false; }
155  bool _fastcall Intersect(const trect<T> &prect)
156  { if (prect.xe < xs) return false;
157  if (prect.xs > xe) return false;
158  if (prect.ye < ys) return false;
159  if (prect.ys > ye) return false;
160  return true;};
161  bool _fastcall Union(const trect<T> &prect)
162  { if (!Intersect(prect)) return false;
163  if (xs < prect.xs) xs = prect.xs;
164  if (ys < prect.ys) ys = prect.ys;
165  if (xe > prect.xe) xe = prect.xe;
166  if (ye > prect.ye) ye = prect.ye;
167  return true;};
168 };
169 
171 
172 template<typename T, int size>
173 Array<T, size>& Array<T, size>::operator+=(Array<T, size> &pA)
174 {
175  for (int i= 0; i< size; i++)
176  data[i] += pA.data[i];
177  return *this;
178 }
179 
180 template<typename T, int size>
182 {
183  T rdata[size];
184  memcpy(rdata, data, size * sizeof(T));
185  for (int i= 0; i< size; i++)
186  rdata[i]*=pScalar;
187  return Array<T,size>(rdata);
188 }
189 
190 template<typename T, int size>
192 {
193  T rdata[size];
194  for (int i= 0; i< size; i++)
195  rdata[i]=data[i] + pA.data[i];
196  return Array<T,size>(rdata);
197 };
198 
199 template<typename T>
201 {
202  T Rdata[16];
203  for(k = 0; k < 16; k++)
204  Rdata[k] = data[k] + pm.data[k];
205  return matrix<T>(Rdata);
206 }
207 
208 template<typename T>
210 {
211  T Rdata[16];
212  for(k = 0; k < 16; k++)
213  Rdata[k] = data[k] - pm.data[k];
214  return matrix<T>(Rdata);
215 }
216 
217 template<typename T>
219 {
220  T Rdata[16];
221  for (row = 0; row < 16; row +=4)
222  for (col = 0; col < 4; col ++)
223  {
224  l = 0;
225  Rdata[row + col] = 0;
226  for (k = 0; k < 4; k++, l += 4)
227  Rdata[row + col] += data[row + k] * pm.data[l + col];
228  }
229  return matrix<T>(Rdata);
230 }
231 
232 template<typename T>
234 {
235  T Rdata[16];
236  for (row = 0; row < 16; row +=4)
237  for (col = 0; col < 4; col ++)
238  {
239  l = 0;
240  Rdata[row + col] = 0;
241  for (k = 0; k < 4; k++, l += 4)
242  Rdata[row + col] += data[row + k] * pm.data[l + col];
243  }
244  memcpy(data, Rdata, 16*sizeof(T));
245  return *this;
246 }
247 
248 template<typename T>
250 {
251  T Rdata[16];
252  memcpy(Rdata, data, 16 * sizeof(T));
253  for(k = 0; k < 16; k++)
254  Rdata[k] *= pT;
255  return matrix<T>(Rdata);
256 }
257 
258 template<typename T>
260 {
261  T vdata[4], pvdata[4];
262  pvdata[0] = pV.x;
263  pvdata[1] = pV.y;
264  pvdata[2] = pV.z;
265  pvdata[3] = 0;
266  for (col = 0; col < 4; col++)
267  {
268  vdata[row] = 0;
269  k=0;
270  for (row = 0; row < 4; row++, k+=4)
271  vdata[col] += data[k + col]*pvdata[row];
272  }
273  return vector<T>(vdata);
274 }
275 
276 template<typename T>
278 {
279  T vdata[4], pvdata[4];
280 
281  memcpy(pvdata, pV.data, 3*sizeof(T));
282  pvdata[3] = 1.0f;
283 
284  for (col = 0; col < 4; col++)
285  {
286  k = 0;
287  vdata[col] = 0;
288  for (row = 0; row < 4; row++, k+=4)
289  vdata[col] += data[k + col]*pvdata[row];
290  }
291 
292  return Array<T, 3>(vdata);
293 }
294 
295 };
296 
297 //#include "framework\Frm_3dMath.cpp"
298 
299 #endif
Using X files without the sources and the makefile How to use you just create a debug directory e the sample3 directory must contain Sample3 Final Sample3 exe Sample3 Final Debug Sample3 Final Gfx OpenGL bmp Sample3 Final Gfx tiny_skin bmp Sample3 Final Gfx tiny_4anim x The source files have the DevCpp project file plus the makefile The demos use standard FreeGlut functions Technical without warranty Neither Paul Coppens nor GameDev net make any or either express or with respect to the their or fitness for a specific purpose neither Paul Coppens nor GameDev net shall have any liability to you or any other person or entity with respect to any or damage caused or alleged to have been caused directly or indirectly by the programs provided by Paul Coppens and GameDev net This but is not limited interruption of loss of data
Definition: Readme.txt:39
Definition: Frm_3DMath.h:79
matrix< T > operator*(T pT)
Definition: Frm_3DMath.h:249
T & operator[](int index)
Definition: Frm_3DMath.h:97
vector< T > GetRow(int index)
Definition: Frm_3DMath.h:102
void Zero(void)
Definition: Frm_3DMath.h:85
matrix< T > operator*(matrix< T > &pm)
Definition: Frm_3DMath.h:218
matrix(const T *pT)
Definition: Frm_3DMath.h:84
void Identity(void)
Definition: Frm_3DMath.h:86
matrix(const matrix< T > &pm)
Definition: Frm_3DMath.h:83
matrix< T > operator-(matrix< T > &pm)
Definition: Frm_3DMath.h:209
matrix< T > & operator*=(matrix< T > &pm)
Definition: Frm_3DMath.h:233
T data[16]
Definition: Frm_3DMath.h:81
matrix< T > & operator=(const matrix< T > &pm)
Definition: Frm_3DMath.h:87
vector< T > GetColumn(int index)
Definition: Frm_3DMath.h:103
matrix< T > operator+(matrix< T > &pm)
Definition: Frm_3DMath.h:200
T operator()(int pRow, int pCol) const
Definition: Frm_3DMath.h:100
T _fastcall Index(int pRow, int pCol) const
Definition: Frm_3DMath.h:99
Array< T, 3 > operator*(const Array< T, 3 > &pV)
Definition: Frm_3DMath.h:277
matrix(void)
Definition: Frm_3DMath.h:82
vector< T > operator*(const vector< T > &pV)
Definition: Frm_3DMath.h:259
T operator[](int index) const
Definition: Frm_3DMath.h:96
T & operator()(int pRow, int pCol)
Definition: Frm_3DMath.h:101
Definition: Frm_3DMath.h:114
bool _fastcall Union(const trect< T > &prect)
Definition: Frm_3DMath.h:161
trect< T > & operator=(const trect< T > &prect)
Definition: Frm_3DMath.h:120
bool _fastcall InRect(const trect< T > &prect)
Definition: Frm_3DMath.h:148
trect(const T pxs, const T pys, const T pxe, const T pye)
Definition: Frm_3DMath.h:126
trect(void)
Definition: Frm_3DMath.h:117
T ys
Definition: Frm_3DMath.h:116
bool _fastcall InRect(const T &px, const T &py)
Definition: Frm_3DMath.h:130
bool _fastcall InRect(const vector< T > &pv)
Definition: Frm_3DMath.h:136
trect< T > & operator+=(const vector< T > &pV)
Definition: Frm_3DMath.h:122
trect(const trect< T > &prect)
Definition: Frm_3DMath.h:118
bool _fastcall Intersect(const trect< T > &prect)
Definition: Frm_3DMath.h:155
T xs
Definition: Frm_3DMath.h:116
T xe
Definition: Frm_3DMath.h:116
T ye
Definition: Frm_3DMath.h:116
void Set(const T pxs, const T pys, const T pxe, const T pye)
Definition: Frm_3DMath.h:128
Definition: Frm_3DMath.h:16
vector< T > & operator=(const vector< T > &pv)
Definition: Frm_3DMath.h:26
T y
Definition: Frm_3DMath.h:18
T & operator[](int index)
Definition: Frm_3DMath.h:32
vector< T > operator*(T pT) const
Definition: Frm_3DMath.h:36
vector< T > Cross(vector< T > pV) const
Definition: Frm_3DMath.h:40
bool operator>(const vector< T > &pv)
Definition: Frm_3DMath.h:29
vector(T px, T py, T pz)
Definition: Frm_3DMath.h:22
void Set(T px, T py, T pz)
Definition: Frm_3DMath.h:28
vector< T > operator+(vector< T > pv) const
Definition: Frm_3DMath.h:34
T x
Definition: Frm_3DMath.h:18
vector(const T *&pv)
Definition: Frm_3DMath.h:24
vector< T > operator-(vector< T > pv) const
Definition: Frm_3DMath.h:35
vector(const vector< T > &pv)
Definition: Frm_3DMath.h:23
vector(void)
Definition: Frm_3DMath.h:21
T operator[](int index) const
Definition: Frm_3DMath.h:31
vector< T > UnitCross(vector< T > pV) const
Definition: Frm_3DMath.h:41
T Dot(vector< T > pV) const
Definition: Frm_3DMath.h:39
void Normalize(void)
Definition: Frm_3DMath.h:38
T z
Definition: Frm_3DMath.h:18
Definition: Frm.h:12
float ACos(float pValue)
Array< T, 2 > Set2(T pu, T pv)
Definition: Frm_3DMath.h:42
Array< T, 3 > Set3(T px, T py, T pz)
Definition: Frm_3DMath.h:39
Array< float, 3 > Vertex
Definition: Frm_3DMath.h:30
matrix< float > glMatrix
Definition: Frm_3DMath.h:109
Array< float, 4 > Color4
Definition: Frm_3DMath.h:32
Array< T, 4 > Set4(T px, T py, T pz, T pw)
Definition: Frm_3DMath.h:36
Array< float, 2 > TCoord
Definition: Frm_3DMath.h:31
Array< float, 3 > Color3
Definition: Frm_3DMath.h:33
Array< unsigned short, 3 > Face
Definition: Frm_3DMath.h:58
uint16 index
Definition: sample3.cpp:29
Definition: Frm_3DMath.h:15
Array(const T *pT)
Definition: Frm_3DMath.h:49
Array< T, size > operator+(Array< T, size > &pA)
Definition: Frm_3DMath.h:65
Array(void)
Definition: Frm_3DMath.h:47
Array< T, size > & operator=(Array< T, size > pA)
Definition: Frm_3DMath.h:51
Array< T, size > operator*(const T pScalar)
Definition: Frm_3DMath.h:181
T data[size]
Definition: Frm_3DMath.h:16
T & operator[](int index)
Definition: Frm_3DMath.h:57
T operator[](int index) const
Definition: Frm_3DMath.h:56
Array(const Array< T, size > &pA)
Definition: Frm_3DMath.h:48
Array< T, size > & operator+=(Array< T, size > &pA)
Definition: Frm_3DMath.h:47
Array< T, size > & operator+=(Array< T, size > &pA)
Array< T, size > operator+(Array< T, size > &pA)