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

Comparing libgender/view.C (file contents):
Revision 1.75 by root, Sun Nov 7 02:28:18 2004 UTC vs.
Revision 1.79 by root, Wed Nov 10 01:57:16 2004 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"
7 8
8using namespace gl; 9using namespace gl;
9 10
10pass pass_depth (0); 11pass_data pass_depth (0, DEPTH);
12pass_data pass_postdepth (0, POSTDEPTH);
11 13
12vector<GLuint> occ_query_objects; 14vector<GLuint> occ_query_objects;
13 15
14static GLuint begin_occ_query () 16static GLuint begin_occ_query ()
15{ 17{
57} 59}
58 60
59void view::end_occ_query () 61void view::end_occ_query ()
60{ 62{
61 ::end_occ_query (); 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
62} 82}
63 83
64bool view::may_draw (const entity *e) 84bool view::may_draw (const entity *e)
65{ 85{
66 if (drawn.find (e) != drawn.end ()) 86 if (drawn.find (e) != drawn.end ())
101 renormalize (orig, p); 121 renormalize (orig, p);
102 122
103 glViewport (0, 0, w, h); 123 glViewport (0, 0, w, h);
104 124
105 glMatrixMode (GL_PROJECTION); 125 glMatrixMode (GL_PROJECTION);
106 glLoadIdentity ();
107 126
108 GLdouble aspect = (GLdouble)w/h; 127 GLdouble aspect = (GLdouble)w/h;
109 GLdouble ftan = tan (fov * (.5 * M_PI / 180.)); 128 GLdouble ftan = tanf (fov * GLfloat (.5 * M_PI / 180.));
110 GLdouble ymax = z_near * ftan; 129 GLdouble ymax = ftan;
111 GLdouble xmax = ymax * aspect; 130 GLdouble xmax = ymax * aspect;
112 131
113 matrix perspective; 132 matrix perspective;
114#if 1
115 { 133 {
116 matrix &m = perspective; 134 matrix &m = perspective;
117 135
118 m(0,0) = 1.F / xmax; m(0,1) = 0; m(0,2) = 0; m(0,3) = 0; 136 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; 137 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; 138 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; 139 m(3,0) = 0; m(3,1) = 0; m(3,2) = -1; m(3,3) = 0;
122 140
123 glLoadMatrixf (perspective); 141 glLoadMatrixf (perspective);
124 } 142 }
125#else
126 glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); 143 //glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far);
127#endif
128 144
129 glGetFloatv (GL_PROJECTION_MATRIX, perspective); 145 glGetFloatv (GL_PROJECTION_MATRIX, perspective);
130 146
131 perspfact = z_near / ymax * 0.5F * h; 147 perspfact = z_near / ymax * 0.5F * h;
132 148
150 //printf ("diagfact = %f\n", diagfact); 166 //printf ("diagfact = %f\n", diagfact);
151 diagfact = sqrtf (3.);//D WHY??? 167 diagfact = sqrtf (3.);//D WHY???
152 168
153 glMultMatrixf (m); 169 glMultMatrixf (m);
154 170
171 glTranslatef (-p.x, -p.y, -p.z);
172
155 glGetFloatv (GL_MODELVIEW_MATRIX, m); 173 glGetFloatv (GL_MODELVIEW_MATRIX, m);
156 174
157 m = perspective * m; 175 m = perspective * m;
158 176
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) ); 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) );
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) ); 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) );
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) ); 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) );
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) ); 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) );
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) ); 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) );
165 183
184#if 0
166 { 185 {
167 GLdouble frustlen = z_far - z_near; 186 GLdouble frustlen = z_far - z_near;
168 GLdouble fheight = frustlen * ftan; 187 GLdouble fheight = frustlen * ftan;
169 GLdouble fwidth = fheight * w / h; 188 GLdouble fwidth = fheight * w / h;
170 point half (0, 0, z_near + frustlen * .5); 189 point half (0, 0, z_near + frustlen * .5);
171 point corner (fwidth, fheight, frustlen); 190 point corner (fwidth, fheight, frustlen);
172 191
173 frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half)); 192 frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half));
174 } 193 }
194#endif
175 195
176 { 196 {
177 GLdouble depth = h / ftan; 197 GLdouble depth = h / ftan;
178 198
179 frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); 199 frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h));
180 } 200 }
181
182 glTranslatef (-p.x, -p.y, -p.z);
183} 201}
184 202
185void view::begin () 203void view::begin ()
186{ 204{
187 generation++; 205 generation++;
194 212
195 reset_projection (); 213 reset_projection ();
196 214
197 nz_near = 100.; 215 nz_near = 100.;
198 nc_far = nz_far = 1.F; 216 nc_far = nz_far = 1.F;
217
218 first_lighted = false;
199} 219}
200 220
201void view::end () 221void view::end ()
202{ 222{
203 vislist.clear (); 223 vislist.clear ();
204} 224}
205 225
206#define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) 226#define DEPTH_OFFSET (1. / (GLdouble)(1L << 16))
207 227
208void view::render (enum pass_type p, pass &data) 228void view::render (pass_data &pass)
209{ 229{
210 pass_type = p; 230 this->pass = &pass;
211 pass_data = &data;
212 231
232 stat1 = stat2 = 0; //D
233
213 switch (pass_type) 234 switch (pass.type)
214 { 235 {
215 case DEPTH: 236 case DEPTH:
216 glColorMask (1, 1, 1, 1); 237 glColorMask (1, 1, 1, 1);
217 glDepthMask (1); 238 glDepthMask (1);
218 glDisable (GL_BLEND); 239 glDisable (GL_BLEND);
234 glColorMask (0, 0, 0, 0); 255 glColorMask (0, 0, 0, 0);
235 glDisable (GL_DEPTH_CLAMP_NV); 256 glDisable (GL_DEPTH_CLAMP_NV);
236 257
237 world.detect_visibility (*this); 258 world.detect_visibility (*this);
238 259
260 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
261 (*i)->draw_depth (*this);
262
263 if (pass.type == DEPTH)
264 printf ("fps %f NF %f:%f vis %d,%d (%d,%d) CAM (%ld,%ld,%ld)\n",
265 timer.fps, z_near, z_far, vislist.size (), drawn.size (), stat1, stat2, orig.x, orig.y, orig.z);//D
266
239 break; 267 break;
240 268
241 case POSTDEPTH: 269 case POSTDEPTH:
242 glDepthRange (0., 1. - DEPTH_OFFSET); 270 glDepthRange (0., 1. - DEPTH_OFFSET);
243 glDepthFunc (GL_LESS); 271 glDepthFunc (GL_LESS);
245 glDepthMask (0); 273 glDepthMask (0);
246 glEnable (GL_DEPTH_CLAMP_NV); 274 glEnable (GL_DEPTH_CLAMP_NV);
247 glDisable (GL_STENCIL_TEST); 275 glDisable (GL_STENCIL_TEST);
248 glDisable (GL_CULL_FACE); 276 glDisable (GL_CULL_FACE);
249 277
278 occ_query_material.enable (*this);
279
250 // check occlusion queries 280 // check occlusion queries
251 for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) 281 for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i)
252 { 282 {
253 occ_query oq(*this, i->data, ::occ_query_result (i->id)); 283 occ_query oq(*this, i->data, ::occ_query_result (i->id));
254 i->recv->event (oq); 284 i->recv->event (oq);
255 } 285 }
256 286
287 occ_query_material.disable (*this);
288
257 occ_queries.clear (); 289 occ_queries.clear ();
258 290
291 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
292 (*i)->draw_postdepth (*this);
293
294 first_lighted = true;
295
259 break; 296 break;
260 297
261 case LIGHTED: 298 case LIGHTED:
262 glEnable (GL_CULL_FACE); 299 if (first_lighted)
263 //glEnable (GL_BLEND);//TODO, only when not first lighted pass 300 glDisable (GL_BLEND);
301 else
302 {
264 glBlendFunc (GL_ONE, GL_ONE); 303 glBlendFunc (GL_ONE, GL_ONE);
304 glEnable (GL_BLEND);
305 }
306
265 //glClear (GL_STENCIL_BUFFER_BIT); 307 //glClear (GL_STENCIL_BUFFER_BIT);
266 //glEnable (GL_STENCIL_TEST); 308 //glEnable (GL_STENCIL_TEST);
309 glEnable (GL_CULL_FACE);
267 glEnable (GL_MINMAX); 310 glEnable (GL_MINMAX);
268 glDepthRange (0., 1. - DEPTH_OFFSET); 311 glDepthRange (0., 1. - DEPTH_OFFSET);
269 glDepthFunc (GL_LESS); 312 glDepthFunc (GL_LESS);
270 glColorMask (1, 1, 1, 1); 313 glColorMask (1, 1, 1, 1);
271 glDepthMask (0); 314 glDepthMask (0);
272 glDisable (GL_DEPTH_CLAMP_NV); 315 glDisable (GL_DEPTH_CLAMP_NV);
273 316
317 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
318 (*i)->draw_lighted (*this);
319
320 first_lighted = false;
321
274 break; 322 break;
275 } 323 }
276 324
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 (); 325 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} 326}
303 327
304void light::enable () 328void light::enable ()
305{ 329{
306 lightpos->set (p); 330 lightpos->set (p);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines