--- libgender/view.C 2004/10/06 01:41:30 1.18 +++ libgender/view.C 2004/10/10 00:19:45 1.43 @@ -1,7 +1,44 @@ #include -#include "oct.h" +#define GL_GLEXT_PROTOTYPES +#include +#include + #include "view.h" +#include "oct.h" + +vector occ_query_objects; + +static GLuint begin_occ_query () +{ + GLuint id; + + if (occ_query_objects.size ()) + { + id = *(occ_query_objects.end () - 1); + occ_query_objects.pop_back (); + } + else + glGenQueriesARB (1, &id); + + glBeginQueryARB (GL_SAMPLES_PASSED, id); + return id; +} + +inline void end_occ_query () +{ + glEndQueryARB (GL_SAMPLES_PASSED); +} + +static GLuint occ_query_result (GLuint id) +{ + GLuint count; + + glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count); + occ_query_objects.push_back (id); + + return count; +} view::view () : gamma(1.0) @@ -12,7 +49,17 @@ { } -bool view::may_draw (entity_base *e) +void view::begin_occ_query (recv_occ_query &recv) +{ + occ_queries.push_back (oq_data (&recv, ::begin_occ_query ())); +} + +void view::end_occ_query () +{ + ::end_occ_query (); +} + +bool view::may_draw (const entity *e) { if (drawn.find (e) != drawn.end ()) return false; @@ -31,14 +78,13 @@ glLoadIdentity (); GLdouble aspect = (GLdouble)w/h; - GLdouble zNear = 0.1; - GLdouble zFar = 50.; + GLdouble ymax = z_near * tan (fov * (M_PI / 360.0)); - GLdouble ymax = zNear * tan (fov * (M_PI / 360.0)); - glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar); + glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, z_near, z_far); d = normalize (d);//D u = normalize (u);//D + vec3 rz = -d; vec3 rx = cross (u, rz); vec3 ry = cross (rz, rx); @@ -49,9 +95,14 @@ m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; - glMultMatrixf ((GLfloat *)m.data); + diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); + diagfact = sqrtf (3.);//D WHY??? + //printf ("diagfact = %f\n", diagfact); - glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&m); + glMultMatrixf (m); + glTranslatef (-p.x, -p.y, -p.z); + + glGetFloatv (GL_PROJECTION_MATRIX, m); 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) ); 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) ); @@ -62,26 +113,46 @@ glMatrixMode (GL_MODELVIEW); glLoadIdentity (); - glTranslatef (-p.x, -p.y, -p.z); } void view::begin () { - vismap.clear (); + glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glDepthRange (0, 1. - (1. / pow (2., 16.))); + + vislist.clear (); generation++; - reset_projection (); + farlist.clear (); + + // check occlusion queries + for (vector::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) + { + occ_query oq(*this, i->second, occ_query_result (i->second)); + i->first->event (oq); + } - // check occlusion queries here - checklist.clear (); + occ_queries.clear (); + z_far = nz_far; + c_far = nc_far; + reset_projection (); + + nc_far = nz_far = z_near + 1.F; world.detect_visibility (*this); + + printf ("far %f cf %f RCP %f,%f,%f +%f\n", z_far, c_far, + frustum.r.n.x, + frustum.r.n.y, + frustum.r.n.z, + frustum.r.d + );//D } void view::end () { - vismap.clear (); + vislist.clear (); } void view::pass (enum mode m) @@ -98,26 +169,70 @@ glDepthFunc (GL_LESS); glDisable (GL_LIGHTING); glColorMask (0, 0, 0, 0); - //cgGLDisableProfile (CG_PROFILE_ARBVP1); - cgGLDisableProfile (CG_PROFILE_ARBFP1); + cgGLDisableProfile (vsh_profile);// z-fighting?? + cgGLDisableProfile (fsh_profile); } else { glEnable (GL_MINMAX); glDisable (GL_POLYGON_OFFSET_FILL); glDepthFunc (GL_LESS); - glDepthMask (0); - cgGLEnableProfile (CG_PROFILE_ARBVP1); - cgGLEnableProfile (CG_PROFILE_ARBFP1); + glDepthMask (1); + cgGLEnableProfile (vsh_profile); + cgGLEnableProfile (fsh_profile); } for (vector::iterator i = vislist.begin (); i != vislist.end (); ++i) - (*i)->display (*this); + { + visibility_state &vs = vismap[*i]; + bool oq = (vs.visibility == visibility_state::PARTIAL + || vs.visibility == visibility_state::FULL) + && (vs.last + 1. < timer.now) + && (mode == LIGHTED); + + if (oq) + begin_occ_query (**i); + + (*i)->display (*this); + + if (oq) + end_occ_query (); + + } drawn.clear (); + if (mode == view::DEPTH) + { + glEnable (GL_DEPTH_CLAMP_NV); + glDepthMask (0); + glDepthFunc (GL_LESS); + if (mode == view::LIGHTED) glDepthFunc (GL_LEQUAL); + glDisable (GL_LIGHTING); + cgGLDisableProfile (vsh_profile); + cgGLDisableProfile (fsh_profile); + +#if 0 + static int count; count++; + if (farlist.size ()) + printf ("%d: size %d\n", count, farlist.size ()); +#endif + + for (vector::iterator i = farlist.begin (); i != farlist.end (); ++i) + { + if (mode == view::DEPTH) begin_occ_query (**i); + if (mode == view::LIGHTED) glColor3f ((((long)*i >> 6) & 15) / 31. + 0.5,0,0); + (*i)->draw_bbox (*this); + if (mode == view::DEPTH) end_occ_query (); + } + + glDisable (GL_DEPTH_CLAMP_NV); + glDepthMask (1); + } + glColorMask (1, 1, 1, 0); glDepthMask (1); + }