… | |
… | |
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" |
9 | |
10 | |
10 | using namespace std; |
11 | using namespace std; |
11 | |
|
|
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 | |
12 | |
50 | struct entity_base { |
13 | struct entity_base { |
51 | struct entity_base *parent; |
14 | struct entity_base *parent; |
52 | vector<octant *> o; |
15 | vector<octant *> o; |
53 | box bbox; |
16 | box bbox; |
54 | |
17 | |
55 | virtual void update_bbox (); |
18 | virtual void update_bbox (); |
56 | virtual void show (const sector &sec) { }; |
19 | virtual void show (const sector &sec) { }; |
57 | void hide (); |
20 | void hide (); |
58 | virtual void draw () = 0; |
21 | virtual void draw (const draw_context &ctx) = 0; |
59 | virtual ~entity_base () |
22 | virtual ~entity_base () |
60 | { |
23 | { |
61 | hide (); |
24 | hide (); |
62 | }; |
25 | }; |
63 | }; |
26 | }; |
64 | |
27 | |
65 | struct entity_container : entity_base, protected vector<entity_base *> { |
28 | struct entity_container : entity_base, protected vector<entity_base *> { |
|
|
29 | void add (entity_base *e) { push_back (e); e->parent = this; } |
66 | void update_bbox (); |
30 | void update_bbox (); |
67 | void show (const sector &sec); |
31 | void show (const sector &sec); |
68 | void draw (); |
32 | void draw (const draw_context &ctx); |
69 | ~entity_container (); |
33 | ~entity_container (); |
70 | }; |
34 | }; |
71 | |
35 | |
72 | struct entity_filter : entity_base { |
36 | struct entity_filter : entity_base { |
73 | protected: |
37 | protected: |
… | |
… | |
88 | |
52 | |
89 | entity_base *content () { return e; }; |
53 | entity_base *content () { return e; }; |
90 | |
54 | |
91 | void update_bbox (); |
55 | void update_bbox (); |
92 | void show (const sector &sec); |
56 | void show (const sector &sec); |
93 | void draw (); |
57 | void draw (const draw_context &ctx); |
94 | ~entity_filter (); |
58 | ~entity_filter (); |
95 | }; |
59 | }; |
96 | |
60 | |
97 | struct entity : entity_filter { |
61 | struct entity : entity_filter { |
98 | sector sec; |
62 | sector sec; |
99 | |
63 | |
100 | void show (const sector &sec); |
64 | void show (const sector &sec); |
101 | void draw (); |
65 | void show (); |
|
|
66 | void draw (const draw_context &ctx); |
102 | }; |
67 | }; |
103 | |
68 | |
104 | struct entity_affine : entity_filter { |
69 | struct entity_affine : entity_filter { |
105 | //matrix m; |
70 | //matrix m; |
106 | }; |
71 | }; |
107 | |
72 | |
108 | struct vertex1d { |
73 | struct vertex1d { |
109 | point v; // vertex |
|
|
110 | colour c; // colour |
74 | colour c; // colour |
|
|
75 | point p; // vertex |
111 | }; |
76 | }; |
112 | |
77 | |
113 | struct vertex2d { |
78 | struct vertex2d { |
114 | point v; // vertex |
79 | point p; // vertex |
115 | colour c; // colour |
|
|
116 | vect n; // normal |
80 | vec3 n; // normal |
117 | texc t; // texture |
81 | texc t; // texture |
|
|
82 | |
|
|
83 | vertex2d () { }; |
|
|
84 | vertex2d (point p, vec3 n, texc t = texc()) : p(p), n(n), t(t) { }; |
|
|
85 | }; |
|
|
86 | |
|
|
87 | struct entity_opengl : entity_base { |
118 | }; |
88 | }; |
119 | |
89 | |
120 | template<GLenum type> |
90 | template<GLenum type> |
121 | struct entity_opengl1d : entity_base, vector<vertex1d> { |
91 | struct entity_opengl1d : entity_opengl, vector<vertex1d> { |
122 | void update_bbox (); |
92 | void update_bbox (); |
123 | void show (const sector &sec); |
93 | void show (const sector &sec); |
124 | void draw (); |
94 | void draw (const draw_context &ctx); |
125 | }; |
95 | }; |
126 | |
96 | |
127 | template<GLenum type> |
97 | template<GLenum type> |
128 | struct entity_opengl2d : entity_base, vector<vertex2d> { |
98 | struct entity_opengl2d : entity_opengl, vector<vertex2d> { |
|
|
99 | material m; |
|
|
100 | |
129 | void update_bbox (); |
101 | void update_bbox (); |
130 | void show (const sector &sec); |
102 | void show (const sector &sec); |
131 | void draw (); |
103 | void draw (const draw_context &ctx); |
132 | }; |
104 | }; |
133 | |
105 | |
134 | typedef entity_opengl1d<GL_POINTS> entity_points; |
106 | typedef entity_opengl1d<GL_POINTS> entity_points; |
135 | typedef entity_opengl1d<GL_LINES> entity_lines; |
107 | typedef entity_opengl1d<GL_LINES> entity_lines; |
136 | typedef entity_opengl1d<GL_LINE_STRIP> entity_linestrip; |
108 | typedef entity_opengl1d<GL_LINE_STRIP> entity_line_strip; |
137 | typedef entity_opengl1d<GL_LINE_LOOP> entity_lineloop; |
109 | typedef entity_opengl1d<GL_LINE_LOOP> entity_line_loop; |
138 | typedef entity_opengl2d<GL_TRIANGLES> entity_triangles; |
110 | typedef entity_opengl2d<GL_TRIANGLES> entity_triangles; |
139 | typedef entity_opengl2d<GL_TRIANGLE_STRIP> entity_trianglestrip; |
111 | typedef entity_opengl2d<GL_TRIANGLE_STRIP> entity_triangle_strip; |
140 | typedef entity_opengl2d<GL_TRIANGLE_FAN> entity_trianglefan; |
112 | typedef entity_opengl2d<GL_TRIANGLE_FAN> entity_triangle_fan; |
141 | typedef entity_opengl2d<GL_QUADS> entity_quads; |
113 | typedef entity_opengl2d<GL_QUADS> entity_quads; |
142 | typedef entity_opengl2d<GL_QUAD_STRIP> entity_quad_strip; |
114 | typedef entity_opengl2d<GL_QUAD_STRIP> entity_quad_strip; |
143 | typedef entity_opengl2d<GL_POLYGON> entity_polygon; |
115 | typedef entity_opengl2d<GL_POLYGON> entity_polygon; |
|
|
116 | |
|
|
117 | struct view { |
|
|
118 | point p; |
|
|
119 | vec3 d, u; |
|
|
120 | float fov; |
|
|
121 | int w, h; |
|
|
122 | |
|
|
123 | void draw (const draw_context &ctx); |
|
|
124 | }; |
144 | |
125 | |
145 | #endif |
126 | #endif |
146 | |
127 | |
147 | |
128 | |
148 | |
129 | |