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