… | |
… | |
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 | |
|
|
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 | { |
|
|
43 | a.x = a.y = a.z = GLFLOAT_MAX; |
|
|
44 | b.x = b.y = b.z = GLFLOAT_MIN; |
|
|
45 | } |
|
|
46 | |
|
|
47 | void add (const box &o); |
|
|
48 | }; |
|
|
49 | |
|
|
50 | struct entity_base { |
14 | struct entity_base { |
51 | struct entity_base *parent; |
15 | struct entity_base *parent; |
|
|
16 | sector orig; |
52 | vector<octant *> o; |
17 | vector<octant *> o; |
53 | box bbox; |
18 | box bbox; |
54 | |
19 | |
55 | virtual void update_bbox (); |
20 | virtual void update_bbox (); |
56 | virtual void show (const sector &sec) { }; |
21 | virtual void show () { world.add (this); }; |
57 | void hide (); |
22 | void hide (); |
|
|
23 | void display (draw_context &ctx); |
58 | virtual void draw () = 0; |
24 | virtual void draw (draw_context &ctx) = 0; |
|
|
25 | |
|
|
26 | entity_base (); |
59 | virtual ~entity_base () |
27 | virtual ~entity_base (); |
60 | { |
|
|
61 | hide (); |
|
|
62 | }; |
|
|
63 | }; |
28 | }; |
64 | |
29 | |
65 | 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) { push_back (e); e->parent = this; } |
66 | void update_bbox (); |
32 | void update_bbox (); |
67 | void show (const sector &sec); |
|
|
68 | void draw (); |
33 | void show (); |
|
|
34 | void draw (draw_context &ctx); |
69 | ~entity_container (); |
35 | ~entity_container (); |
70 | }; |
36 | }; |
71 | |
37 | |
72 | struct entity_filter : entity_base { |
38 | struct entity_filter : entity_base { |
73 | protected: |
39 | protected: |
… | |
… | |
87 | } |
53 | } |
88 | |
54 | |
89 | entity_base *content () { return e; }; |
55 | entity_base *content () { return e; }; |
90 | |
56 | |
91 | void update_bbox (); |
57 | void update_bbox (); |
92 | void show (const sector &sec); |
|
|
93 | void draw (); |
58 | void show (); |
|
|
59 | void draw (draw_context &ctx); |
94 | ~entity_filter (); |
60 | ~entity_filter (); |
95 | }; |
61 | }; |
96 | |
62 | |
97 | struct entity : entity_filter { |
63 | 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 | }; |
64 | }; |
107 | |
65 | |
108 | struct vertex1d { |
66 | struct vertex1d { |
|
|
67 | colour c; // colour |
109 | point p; // vertex |
68 | point p; // vertex |
110 | colour c; // colour |
|
|
111 | }; |
69 | }; |
112 | |
70 | |
113 | struct vertex2d { |
71 | struct vertex2d { |
114 | point p; // vertex |
72 | point p; // vertex |
115 | colour c; // colour |
|
|
116 | vect n; // normal |
73 | vec3 n; // normal |
117 | texc t; // texture |
74 | texc t; // texture |
|
|
75 | |
|
|
76 | vertex2d () { }; |
|
|
77 | vertex2d (point p, vec3 n, texc t = texc()) : p(p), n(n), t(t) { }; |
|
|
78 | }; |
|
|
79 | |
|
|
80 | struct entity_opengl : entity_base { |
|
|
81 | GLuint list; |
|
|
82 | |
|
|
83 | entity_opengl (); |
|
|
84 | ~entity_opengl (); |
118 | }; |
85 | }; |
119 | |
86 | |
120 | template<GLenum type> |
87 | template<GLenum type> |
121 | struct entity_opengl1d : entity_base, vector<vertex1d> { |
88 | struct entity_opengl1d : entity_opengl, vector<vertex1d> { |
122 | void update_bbox (); |
89 | void update_bbox (); |
123 | void show (const sector &sec); |
90 | void draw (draw_context &ctx); |
124 | void draw (); |
|
|
125 | }; |
91 | }; |
126 | |
92 | |
127 | template<GLenum type> |
93 | template<GLenum type> |
128 | struct entity_opengl2d : entity_base, vector<vertex2d> { |
94 | struct entity_opengl2d : entity_opengl { |
129 | void update_bbox (); |
95 | material m; |
130 | void show (const sector &sec); |
96 | |
131 | void draw (); |
97 | void set (const vector<vertex2d> &v); |
|
|
98 | void draw (draw_context &ctx); |
|
|
99 | |
132 | }; |
100 | }; |
133 | |
101 | |
134 | typedef entity_opengl1d<GL_POINTS> entity_points; |
102 | typedef entity_opengl1d<GL_POINTS> entity_points; |
135 | typedef entity_opengl1d<GL_LINES> entity_lines; |
103 | typedef entity_opengl1d<GL_LINES> entity_lines; |
136 | typedef entity_opengl1d<GL_LINE_STRIP> entity_line_strip; |
104 | typedef entity_opengl1d<GL_LINE_STRIP> entity_line_strip; |
… | |
… | |
140 | typedef entity_opengl2d<GL_TRIANGLE_FAN> entity_triangle_fan; |
108 | typedef entity_opengl2d<GL_TRIANGLE_FAN> entity_triangle_fan; |
141 | typedef entity_opengl2d<GL_QUADS> entity_quads; |
109 | typedef entity_opengl2d<GL_QUADS> entity_quads; |
142 | typedef entity_opengl2d<GL_QUAD_STRIP> entity_quad_strip; |
110 | typedef entity_opengl2d<GL_QUAD_STRIP> entity_quad_strip; |
143 | typedef entity_opengl2d<GL_POLYGON> entity_polygon; |
111 | typedef entity_opengl2d<GL_POLYGON> entity_polygon; |
144 | |
112 | |
145 | struct light { |
113 | struct entity_transform : entity_filter { |
146 | point p; |
114 | gl_matrix m; |
147 | colour c; |
115 | |
148 | GLfloat intensity; |
116 | entity_transform (); |
149 | GLfloat radius; |
117 | |
|
|
118 | void update_bbox (); |
|
|
119 | void show (); |
|
|
120 | void draw (draw_context &ctx); |
150 | }; |
121 | }; |
151 | |
122 | |
152 | struct draw_context { |
123 | struct entity_anim : entity_transform { |
153 | enum { DEPTH, AMBIENT, LIGHTED } mode; |
124 | GLfloat vx, vy, vz; |
154 | light *l; |
|
|
155 | }; |
|
|
156 | |
|
|
157 | struct view { |
|
|
158 | point p; |
|
|
159 | vect d; |
|
|
160 | float fov; |
|
|
161 | |
|
|
162 | int width, height; |
|
|
163 | |
|
|
164 | void draw (const draw_context &c); |
125 | void draw (draw_context &ctx); |
165 | }; |
126 | }; |
166 | |
127 | |
167 | #endif |
128 | #endif |
168 | |
129 | |
169 | |
130 | |