--- libgender/view.C 2004/10/06 17:55:40 1.32 +++ libgender/view.C 2004/10/11 15:26:34 1.49 @@ -1,12 +1,43 @@ #include -#include -#include +#include "opengl.h" -#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; +} - static GLenum gl_error; view::view () : gamma(1.0) { @@ -16,7 +47,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; @@ -34,36 +75,35 @@ glMatrixMode (GL_PROJECTION); glLoadIdentity (); - gl_error = glGetError (); GLdouble aspect = (GLdouble)w/h; - GLdouble zNear = 0.1; - GLdouble zFar = 50.; + GLdouble ymax = z_near * tan (fov * (M_PI / 360.0)); + + glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, z_near, z_far); - gl_error = glGetError (); - GLdouble ymax = zNear * tan (fov * (M_PI / 360.0)); - glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar); + perspfact = z_near / ymax * 0.5 * (GLdouble)h; - gl_error = glGetError (); d = normalize (d);//D u = normalize (u);//D + vec3 rz = -d; vec3 rx = cross (u, rz); vec3 ry = cross (rz, rx); - gl_error = glGetError (); matrix &m = projection; m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; 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; - gl_error = glGetError (); - glMultMatrixf ((GLfloat *)m.data); + diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); + //printf ("diagfact = %f\n", diagfact); + diagfact = sqrtf (3.);//D WHY??? - gl_error = glGetError (); - glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&m); + glMultMatrixf (m); + glTranslatef (-p.x, -p.y, -p.z); + + glGetFloatv (GL_PROJECTION_MATRIX, m); - gl_error = glGetError (); 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) ); 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) ); @@ -71,11 +111,8 @@ 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) ); 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) ); - gl_error = glGetError (); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); - glTranslatef (-p.x, -p.y, -p.z); - gl_error = glGetError (); } void view::begin () @@ -83,32 +120,38 @@ glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDepthRange (0, 1. - (1. / pow (2., 16.))); - vismap.clear (); vislist.clear (); generation++; - reset_projection (); - - for (vector::iterator i = farlist.begin (); i != farlist.end (); ++i) - ; farlist.clear (); - // 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); + // 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 (); + + z_far = nz_far; + c_far = nc_far; - checklist.clear (); -#endif + if (z_far < z_near) + z_far = z_near * 2.; + reset_projection (); + + nc_far = nz_far = z_near + 1.F; world.detect_visibility (*this); + + printf ("far %f cf %f CAM (%d,%d,%d)\n", z_far, c_far, orig.x, orig.y, orig.z);//D } void view::end () { - vismap.clear (); + vislist.clear (); } void view::pass (enum mode m) @@ -117,16 +160,17 @@ glDisable (GL_ALPHA_TEST); glDisable (GL_BLEND); + //glDisable (GL_DEPTH_TEST); if (mode == view::DEPTH) { glEnable (GL_POLYGON_OFFSET_FILL); - glPolygonOffset (0, 1); + glPolygonOffset (0, 5); glDepthFunc (GL_LESS); glDisable (GL_LIGHTING); glColorMask (0, 0, 0, 0); - cgGLEnableProfile (vsh_profile);// z-fighting?? - cgGLDisableProfile (fsh_profile); + //cgGLDisableProfile (vsh_profile);// z-fighting?? + //cgGLDisableProfile (fsh_profile); } else { @@ -134,40 +178,52 @@ glDisable (GL_POLYGON_OFFSET_FILL); glDepthFunc (GL_LESS); glDepthMask (0); - cgGLEnableProfile (vsh_profile); - cgGLEnableProfile (fsh_profile); + //cgGLEnableProfile (vsh_profile); + //cgGLEnableProfile (fsh_profile); } for (vector::iterator i = vislist.begin (); i != vislist.end (); ++i) - (*i)->display (*this); + { + octant *o = *i; + + visibility_state &vs = vismap[o]; + bool oq = mode == LIGHTED + && vs.last + 1. < timer.now + && (vs.visibility == visibility_state::PARTIAL + || vs.visibility == visibility_state::FULL); + + if (oq) + begin_occ_query (*o); + + o->display (*this); + + if (oq) // && o->fill) WHY??? + { + glColorMask (0, 0, 0, 0); + o->draw_bbox (*this); + glColorMask (1, 1, 1, 0); + } + + if (oq) + end_occ_query (); + } drawn.clear (); - if (mode == view::LIGHTED) + if (mode == view::DEPTH) { glEnable (GL_DEPTH_CLAMP_NV); + glDepthMask (0); glDepthFunc (GL_LESS); - glEnable (GL_COLOR_MATERIAL); - glDisable (GL_LIGHTING); -#if 1 - cgGLDisableProfile (vsh_profile); - cgGLDisableProfile (fsh_profile); -#endif - glShadeModel (GL_FLAT); - - static int count; count++; - if (farlist.size ()) - printf ("%d: size %d\n", count, farlist.size ()); - - for (vector::iterator i = farlist.begin (); i != farlist.end (); ++i) - { - glColor3f (1 / (GLfloat)(i - farlist.begin()), 1, 1 / (GLfloat)(i - farlist.begin())); - (*i)->draw_bbox (*this); + + for (vector::iterator i = farlist.begin (); i != farlist.end (); ++i) + { + begin_occ_query (**i); + (*i)->draw_bbox (*this); + end_occ_query (); } - glEnable (GL_DEPTH_TEST); glDisable (GL_DEPTH_CLAMP_NV); - glShadeModel (GL_SMOOTH); } glColorMask (1, 1, 1, 0);