1 |
root |
1.1 |
#ifndef ENTITY_H |
2 |
|
|
#define ENTITY_H |
3 |
|
|
|
4 |
|
|
#include <GL/gl.h> |
5 |
|
|
|
6 |
|
|
#include <vector> |
7 |
|
|
|
8 |
|
|
#include "oct.h" |
9 |
|
|
|
10 |
|
|
using namespace std; |
11 |
|
|
|
12 |
|
|
typedef unsigned int soffs; // 32 bit |
13 |
|
|
const soffs soffs_max = 1UL << 31; |
14 |
|
|
|
15 |
|
|
#define GLFLOAT_MAX 1e30 |
16 |
|
|
#define GLFLOAT_MIN -1e30 |
17 |
|
|
|
18 |
|
|
struct sector { |
19 |
|
|
soffs x, y, z; |
20 |
|
|
}; |
21 |
|
|
|
22 |
|
|
struct point { |
23 |
|
|
GLfloat x, y, z; |
24 |
root |
1.3 |
|
25 |
|
|
point () { }; |
26 |
|
|
point (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
27 |
root |
1.1 |
}; |
28 |
|
|
|
29 |
|
|
struct colour { |
30 |
|
|
GLfloat r, g, b; |
31 |
root |
1.3 |
colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1.) : r(r), g(g), b(b) { }; |
32 |
root |
1.1 |
}; |
33 |
|
|
|
34 |
root |
1.3 |
struct vec3 { |
35 |
root |
1.1 |
GLfloat x, y, z; |
36 |
root |
1.3 |
vec3 () { }; |
37 |
|
|
vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
38 |
|
|
|
39 |
|
|
const vec3 operator -() const |
40 |
|
|
{ return vec3 (-x, -y, -z); } |
41 |
root |
1.1 |
}; |
42 |
|
|
|
43 |
root |
1.3 |
const vec3 cross (const vec3 &a, const vec3 &b); |
44 |
|
|
GLfloat dot (const vec3 &a, const vec3 &b); |
45 |
|
|
|
46 |
root |
1.1 |
struct texc { |
47 |
root |
1.3 |
GLfloat s, t; |
48 |
|
|
texc () { }; |
49 |
|
|
texc (GLfloat s, GLfloat t) : s(s), t(t) { }; |
50 |
root |
1.1 |
}; |
51 |
|
|
|
52 |
|
|
struct box { |
53 |
|
|
point a, b; |
54 |
|
|
|
55 |
|
|
void reset () |
56 |
|
|
{ |
57 |
root |
1.3 |
a = point (GLFLOAT_MAX, GLFLOAT_MAX, GLFLOAT_MAX); |
58 |
|
|
b = point (GLFLOAT_MIN, GLFLOAT_MIN, GLFLOAT_MIN); |
59 |
root |
1.1 |
} |
60 |
|
|
|
61 |
|
|
void add (const box &o); |
62 |
|
|
}; |
63 |
|
|
|
64 |
|
|
struct entity_base { |
65 |
|
|
struct entity_base *parent; |
66 |
|
|
vector<octant *> o; |
67 |
|
|
box bbox; |
68 |
|
|
|
69 |
|
|
virtual void update_bbox (); |
70 |
|
|
virtual void show (const sector &sec) { }; |
71 |
|
|
void hide (); |
72 |
|
|
virtual void draw () = 0; |
73 |
|
|
virtual ~entity_base () |
74 |
|
|
{ |
75 |
|
|
hide (); |
76 |
|
|
}; |
77 |
|
|
}; |
78 |
|
|
|
79 |
|
|
struct entity_container : entity_base, protected vector<entity_base *> { |
80 |
|
|
void update_bbox (); |
81 |
|
|
void show (const sector &sec); |
82 |
|
|
void draw (); |
83 |
|
|
~entity_container (); |
84 |
|
|
}; |
85 |
|
|
|
86 |
|
|
struct entity_filter : entity_base { |
87 |
|
|
protected: |
88 |
|
|
entity_base *e; |
89 |
|
|
public: |
90 |
|
|
|
91 |
|
|
void set (entity_base *e) |
92 |
|
|
{ |
93 |
|
|
this->e = e; |
94 |
|
|
e->parent = this; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
void remove () |
98 |
|
|
{ |
99 |
|
|
this->e->parent = 0; |
100 |
|
|
this->e = 0; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
entity_base *content () { return e; }; |
104 |
|
|
|
105 |
|
|
void update_bbox (); |
106 |
|
|
void show (const sector &sec); |
107 |
|
|
void draw (); |
108 |
|
|
~entity_filter (); |
109 |
|
|
}; |
110 |
|
|
|
111 |
|
|
struct entity : entity_filter { |
112 |
|
|
sector sec; |
113 |
|
|
|
114 |
|
|
void show (const sector &sec); |
115 |
root |
1.3 |
void show (); |
116 |
root |
1.1 |
void draw (); |
117 |
|
|
}; |
118 |
|
|
|
119 |
|
|
struct entity_affine : entity_filter { |
120 |
|
|
//matrix m; |
121 |
|
|
}; |
122 |
|
|
|
123 |
|
|
struct vertex1d { |
124 |
root |
1.3 |
colour c; // colour |
125 |
root |
1.2 |
point p; // vertex |
126 |
root |
1.1 |
}; |
127 |
|
|
|
128 |
|
|
struct vertex2d { |
129 |
root |
1.3 |
colour c; // colour |
130 |
root |
1.2 |
point p; // vertex |
131 |
root |
1.3 |
vec3 n; // normal |
132 |
root |
1.1 |
texc t; // texture |
133 |
root |
1.3 |
|
134 |
|
|
vertex2d () { }; |
135 |
|
|
vertex2d (colour c, point p, vec3 n, texc t = texc()) : c(c), p(p), n(n), t(t) { }; |
136 |
root |
1.1 |
}; |
137 |
|
|
|
138 |
|
|
template<GLenum type> |
139 |
|
|
struct entity_opengl1d : entity_base, vector<vertex1d> { |
140 |
|
|
void update_bbox (); |
141 |
|
|
void show (const sector &sec); |
142 |
|
|
void draw (); |
143 |
|
|
}; |
144 |
|
|
|
145 |
|
|
template<GLenum type> |
146 |
|
|
struct entity_opengl2d : entity_base, vector<vertex2d> { |
147 |
|
|
void update_bbox (); |
148 |
|
|
void show (const sector &sec); |
149 |
|
|
void draw (); |
150 |
|
|
}; |
151 |
|
|
|
152 |
|
|
typedef entity_opengl1d<GL_POINTS> entity_points; |
153 |
|
|
typedef entity_opengl1d<GL_LINES> entity_lines; |
154 |
root |
1.2 |
typedef entity_opengl1d<GL_LINE_STRIP> entity_line_strip; |
155 |
|
|
typedef entity_opengl1d<GL_LINE_LOOP> entity_line_loop; |
156 |
root |
1.1 |
typedef entity_opengl2d<GL_TRIANGLES> entity_triangles; |
157 |
root |
1.2 |
typedef entity_opengl2d<GL_TRIANGLE_STRIP> entity_triangle_strip; |
158 |
|
|
typedef entity_opengl2d<GL_TRIANGLE_FAN> entity_triangle_fan; |
159 |
root |
1.1 |
typedef entity_opengl2d<GL_QUADS> entity_quads; |
160 |
|
|
typedef entity_opengl2d<GL_QUAD_STRIP> entity_quad_strip; |
161 |
|
|
typedef entity_opengl2d<GL_POLYGON> entity_polygon; |
162 |
root |
1.2 |
|
163 |
|
|
struct light { |
164 |
|
|
point p; |
165 |
|
|
colour c; |
166 |
|
|
GLfloat intensity; |
167 |
|
|
GLfloat radius; |
168 |
|
|
}; |
169 |
|
|
|
170 |
|
|
struct draw_context { |
171 |
|
|
enum { DEPTH, AMBIENT, LIGHTED } mode; |
172 |
|
|
light *l; |
173 |
|
|
}; |
174 |
|
|
|
175 |
|
|
struct view { |
176 |
|
|
point p; |
177 |
root |
1.3 |
vec3 d, u; |
178 |
root |
1.2 |
float fov; |
179 |
root |
1.3 |
int w, h; |
180 |
root |
1.2 |
|
181 |
|
|
void draw (const draw_context &c); |
182 |
|
|
}; |
183 |
root |
1.1 |
|
184 |
|
|
#endif |
185 |
|
|
|
186 |
|
|
|
187 |
|
|
|