This repository has been archived by the owner on Feb 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Geometry.h
171 lines (121 loc) · 3.51 KB
/
Geometry.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// CVector.h -
#ifndef __CGEOMETRY_INCLUDED
#define __CGEOMETRY_INCLUDED
#include <cmath>
#include <memory.h>
#define PI 3.1415926f
#define PI2 6.2831853f
#define PI_2 1.5707963f
//////////////////
class CVector;
class CVector2D;
class CMatrix;
class CBoundBox;
//////////////////
// 3D vector
class CVector
{
public:
float x,y,z; // components
//float nx, ny, nz; // normals
CVector() { x = y = z = 0.0f; };
CVector( float X, float Y, float Z );
CVector( const CVector &v );
CVector( CVector v1, CVector v2 );
void operator=( CVector v );
CVector operator+( CVector v );
CVector operator-( CVector v );
CVector operator*( float );
CVector operator/( float );
void operator+=( CVector v );
void operator-=( CVector v );
void operator*=( float );
void operator/=( float );
bool operator==( CVector v );
bool operator!=( CVector v );
void ToArray( float* );
void NormalsToArray( float* );
void Product( CVector v1, CVector v2 );
float GetMagnitude();
//void SetNormals();
void Normalize();
void NormalizeTriangle( CVector triangle[] );
void glNormal();
void glVector() { glVertex3f( x, y, z ); };
};
// 2D vector ( stores UV coordinates )
class CVector2D
{
public:
float x,y; // components
CVector2D() { x = y = 0.0f; };
CVector2D( float X, float Y );
CVector2D( const CVector2D &v );
CVector2D( CVector2D v1, CVector2D v2 );
void operator=( CVector2D v );
CVector2D operator+( CVector2D v );
CVector2D operator-( CVector2D v );
CVector2D operator*( float );
CVector2D operator/( float );
void operator+=( CVector2D v );
void operator-=( CVector2D v );
void operator*=( float );
void operator/=( float );
bool operator==( CVector2D v );
bool operator!=( CVector2D v );
void ToArray( float* );
float GetMagnitude();
void Normalize();
};
// Bounding box - class
class CBoundBox
{
private:
CVector pos; // position in 3D space
CVector dims; // w,h,l - dimensions
CVector edge; // pos + dims
public:
CBoundBox();
CBoundBox( CVector position, CVector dimensions );
// check for vertex collision
bool Collide( const CVector &v );
// check if it collide with another box
bool Collide( CBoundBox &bb );
void setPosition( CVector position ) { pos = position; edge = pos + dims; };
void setDimensions( CVector dimensions ) { dims = dimensions; edge = pos + dims; };
CVector getPosition() { return pos; };
CVector getDimensions() { return dims; };
CVector getEdge() { return edge; };
};
/*
// identity matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
*/
// Matrices {!} - To be completed...
class CMatrix
{
public:
float mat[16];
CMatrix() { Identity(); };
CMatrix( const CMatrix &m ) { memcpy( mat, m.mat, sizeof(mat) ); };
CMatrix( float *m ) { memcpy( mat, m, sizeof(mat) ); };
void Identity() { Clear(); mat[0] = mat[5] = mat[10] = mat[15] = 1.0f; };
void Clear() { memset( mat, 0, sizeof(mat) ); /*for( int i = 0; i < 16; i++ ) mat[i] = 0.0f;*/ };
CMatrix operator* ( CMatrix &m );
CMatrix operator*= ( CMatrix &m );
CMatrix operator+ ( CMatrix &m );
CMatrix operator+= ( CMatrix &m );
CVector operator *( CVector &v );
void Transpose();
void Translate( float x, float y, float z );
void Scale( float sx, float sy, float sz );
void RotateX( int angle );
void RotateY( int angle );
void RotateZ( int angle );
void Rotate( int xa, int ya, int za );
void FlipX();
};
#endif