--- libgender/view.C 2004/10/10 18:14:22 1.46 +++ libgender/view.C 2004/10/23 02:27:52 1.64 @@ -5,6 +5,8 @@ #include "view.h" #include "oct.h" +using namespace gl; + vector occ_query_objects; static GLuint begin_occ_query () @@ -39,7 +41,7 @@ } view::view () -: gamma(1.0) +: gamma(1.0), nz_near (1.F), nz_far (2.F) { } @@ -47,9 +49,9 @@ { } -void view::begin_occ_query (recv_occ_query &recv) +void view::begin_occ_query (recv_occ_query &recv, void *id) { - occ_queries.push_back (oq_data (&recv, ::begin_occ_query ())); + occ_queries.push_back (oq_data (&recv, ::begin_occ_query (), id)); } void view::end_occ_query () @@ -66,6 +68,32 @@ return true; } +visibility_base *visible::get_visibility (view &ctx) +{ + view::visibility_map::iterator i = ctx.vismap.find (this); + visibility_base *vs; + + if (i == ctx.vismap.end ()) + { + vs = new_visibility (); + ctx.vismap.insert (view::visibility_map::value_type (this, vs)); + } + else + { + vs = i->second; + + if (vs->generation != ctx.generation) + { + if (vs->generation + 1 != ctx.generation) + clear_visibility (vs); + + vs->generation = ctx.generation; + } + } + + return vs; +} + void view::reset_projection () { renormalize (orig, p); @@ -76,9 +104,13 @@ glLoadIdentity (); GLdouble aspect = (GLdouble)w/h; - GLdouble ymax = z_near * tan (fov * (M_PI / 360.0)); + GLdouble ftan = tan (fov * (.5 * M_PI / 180.)); + GLdouble ymax = z_near * ftan; + GLdouble xmax = ymax * aspect; - glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, z_near, z_far); + glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); + + perspfact = z_near / ymax * 0.5F * h; d = normalize (d);//D u = normalize (u);//D @@ -94,12 +126,12 @@ m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); - diagfact = sqrtf (3.);//D WHY??? //printf ("diagfact = %f\n", diagfact); + diagfact = sqrtf (3.);//D WHY??? 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) ); @@ -109,43 +141,40 @@ 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) ); + { + GLdouble frustlen = z_far - z_near; + GLdouble fheight = frustlen * ftan; + GLdouble fwidth = fheight * w / h; + point half (0, 0, z_near + frustlen * .5); + point corner (fwidth, fheight, frustlen); + + frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half)); + } + + { + GLdouble depth = h / ftan; + + frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); + } + glMatrixMode (GL_MODELVIEW); glLoadIdentity (); } void view::begin () { - glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glDepthRange (0, 1. - (1. / pow (2., 16.))); - - vislist.clear (); - generation++; - 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); - } - - occ_queries.clear (); + vislist.clear (); - z_far = nz_far; + z_near = max (nz_near, 1.F); + z_far = max (nz_far, z_near * 2.F); c_far = nc_far; - reset_projection (); - nc_far = nz_far = z_near + 1.F; - world.detect_visibility (*this); + reset_projection (); - 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 + nz_near = 1e37; + nc_far = nz_far = 1.F; } void view::end () @@ -153,92 +182,94 @@ vislist.clear (); } -void view::pass (enum mode m) -{ - mode = m; +#define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) - glDisable (GL_ALPHA_TEST); - glDisable (GL_BLEND); +void view::render (enum pass p) +{ + pass = p; - if (mode == view::DEPTH) - { - glEnable (GL_POLYGON_OFFSET_FILL); - glPolygonOffset (0, 1); - glDepthFunc (GL_LESS); - glDisable (GL_LIGHTING); - glColorMask (0, 0, 0, 0); - cgGLDisableProfile (vsh_profile);// z-fighting?? - cgGLDisableProfile (fsh_profile); - } - else + switch (pass) { - glEnable (GL_MINMAX); - glDisable (GL_POLYGON_OFFSET_FILL); - glDepthFunc (GL_LESS); - glDepthMask (0); - cgGLEnableProfile (vsh_profile); - cgGLEnableProfile (fsh_profile); + case DEPTH: + glColorMask (1, 1, 1, 1); + glDepthMask (1); + + glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + + //glEnable (GL_STENCIL_TEST); // for depth-passes + //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); + //glStencilFunc (GL_LESS, 1, 255); + + //glEnable (GL_POLYGON_OFFSET_FILL); + //glPolygonOffset (0, 5); + glDisable (GL_MINMAX); + glDepthRange (DEPTH_OFFSET, 1.); + glDepthFunc (GL_LESS); + glEnable (GL_DEPTH_TEST); + glColorMask (0, 0, 0, 0); + glDisable (GL_DEPTH_CLAMP_NV); + + world.detect_visibility (*this); + + break; + + case POSTDEPTH: + glDepthRange (0., 1. - DEPTH_OFFSET); + glDepthFunc (GL_LESS); + glColorMask (0, 0, 0, 0); + glDepthMask (0); + glEnable (GL_DEPTH_CLAMP_NV); + glDisable (GL_STENCIL_TEST); + + // check occlusion queries + for (vector::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) + { + occ_query oq(*this, i->data, ::occ_query_result (i->id)); + i->recv->event (oq); + } + + occ_queries.clear (); + + break; + + case LIGHTED: + glClear (GL_STENCIL_BUFFER_BIT); + //glEnable (GL_STENCIL_TEST); + glEnable (GL_MINMAX); + glDepthRange (0., 1. - DEPTH_OFFSET); + glDepthFunc (GL_LESS); + glColorMask (1, 1, 1, 1); + glDepthMask (0); + glDisable (GL_DEPTH_CLAMP_NV); + + break; } for (vector::iterator i = vislist.begin (); i != vislist.end (); ++i) - { - visibility_state &vs = vismap[*i]; - bool oq = mode == LIGHTED - && vs.last + 1. < timer.now - && (vs.visibility == visibility_state::PARTIAL - || vs.visibility == visibility_state::FULL); + (*i)->display (*this); - if (oq) - begin_occ_query (**i); - - (*i)->display (*this); - - if (oq) - { - glColorMask (0, 0, 0, 0); - (*i)->draw_bbox (*this); - glColorMask (1, 1, 1, 0); - } - - if (oq) - end_occ_query (); - } + if (pass == LIGHTED) + 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 drawn.clear (); - if (mode == view::DEPTH) +#if 0 + if (pass == view::DEPTH) { glEnable (GL_DEPTH_CLAMP_NV); glDepthMask (0); - glColorMask (0, 0, 0, 0); glDepthFunc (GL_LESS); - if (mode == view::LIGHTED) glDepthFunc (GL_LEQUAL); - if (mode == view::LIGHTED) glColorMask (1, 1, 1, 0); - 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); + begin_occ_query (**i); (*i)->draw_bbox (*this); - if (mode == view::DEPTH) end_occ_query (); + end_occ_query (); } glDisable (GL_DEPTH_CLAMP_NV); - glDepthMask (1); } - - glColorMask (1, 1, 1, 0); - glDepthMask (1); - +#endif }