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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines