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

Comparing libgender/view.C (file contents):
Revision 1.66 by root, Fri Oct 29 23:27:02 2004 UTC vs.
Revision 1.90 by root, Wed Aug 10 02:14:50 2005 UTC

2 2
3#include "opengl.h" 3#include "opengl.h"
4 4
5#include "view.h" 5#include "view.h"
6#include "oct.h" 6#include "oct.h"
7#include "material.h"
8
9#include "entity.h"
7 10
8using namespace gl; 11using namespace gl;
12
13skybox *world_skybox;
14
15pass_data pass_depth (0, DEPTH);
16pass_data pass_postdepth (0, POSTDEPTH);
9 17
10vector<GLuint> occ_query_objects; 18vector<GLuint> occ_query_objects;
11 19
12static GLuint begin_occ_query () 20static GLuint begin_occ_query ()
13{ 21{
55} 63}
56 64
57void view::end_occ_query () 65void view::end_occ_query ()
58{ 66{
59 ::end_occ_query (); 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
60} 86}
61 87
62bool view::may_draw (const entity *e) 88bool view::may_draw (const entity *e)
63{ 89{
64 if (drawn.find (e) != drawn.end ()) 90 if (drawn.find (e) != drawn.end ())
82 { 108 {
83 vs = i->second; 109 vs = i->second;
84 110
85 if (vs->generation != ctx.generation) 111 if (vs->generation != ctx.generation)
86 { 112 {
87 if (vs->generation + 1 != ctx.generation) 113 if (ctx.generation - vs->generation > 2)
88 clear_visibility (vs); 114 clear_visibility (vs);
89 115
90 vs->generation = ctx.generation; 116 vs->generation = ctx.generation;
91 } 117 }
92 } 118 }
98{ 124{
99 renormalize (orig, p); 125 renormalize (orig, p);
100 126
101 glViewport (0, 0, w, h); 127 glViewport (0, 0, w, h);
102 128
103 glMatrixMode (GL_PROJECTION);
104 glLoadIdentity ();
105
106 GLdouble aspect = (GLdouble)w/h; 129 GLdouble aspect = (GLdouble)w/h;
107 GLdouble ftan = tan (fov * (.5 * M_PI / 180.)); 130 GLdouble ftan = tanf (fov * GLfloat (.5 * M_PI / 180.));
108 GLdouble ymax = z_near * ftan; 131 GLdouble ymax = ftan;
109 GLdouble xmax = ymax * aspect; 132 GLdouble xmax = ymax * aspect;
110 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 }
111 glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); 145 //glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far);
146
147 //glGetFloatv (GL_PROJECTION_MATRIX, perspective);
112 148
113 perspfact = z_near / ymax * 0.5F * h; 149 perspfact = z_near / ymax * 0.5F * h;
114 150
151 matrix view;
152 {
153 matrix &m = view;
154
115 d = normalize (d);//D 155 d = normalize (d);//D
116 u = normalize (u);//D 156 u = normalize (u);//D
117 157
118 vec3 rz = -d; 158 vec3 rz = -d;
119 vec3 rx = cross (u, rz); 159 vec3 rx = cross (u, rz);
120 vec3 ry = cross (rz, rx); 160 vec3 ry = cross (rz, rx);
121 161
122 matrix &m = projection;
123 m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; 162 m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0;
124 m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; 163 m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0;
125 m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; 164 m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0;
126 m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; 165 m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1.F;
127 166
128 diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); 167 //diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z);
168 diagfact = sqrtf (3.F);
169 }
170
129 //printf ("diagfact = %f\n", diagfact); 171 //printf ("diagfact = %f\n", diagfact);
130 diagfact = sqrtf (3.);//D WHY??? 172 //diagfact = sqrtf (3.);//D WHY???
173 //
174 view = view * matrix::translation (-p);
131 175
132 glMultMatrixf (m); 176 matrix projection = perspective * view;
133 glTranslatef (-p.x, -p.y, -p.z); 177
134 178 {
135 glGetFloatv (GL_PROJECTION_MATRIX, m); 179 matrix &m = projection;
136 180
137 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) ); 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) );
138 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) ); 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) );
139 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) ); 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) );
140 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) ); 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) );
141 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) ); 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) );
142 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) ); 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 }
143 188
189#if 0
144 { 190 {
145 GLdouble frustlen = z_far - z_near; 191 GLdouble frustlen = z_far - z_near;
146 GLdouble fheight = frustlen * ftan; 192 GLdouble fheight = frustlen * ftan;
147 GLdouble fwidth = fheight * w / h; 193 GLdouble fwidth = fheight * w / h;
148 point half (0, 0, z_near + frustlen * .5); 194 point half (0, 0, z_near + frustlen * .5);
149 point corner (fwidth, fheight, frustlen); 195 point corner (fwidth, fheight, frustlen);
150 196
151 frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half)); 197 frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half));
152 } 198 }
199#endif
153 200
154 {
155 GLdouble depth = h / ftan; 201 GLdouble depth = h / ftan;
156
157 frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); 202 frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan));
158 } 203
204 glMatrixMode (GL_PROJECTION);
205 glLoadMatrixf (projection);
159 206
160 glMatrixMode (GL_MODELVIEW); 207 glMatrixMode (GL_MODELVIEW);
161 glLoadIdentity (); 208 glLoadIdentity ();
162} 209}
163 210
164void view::begin () 211void view::begin ()
165{ 212{
166 generation++; 213 generation++;
167 214
215 stat1 = stat2 = 0; //D
216
168 vislist.clear (); 217 vislist.clear ();
218 postdepthlist.clear ();
169 219
170 z_near = max (nz_near, 1.F); 220 z_near = max (nz_near, 1.F);
171 z_far = max (nz_far, z_near * 2.F); 221 z_far = max (nz_far, 1E10F);//D
172 c_far = nc_far; 222 c_far = nc_far;
173 223
174 reset_projection (); 224 reset_projection ();
175 225
176 nz_near = 100.; 226 nz_near = 100.;
177 nc_far = nz_far = 1.F; 227 nc_far = nz_far = 1.F;
228
229 first_lighted = false;
178} 230}
179 231
180void view::end () 232void view::end ()
181{ 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
182 vislist.clear (); 237 vislist.clear ();
183} 238}
184 239
185#define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) 240#define DEPTH_OFFSET (1. / (GLdouble)(1L << 16))
186 241
187void view::render (enum pass p) 242void view::render (pass_data &pass)
188{ 243{
189 pass = p; 244 this->pass = &pass;
190 245
191 switch (pass) 246 switch (pass.type)
192 { 247 {
193 case DEPTH: 248 case DEPTH:
249 glEnable (GL_CULL_FACE);
250 glDisable (GL_MINMAX);
251 glDepthRange (DEPTH_OFFSET, 1.);
252 glDepthFunc (GL_LESS);
194 glColorMask (1, 1, 1, 1); 253 glColorMask (1, 1, 1, 1);
254 glDisable (GL_BLEND);
195 glDepthMask (1); 255 glDepthMask (1);
196 256
197 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 257 glClear (GL_DEPTH_BUFFER_BIT | (world_skybox ? 0 : GL_COLOR_BUFFER_BIT));
198 258
199 //glEnable (GL_STENCIL_TEST); // for depth-passes 259 //glEnable (GL_STENCIL_TEST); // for depth-passes
200 //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); 260 //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
201 //glStencilFunc (GL_LESS, 1, 255); 261 //glStencilFunc (GL_LESS, 1, 255);
202 262
203 //glEnable (GL_POLYGON_OFFSET_FILL); 263 //glEnable (GL_POLYGON_OFFSET_FILL);
204 //glPolygonOffset (0, 5); 264 //glPolygonOffset (0, 5);
205 glDisable (GL_MINMAX);
206 glDepthRange (DEPTH_OFFSET, 1.);
207 glDepthFunc (GL_LESS);
208 glEnable (GL_DEPTH_TEST); 265 glEnable (GL_DEPTH_TEST);
209 glColorMask (0, 0, 0, 0); 266 glColorMask (0, 0, 0, 0);
210 glDisable (GL_DEPTH_CLAMP_NV);
211 267
212 world.detect_visibility (*this); 268 world.detect_visibility (*this);
269
270 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
271 (*i)->draw_depth (*this);
213 272
214 break; 273 break;
215 274
216 case POSTDEPTH: 275 case POSTDEPTH:
217 glDepthRange (0., 1. - DEPTH_OFFSET); 276 glDepthRange (0., 1. - DEPTH_OFFSET);
218 glDepthFunc (GL_LESS); 277 glDepthFunc (GL_LESS);
219 glColorMask (0, 0, 0, 0); 278 glColorMask (0, 0, 0, 0);
220 glDepthMask (0); 279 glDepthMask (0);
280 glDisable (GL_STENCIL_TEST);
281 glDisable (GL_CULL_FACE);
282
221 glEnable (GL_DEPTH_CLAMP_NV); 283 glEnable (GL_DEPTH_CLAMP_NV);
222 glDisable (GL_STENCIL_TEST);
223 284
285 occ_query_material.enable (*this);
286
224 // check occlusion queries 287 // check occlusion queries
225 for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) 288 for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i)
226 { 289 {
227 occ_query oq(*this, i->data, ::occ_query_result (i->id)); 290 //occ_query oq(*this, i->data, ::occ_query_result (i->id));
228 i->recv->event (oq); 291 //i->recv->event (oq);
229 } 292 }
230 293
294 occ_query_material.disable (*this);
295
231 occ_queries.clear (); 296 occ_queries.clear ();
232 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
233 break; 305 break;
234 306
235 case LIGHTED: 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
236 glClear (GL_STENCIL_BUFFER_BIT); 316 //glClear (GL_STENCIL_BUFFER_BIT);
237 //glEnable (GL_STENCIL_TEST); 317 //glEnable (GL_STENCIL_TEST);
318 glEnable (GL_CULL_FACE);
238 glEnable (GL_MINMAX); 319 glEnable (GL_MINMAX);
239 glDepthRange (0., 1. - DEPTH_OFFSET); 320 glDepthRange (0., 1. - DEPTH_OFFSET);
240 glDepthFunc (GL_LESS); 321 glDepthFunc (GL_LESS);
241 glColorMask (1, 1, 1, 1); 322 glColorMask (1, 1, 1, 1);
242 glDepthMask (0); 323 glDepthMask (0);
324
325 if (world_skybox)
326 {
243 glDisable (GL_DEPTH_CLAMP_NV); 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;
244 336
245 break; 337 break;
246 } 338 }
247 339
248 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
249 (*i)->display (*this);
250
251 if (pass == LIGHTED)
252 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
253
254 drawn.clear (); 340 drawn.clear ();
255
256#if 0
257 if (pass == view::DEPTH)
258 {
259 glEnable (GL_DEPTH_CLAMP_NV);
260 glDepthMask (0);
261 glDepthFunc (GL_LESS);
262
263 for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i)
264 {
265 begin_occ_query (**i);
266 (*i)->draw_bbox (*this);
267 end_occ_query ();
268 }
269
270 glDisable (GL_DEPTH_CLAMP_NV);
271 }
272#endif
273} 341}
274 342
343void light::enable (view &ctx)
344{
345 lightpos->set (p + (orig - ctx.orig));
346}
275 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