1 | #ifndef ENTITY_H |
1 | #ifndef ENTITY_H |
2 | #define ENTITY_H |
2 | #define ENTITY_H |
3 | |
3 | |
4 | #include <GL/gl.h> |
|
|
5 | |
|
|
6 | #include <vector> |
4 | #include <vector> |
|
|
5 | |
|
|
6 | #include <iostream> |
|
|
7 | #include "opengl.h" |
7 | |
8 | |
8 | #include "util.h" |
9 | #include "util.h" |
9 | #include "oct.h" |
10 | #include "oct.h" |
10 | #include "view.h" |
11 | #include "view.h" |
|
|
12 | #include "material.h" |
11 | |
13 | |
12 | using namespace std; |
14 | using namespace std; |
|
|
15 | using namespace gl; |
13 | |
16 | |
14 | struct entity_base { |
17 | struct geometry |
15 | struct entity_base *parent; |
18 | { |
|
|
19 | geometry *parent; |
|
|
20 | box bbox; |
|
|
21 | |
|
|
22 | virtual void update () |
|
|
23 | { |
|
|
24 | if (parent) |
|
|
25 | parent->update (); |
|
|
26 | } |
|
|
27 | |
|
|
28 | virtual void draw (view &ctx) = 0; |
|
|
29 | geometry (geometry *parent = 0) : parent(parent) { }; |
|
|
30 | virtual ~geometry (); |
|
|
31 | }; |
|
|
32 | |
|
|
33 | struct geometry_opengl : geometry |
|
|
34 | { |
|
|
35 | GLuint list; // TODO: dynamic caching |
|
|
36 | |
|
|
37 | geometry_opengl (); |
|
|
38 | ~geometry_opengl (); |
|
|
39 | }; |
|
|
40 | |
|
|
41 | template<GLenum type> |
|
|
42 | struct geometry_opengl1d : geometry_opengl, vector<vertex_v3f> |
|
|
43 | { |
|
|
44 | void set (const vector<vertex_v3f> &v); |
|
|
45 | void draw (view &ctx); |
|
|
46 | }; |
|
|
47 | |
|
|
48 | template<GLenum type> |
|
|
49 | struct geometry_opengl2d : geometry_opengl |
|
|
50 | { |
|
|
51 | material *m; |
|
|
52 | |
|
|
53 | geometry_opengl2d () : m(0) { }; |
|
|
54 | |
|
|
55 | void set (const vector<vertex_t2f_n3f_v3f> &v); |
|
|
56 | void draw (view &ctx); |
|
|
57 | }; |
|
|
58 | |
|
|
59 | typedef geometry_opengl1d<GL_POINTS> geometry_points; |
|
|
60 | typedef geometry_opengl1d<GL_LINES> geometry_lines; |
|
|
61 | typedef geometry_opengl1d<GL_LINE_STRIP> geometry_line_strip; |
|
|
62 | typedef geometry_opengl1d<GL_LINE_LOOP> geometry_line_loop; |
|
|
63 | typedef geometry_opengl2d<GL_TRIANGLES> geometry_triangles; |
|
|
64 | typedef geometry_opengl2d<GL_TRIANGLE_STRIP> geometry_triangle_strip; |
|
|
65 | typedef geometry_opengl2d<GL_TRIANGLE_FAN> geometry_triangle_fan; |
|
|
66 | typedef geometry_opengl2d<GL_QUADS> geometry_quads; |
|
|
67 | typedef geometry_opengl2d<GL_QUAD_STRIP> geometry_quad_strip; |
|
|
68 | typedef geometry_opengl2d<GL_POLYGON> geometry_polygon; |
|
|
69 | |
|
|
70 | struct geometry_nurbs : geometry, vector<vertex_t2f_n3f_v3f> |
|
|
71 | { |
|
|
72 | GLUnurbsObj *nurb; |
|
|
73 | GLUtesselator *tess; |
|
|
74 | GLfloat ctlpoints[4][4][3]; |
|
|
75 | |
|
|
76 | geometry_nurbs() : tess(0), nurb(0) { } |
|
|
77 | virtual void draw (view &ctx); |
|
|
78 | void set (); |
|
|
79 | }; |
|
|
80 | |
|
|
81 | struct geometry_sphere : geometry |
|
|
82 | { |
|
|
83 | material *m; |
|
|
84 | GLfloat radius; |
|
|
85 | |
|
|
86 | void update (); |
|
|
87 | void draw (view &ctx); |
|
|
88 | geometry_sphere (material *m, GLfloat radius) : m(m), radius(radius) { update (); }; |
|
|
89 | }; |
|
|
90 | |
|
|
91 | struct geometry_indexed_2d : geometry |
|
|
92 | { |
|
|
93 | material *m; |
|
|
94 | |
|
|
95 | GLenum type; |
|
|
96 | |
|
|
97 | vertex_buffer vb; |
|
|
98 | index_buffer ib; |
|
|
99 | |
|
|
100 | void draw (view &ctx); |
|
|
101 | |
|
|
102 | template <typename vertex, typename index> |
|
|
103 | geometry_indexed_2d (material *m, GLenum type, const vector<vertex> &v, const vector<index> &i) |
|
|
104 | : m(m), type(type) |
|
|
105 | { |
|
|
106 | vb.set (v); |
|
|
107 | ib.set (i); |
|
|
108 | |
|
|
109 | bbox.reset (); |
|
|
110 | for (typename vector<vertex>::const_iterator i = v.begin () ; i != v.end (); ++i) |
|
|
111 | bbox.add (i->v); |
|
|
112 | } |
|
|
113 | |
|
|
114 | }; |
|
|
115 | |
|
|
116 | ///////////////////////////////////////////////////////////////////////////// |
|
|
117 | |
|
|
118 | struct geometry_filter : geometry |
|
|
119 | { |
|
|
120 | protected: |
|
|
121 | geometry *g; |
|
|
122 | public: |
|
|
123 | |
|
|
124 | void set (geometry *g); |
|
|
125 | |
|
|
126 | geometry *content () { return g; }; |
|
|
127 | |
|
|
128 | void update (); |
|
|
129 | void show (); |
|
|
130 | void draw (view &ctx); |
|
|
131 | geometry_filter (geometry *g = 0) { set (g); } |
|
|
132 | ~geometry_filter (); |
|
|
133 | }; |
|
|
134 | |
|
|
135 | struct geometry_transform : geometry_filter |
|
|
136 | { |
|
|
137 | protected: |
|
|
138 | matrix m; |
|
|
139 | public: |
|
|
140 | void update (); |
|
|
141 | |
|
|
142 | void show (); |
|
|
143 | void draw (view &ctx); |
|
|
144 | void set_matrix (const matrix &xfrm); |
|
|
145 | void update (const matrix &xfrm); |
|
|
146 | |
|
|
147 | geometry_transform (geometry *g) : geometry_filter(g), m(1) { }; |
|
|
148 | geometry_transform () : geometry_filter(0), m(1) { }; |
|
|
149 | }; |
|
|
150 | |
|
|
151 | struct geometry_anim : geometry_transform |
|
|
152 | { |
|
|
153 | GLfloat vx, vy, vz; |
|
|
154 | void draw (view &ctx); |
|
|
155 | }; |
|
|
156 | |
|
|
157 | struct geometry_container : geometry, protected vector<geometry *> |
|
|
158 | { |
|
|
159 | void update (); |
|
|
160 | |
|
|
161 | void add (geometry *g); |
|
|
162 | void draw (view &ctx); |
|
|
163 | ~geometry_container (); |
|
|
164 | }; |
|
|
165 | |
|
|
166 | ///////////////////////////////////////////////////////////////////////////// |
|
|
167 | |
|
|
168 | struct entity : geometry_filter |
|
|
169 | { |
16 | sector orig; |
170 | sector orig; |
|
|
171 | point p; |
|
|
172 | sector a, b; // bounding box corners |
|
|
173 | |
17 | vector<octant *> o; |
174 | vector<octant *> o; |
18 | box bbox; |
|
|
19 | |
175 | |
20 | virtual void update_bbox (); |
|
|
21 | virtual void show () { world.add (this); }; |
|
|
22 | void hide (); |
|
|
23 | void display (draw_context &ctx); |
|
|
24 | virtual void draw (draw_context &ctx) = 0; |
|
|
25 | |
|
|
26 | entity_base (); |
|
|
27 | virtual ~entity_base (); |
|
|
28 | }; |
|
|
29 | |
|
|
30 | struct entity_container : entity_base, protected vector<entity_base *> { |
|
|
31 | void add (entity_base *e) { push_back (e); e->parent = this; } |
|
|
32 | void update_bbox (); |
176 | void update (); |
|
|
177 | void draw (view &ctx); |
|
|
178 | |
|
|
179 | void move (const vec3 &v); |
|
|
180 | |
33 | void show (); |
181 | virtual void show (); |
34 | void draw (draw_context &ctx); |
182 | virtual void hide (); |
35 | ~entity_container (); |
|
|
36 | }; |
|
|
37 | |
183 | |
|
|
184 | entity (geometry *g = 0); |
|
|
185 | ~entity (); |
|
|
186 | }; |
|
|
187 | |
|
|
188 | struct entity_moveable : entity |
|
|
189 | { |
|
|
190 | vec3 v; |
|
|
191 | entity_moveable (geometry *g = 0) : entity (g) { } |
|
|
192 | |
|
|
193 | void perform_step (double t); |
|
|
194 | }; |
|
|
195 | |
38 | struct entity_filter : entity_base { |
196 | struct entity_light : entity |
39 | protected: |
197 | { |
40 | entity_base *e; |
198 | light *lview; |
41 | public: |
199 | }; |
42 | |
200 | |
43 | void set (entity_base *e) |
201 | ///////////////////////////////////////////////////////////////////////////// |
44 | { |
202 | // not the final API(!) |
45 | this->e = e; |
|
|
46 | e->parent = this; |
|
|
47 | } |
|
|
48 | |
203 | |
49 | void remove () |
204 | struct skybox |
50 | { |
205 | { |
51 | this->e->parent = 0; |
206 | texture *tex[6]; |
52 | this->e = 0; |
|
|
53 | } |
|
|
54 | |
207 | |
55 | entity_base *content () { return e; }; |
208 | skybox ( |
|
|
209 | const char *left, |
|
|
210 | const char *front, |
|
|
211 | const char *right, |
|
|
212 | const char *back, |
|
|
213 | const char *top, |
|
|
214 | const char *bottom |
|
|
215 | ); |
56 | |
216 | |
|
|
217 | ~skybox (); |
|
|
218 | |
|
|
219 | void draw (view &ctx); |
|
|
220 | }; |
|
|
221 | |
|
|
222 | ///////////////////////////////////////////////////////////////////////////// |
|
|
223 | // |
|
|
224 | // VERY EXPERIMENTAL HEIGHTMAP |
|
|
225 | |
|
|
226 | struct geometry_heightfield : geometry |
|
|
227 | { |
|
|
228 | struct node; |
|
|
229 | |
|
|
230 | node *tree; |
|
|
231 | |
|
|
232 | GLfloat sx, sy, sm; |
|
|
233 | |
57 | void update_bbox (); |
234 | void update (); |
58 | void show (); |
|
|
59 | void draw (draw_context &ctx); |
235 | void draw (view &ctx); |
60 | ~entity_filter (); |
236 | geometry_heightfield (GLfloat sx, GLfloat sy); |
61 | }; |
|
|
62 | |
|
|
63 | struct entity : entity_filter { |
|
|
64 | }; |
|
|
65 | |
|
|
66 | struct vertex1d { |
|
|
67 | colour c; // colour |
|
|
68 | point p; // vertex |
|
|
69 | }; |
|
|
70 | |
|
|
71 | struct vertex2d { |
|
|
72 | point p; // vertex |
|
|
73 | vec3 n; // normal |
|
|
74 | texc t; // texture |
|
|
75 | |
|
|
76 | vertex2d () { }; |
|
|
77 | vertex2d (point p, vec3 n, texc t = texc()) : p(p), n(n), t(t) { }; |
|
|
78 | }; |
|
|
79 | |
|
|
80 | struct entity_opengl : entity_base { |
|
|
81 | GLuint list; |
|
|
82 | |
|
|
83 | entity_opengl (); |
|
|
84 | ~entity_opengl (); |
|
|
85 | }; |
|
|
86 | |
|
|
87 | template<GLenum type> |
|
|
88 | struct entity_opengl1d : entity_opengl, vector<vertex1d> { |
|
|
89 | void update_bbox (); |
|
|
90 | void draw (draw_context &ctx); |
|
|
91 | }; |
|
|
92 | |
|
|
93 | template<GLenum type> |
|
|
94 | struct entity_opengl2d : entity_opengl { |
|
|
95 | material m; |
|
|
96 | |
|
|
97 | void set (const vector<vertex2d> &v); |
|
|
98 | void draw (draw_context &ctx); |
|
|
99 | |
|
|
100 | }; |
|
|
101 | |
|
|
102 | typedef entity_opengl1d<GL_POINTS> entity_points; |
|
|
103 | typedef entity_opengl1d<GL_LINES> entity_lines; |
|
|
104 | typedef entity_opengl1d<GL_LINE_STRIP> entity_line_strip; |
|
|
105 | typedef entity_opengl1d<GL_LINE_LOOP> entity_line_loop; |
|
|
106 | typedef entity_opengl2d<GL_TRIANGLES> entity_triangles; |
|
|
107 | typedef entity_opengl2d<GL_TRIANGLE_STRIP> entity_triangle_strip; |
|
|
108 | typedef entity_opengl2d<GL_TRIANGLE_FAN> entity_triangle_fan; |
|
|
109 | typedef entity_opengl2d<GL_QUADS> entity_quads; |
|
|
110 | typedef entity_opengl2d<GL_QUAD_STRIP> entity_quad_strip; |
|
|
111 | typedef entity_opengl2d<GL_POLYGON> entity_polygon; |
|
|
112 | |
|
|
113 | struct entity_transform : entity_filter { |
|
|
114 | gl_matrix m; |
|
|
115 | |
|
|
116 | entity_transform (); |
|
|
117 | |
|
|
118 | void update_bbox (); |
|
|
119 | void show (); |
|
|
120 | void draw (draw_context &ctx); |
|
|
121 | }; |
|
|
122 | |
|
|
123 | struct entity_anim : entity_transform { |
|
|
124 | GLfloat vx, vy, vz; |
|
|
125 | void draw (draw_context &ctx); |
|
|
126 | }; |
237 | }; |
127 | |
238 | |
128 | #endif |
239 | #endif |
129 | |
240 | |
130 | |
241 | |