--- libgender/view.C 2004/10/08 17:40:26 1.35 +++ libgender/view.C 2004/10/10 00:00:52 1.42 @@ -1,12 +1,45 @@ #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; +} - static GLenum gl_error; view::view () : gamma(1.0) { @@ -16,6 +49,16 @@ { } +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 ()) @@ -34,36 +77,33 @@ glMatrixMode (GL_PROJECTION); glLoadIdentity (); - gl_error = glGetError (); GLdouble aspect = (GLdouble)w/h; - GLdouble zNear = 1.; - GLdouble zFar = 500.; + GLdouble ymax = z_near * tan (fov * (M_PI / 360.0)); - gl_error = glGetError (); - 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); - 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); + diagfact = sqrtf (3.);//D WHY??? + //printf ("diagfact = %f\n", diagfact); + + glMultMatrixf (m); + glTranslatef (-p.x, -p.y, -p.z); - gl_error = glGetError (); - glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&m); + 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,40 @@ 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); + } - checklist.clear (); -#endif + occ_queries.clear (); + nc_far = nz_far = z_near + 1.F; world.detect_visibility (*this); + z_far = nz_far; + c_far = nc_far; + z_far = c_far;//D + + 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) @@ -125,7 +170,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 @@ -133,27 +178,40 @@ glEnable (GL_MINMAX); glDisable (GL_POLYGON_OFFSET_FILL); glDepthFunc (GL_LESS); - glDepthMask (0); + 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 || 1) + if (mode == view::DEPTH) { glEnable (GL_DEPTH_CLAMP_NV); + glDepthMask (0); glDepthFunc (GL_LESS); - glEnable (GL_COLOR_MATERIAL); + if (mode == view::LIGHTED) glDepthFunc (GL_LEQUAL); glDisable (GL_LIGHTING); -#if 1 cgGLDisableProfile (vsh_profile); cgGLDisableProfile (fsh_profile); -#endif - glShadeModel (GL_FLAT); #if 0 static int count; count++; @@ -161,15 +219,16 @@ printf ("%d: size %d\n", count, farlist.size ()); #endif - for (vector::iterator i = farlist.begin (); i != farlist.end (); ++i) - { - glColor3f (1,0,0); - (*i)->draw_bbox (*this); + 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 (); } - glEnable (GL_DEPTH_TEST); glDisable (GL_DEPTH_CLAMP_NV); - glShadeModel (GL_SMOOTH); + glDepthMask (1); } glColorMask (1, 1, 1, 0);