1 | #ifndef ENTITY_H |
1 | #ifndef ENTITY_H |
2 | #define ENTITY_H |
2 | #define ENTITY_H |
3 | |
3 | |
4 | #include <GL/gl.h> |
4 | #include <vector> |
5 | |
5 | |
6 | #include <vector> |
6 | #include "opengl.h" |
7 | |
7 | |
8 | #include "util.h" |
8 | #include "util.h" |
9 | #include "oct.h" |
9 | #include "oct.h" |
10 | #include "view.h" |
10 | #include "view.h" |
|
|
11 | #include "material.h" |
11 | |
12 | |
12 | using namespace std; |
13 | using namespace std; |
|
|
14 | using namespace gl; |
13 | |
15 | |
14 | struct entity_base { |
16 | struct geometry |
15 | struct entity_base *parent; |
17 | { |
16 | sector orig; |
18 | geometry *parent; |
17 | vector<octant *> o; |
|
|
18 | box bbox; |
19 | box bbox; |
19 | |
20 | |
20 | virtual void update (); |
21 | virtual void update () |
21 | virtual void show () { world.add (this); }; |
22 | { |
22 | void hide (); |
23 | if (parent) |
23 | void display (draw_context &ctx); |
24 | parent->update (); |
24 | virtual void draw (draw_context &ctx) = 0; |
25 | } |
25 | |
26 | |
26 | entity_base (); |
27 | virtual void draw (view &ctx) = 0; |
27 | virtual ~entity_base (); |
28 | geometry (geometry *parent = 0) : parent(parent) { }; |
|
|
29 | virtual ~geometry (); |
28 | }; |
30 | }; |
29 | |
31 | |
30 | struct entity_container : entity_base, protected vector<entity_base *> { |
32 | struct geometry_opengl : geometry |
31 | void add (entity_base *e) |
33 | { |
32 | { |
34 | GLuint list; // TODO: dynamic caching |
33 | push_back (e); |
35 | |
34 | e->parent = this; |
36 | geometry_opengl (); |
|
|
37 | ~geometry_opengl (); |
|
|
38 | }; |
|
|
39 | |
|
|
40 | template<GLenum type> |
|
|
41 | struct geometry_opengl1d : geometry_opengl, vector<vertex_v3f> |
|
|
42 | { |
|
|
43 | void set (const vector<vertex_v3f> &v); |
|
|
44 | void draw (view &ctx); |
|
|
45 | }; |
|
|
46 | |
|
|
47 | template<GLenum type> |
|
|
48 | struct geometry_opengl2d : geometry_opengl |
|
|
49 | { |
|
|
50 | material *m; |
|
|
51 | |
|
|
52 | geometry_opengl2d () : m(0) { }; |
|
|
53 | |
|
|
54 | void set (const vector<vertex_t2f_n3f_v3f> &v); |
|
|
55 | void draw (view &ctx); |
|
|
56 | }; |
|
|
57 | |
|
|
58 | typedef geometry_opengl1d<GL_POINTS> geometry_points; |
|
|
59 | typedef geometry_opengl1d<GL_LINES> geometry_lines; |
|
|
60 | typedef geometry_opengl1d<GL_LINE_STRIP> geometry_line_strip; |
|
|
61 | typedef geometry_opengl1d<GL_LINE_LOOP> geometry_line_loop; |
|
|
62 | typedef geometry_opengl2d<GL_TRIANGLES> geometry_triangles; |
|
|
63 | typedef geometry_opengl2d<GL_TRIANGLE_STRIP> geometry_triangle_strip; |
|
|
64 | typedef geometry_opengl2d<GL_TRIANGLE_FAN> geometry_triangle_fan; |
|
|
65 | typedef geometry_opengl2d<GL_QUADS> geometry_quads; |
|
|
66 | typedef geometry_opengl2d<GL_QUAD_STRIP> geometry_quad_strip; |
|
|
67 | typedef geometry_opengl2d<GL_POLYGON> geometry_polygon; |
|
|
68 | |
|
|
69 | struct geometry_nurbs : geometry, vector<vertex_t2f_n3f_v3f> |
|
|
70 | { |
|
|
71 | GLUnurbsObj *nurb; |
|
|
72 | GLUtesselator *tess; |
|
|
73 | GLfloat ctlpoints[4][4][3]; |
|
|
74 | |
|
|
75 | geometry_nurbs() : tess(0), nurb(0) { } |
|
|
76 | virtual void draw (view &ctx); |
|
|
77 | void set (); |
|
|
78 | }; |
|
|
79 | |
|
|
80 | struct geometry_sphere : geometry |
|
|
81 | { |
|
|
82 | GLfloat radius; |
|
|
83 | |
35 | update (); |
84 | void update (); |
36 | } |
85 | void draw (view &ctx); |
|
|
86 | geometry_sphere (GLfloat radius) : radius(radius) { update (); }; |
|
|
87 | }; |
|
|
88 | |
|
|
89 | ///////////////////////////////////////////////////////////////////////////// |
|
|
90 | |
|
|
91 | struct geometry_filter : geometry |
|
|
92 | { |
|
|
93 | protected: |
|
|
94 | geometry *g; |
|
|
95 | public: |
|
|
96 | |
|
|
97 | void set (geometry *g); |
|
|
98 | |
|
|
99 | geometry *content () { return g; }; |
37 | |
100 | |
38 | void update (); |
101 | void update (); |
39 | void show (); |
102 | void show (); |
40 | void draw (draw_context &ctx); |
103 | void draw (view &ctx); |
41 | ~entity_container (); |
104 | geometry_filter (geometry *g = 0) { set (g); } |
|
|
105 | ~geometry_filter (); |
42 | }; |
106 | }; |
43 | |
107 | |
44 | struct entity_filter : entity_base { |
108 | struct geometry_transform : geometry_filter |
|
|
109 | { |
45 | protected: |
110 | protected: |
46 | entity_base *e; |
111 | matrix m; |
47 | public: |
112 | public: |
|
|
113 | void update (); |
48 | |
114 | |
49 | void set (entity_base *e) |
115 | void show (); |
50 | { |
116 | void draw (view &ctx); |
51 | this->e = e; |
117 | void set_matrix (const matrix &xfrm); |
52 | e->parent = this; |
118 | void update (const matrix &xfrm); |
53 | update (); |
|
|
54 | } |
|
|
55 | |
119 | |
56 | void remove () |
120 | geometry_transform (geometry *g) : geometry_filter(g), m(1) { }; |
57 | { |
121 | geometry_transform () : geometry_filter(0), m(1) { }; |
58 | this->e->parent = 0; |
122 | }; |
59 | this->e = 0; |
|
|
60 | } |
|
|
61 | |
123 | |
62 | entity_base *content () { return e; }; |
124 | struct geometry_anim : geometry_transform |
|
|
125 | { |
|
|
126 | GLfloat vx, vy, vz; |
|
|
127 | void draw (view &ctx); |
|
|
128 | }; |
|
|
129 | |
|
|
130 | struct geometry_container : geometry, protected vector<geometry *> |
|
|
131 | { |
|
|
132 | void update (); |
|
|
133 | |
|
|
134 | void add (geometry *g); |
|
|
135 | void draw (view &ctx); |
|
|
136 | ~geometry_container (); |
|
|
137 | }; |
|
|
138 | |
|
|
139 | ///////////////////////////////////////////////////////////////////////////// |
|
|
140 | |
|
|
141 | struct entity : geometry_filter |
|
|
142 | { |
|
|
143 | sector orig; |
|
|
144 | point p; |
|
|
145 | sector a, b; // bounding box corners |
|
|
146 | gl::vertex_buffer_object vb_bbox; |
|
|
147 | |
|
|
148 | vector<octant *> o; |
63 | |
149 | |
64 | void update (); |
150 | void update (); |
|
|
151 | void draw (view &ctx); |
|
|
152 | |
|
|
153 | void move (const vec3 &v); |
|
|
154 | |
65 | void show (); |
155 | virtual void show (); |
66 | void draw (draw_context &ctx); |
156 | virtual void hide (); |
|
|
157 | |
|
|
158 | entity (geometry *g = 0); |
67 | ~entity_filter (); |
159 | ~entity (); |
68 | }; |
160 | }; |
69 | |
161 | |
70 | struct entity : entity_filter { |
162 | struct light : entity |
71 | }; |
163 | { |
72 | |
164 | point p; |
73 | struct vertex1d { |
165 | colour c; |
74 | colour c; // colour |
166 | GLfloat intensity; |
75 | point p; // vertex |
167 | GLfloat radius; |
76 | }; |
|
|
77 | |
|
|
78 | struct vertex2d { |
|
|
79 | point p; // vertex |
|
|
80 | vec3 n; // normal |
|
|
81 | texc t; // texture |
|
|
82 | |
|
|
83 | vertex2d () { }; |
|
|
84 | vertex2d (point p, vec3 n, texc t = texc()) : p(p), n(n), t(t) { }; |
|
|
85 | }; |
|
|
86 | |
|
|
87 | struct entity_opengl : entity_base { |
|
|
88 | GLuint list; |
|
|
89 | |
|
|
90 | entity_opengl (); |
|
|
91 | ~entity_opengl (); |
|
|
92 | }; |
|
|
93 | |
|
|
94 | template<GLenum type> |
|
|
95 | struct entity_opengl1d : entity_opengl, vector<vertex1d> { |
|
|
96 | void update (); |
|
|
97 | void draw (draw_context &ctx); |
|
|
98 | }; |
|
|
99 | |
|
|
100 | template<GLenum type> |
|
|
101 | struct entity_opengl2d : entity_opengl { |
|
|
102 | material m; |
|
|
103 | |
|
|
104 | void set (const vector<vertex2d> &v); |
|
|
105 | void draw (draw_context &ctx); |
|
|
106 | }; |
|
|
107 | |
|
|
108 | typedef entity_opengl1d<GL_POINTS> entity_points; |
|
|
109 | typedef entity_opengl1d<GL_LINES> entity_lines; |
|
|
110 | typedef entity_opengl1d<GL_LINE_STRIP> entity_line_strip; |
|
|
111 | typedef entity_opengl1d<GL_LINE_LOOP> entity_line_loop; |
|
|
112 | typedef entity_opengl2d<GL_TRIANGLES> entity_triangles; |
|
|
113 | typedef entity_opengl2d<GL_TRIANGLE_STRIP> entity_triangle_strip; |
|
|
114 | typedef entity_opengl2d<GL_TRIANGLE_FAN> entity_triangle_fan; |
|
|
115 | typedef entity_opengl2d<GL_QUADS> entity_quads; |
|
|
116 | typedef entity_opengl2d<GL_QUAD_STRIP> entity_quad_strip; |
|
|
117 | typedef entity_opengl2d<GL_POLYGON> entity_polygon; |
|
|
118 | |
|
|
119 | struct entity_transform : entity_filter { |
|
|
120 | protected: |
|
|
121 | matrix m; |
|
|
122 | void renormalize (); |
|
|
123 | public: |
|
|
124 | |
|
|
125 | entity_transform (); |
|
|
126 | |
|
|
127 | void update (); |
|
|
128 | void show (); |
|
|
129 | void draw (draw_context &ctx); |
|
|
130 | void set_matrix (const matrix &xfrm); |
|
|
131 | void update (const matrix &xfrm); |
|
|
132 | }; |
|
|
133 | |
|
|
134 | struct entity_anim : entity_transform { |
|
|
135 | GLfloat vx, vy, vz; |
|
|
136 | void draw (draw_context &ctx); |
|
|
137 | }; |
168 | }; |
138 | |
169 | |
139 | #endif |
170 | #endif |
140 | |
171 | |
141 | |
172 | |