ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.h
Revision: 1.10
Committed: Mon Oct 4 10:18:13 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.9: +48 -38 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #ifndef UTIL_H
2     #define UTIL_H
3    
4 root 1.7 #include <cmath>
5 root 1.1 #include <vector>
6    
7     using namespace std;
8    
9 root 1.7 #include <GL/gl.h>
10    
11 root 1.3 typedef int soffs; // 32 bit
12     typedef unsigned int uoffs;
13 root 1.4 #define OFFS_BITS 31
14 root 1.9 #define SOFFS_MIN (soffs)-(1 << (OFFS_BITS - 2))
15     #define SOFFS_MAX (soffs)+(1 << (OFFS_BITS - 2))
16     #define MAXEXTENT (1UL << (OFFS_BITS - 1))
17 root 1.1
18     #define GLFLOAT_MAX 1e30
19     #define GLFLOAT_MIN -1e30
20    
21     struct vec3 {
22     GLfloat x, y, z;
23     vec3 () { };
24     vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { };
25    
26     const vec3 operator -() const
27     { return vec3 (-x, -y, -z); }
28     };
29    
30     const vec3 normalize (const vec3 &v);
31     const vec3 cross (const vec3 &a, const vec3 &b);
32 root 1.6
33     inline const vec3 operator *(const vec3 &a, GLfloat s)
34     {
35     return vec3 (a.x * s, a.y * s, a.z * s);
36     }
37    
38     inline GLfloat dot (const vec3 &a, const vec3 &b)
39     {
40     return a.x * b.x + a.y * b.y + a.z * b.z;
41     }
42    
43     inline const GLfloat abs (const vec3 &v)
44     {
45     return sqrtf (dot (v, v));
46     }
47 root 1.1
48 root 1.5 struct gl_matrix {
49     GLfloat data[4][4];
50    
51     const GLfloat operator ()(int i, int j) const { return data[i][j]; };
52     GLfloat &operator ()(int i, int j) { return data[i][j]; };
53    
54     void diagonal (GLfloat v);
55     void clear () { diagonal (0.); };
56     void identity () { diagonal (1.); };
57    
58     void print (); // ugly
59    
60     void translate (const vec3 &v);
61 root 1.6 void rotate (GLfloat degrees, const vec3 &axis);
62 root 1.5
63     gl_matrix () { };
64     gl_matrix (GLfloat diag) { diagonal (diag); };
65     };
66    
67     const gl_matrix operator *(const gl_matrix &a, const gl_matrix &b);
68 root 1.6 const vec3 operator *(const gl_matrix &a, const vec3 &v);
69 root 1.10
70     typedef vec3 point;
71    
72     // a generic plane
73     class plane {
74     vec3 n;
75     GLfloat d;
76     public:
77    
78     GLfloat distance (const point &p) const
79     {
80     return dot (n, p) + d;
81     }
82    
83     plane () { };
84     plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d);
85     };
86    
87     struct sector {
88     soffs x, y, z;
89    
90     sector (soffs x = 0, soffs y = 0, soffs z = 0) : x(x), y(y), z(z) { };
91    
92     void offset (int subindex, uoffs extent)
93     {
94     if (subindex & 1) x += extent;
95     if (subindex & 2) y += extent;
96     if (subindex & 4) z += extent;
97     }
98     };
99    
100     inline const sector translate (const sector &p, const sector &src, const sector &dst)
101     {
102     sector r;
103    
104     r.x = p.x + (dst.x - src.x);
105     r.y = p.y + (dst.y - src.y);
106     r.z = p.z + (dst.z - src.z);
107    
108     return r;
109     }
110    
111     void renormalize (sector &s, point &p);
112    
113     struct colour {
114     GLfloat r, g, b, a;
115     colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { };
116     };
117 root 1.5
118 root 1.1 struct texc {
119     GLfloat s, t;
120     texc () { };
121     texc (GLfloat s, GLfloat t) : s(s), t(t) { };
122     };
123    
124     struct box {
125 root 1.8 sector a, b;
126    
127     box() { };
128 root 1.1
129     void reset ()
130     {
131 root 1.9 a = sector (SOFFS_MAX, SOFFS_MAX, SOFFS_MAX);
132     b = sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN);
133 root 1.1 }
134    
135     void add (const box &o);
136 root 1.8 void add (const sector &p);
137 root 1.1 void add (const point &p);
138     };
139 root 1.5
140     inline const box translate (const box &b, const sector &src, const sector &dst)
141     {
142     box r;
143    
144     r.a = translate (b.a, src, dst);
145     r.b = translate (b.b, src, dst);
146    
147     return r;
148     }
149 root 1.1
150     struct light {
151     point p;
152     colour c;
153     GLfloat intensity;
154     GLfloat radius;
155 root 1.2 };
156    
157     struct material {
158     colour diffuse, specular, emission;
159     GLfloat shininess;
160 root 1.1 };
161    
162 root 1.4 struct entity_base;
163     struct draw_context;
164 root 1.7
165     extern struct timer {
166     static double now;
167     static double diff;
168    
169     static void frame ();
170     timer ();
171     } timer;
172 root 1.1
173     #endif
174