ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/util.h
Revision: 1.5
Committed: Sun Oct 3 22:46:03 2004 UTC (22 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +41 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #ifndef UTIL_H
2     #define UTIL_H
3    
4     #include <GL/gl.h>
5    
6     #include <vector>
7    
8     using namespace std;
9    
10 root 1.3 typedef int soffs; // 32 bit
11     typedef unsigned int uoffs;
12 root 1.4 #define OFFS_BITS 31
13     #define SOFFS_MIN (soffs)-(1 << (OFFS_BITS - 1))
14     #define MAXEXTENT ((1UL << OFFS_BITS) - 1)
15 root 1.1
16     #define GLFLOAT_MAX 1e30
17     #define GLFLOAT_MIN -1e30
18    
19     struct sector {
20     soffs x, y, z;
21 root 1.3
22     void offset (int subindex, uoffs extent)
23     {
24     if (subindex & 1) x += extent;
25     if (subindex & 2) y += extent;
26     if (subindex & 4) z += extent;
27     }
28 root 1.1 };
29    
30     struct point {
31     GLfloat x, y, z;
32    
33     point () { };
34     point (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { };
35     };
36    
37 root 1.5 inline const point translate (const point &p, const sector &src, const sector &dst)
38     {
39     point r;
40    
41     r.x = p.x + (dst.x - src.x);
42     r.y = p.y + (dst.y - src.y);
43     r.z = p.z + (dst.z - src.z);
44    
45     return r;
46     }
47    
48 root 1.1 struct colour {
49 root 1.2 GLfloat r, g, b, a;
50     colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { };
51 root 1.1 };
52    
53     struct vec3 {
54     GLfloat x, y, z;
55     vec3 () { };
56     vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { };
57    
58     const vec3 operator -() const
59     { return vec3 (-x, -y, -z); }
60     };
61    
62     const vec3 normalize (const vec3 &v);
63     const vec3 cross (const vec3 &a, const vec3 &b);
64     GLfloat dot (const vec3 &a, const vec3 &b);
65    
66 root 1.5 struct gl_matrix {
67     GLfloat data[4][4];
68    
69     const GLfloat operator ()(int i, int j) const { return data[i][j]; };
70     GLfloat &operator ()(int i, int j) { return data[i][j]; };
71    
72     void diagonal (GLfloat v);
73     void clear () { diagonal (0.); };
74     void identity () { diagonal (1.); };
75    
76     void print (); // ugly
77    
78     void translate (const vec3 &v);
79    
80     gl_matrix () { };
81     gl_matrix (GLfloat diag) { diagonal (diag); };
82     };
83    
84     const gl_matrix operator *(const gl_matrix &a, const gl_matrix &b);
85    
86 root 1.1 struct texc {
87     GLfloat s, t;
88     texc () { };
89     texc (GLfloat s, GLfloat t) : s(s), t(t) { };
90     };
91    
92     struct box {
93     point a, b;
94    
95     void reset ()
96     {
97     a = point (GLFLOAT_MAX, GLFLOAT_MAX, GLFLOAT_MAX);
98     b = point (GLFLOAT_MIN, GLFLOAT_MIN, GLFLOAT_MIN);
99     }
100    
101     void add (const box &o);
102     void add (const point &p);
103     };
104 root 1.5
105     inline const box translate (const box &b, const sector &src, const sector &dst)
106     {
107     box r;
108    
109     r.a = translate (b.a, src, dst);
110     r.b = translate (b.b, src, dst);
111    
112     return r;
113     }
114 root 1.1
115     struct light {
116     point p;
117     colour c;
118     GLfloat intensity;
119     GLfloat radius;
120 root 1.2 };
121    
122     struct material {
123     colour diffuse, specular, emission;
124     GLfloat shininess;
125 root 1.1 };
126    
127 root 1.4 struct entity_base;
128     struct draw_context;
129 root 1.1
130     #endif
131