1 |
#include <cmath> |
2 |
|
3 |
#include "opengl.h" |
4 |
|
5 |
#include "view.h" |
6 |
#include "oct.h" |
7 |
|
8 |
using namespace gl; |
9 |
|
10 |
pass pass_depth (0); |
11 |
|
12 |
vector<GLuint> occ_query_objects; |
13 |
|
14 |
static GLuint begin_occ_query () |
15 |
{ |
16 |
GLuint id; |
17 |
|
18 |
if (occ_query_objects.size ()) |
19 |
{ |
20 |
id = *(occ_query_objects.end () - 1); |
21 |
occ_query_objects.pop_back (); |
22 |
} |
23 |
else |
24 |
glGenQueries (1, &id); |
25 |
|
26 |
glBeginQuery (GL_SAMPLES_PASSED, id); |
27 |
return id; |
28 |
} |
29 |
|
30 |
inline void end_occ_query () |
31 |
{ |
32 |
glEndQuery (GL_SAMPLES_PASSED); |
33 |
} |
34 |
|
35 |
static GLuint occ_query_result (GLuint id) |
36 |
{ |
37 |
GLuint count; |
38 |
|
39 |
glGetQueryObjectuiv (id, GL_QUERY_RESULT, &count); |
40 |
occ_query_objects.push_back (id); |
41 |
|
42 |
return count; |
43 |
} |
44 |
|
45 |
view::view () |
46 |
: gamma(1.0), nz_near (1.F), nz_far (2.F) |
47 |
{ |
48 |
} |
49 |
|
50 |
view::~view () |
51 |
{ |
52 |
} |
53 |
|
54 |
void view::begin_occ_query (recv_occ_query &recv, void *id) |
55 |
{ |
56 |
occ_queries.push_back (oq_data (&recv, ::begin_occ_query (), id)); |
57 |
} |
58 |
|
59 |
void view::end_occ_query () |
60 |
{ |
61 |
::end_occ_query (); |
62 |
} |
63 |
|
64 |
bool view::may_draw (const entity *e) |
65 |
{ |
66 |
if (drawn.find (e) != drawn.end ()) |
67 |
return false; |
68 |
|
69 |
drawn.insert (e); |
70 |
return true; |
71 |
} |
72 |
|
73 |
visibility_base *visible::get_visibility (view &ctx) |
74 |
{ |
75 |
view::visibility_map::iterator i = ctx.vismap.find (this); |
76 |
visibility_base *vs; |
77 |
|
78 |
if (i == ctx.vismap.end ()) |
79 |
{ |
80 |
vs = new_visibility (); |
81 |
ctx.vismap.insert (view::visibility_map::value_type (this, vs)); |
82 |
} |
83 |
else |
84 |
{ |
85 |
vs = i->second; |
86 |
|
87 |
if (vs->generation != ctx.generation) |
88 |
{ |
89 |
if (vs->generation + 1 != ctx.generation) |
90 |
clear_visibility (vs); |
91 |
|
92 |
vs->generation = ctx.generation; |
93 |
} |
94 |
} |
95 |
|
96 |
return vs; |
97 |
} |
98 |
|
99 |
void view::reset_projection () |
100 |
{ |
101 |
renormalize (orig, p); |
102 |
|
103 |
glViewport (0, 0, w, h); |
104 |
|
105 |
glMatrixMode (GL_PROJECTION); |
106 |
glLoadIdentity (); |
107 |
|
108 |
GLdouble aspect = (GLdouble)w/h; |
109 |
GLdouble ftan = tan (fov * (.5 * M_PI / 180.)); |
110 |
GLdouble ymax = z_near * ftan; |
111 |
GLdouble xmax = ymax * aspect; |
112 |
|
113 |
matrix perspective; |
114 |
#if 1 |
115 |
{ |
116 |
matrix &m = perspective; |
117 |
|
118 |
m(0,0) = 1.F / xmax; m(0,1) = 0; m(0,2) = 0; m(0,3) = 0; |
119 |
m(1,0) = 0; m(1,1) = 1.F / ymax; m(1,2) = 0; m(1,3) = 0; |
120 |
m(2,0) = 0; m(2,1) = 0; m(2,2) = -1e10F / z_far; m(2,3) = 1; |
121 |
m(3,0) = 0; m(3,1) = 0; m(3,2) = -1; m(3,3) = 0; |
122 |
|
123 |
glLoadMatrixf (perspective); |
124 |
} |
125 |
#else |
126 |
glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); |
127 |
#endif |
128 |
|
129 |
glGetFloatv (GL_PROJECTION_MATRIX, perspective); |
130 |
|
131 |
perspfact = z_near / ymax * 0.5F * h; |
132 |
|
133 |
glMatrixMode (GL_MODELVIEW); |
134 |
glLoadIdentity (); |
135 |
matrix &m = projection; |
136 |
|
137 |
d = normalize (d);//D |
138 |
u = normalize (u);//D |
139 |
|
140 |
vec3 rz = -d; |
141 |
vec3 rx = cross (u, rz); |
142 |
vec3 ry = cross (rz, rx); |
143 |
|
144 |
m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
145 |
m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
146 |
m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
147 |
m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
148 |
|
149 |
diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); |
150 |
//printf ("diagfact = %f\n", diagfact); |
151 |
diagfact = sqrtf (3.);//D WHY??? |
152 |
|
153 |
glMultMatrixf (m); |
154 |
|
155 |
glGetFloatv (GL_MODELVIEW_MATRIX, m); |
156 |
|
157 |
m = perspective * m; |
158 |
|
159 |
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) ); |
160 |
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) ); |
161 |
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) ); |
162 |
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) ); |
163 |
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) ); |
164 |
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) ); |
165 |
|
166 |
{ |
167 |
GLdouble frustlen = z_far - z_near; |
168 |
GLdouble fheight = frustlen * ftan; |
169 |
GLdouble fwidth = fheight * w / h; |
170 |
point half (0, 0, z_near + frustlen * .5); |
171 |
point corner (fwidth, fheight, frustlen); |
172 |
|
173 |
frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half)); |
174 |
} |
175 |
|
176 |
{ |
177 |
GLdouble depth = h / ftan; |
178 |
|
179 |
frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); |
180 |
} |
181 |
|
182 |
glTranslatef (-p.x, -p.y, -p.z); |
183 |
} |
184 |
|
185 |
void view::begin () |
186 |
{ |
187 |
generation++; |
188 |
|
189 |
vislist.clear (); |
190 |
|
191 |
z_near = max (nz_near, 1.F); |
192 |
z_far = max (nz_far, z_near * 2.F); |
193 |
c_far = nc_far; |
194 |
|
195 |
reset_projection (); |
196 |
|
197 |
nz_near = 100.; |
198 |
nc_far = nz_far = 1.F; |
199 |
} |
200 |
|
201 |
void view::end () |
202 |
{ |
203 |
vislist.clear (); |
204 |
} |
205 |
|
206 |
#define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) |
207 |
|
208 |
void view::render (enum pass_type p, pass &data) |
209 |
{ |
210 |
pass_type = p; |
211 |
pass_data = &data; |
212 |
|
213 |
switch (pass_type) |
214 |
{ |
215 |
case DEPTH: |
216 |
glColorMask (1, 1, 1, 1); |
217 |
glDepthMask (1); |
218 |
glDisable (GL_BLEND); |
219 |
|
220 |
//glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
221 |
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
222 |
|
223 |
//glEnable (GL_STENCIL_TEST); // for depth-passes |
224 |
//glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); |
225 |
//glStencilFunc (GL_LESS, 1, 255); |
226 |
|
227 |
//glEnable (GL_POLYGON_OFFSET_FILL); |
228 |
//glPolygonOffset (0, 5); |
229 |
glEnable (GL_CULL_FACE); |
230 |
glDisable (GL_MINMAX); |
231 |
glDepthRange (DEPTH_OFFSET, 1.); |
232 |
glDepthFunc (GL_LESS); |
233 |
glEnable (GL_DEPTH_TEST); |
234 |
glColorMask (0, 0, 0, 0); |
235 |
glDisable (GL_DEPTH_CLAMP_NV); |
236 |
|
237 |
world.detect_visibility (*this); |
238 |
|
239 |
break; |
240 |
|
241 |
case POSTDEPTH: |
242 |
glDepthRange (0., 1. - DEPTH_OFFSET); |
243 |
glDepthFunc (GL_LESS); |
244 |
glColorMask (0, 0, 0, 0); |
245 |
glDepthMask (0); |
246 |
glEnable (GL_DEPTH_CLAMP_NV); |
247 |
glDisable (GL_STENCIL_TEST); |
248 |
glDisable (GL_CULL_FACE); |
249 |
|
250 |
// check occlusion queries |
251 |
for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) |
252 |
{ |
253 |
occ_query oq(*this, i->data, ::occ_query_result (i->id)); |
254 |
i->recv->event (oq); |
255 |
} |
256 |
|
257 |
occ_queries.clear (); |
258 |
|
259 |
break; |
260 |
|
261 |
case LIGHTED: |
262 |
glEnable (GL_CULL_FACE); |
263 |
//glEnable (GL_BLEND);//TODO, only when not first lighted pass |
264 |
glBlendFunc (GL_ONE, GL_ONE); |
265 |
//glClear (GL_STENCIL_BUFFER_BIT); |
266 |
//glEnable (GL_STENCIL_TEST); |
267 |
glEnable (GL_MINMAX); |
268 |
glDepthRange (0., 1. - DEPTH_OFFSET); |
269 |
glDepthFunc (GL_LESS); |
270 |
glColorMask (1, 1, 1, 1); |
271 |
glDepthMask (0); |
272 |
glDisable (GL_DEPTH_CLAMP_NV); |
273 |
|
274 |
break; |
275 |
} |
276 |
|
277 |
for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
278 |
(*i)->display (*this); |
279 |
|
280 |
if (pass_type == DEPTH) |
281 |
printf ("fps %f NF %f:%f vis %d CAM (%d,%d,%d)\n", timer.fps, z_near, z_far, drawn.size (), orig.x, orig.y, orig.z);//D |
282 |
|
283 |
drawn.clear (); |
284 |
|
285 |
#if 0 |
286 |
if (pass == view::DEPTH) |
287 |
{ |
288 |
glEnable (GL_DEPTH_CLAMP_NV); |
289 |
glDepthMask (0); |
290 |
glDepthFunc (GL_LESS); |
291 |
|
292 |
for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i) |
293 |
{ |
294 |
begin_occ_query (**i); |
295 |
(*i)->draw_bbox (*this); |
296 |
end_occ_query (); |
297 |
} |
298 |
|
299 |
glDisable (GL_DEPTH_CLAMP_NV); |
300 |
} |
301 |
#endif |
302 |
} |
303 |
|
304 |
void light::enable () |
305 |
{ |
306 |
lightpos->set (p); |
307 |
} |
308 |
|
309 |
void light::disable () |
310 |
{ |
311 |
} |
312 |
|
313 |
static shader::varying_1f camdist; |
314 |
static shader::varying_3f lightvec; |
315 |
|
316 |
void linear_light::vsh () |
317 |
{ |
318 |
using namespace shader::compile; |
319 |
|
320 |
lightvec = xyz (lightpos - model_view_matrix * vin.vertex); |
321 |
camdist = max (1 - length (lightvec) / radius, 0); |
322 |
|
323 |
fsh (); |
324 |
} |
325 |
|
326 |
void linear_light::fsh () |
327 |
{ |
328 |
using namespace shader::compile; |
329 |
|
330 |
sh_lightvec = lightvec; |
331 |
sh_colour = float3 (c.r / 255.F, c.g / 255.F, c.b / 255.F) * (min (intensity * camdist * 100.F + 0.9F, 1.F)); |
332 |
} |
333 |
|
334 |
|