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