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