--- libgender/view.C 2004/10/06 15:57:51 1.29 +++ libgender/view.C 2004/10/09 21:25:35 1.39 @@ -1,10 +1,44 @@ #include +#define GL_GLEXT_PROTOTYPES #include #include -#include "oct.h" #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) @@ -15,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; @@ -26,7 +70,7 @@ void view::reset_projection () { - renormalize (orig, p); + //renormalize (orig, p); glViewport (0, 0, w, h); @@ -34,11 +78,9 @@ glLoadIdentity (); GLdouble aspect = (GLdouble)w/h; - GLdouble zNear = 0.1; - GLdouble zFar = 50.; + GLdouble ymax = 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, near, far); d = normalize (d);//D u = normalize (u);//D @@ -52,9 +94,12 @@ 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); - 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) ); @@ -65,11 +110,13 @@ glMatrixMode (GL_MODELVIEW); glLoadIdentity (); - glTranslatef (-p.x, -p.y, -p.z); } void view::begin () { + glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glDepthRange (0, 1. - (1. / pow (2., 16.))); + vismap.clear (); vislist.clear (); @@ -77,16 +124,28 @@ reset_projection (); - // check occlusion queries here -#if 0 - for (vector::iterator i = checklist.begin (); i != checklist.end (); ++i) - //printf ("%p %d\n", *i, occ_query_result ((*i)->occ_query)); - occ_query_result ((*i)->occ_query); + farlist.clear (); - checklist.clear (); -#endif + // 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); + } + occ_queries.clear (); + + nextfar = near; world.detect_visibility (*this); + printf ("far %f nf %f RCP %f,%f,%f +%f\n", far, nextfar, + frustum.r.n.x, + frustum.r.n.y, + frustum.r.n.z, + frustum.r.d + );//D + far = nextfar; + + reset_projection (); } void view::end () @@ -108,7 +167,7 @@ glDepthFunc (GL_LESS); glDisable (GL_LIGHTING); glColorMask (0, 0, 0, 0); - cgGLEnableProfile (vsh_profile);// z-fighting?? + cgGLDisableProfile (vsh_profile);// z-fighting?? cgGLDisableProfile (fsh_profile); } else @@ -116,7 +175,7 @@ glEnable (GL_MINMAX); glDisable (GL_POLYGON_OFFSET_FILL); glDepthFunc (GL_LESS); - glDepthMask (0); + glDepthMask (1); cgGLEnableProfile (vsh_profile); cgGLEnableProfile (fsh_profile); } @@ -126,34 +185,33 @@ drawn.clear (); - if (mode == view::LIGHTED) + if (mode == view::DEPTH) { glEnable (GL_DEPTH_CLAMP_NV); - glDepthFunc (GL_LEQUAL); - glEnable (GL_COLOR_MATERIAL); - glDisable (GL_CULL_FACE); -#if 0 + glDepthFunc (GL_LESS); + if (mode == view::LIGHTED) glDepthFunc (GL_LEQUAL); + glDisable (GL_LIGHTING); cgGLDisableProfile (vsh_profile); cgGLDisableProfile (fsh_profile); -#endif - glShadeModel (GL_FLAT); - GLfloat m[4] = { 1, 0, 1, 0 }; - glColor3f (1, 0, 1); - //glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, m); - //glLightModelfv (GL_LIGHT_MODEL_AMBIENT, m); + //glShadeModel (GL_FLAT); +#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) - (*i)->draw_bbox (*this); - - farlist.clear (); + { + 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 (); + } glEnable (GL_DEPTH_TEST); glDisable (GL_DEPTH_CLAMP_NV); - glShadeModel (GL_SMOOTH); + //glShadeModel (GL_SMOOTH); } glColorMask (1, 1, 1, 0);