… | |
… | |
3 | |
3 | |
4 | #include <GL/gl.h> |
4 | #include <GL/gl.h> |
5 | |
5 | |
6 | #include <vector> |
6 | #include <vector> |
7 | |
7 | |
|
|
8 | #include "util.h" |
8 | #include "oct.h" |
9 | #include "oct.h" |
|
|
10 | #include "view.h" |
9 | |
11 | |
10 | using namespace std; |
12 | using namespace std; |
11 | |
13 | |
12 | typedef unsigned int soffs; // 32 bit |
|
|
13 | const soffs soffs_max = 1UL << 31; |
|
|
14 | |
|
|
15 | #define GLFLOAT_MAX 1e30 |
|
|
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 | point () { }; |
|
|
26 | point (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
|
|
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 { |
14 | struct entity_base { |
65 | struct entity_base *parent; |
15 | struct entity_base *parent; |
|
|
16 | sector orig; |
66 | vector<octant *> o; |
17 | vector<octant *> o; |
67 | box bbox; |
18 | box bbox; |
68 | |
19 | |
69 | virtual void update_bbox (); |
20 | virtual void update (); |
70 | virtual void show (const sector &sec) { }; |
21 | virtual void show () { world.add (this); }; |
71 | void hide (); |
22 | void hide (); |
|
|
23 | void display (draw_context &ctx); |
72 | virtual void draw () = 0; |
24 | virtual void draw (draw_context &ctx) = 0; |
|
|
25 | |
|
|
26 | entity_base (); |
73 | virtual ~entity_base () |
27 | virtual ~entity_base (); |
74 | { |
|
|
75 | hide (); |
|
|
76 | }; |
|
|
77 | }; |
28 | }; |
78 | |
29 | |
79 | struct entity_container : entity_base, protected vector<entity_base *> { |
30 | struct entity_container : entity_base, protected vector<entity_base *> { |
|
|
31 | void add (entity_base *e) |
|
|
32 | { |
|
|
33 | push_back (e); |
|
|
34 | e->parent = this; |
|
|
35 | update (); |
|
|
36 | } |
|
|
37 | |
80 | void update_bbox (); |
38 | void update (); |
81 | void show (const sector &sec); |
|
|
82 | void draw (); |
39 | void show (); |
|
|
40 | void draw (draw_context &ctx); |
83 | ~entity_container (); |
41 | ~entity_container (); |
84 | }; |
42 | }; |
85 | |
43 | |
86 | struct entity_filter : entity_base { |
44 | struct entity_filter : entity_base { |
87 | protected: |
45 | protected: |
… | |
… | |
90 | |
48 | |
91 | void set (entity_base *e) |
49 | void set (entity_base *e) |
92 | { |
50 | { |
93 | this->e = e; |
51 | this->e = e; |
94 | e->parent = this; |
52 | e->parent = this; |
|
|
53 | update (); |
95 | } |
54 | } |
96 | |
55 | |
97 | void remove () |
56 | void remove () |
98 | { |
57 | { |
99 | this->e->parent = 0; |
58 | this->e->parent = 0; |
100 | this->e = 0; |
59 | this->e = 0; |
101 | } |
60 | } |
102 | |
61 | |
103 | entity_base *content () { return e; }; |
62 | entity_base *content () { return e; }; |
104 | |
63 | |
105 | void update_bbox (); |
64 | void update (); |
106 | void show (const sector &sec); |
|
|
107 | void draw (); |
65 | void show (); |
|
|
66 | void draw (draw_context &ctx); |
108 | ~entity_filter (); |
67 | ~entity_filter (); |
109 | }; |
68 | }; |
110 | |
69 | |
111 | struct entity : entity_filter { |
70 | struct entity : entity_filter { |
112 | sector sec; |
|
|
113 | |
|
|
114 | void show (const sector &sec); |
|
|
115 | void show (); |
|
|
116 | void draw (); |
|
|
117 | }; |
|
|
118 | |
|
|
119 | struct entity_affine : entity_filter { |
|
|
120 | //matrix m; |
|
|
121 | }; |
71 | }; |
122 | |
72 | |
123 | struct vertex1d { |
73 | struct vertex1d { |
124 | colour c; // colour |
74 | colour c; // colour |
125 | point p; // vertex |
75 | point p; // vertex |
126 | }; |
76 | }; |
127 | |
77 | |
128 | struct vertex2d { |
78 | struct vertex2d { |
129 | colour c; // colour |
|
|
130 | point p; // vertex |
79 | point p; // vertex |
131 | vec3 n; // normal |
80 | vec3 n; // normal |
132 | texc t; // texture |
81 | texc t; // texture |
133 | |
82 | |
134 | vertex2d () { }; |
83 | vertex2d () { }; |
135 | vertex2d (colour c, point p, vec3 n, texc t = texc()) : c(c), p(p), n(n), t(t) { }; |
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 (); |
136 | }; |
92 | }; |
137 | |
93 | |
138 | template<GLenum type> |
94 | template<GLenum type> |
139 | struct entity_opengl1d : entity_base, vector<vertex1d> { |
95 | struct entity_opengl1d : entity_opengl, vector<vertex1d> { |
140 | void update_bbox (); |
96 | void update (); |
141 | void show (const sector &sec); |
97 | void draw (draw_context &ctx); |
142 | void draw (); |
|
|
143 | }; |
98 | }; |
144 | |
99 | |
145 | template<GLenum type> |
100 | template<GLenum type> |
146 | struct entity_opengl2d : entity_base, vector<vertex2d> { |
101 | struct entity_opengl2d : entity_opengl { |
147 | void update_bbox (); |
102 | material m; |
148 | void show (const sector &sec); |
103 | |
149 | void draw (); |
104 | void set (const vector<vertex2d> &v); |
|
|
105 | void draw (draw_context &ctx); |
150 | }; |
106 | }; |
151 | |
107 | |
152 | typedef entity_opengl1d<GL_POINTS> entity_points; |
108 | typedef entity_opengl1d<GL_POINTS> entity_points; |
153 | typedef entity_opengl1d<GL_LINES> entity_lines; |
109 | typedef entity_opengl1d<GL_LINES> entity_lines; |
154 | typedef entity_opengl1d<GL_LINE_STRIP> entity_line_strip; |
110 | typedef entity_opengl1d<GL_LINE_STRIP> entity_line_strip; |
… | |
… | |
158 | typedef entity_opengl2d<GL_TRIANGLE_FAN> entity_triangle_fan; |
114 | typedef entity_opengl2d<GL_TRIANGLE_FAN> entity_triangle_fan; |
159 | typedef entity_opengl2d<GL_QUADS> entity_quads; |
115 | typedef entity_opengl2d<GL_QUADS> entity_quads; |
160 | typedef entity_opengl2d<GL_QUAD_STRIP> entity_quad_strip; |
116 | typedef entity_opengl2d<GL_QUAD_STRIP> entity_quad_strip; |
161 | typedef entity_opengl2d<GL_POLYGON> entity_polygon; |
117 | typedef entity_opengl2d<GL_POLYGON> entity_polygon; |
162 | |
118 | |
163 | struct light { |
119 | struct entity_transform : entity_filter { |
164 | point p; |
120 | protected: |
165 | colour c; |
121 | gl_matrix m; |
166 | GLfloat intensity; |
122 | void renormalize (); |
167 | GLfloat radius; |
123 | public: |
|
|
124 | |
|
|
125 | entity_transform (); |
|
|
126 | |
|
|
127 | void update (); |
|
|
128 | void show (); |
|
|
129 | void draw (draw_context &ctx); |
|
|
130 | void set_matrix (const gl_matrix &xfrm); |
|
|
131 | void update (const gl_matrix &xfrm); |
168 | }; |
132 | }; |
169 | |
133 | |
170 | struct draw_context { |
134 | struct entity_anim : entity_transform { |
171 | enum { DEPTH, AMBIENT, LIGHTED } mode; |
135 | GLfloat vx, vy, vz; |
172 | light *l; |
|
|
173 | }; |
|
|
174 | |
|
|
175 | struct view { |
|
|
176 | point p; |
|
|
177 | vec3 d, u; |
|
|
178 | float fov; |
|
|
179 | int w, h; |
|
|
180 | |
|
|
181 | void draw (const draw_context &c); |
136 | void draw (draw_context &ctx); |
182 | }; |
137 | }; |
183 | |
138 | |
184 | #endif |
139 | #endif |
185 | |
140 | |
186 | |
141 | |