… | |
… | |
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_bbox (); |
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 *> { |
80 | void add (entity_base *e) { push_back (e); e->parent = this; } |
31 | void add (entity_base *e) { push_back (e); e->parent = this; } |
81 | void update_bbox (); |
32 | void update_bbox (); |
82 | void show (const sector &sec); |
|
|
83 | void draw (); |
33 | void show (); |
|
|
34 | void draw (draw_context &ctx); |
84 | ~entity_container (); |
35 | ~entity_container (); |
85 | }; |
36 | }; |
86 | |
37 | |
87 | struct entity_filter : entity_base { |
38 | struct entity_filter : entity_base { |
88 | protected: |
39 | protected: |
… | |
… | |
102 | } |
53 | } |
103 | |
54 | |
104 | entity_base *content () { return e; }; |
55 | entity_base *content () { return e; }; |
105 | |
56 | |
106 | void update_bbox (); |
57 | void update_bbox (); |
107 | void show (const sector &sec); |
|
|
108 | void draw (); |
58 | void show (); |
|
|
59 | void draw (draw_context &ctx); |
109 | ~entity_filter (); |
60 | ~entity_filter (); |
110 | }; |
61 | }; |
111 | |
62 | |
112 | struct entity : entity_filter { |
63 | 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 | }; |
64 | }; |
123 | |
65 | |
124 | struct vertex1d { |
66 | struct vertex1d { |
125 | colour c; // colour |
67 | colour c; // colour |
126 | point p; // vertex |
68 | point p; // vertex |
127 | }; |
69 | }; |
128 | |
70 | |
129 | struct vertex2d { |
71 | struct vertex2d { |
130 | colour c; // colour |
|
|
131 | point p; // vertex |
72 | point p; // vertex |
132 | vec3 n; // normal |
73 | vec3 n; // normal |
133 | texc t; // texture |
74 | texc t; // texture |
134 | |
75 | |
135 | vertex2d () { }; |
76 | vertex2d () { }; |
136 | vertex2d (colour c, point p, vec3 n, texc t = texc()) : c(c), p(p), n(n), t(t) { }; |
77 | vertex2d (point p, vec3 n, texc t = texc()) : p(p), n(n), t(t) { }; |
|
|
78 | }; |
|
|
79 | |
|
|
80 | struct entity_opengl : entity_base { |
137 | }; |
81 | }; |
138 | |
82 | |
139 | template<GLenum type> |
83 | template<GLenum type> |
140 | struct entity_opengl1d : entity_base, vector<vertex1d> { |
84 | struct entity_opengl1d : entity_opengl, vector<vertex1d> { |
141 | void update_bbox (); |
85 | void update_bbox (); |
142 | void show (const sector &sec); |
86 | void draw (draw_context &ctx); |
143 | void draw (); |
|
|
144 | }; |
87 | }; |
145 | |
88 | |
146 | template<GLenum type> |
89 | template<GLenum type> |
147 | struct entity_opengl2d : entity_base, vector<vertex2d> { |
90 | struct entity_opengl2d : entity_opengl, vector<vertex2d> { |
|
|
91 | material m; |
|
|
92 | GLuint list; |
|
|
93 | |
148 | void update_bbox (); |
94 | void update_bbox (); |
149 | void show (const sector &sec); |
95 | void draw (draw_context &ctx); |
150 | void draw (); |
96 | |
|
|
97 | entity_opengl2d () |
|
|
98 | : list(0) |
|
|
99 | { |
|
|
100 | } |
|
|
101 | |
|
|
102 | ~entity_opengl2d () |
|
|
103 | { |
|
|
104 | if (list) |
|
|
105 | glDeleteLists (list, 1); |
|
|
106 | } |
151 | }; |
107 | }; |
152 | |
108 | |
153 | typedef entity_opengl1d<GL_POINTS> entity_points; |
109 | typedef entity_opengl1d<GL_POINTS> entity_points; |
154 | typedef entity_opengl1d<GL_LINES> entity_lines; |
110 | typedef entity_opengl1d<GL_LINES> entity_lines; |
155 | typedef entity_opengl1d<GL_LINE_STRIP> entity_line_strip; |
111 | typedef entity_opengl1d<GL_LINE_STRIP> entity_line_strip; |
… | |
… | |
159 | typedef entity_opengl2d<GL_TRIANGLE_FAN> entity_triangle_fan; |
115 | typedef entity_opengl2d<GL_TRIANGLE_FAN> entity_triangle_fan; |
160 | typedef entity_opengl2d<GL_QUADS> entity_quads; |
116 | typedef entity_opengl2d<GL_QUADS> entity_quads; |
161 | typedef entity_opengl2d<GL_QUAD_STRIP> entity_quad_strip; |
117 | typedef entity_opengl2d<GL_QUAD_STRIP> entity_quad_strip; |
162 | typedef entity_opengl2d<GL_POLYGON> entity_polygon; |
118 | typedef entity_opengl2d<GL_POLYGON> entity_polygon; |
163 | |
119 | |
164 | struct light { |
120 | struct entity_transform : entity_filter { |
165 | point p; |
121 | gl_matrix m; |
166 | colour c; |
122 | |
167 | GLfloat intensity; |
123 | entity_transform (); |
168 | GLfloat radius; |
124 | |
|
|
125 | void update_bbox (); |
|
|
126 | void show (); |
|
|
127 | void draw (draw_context &ctx); |
169 | }; |
128 | }; |
170 | |
129 | |
171 | struct draw_context { |
130 | struct entity_anim : entity_transform { |
172 | enum { DEPTH, AMBIENT, LIGHTED } mode; |
131 | GLfloat vx, vy, vz; |
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); |
132 | void draw (draw_context &ctx); |
183 | }; |
133 | }; |
184 | |
134 | |
185 | #endif |
135 | #endif |
186 | |
136 | |
187 | |
137 | |