ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/view.C
Revision: 1.84
Committed: Thu Jan 6 03:09:24 2005 UTC (19 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.83: +3 -2 lines
Log Message:
*** empty log message ***

File Contents

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