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