1 | #include <cmath> |
1 | #include <cmath> |
2 | |
2 | |
|
|
3 | #include "opengl.h" |
|
|
4 | |
|
|
5 | #include "view.h" |
3 | #include "oct.h" |
6 | #include "oct.h" |
4 | #include "view.h" |
7 | |
|
|
8 | using namespace gl; |
|
|
9 | |
|
|
10 | vector<GLuint> occ_query_objects; |
|
|
11 | |
|
|
12 | static GLuint begin_occ_query () |
|
|
13 | { |
|
|
14 | GLuint id; |
|
|
15 | |
|
|
16 | if (occ_query_objects.size ()) |
|
|
17 | { |
|
|
18 | id = *(occ_query_objects.end () - 1); |
|
|
19 | occ_query_objects.pop_back (); |
|
|
20 | } |
|
|
21 | else |
|
|
22 | glGenQueriesARB (1, &id); |
|
|
23 | |
|
|
24 | glBeginQueryARB (GL_SAMPLES_PASSED, id); |
|
|
25 | return id; |
|
|
26 | } |
|
|
27 | |
|
|
28 | inline void end_occ_query () |
|
|
29 | { |
|
|
30 | glEndQueryARB (GL_SAMPLES_PASSED); |
|
|
31 | } |
|
|
32 | |
|
|
33 | static GLuint occ_query_result (GLuint id) |
|
|
34 | { |
|
|
35 | GLuint count; |
|
|
36 | |
|
|
37 | glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count); |
|
|
38 | occ_query_objects.push_back (id); |
|
|
39 | |
|
|
40 | return count; |
|
|
41 | } |
5 | |
42 | |
6 | view::view () |
43 | view::view () |
7 | : gamma(1.0) |
44 | : gamma(1.0) |
8 | { |
45 | { |
9 | } |
46 | } |
10 | |
47 | |
11 | view::~view () |
48 | view::~view () |
12 | { |
49 | { |
13 | } |
50 | } |
14 | |
51 | |
|
|
52 | void view::begin_occ_query (recv_occ_query &recv, void *id) |
|
|
53 | { |
|
|
54 | occ_queries.push_back (oq_data (&recv, ::begin_occ_query (), id)); |
|
|
55 | } |
|
|
56 | |
|
|
57 | void view::end_occ_query () |
|
|
58 | { |
|
|
59 | ::end_occ_query (); |
|
|
60 | } |
|
|
61 | |
15 | bool view::may_draw (entity_base *e) |
62 | bool view::may_draw (const entity *e) |
16 | { |
63 | { |
17 | if (drawn.find (e) != drawn.end ()) |
64 | if (drawn.find (e) != drawn.end ()) |
18 | return false; |
65 | return false; |
19 | |
66 | |
20 | drawn.insert (e); |
67 | drawn.insert (e); |
21 | return true; |
68 | return true; |
22 | } |
69 | } |
23 | |
70 | |
|
|
71 | visibility_base *visible::get_visibility (view &ctx) |
|
|
72 | { |
|
|
73 | view::visibility_map::iterator i = ctx.vismap.find (this); |
|
|
74 | visibility_base *vs; |
|
|
75 | |
|
|
76 | if (i == ctx.vismap.end ()) |
|
|
77 | { |
|
|
78 | vs = new_visibility (); |
|
|
79 | ctx.vismap.insert (view::visibility_map::value_type (this, vs)); |
|
|
80 | } |
|
|
81 | else |
|
|
82 | { |
|
|
83 | vs = i->second; |
|
|
84 | |
|
|
85 | if (vs->generation != ctx.generation) |
|
|
86 | { |
|
|
87 | if (vs->generation + 1 != ctx.generation) |
|
|
88 | clear_visibility (vs); |
|
|
89 | |
|
|
90 | vs->generation = ctx.generation; |
|
|
91 | } |
|
|
92 | } |
|
|
93 | |
|
|
94 | return vs; |
|
|
95 | |
|
|
96 | } |
|
|
97 | |
24 | void view::reset_projection () |
98 | void view::reset_projection () |
25 | { |
99 | { |
26 | renormalize (orig, p); |
100 | renormalize (orig, p); |
27 | |
101 | |
28 | glViewport (0, 0, w, h); |
102 | glViewport (0, 0, w, h); |
29 | |
103 | |
30 | glMatrixMode (GL_PROJECTION); |
104 | glMatrixMode (GL_PROJECTION); |
31 | glLoadIdentity (); |
105 | glLoadIdentity (); |
32 | |
106 | |
33 | GLdouble aspect = (GLdouble)w/h; |
107 | GLdouble aspect = (GLdouble)w/h; |
34 | GLdouble zNear = 0.1; |
|
|
35 | GLdouble zFar = 50.; |
|
|
36 | |
|
|
37 | GLdouble ymax = zNear * tan (fov * (M_PI / 360.0)); |
108 | GLdouble ymax = z_near * tan (fov * (M_PI / 360.0)); |
|
|
109 | |
38 | glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar); |
110 | glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, z_near, z_far); |
|
|
111 | |
|
|
112 | perspfact = z_near / ymax * 0.5 * (GLdouble)h; |
39 | |
113 | |
40 | d = normalize (d);//D |
114 | d = normalize (d);//D |
41 | u = normalize (u);//D |
115 | u = normalize (u);//D |
|
|
116 | |
42 | vec3 rz = -d; |
117 | vec3 rz = -d; |
43 | vec3 rx = cross (u, rz); |
118 | vec3 rx = cross (u, rz); |
44 | vec3 ry = cross (rz, rx); |
119 | vec3 ry = cross (rz, rx); |
45 | |
120 | |
46 | matrix &m = projection; |
121 | matrix &m = projection; |
47 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
122 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
48 | m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
123 | m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
49 | m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
124 | m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
50 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
125 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
51 | |
126 | |
52 | glMultMatrixf ((GLfloat *)m.data); |
127 | diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); |
|
|
128 | //printf ("diagfact = %f\n", diagfact); |
|
|
129 | diagfact = sqrtf (3.);//D WHY??? |
53 | |
130 | |
|
|
131 | glMultMatrixf (m); |
|
|
132 | glTranslatef (-p.x, -p.y, -p.z); |
|
|
133 | |
54 | glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&m); |
134 | glGetFloatv (GL_PROJECTION_MATRIX, m); |
55 | |
135 | |
56 | frustum.l = plane ( m(3,0) + m(0,0), m(3,1) + m(0,1), m(3,2) + m(0,2), m(3,3) + m(0,3) ); |
136 | frustum.l = plane ( m(3,0) + m(0,0), m(3,1) + m(0,1), m(3,2) + m(0,2), m(3,3) + m(0,3) ); |
57 | frustum.r = plane ( m(3,0) - m(0,0), m(3,1) - m(0,1), m(3,2) - m(0,2), m(3,3) - m(0,3) ); |
137 | frustum.r = plane ( m(3,0) - m(0,0), m(3,1) - m(0,1), m(3,2) - m(0,2), m(3,3) - m(0,3) ); |
58 | frustum.b = plane ( m(3,0) + m(1,0), m(3,1) + m(1,1), m(3,2) + m(1,2), m(3,3) + m(1,3) ); |
138 | frustum.b = plane ( m(3,0) + m(1,0), m(3,1) + m(1,1), m(3,2) + m(1,2), m(3,3) + m(1,3) ); |
59 | frustum.t = plane ( m(3,0) - m(1,0), m(3,1) - m(1,1), m(3,2) - m(1,2), m(3,3) - m(1,3) ); |
139 | frustum.t = plane ( m(3,0) - m(1,0), m(3,1) - m(1,1), m(3,2) - m(1,2), m(3,3) - m(1,3) ); |
60 | frustum.n = plane ( m(3,0) + m(2,0), m(3,1) + m(2,1), m(3,2) + m(2,2), m(3,3) + m(2,3) ); |
140 | frustum.n = plane ( m(3,0) + m(2,0), m(3,1) + m(2,1), m(3,2) + m(2,2), m(3,3) + m(2,3) ); |
61 | frustum.f = plane ( m(3,0) - m(2,0), m(3,1) - m(2,1), m(3,2) - m(2,2), m(3,3) - m(2,3) ); |
141 | frustum.f = plane ( m(3,0) - m(2,0), m(3,1) - m(2,1), m(3,2) - m(2,2), m(3,3) - m(2,3) ); |
62 | |
142 | |
63 | glMatrixMode (GL_MODELVIEW); |
143 | glMatrixMode (GL_MODELVIEW); |
64 | glLoadIdentity (); |
144 | glLoadIdentity (); |
65 | glTranslatef (-p.x, -p.y, -p.z); |
|
|
66 | } |
145 | } |
67 | |
146 | |
68 | void view::begin () |
147 | void view::begin () |
69 | { |
148 | { |
70 | vismap.clear (); |
149 | generation++; |
|
|
150 | |
|
|
151 | glColorMask (1, 1, 1, 1); |
|
|
152 | glDepthMask (1); |
|
|
153 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
154 | |
71 | vislist.clear (); |
155 | vislist.clear (); |
72 | |
156 | |
73 | generation++; |
157 | z_far = nz_far; |
|
|
158 | c_far = nc_far; |
|
|
159 | |
|
|
160 | if (z_far < z_near) |
|
|
161 | z_far = z_near * 2.; |
74 | |
162 | |
75 | reset_projection (); |
163 | reset_projection (); |
76 | |
164 | |
77 | // check occlusion queries here |
165 | nc_far = nz_far = z_near + 1.F; |
78 | for (vector<octant *>::iterator i = checklist.begin (); i != checklist.end (); ++i) |
|
|
79 | //printf ("%p %d\n", *i, occ_query_result ((*i)->occ_query)); |
|
|
80 | occ_query_result ((*i)->occ_query); |
|
|
81 | |
|
|
82 | checklist.clear (); |
|
|
83 | |
|
|
84 | world.detect_visibility (*this); |
|
|
85 | } |
166 | } |
86 | |
167 | |
87 | void view::end () |
168 | void view::end () |
88 | { |
169 | { |
89 | vismap.clear (); |
170 | vislist.clear (); |
90 | } |
171 | } |
91 | |
172 | |
92 | void view::pass (enum mode m) |
173 | #define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) |
|
|
174 | |
|
|
175 | void view::render (enum pass p) |
93 | { |
176 | { |
94 | mode = m; |
177 | pass = p; |
95 | |
178 | |
96 | glDisable (GL_ALPHA_TEST); |
179 | glDisable (GL_ALPHA_TEST); |
97 | glDisable (GL_BLEND); |
180 | glDisable (GL_BLEND); |
98 | |
181 | |
99 | if (mode == view::DEPTH) |
182 | switch (pass) |
100 | { |
183 | { |
|
|
184 | case DEPTH: |
101 | glEnable (GL_POLYGON_OFFSET_FILL); |
185 | //glEnable (GL_POLYGON_OFFSET_FILL); |
102 | glPolygonOffset (0, 1); |
186 | //glPolygonOffset (0, 5); |
|
|
187 | glDisable (GL_MINMAX); |
|
|
188 | glDepthRange (DEPTH_OFFSET, 1.); |
|
|
189 | glDepthFunc (GL_LESS); |
|
|
190 | glEnable (GL_DEPTH_TEST); |
|
|
191 | glColorMask (0, 0, 0, 0); |
|
|
192 | glDepthMask (1); |
|
|
193 | |
|
|
194 | world.depth_pass (*this); |
|
|
195 | |
|
|
196 | printf ("far %f cf %f CAM (%d,%d,%d)\n", z_far, c_far, orig.x, orig.y, orig.z);//D |
|
|
197 | break; |
|
|
198 | |
|
|
199 | case POSTDEPTH: |
|
|
200 | glEnable (GL_DEPTH_TEST); |
|
|
201 | glDepthRange (DEPTH_OFFSET, 1.); |
|
|
202 | glColorMask (0, 0, 0, 0); |
|
|
203 | glDepthMask (0); |
|
|
204 | |
|
|
205 | // check occlusion queries |
|
|
206 | for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) |
|
|
207 | { |
|
|
208 | occ_query oq(*this, i->data, ::occ_query_result (i->id)); |
|
|
209 | i->recv->event (oq); |
|
|
210 | } |
|
|
211 | |
|
|
212 | occ_queries.clear (); |
|
|
213 | |
|
|
214 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
|
|
215 | (*i)->display (*this); |
|
|
216 | |
|
|
217 | break; |
|
|
218 | |
|
|
219 | case LIGHTED: |
|
|
220 | glEnable (GL_MINMAX); |
|
|
221 | glDepthRange (0., 1. - DEPTH_OFFSET); |
|
|
222 | glDepthFunc (GL_LESS); |
|
|
223 | glColorMask (1, 1, 1, 1); |
|
|
224 | glDepthMask (0); |
|
|
225 | |
|
|
226 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
|
|
227 | (*i)->display (*this); |
|
|
228 | |
|
|
229 | break; |
|
|
230 | } |
|
|
231 | |
|
|
232 | drawn.clear (); |
|
|
233 | |
|
|
234 | #if 0 |
|
|
235 | if (pass == view::DEPTH) |
|
|
236 | { |
|
|
237 | glEnable (GL_DEPTH_CLAMP_NV); |
|
|
238 | glDepthMask (0); |
103 | glDepthFunc (GL_LESS); |
239 | glDepthFunc (GL_LESS); |
104 | glDisable (GL_LIGHTING); |
240 | |
105 | glColorMask (0, 0, 0, 0); |
241 | for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i) |
106 | //cgGLDisableProfile (vsh_profile); |
242 | { |
107 | cgGLDisableProfile (fsh_profile); |
243 | begin_occ_query (**i); |
|
|
244 | (*i)->draw_bbox (*this); |
|
|
245 | end_occ_query (); |
108 | } |
246 | } |
109 | else |
247 | |
|
|
248 | glDisable (GL_DEPTH_CLAMP_NV); |
110 | { |
249 | } |
111 | #if 0 |
|
|
112 | glEnable (GL_MINMAX); |
|
|
113 | #endif |
250 | #endif |
114 | glDisable (GL_POLYGON_OFFSET_FILL); |
|
|
115 | glDepthFunc (GL_LESS); |
|
|
116 | glDepthMask (0); |
|
|
117 | cgGLEnableProfile (vsh_profile); |
|
|
118 | cgGLEnableProfile (fsh_profile); |
|
|
119 | } |
|
|
120 | |
|
|
121 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
|
|
122 | (*i)->display (*this); |
|
|
123 | |
|
|
124 | drawn.clear (); |
|
|
125 | |
|
|
126 | glColorMask (1, 1, 1, 0); |
|
|
127 | glDepthMask (1); |
|
|
128 | } |
251 | } |
129 | |
252 | |
130 | |
253 | |