ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/view.C
(Generate patch)

Comparing libgender/view.C (file contents):
Revision 1.35 by root, Fri Oct 8 17:40:26 2004 UTC vs.
Revision 1.90 by root, Wed Aug 10 02:14:50 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines