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