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 //Vector description/////////////////////////
15 template <typename T>
16 class vector {
17  public:
18  T x, y, z;
19  vector(void): x(0), y(0), z(0) {};
20  vector(T px, T py, T pz): x(px), y(py), z(pz) {};
21  vector(const vector<T> &pv):x (pv.x), y(pv.y), z(pv.z){};
22  vector(const T* &pv){x = pv[0]; y = pv[1]; z = pv[2];};
23 
25  {x = pv.x; y = pv.y; z = pv.z; return *this;};
26  inline void Set(T px, T py, T pz){x=px; y=py; z=pz;};
27  bool operator>(const vector<T> &pv){return (x+y)>(pv.x+pv.y);};
28 
29  T operator[](int index)const{if (index== 1) return y; if (index== 2)return z; return x;};
30  T& operator[](int index){if (index== 1) return y; if (index== 2)return z; return x;};
31 
32  vector<T> operator+(vector<T> pv)const {return vector<T>((T)(x + pv.x), (T)(y + pv.y), (T)(z + pv.z));};
33  vector<T> operator-(vector<T>& pv)const{return vector<T>((T)(x - pv.x), (T)(y - pv.y),(T)(z - pv.z));};
34  vector<T> operator*(T& pT)const{return vector<T>((T)(pT * x), (T)(pT * y), (T)(pT * z));};
35 
36  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);};
37  T Dot(vector<T> pV)const{return (T)(x*pV.x + y*pV.y + z*pV.z);};
38  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));};
39  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;};
40 };
41 
42 template<typename T, int size>
43 struct Array {
44  T data[size];
45  Array(void){memset(data, 0, size * sizeof(T));};
46  Array(const Array<T, size> &pA){memcpy(data, pA.data, size * sizeof(T));};
47  Array(const T* pT){memcpy(data, pT, size * sizeof(T));};
48 
50  {memcpy(data, pA.data, size * sizeof(T)); return *this;};
52  Array<T, size> Mult(const T pScalar);
54  T operator[](int index)const{return data[index];};
55  T& operator[](int index){return data[index];};
56 };
57 
59 typedef Array<float, 3> Vertex;
60 typedef Array<float, 2> TCoord;
61 typedef Array<float, 4> Color4;
62 typedef Array<float, 3> Color3;
63 
64 template<typename T>
65 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);};
66 
67 template<typename T>
68 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);};
69 
70 template<typename T>
71 Array<T, 2> Set2(T pu, T pv){T data[2]; data[0]=pu; data[1]=pv;return Array<T, 2>(data);};
72 
73 template<typename T, int size>
74 Array<T, size>& Array<T, size>::operator+=(Array<T, size> &pA)
75 {
76  for (int i= 0; i< size; i++)
77  data[i] += pA.data[i];
78  return *this;
79 }
80 
81 template<typename T, int size>
82 Array<T, size> Array<T, size>::Mult(const T pScalar)
83 {
84  T rdata[size];
85  memcpy(rdata, data, size * sizeof(T));
86  for (int i= 0; i< size; i++)
87  rdata[i]*=pScalar;
88  return Array<T,size>(rdata);
89 }
90 
91 template<typename T, int size>
92 Array<T, size> Array<T, size>::operator+(Array<T, size> &pA)
93 {
94  T rdata[size];
95  for (int i= 0; i< size; i++)
96  rdata[i]=data[i] + pA.data[i];
97  return Array<T,size>(rdata);
98 };
99 
100 
101 };
102 #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:16
T y
Definition: Frm_3DMath.h:18
T & operator[](int index)
Definition: Frm_3DMath.h:30
vector< T > Cross(vector< T > pV) const
Definition: Frm_3DMath.h:38
vector< T > operator-(vector< T > &pv) const
Definition: Frm_3DMath.h:33
bool operator>(const vector< T > &pv)
Definition: Frm_3DMath.h:27
vector< T > & operator=(const vector< T > pv)
Definition: Frm_3DMath.h:24
vector< T > operator*(T &pT) const
Definition: Frm_3DMath.h:34
vector(T px, T py, T pz)
Definition: Frm_3DMath.h:20
void Set(T px, T py, T pz)
Definition: Frm_3DMath.h:26
vector< T > operator+(vector< T > pv) const
Definition: Frm_3DMath.h:32
T x
Definition: Frm_3DMath.h:18
vector(const T *&pv)
Definition: Frm_3DMath.h:22
vector(const vector< T > &pv)
Definition: Frm_3DMath.h:21
vector(void)
Definition: Frm_3DMath.h:19
T operator[](int index) const
Definition: Frm_3DMath.h:29
vector< T > UnitCross(vector< T > pV) const
Definition: Frm_3DMath.h:39
T Dot(vector< T > pV) const
Definition: Frm_3DMath.h:37
void Normalize(void)
Definition: Frm_3DMath.h:36
T z
Definition: Frm_3DMath.h:18
Definition: Frm.h:12
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
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:47
Array< T, size > Mult(const T pScalar)
Definition: Frm_3DMath.h:55
Array< T, size > operator+(Array< T, size > &pA)
Definition: Frm_3DMath.h:65
Array(void)
Definition: Frm_3DMath.h:45
Array< T, size > & operator=(Array< T, size > pA)
Definition: Frm_3DMath.h:49
Array< T, size > Mult(const T pScalar)
T data[size]
Definition: Frm_3DMath.h:16
T & operator[](int index)
Definition: Frm_3DMath.h:55
T operator[](int index) const
Definition: Frm_3DMath.h:54
Array(const Array< T, size > &pA)
Definition: Frm_3DMath.h:46
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)