~ubuntu-branches/ubuntu/edgy/glui/edgy

« back to all changes in this revision

Viewing changes to algebra3.h

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2001-09-23 12:57:28 UTC
  • Revision ID: james.westby@ubuntu.com-20010923125728-qbts7xit2eg1ogo2
Tags: upstream-2.1.0.beta
ImportĀ upstreamĀ versionĀ 2.1.0.beta

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************
 
2
        
 
3
  algebra3.cpp, algebra3.h -  C++ Vector and Matrix Algebra routines           
 
4
 
 
5
  There are three vector classes and two matrix classes: vec2, vec3,
 
6
  vec4, mat3, and mat4.
 
7
 
 
8
  All the standard arithmetic operations are defined, with '*'
 
9
  for dot product of two vectors and multiplication of two matrices,
 
10
  and '^' for cross product of two vectors.
 
11
 
 
12
  Additional functions include length(), normalize(), homogenize for
 
13
  vectors, and print(), set(), apply() for all classes.
 
14
 
 
15
  There is a function transpose() for matrices, but note that it 
 
16
  does not actually change the matrix, 
 
17
 
 
18
  When multiplied with a matrix, a vector is treated as a row vector
 
19
  if it precedes the matrix (v*M), and as a column vector if it
 
20
  follows the matrix (M*v).
 
21
 
 
22
  Matrices are stored in row-major form.
 
23
 
 
24
  A vector of one dimension (2d, 3d, or 4d) can be cast to a vector
 
25
  of a higher or lower dimension.  If casting to a higher dimension,
 
26
  the new component is set by default to 1.0, unless a value is
 
27
  specified:
 
28
     vec3 a(1.0, 2.0, 3.0 );
 
29
     vec4 b( a, 4.0 );       // now b == {1.0, 2.0, 3.0, 4.0};
 
30
  When casting to a lower dimension, the vector is homogenized in
 
31
  the lower dimension.  E.g., if a 4d {X,Y,Z,W} is cast to 3d, the
 
32
  resulting vector is {X/W, Y/W, Z/W}.  It is up to the user to 
 
33
  insure the fourth component is not zero before casting.
 
34
 
 
35
  There are also the following function for building matrices:
 
36
     identity2D(), translation2D(), rotation2D(),
 
37
     scaling2D(),  identity3D(),    translation3D(),
 
38
     rotation3D(), rotation3Drad(),  scaling3D(),
 
39
     perspective3D()
 
40
 
 
41
  NOTE: When compiling for Windows, include this file first, to avoid
 
42
        certain name conflicts
 
43
 
 
44
  ---------------------------------------------------------------------
 
45
  
 
46
  Author: Jean-Francois DOUEg                                   
 
47
  Revised: Paul Rademacher                                      
 
48
  Version 3.2 - Feb 1998
 
49
                                                                
 
50
**************************************************************************/
 
51
 
 
52
#ifndef _ALGEBRA3_H_
 
53
#define _ALGEBRA3_H_
 
54
 
 
55
#include <math.h>
 
56
#include <stdio.h>
 
57
#include <stdlib.h>
 
58
 
 
59
// this line defines a new type: pointer to a function which returns a
 
60
// float and takes as argument a float
 
61
typedef float (*V_FCT_PTR)(float);
 
62
 
 
63
// min-max macros
 
64
#ifndef MIN
 
65
#define MIN(A,B) ((A) < (B) ? (A) : (B))
 
66
#define MAX(A,B) ((A) > (B) ? (A) : (B))
 
67
#endif
 
68
 
 
69
//#include <stream.h>
 
70
// error handling macro
 
71
//#define VEC_ERROR(E) { cerr << E; exit(1); }
 
72
/*#define << 
 
73
#define >>*/
 
74
 
 
75
#ifdef VEC_ERROR_FATAL
 
76
#ifndef VEC_ERROR
 
77
#define VEC_ERROR(E) { printf( "VERROR %s\n", E ); exit(1); }
 
78
#endif
 
79
#else
 
80
#ifndef VEC_ERROR
 
81
#define VEC_ERROR(E) { printf( "VERROR %s\n", E ); }
 
82
#endif
 
83
#endif
 
84
 
 
85
class vec2;
 
86
class vec3;
 
87
class vec4;
 
88
class mat3;
 
89
class mat4;
 
90
 
 
91
/*#ifndef X
 
92
enum {X,Y,Z,W};
 
93
#endif
 
94
*/
 
95
 
 
96
/*#ifndef R
 
97
enum {R,G,B,ALPHA};
 
98
#endif
 
99
*/
 
100
 
 
101
#ifndef M_PI
 
102
#define M_PI 3.141592654
 
103
#endif
 
104
 
 
105
 
 
106
enum {VX, VY, VZ, VW};              // axes
 
107
enum {PA, PB, PC, PD};              // planes
 
108
enum {RED, GREEN, BLUE, ALPHA};     // colors
 
109
enum {KA, KD, KS, ES};              // phong coefficients
 
110
 
 
111
/****************************************************************
 
112
*                                                               *
 
113
*                           2D Vector                           *
 
114
*                                                               *
 
115
****************************************************************/
 
116
 
 
117
class vec2
 
118
{
 
119
protected:
 
120
 
 
121
  float n[2];
 
122
 
 
123
public:
 
124
 
 
125
  // Constructors
 
126
 
 
127
       vec2(void);
 
128
  vec2(const float x, const float y);
 
129
  vec2(const float d);
 
130
  vec2(const vec2& v);                  // copy constructor
 
131
                                             vec2(const vec3& v);                       // cast v3 to v2
 
132
                                                                                             vec2(const vec3& v, int dropAxis); // cast v3 to v2
 
133
 
 
134
                                                                                                                                     // Assignment operators
 
135
 
 
136
                                                                                                                                     vec2& operator     = ( const vec2& v );    // assignment of a vec2
 
137
                                                                                                                                                                                     vec2& operator += ( const vec2& v );       // incrementation by a vec2
 
138
                                                                                                                                                                                                                                     vec2& operator -= ( const vec2& v );       // decrementation by a vec2
 
139
                                                                                                                                                                                                                                                                                     vec2& operator *= ( const float d );       // multiplication by a constant
 
140
                                                                                                                                                                                                                                                                                                                                     vec2& operator /= ( const float d );       // division by a constant
 
141
                                                                                                                                                                                                                                                                                                                                                                                     float& operator [] ( int i);               // indexing
 
142
 
 
143
                                                                                                                                                                                                                                                                                                                                                                                                                                     // special functions
 
144
 
 
145
                                                                                                                                                                                                                                                                                                                                                                                                                                     float length(void);                        // length of a vec2
 
146
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     float length2(void);                       // squared length of a vec2
 
147
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     vec2& normalize(void);                     // normalize a vec2
 
148
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     vec2& apply(V_FCT_PTR fct);                // apply a func. to each component
 
149
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     void  set( float x, float y );  // set vector
 
150
 
 
151
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          // friends
 
152
 
 
153
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          friend vec2 operator - (const vec2& v);                           // -v1
 
154
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec2 operator + (const vec2& a, const vec2& b);     // v1 + v2
 
155
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec2 operator - (const vec2& a, const vec2& b);     // v1 - v2
 
156
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec2 operator * (const vec2& a, const float d);     // v1 * 3.0
 
157
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec2 operator * (const float d, const vec2& a);     // 3.0 * v1
 
158
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec2 operator * (const mat3& a, const vec2& v);     // M . v
 
159
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec2 operator * (const vec2& v, mat3& a);           // v . M
 
160
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend float operator * (const vec2& a, const vec2& b);    // dot product
 
161
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec2 operator / (const vec2& a, const float d);     // v1 / 3.0
 
162
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator ^ (const vec2& a, const vec2& b);     // cross product
 
163
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend int operator == (const vec2& a, const vec2& b);     // v1 == v2 ?
 
164
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend int operator != (const vec2& a, const vec2& b);     // v1 != v2 ?
 
165
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 //friend ostream& operator << (ostream& s, vec2& v);       // output to stream
 
166
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 //friend istream& operator >> (istream& s, vec2& v);       // input from strm.
 
167
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend void swap(vec2& a, vec2& b);                        // swap v1 & v2
 
168
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec2 min_vec(const vec2& a, const vec2& b);                 // min(v1, v2)
 
169
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec2 max_vec(const vec2& a, const vec2& b);                 // max(v1, v2)
 
170
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec2 prod(const vec2& a, const vec2& b);                    // term by term *
 
171
 
 
172
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         // necessary friend declarations
 
173
 
 
174
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend class vec3;
 
175
};
 
176
 
 
177
/****************************************************************
 
178
*                                                               *
 
179
*                           3D Vector                           *
 
180
*                                                               *
 
181
****************************************************************/
 
182
 
 
183
class vec3
 
184
{
 
185
protected:
 
186
 
 
187
  float n[3];
 
188
 
 
189
public:
 
190
 
 
191
  // Constructors
 
192
 
 
193
       vec3(void);
 
194
  vec3(const float x, const float y, const float z);
 
195
  vec3(const float d);
 
196
  vec3(const vec3& v);                      // copy constructor
 
197
                                                 vec3(const vec2& v);                       // cast v2 to v3
 
198
                                                                                                 vec3(const vec2& v, float d);              // cast v2 to v3
 
199
                                                                                                                                                 vec3(const vec4& v);                       // cast v4 to v3
 
200
                                                                                                                                                                                                 vec3(const vec4& v, int dropAxis);         // cast v4 to v3
 
201
 
 
202
                                                                                                                                                                                                                                                 // Assignment operators
 
203
 
 
204
                                                                                                                                                                                                                                                 vec3& operator = ( const vec3& v );        // assignment of a vec3
 
205
                                                                                                                                                                                                                                                                                                 vec3& operator += ( const vec3& v );       // incrementation by a vec3
 
206
                                                                                                                                                                                                                                                                                                                                                 vec3& operator -= ( const vec3& v );       // decrementation by a vec3
 
207
                                                                                                                                                                                                                                                                                                                                                                                                 vec3& operator *= ( const float d );       // multiplication by a constant
 
208
                                                                                                                                                                                                                                                                                                                                                                                                                                                 vec3& operator /= ( const float d );       // division by a constant
 
209
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 float& operator [] ( int i);               // indexing
 
210
 
 
211
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 // special functions
 
212
 
 
213
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 float length(void);                        // length of a vec3
 
214
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 float length2(void);                       // squared length of a vec3
 
215
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 vec3& normalize(void);                     // normalize a vec3
 
216
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 vec3& homogenize(void);                            // homogenize (div by Z)
 
217
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         vec3& apply(V_FCT_PTR fct);                // apply a func. to each component
 
218
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         void  set( float x, float y, float z );     // set vector
 
219
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          void  print( FILE *file, char *name );      // print vector to a file
 
220
 
 
221
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           // friends
 
222
 
 
223
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           friend vec3 operator - (const vec3& v);                          // -v1
 
224
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator + (const vec3& a, const vec3& b);     // v1 + v2
 
225
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator - (const vec3& a, const vec3& b);     // v1 - v2
 
226
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator * (const vec3& a, const float d);     // v1 * 3.0
 
227
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator * (const float d, const vec3& a);     // 3.0 * v1
 
228
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator * (const mat4& a, const vec3& v);     // M . v
 
229
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator * (const vec3& v, mat4& a);           // v . M
 
230
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend float operator * (const vec3& a, const vec3& b);    // dot product
 
231
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator / (const vec3& a, const float d);     // v1 / 3.0
 
232
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator ^ (const vec3& a, const vec3& b);     // cross product
 
233
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend int operator == (const vec3& a, const vec3& b);     // v1 == v2 ?
 
234
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend int operator != (const vec3& a, const vec3& b);     // v1 != v2 ?
 
235
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 //friend ostream& operator << (ostream& s, vec3& v);       // output to stream
 
236
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 //friend istream& operator >> (istream& s, vec3& v);       // input from strm.
 
237
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend void swap(vec3& a, vec3& b);                        // swap v1 & v2
 
238
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 min_vec(const vec3& a, const vec3& b);                 // min(v1, v2)
 
239
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec3 max_vec(const vec3& a, const vec3& b);                 // max(v1, v2)
 
240
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 prod(const vec3& a, const vec3& b);                    // term by term *
 
241
 
 
242
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         // necessary friend declarations
 
243
 
 
244
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend class vec2;
 
245
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend class vec4;
 
246
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend class mat3;
 
247
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec2 operator * (const mat3& a, const vec2& v);     // linear transform
 
248
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator * (const mat3& a, const vec3& v);     // linear transform
 
249
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend mat3 operator * (mat3& a, mat3& b);                 // matrix 3 product
 
250
 
 
251
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 };
 
252
 
 
253
/****************************************************************
 
254
*                                                               *
 
255
*                           4D Vector                           *
 
256
*                                                               *
 
257
****************************************************************/
 
258
 
 
259
class vec4
 
260
{
 
261
protected:
 
262
 
 
263
  float n[4];
 
264
 
 
265
public:
 
266
 
 
267
  // Constructors
 
268
 
 
269
       vec4(void);
 
270
  vec4(const float x, const float y, const float z, const float w);
 
271
  vec4(const float d);
 
272
  vec4(const vec4& v);                      // copy constructor
 
273
                                                 vec4(const vec3& v);                       // cast vec3 to vec4
 
274
                                                                                                 vec4(const vec3& v, const float d);        // cast vec3 to vec4
 
275
 
 
276
                                                                                                                                                 // Assignment operators
 
277
 
 
278
                                                                                                                                                 vec4& operator = ( const vec4& v );        // assignment of a vec4
 
279
                                                                                                                                                                                                 vec4& operator += ( const vec4& v );       // incrementation by a vec4
 
280
                                                                                                                                                                                                                                                 vec4& operator -= ( const vec4& v );       // decrementation by a vec4
 
281
                                                                                                                                                                                                                                                                                                 vec4& operator *= ( const float d );       // multiplication by a constant
 
282
                                                                                                                                                                                                                                                                                                                                                 vec4& operator /= ( const float d );       // division by a constant
 
283
                                                                                                                                                                                                                                                                                                                                                                                                 float& operator [] ( int i);               // indexing
 
284
 
 
285
                                                                                                                                                                                                                                                                                                                                                                                                                                                 // special functions
 
286
 
 
287
                                                                                                                                                                                                                                                                                                                                                                                                                                                 float length(void);                        // length of a vec4
 
288
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 float length2(void);                       // squared length of a vec4
 
289
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 vec4& normalize(void);                     // normalize a vec4
 
290
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 vec4& apply(V_FCT_PTR fct);                // apply a func. to each component
 
291
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 vec4& homogenize(void);
 
292
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 void  print( FILE *file, char *name );      // print vector to a file
 
293
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  void  set( float x, float y, float z, float a );                                              
 
294
 
 
295
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 // friends
 
296
 
 
297
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      friend vec4 operator - (const vec4& v);                       // -v1
 
298
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec4 operator + (const vec4& a, const vec4& b);     // v1 + v2
 
299
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec4 operator - (const vec4& a, const vec4& b);     // v1 - v2
 
300
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec4 operator * (const vec4& a, const float d);     // v1 * 3.0
 
301
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec4 operator * (const float d, const vec4& a);     // 3.0 * v1
 
302
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec4 operator * (const mat4& a, const vec4& v);     // M . v
 
303
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec4 operator * (const vec4& v, mat4& a);           // v . M
 
304
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend float operator * (const vec4& a, const vec4& b);    // dot product
 
305
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec4 operator / (const vec4& a, const float d);     // v1 / 3.0
 
306
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend int operator == (const vec4& a, const vec4& b);     // v1 == v2 ?
 
307
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend int operator != (const vec4& a, const vec4& b);     // v1 != v2 ?
 
308
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //friend ostream& operator << (ostream& s, vec4& v);       // output to stream
 
309
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //friend istream& operator >> (istream& s, vec4& v);       // input from strm.
 
310
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend void swap(vec4& a, vec4& b);                        // swap v1 & v2
 
311
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec4 min_vec(const vec4& a, const vec4& b);                 // min(v1, v2)
 
312
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec4 max_vec(const vec4& a, const vec4& b);                 // max(v1, v2)
 
313
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec4 prod(const vec4& a, const vec4& b);                    // term by term *
 
314
 
 
315
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 // necessary friend declarations
 
316
 
 
317
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend class vec3;
 
318
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend class mat4;
 
319
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec3 operator * (const mat4& a, const vec3& v);     // linear transform
 
320
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend mat4 operator * (mat4& a, mat4& b);                 // matrix 4 product
 
321
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         };
 
322
 
 
323
/****************************************************************
 
324
*                                                               *
 
325
*                          3x3 Matrix                           *
 
326
*                                                               *
 
327
****************************************************************/
 
328
 
 
329
class mat3
 
330
{
 
331
protected:
 
332
 
 
333
  vec3 v[3];
 
334
 
 
335
public:
 
336
 
 
337
  // Constructors
 
338
 
 
339
       mat3(void);
 
340
  mat3(const vec3& v0, const vec3& v1, const vec3& v2);
 
341
  mat3(const float d);
 
342
  mat3(const mat3& m);
 
343
 
 
344
  // Assignment operators
 
345
 
 
346
       mat3& operator   = ( const mat3& m );        // assignment of a mat3
 
347
                                                         mat3& operator += ( const mat3& m );       // incrementation by a mat3
 
348
                                                                                                         mat3& operator -= ( const mat3& m );       // decrementation by a mat3
 
349
                                                                                                                                                         mat3& operator *= ( const float d );       // multiplication by a constant
 
350
                                                                                                                                                                                                         mat3& operator /= ( const float d );       // division by a constant
 
351
                                                                                                                                                                                                                                                         vec3& operator [] ( int i);                // indexing
 
352
 
 
353
                                                                                                                                                                                                                                                                                                         // special functions
 
354
 
 
355
                                                                                                                                                                                                                                                                                                         mat3 transpose(void);                      // transpose
 
356
                                                                                                                                                                                                                                                                                                                                                         mat3 inverse(void);                        // inverse
 
357
                                                                                                                                                                                                                                                                                                                                                                                                         mat3& apply(V_FCT_PTR fct);                // apply a func. to each element
 
358
                                                                                                                                                                                                                                                                                                                                                                                                                                                         void print( FILE *file, char *name );       // print matrix to a file
 
359
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          void set(const vec3& v0, const vec3& v1, const vec3& v2);
 
360
 
 
361
 
 
362
                                                                                                                                                                                                                                                                                                                                                                                                                                                         // friends
 
363
 
 
364
                                                                                                                                                                                                                                                                                                                                                                                                                                                              friend mat3 operator - (const mat3& a);                       // -m1
 
365
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend mat3 operator + (const mat3& a, const mat3& b);     // m1 + m2
 
366
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend mat3 operator - (const mat3& a, const mat3& b);     // m1 - m2
 
367
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend mat3 operator * (mat3& a, mat3& b);                 // m1 * m2
 
368
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend mat3 operator * (const mat3& a, const float d);     // m1 * 3.0
 
369
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend mat3 operator * (const float d, const mat3& a);     // 3.0 * m1
 
370
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend mat3 operator / (const mat3& a, const float d);     // m1 / 3.0
 
371
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend int operator == (const mat3& a, const mat3& b);     // m1 == m2 ?
 
372
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend int operator != (const mat3& a, const mat3& b);     // m1 != m2 ?
 
373
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 //friend ostream& operator << (ostream& s, mat3& m);       // output to stream
 
374
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 //friend istream& operator >> (istream& s, mat3& m);       // input from strm.
 
375
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend void swap(mat3& a, mat3& b);                        // swap m1 & m2
 
376
 
 
377
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 // necessary friend declarations
 
378
 
 
379
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator * (const mat3& a, const vec3& v);     // linear transform
 
380
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec2 operator * (const mat3& a, const vec2& v);     // linear transform
 
381
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 };
 
382
 
 
383
/****************************************************************
 
384
*                                                               *
 
385
*                          4x4 Matrix                           *
 
386
*                                                               *
 
387
****************************************************************/
 
388
 
 
389
class mat4
 
390
{
 
391
protected:
 
392
public:
 
393
 
 
394
  vec4 v[4];
 
395
 
 
396
 
 
397
  // Constructors
 
398
 
 
399
       mat4(void);
 
400
  mat4(const vec4& v0, const vec4& v1, const vec4& v2, const vec4& v3);
 
401
  mat4(const float d);
 
402
  mat4(const mat4& m);
 
403
  mat4(const float a00, const float a01, const float a02, const float a03,
 
404
       const float a10, const float a11, const float a12, const float a13,
 
405
       const float a20, const float a21, const float a22, const float a23,
 
406
       const float a30, const float a31, const float a32, const float a33 );
 
407
 
 
408
 
 
409
  // Assignment operators
 
410
 
 
411
       mat4& operator   = ( const mat4& m );        // assignment of a mat4
 
412
                                                         mat4& operator += ( const mat4& m );       // incrementation by a mat4
 
413
                                                                                                         mat4& operator -= ( const mat4& m );       // decrementation by a mat4
 
414
                                                                                                                                                         mat4& operator *= ( const float d );       // multiplication by a constant
 
415
                                                                                                                                                                                                         mat4& operator /= ( const float d );       // division by a constant
 
416
                                                                                                                                                                                                                                                         vec4& operator [] ( int i);                // indexing
 
417
 
 
418
                                                                                                                                                                                                                                                                                                         // special functions
 
419
 
 
420
                                                                                                                                                                                                                                                                                                         mat4 transpose(void);                      // transpose
 
421
                                                                                                                                                                                                                                                                                                                                                         mat4 inverse(void);                                // inverse
 
422
                                                                                                                                                                                                                                                                                                                                                                                                                 mat4& apply(V_FCT_PTR fct);                // apply a func. to each element
 
423
                                                                                                                                                                                                                                                                                                                                                                                                                                                                 void print( FILE *file, char *name );       // print matrix to a file
 
424
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  void swap_rows( int i, int j );             // swap rows i and j
 
425
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   void swap_cols( int i, int j );             // swap cols i and j
 
426
 
 
427
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // friends
 
428
 
 
429
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    friend mat4 operator - (const mat4& a);                         // -m1
 
430
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend mat4 operator + (const mat4& a, const mat4& b);     // m1 + m2
 
431
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend mat4 operator - (const mat4& a, const mat4& b);     // m1 - m2
 
432
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend mat4 operator * (mat4& a, mat4& b);                 // m1 * m2
 
433
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend mat4 operator * (const mat4& a, const float d);     // m1 * 4.0
 
434
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend mat4 operator * (const float d, const mat4& a);     // 4.0 * m1
 
435
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend mat4 operator / (const mat4& a, const float d);     // m1 / 3.0
 
436
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend int operator == (const mat4& a, const mat4& b);     // m1 == m2 ?
 
437
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend int operator != (const mat4& a, const mat4& b);     // m1 != m2 ?
 
438
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //friend ostream& operator << (ostream& s, mat4& m);       // output to stream
 
439
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //friend istream& operator >> (istream& s, mat4& m);       // input from strm.
 
440
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend void swap(mat4& a, mat4& b);                        // swap m1 & m2
 
441
 
 
442
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         // necessary friend declarations
 
443
 
 
444
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         friend vec4 operator * (const mat4& a, const vec4& v);     // linear transform
 
445
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         //friend vec4 operator * (const vec4& v, const mat4& a);           // linear transform
 
446
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator * (const mat4& a, const vec3& v);     // linear transform
 
447
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 friend vec3 operator * (const vec3& v, const mat4& a);     // linear transform
 
448
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 };
 
449
 
 
450
/****************************************************************
 
451
*                                                               *
 
452
*              2D functions and 3D functions                    *
 
453
*                                                               *
 
454
****************************************************************/
 
455
 
 
456
mat3 identity2D(void);                                      // identity 2D
 
457
mat3 translation2D(vec2& v);                                // translation 2D
 
458
mat3 rotation2D(vec2& Center, const float angleDeg);        // rotation 2D
 
459
mat3 scaling2D(vec2& scaleVector);                          // scaling 2D
 
460
mat4 identity3D(void);                                      // identity 3D
 
461
mat4 translation3D(vec3& v);                                // translation 3D
 
462
mat4 rotation3D(vec3& Axis, const float angleDeg);          // rotation 3D
 
463
mat4 rotation3Drad(vec3& Axis, const float angleRad);       // rotation 3D
 
464
mat4 scaling3D(vec3& scaleVector);                          // scaling 3D
 
465
mat4 perspective3D(const float d);                          // perspective 3D
 
466
 
 
467
 
 
468
vec3 operator * (const vec3& v, mat3& a);
 
469
vec2 operator * (const vec2& v, mat3& a);
 
470
vec3 operator * (const vec3& v, mat4& a);
 
471
vec4 operator * (const vec4& v, mat4& a);
 
472
 
 
473
 
 
474
#endif