--- libgender/view.C 2004/11/04 04:46:58 1.71 +++ libgender/view.C 2004/11/27 03:33:51 1.82 @@ -4,10 +4,12 @@ #include "view.h" #include "oct.h" +#include "material.h" using namespace gl; -pass pass_depth (0); +pass_data pass_depth (0, DEPTH); +pass_data pass_postdepth (0, POSTDEPTH); vector occ_query_objects; @@ -61,6 +63,24 @@ ::end_occ_query (); } +struct occ_query_material : material +{ + void vsh (view &ctx); + void fsh (view &ctx); +}; + +static struct occ_query_material occ_query_material; + +void occ_query_material::vsh (view &ctx) +{ + std_vsh (); +} + +void occ_query_material::fsh (view &ctx) +{ + // nop +} + bool view::may_draw (const entity *e) { if (drawn.find (e) != drawn.end ()) @@ -86,7 +106,7 @@ if (vs->generation != ctx.generation) { - if (vs->generation + 1 != ctx.generation) + if (ctx.generation - vs->generation > 2) clear_visibility (vs); vs->generation = ctx.generation; @@ -103,17 +123,33 @@ glViewport (0, 0, w, h); glMatrixMode (GL_PROJECTION); - glLoadIdentity (); GLdouble aspect = (GLdouble)w/h; - GLdouble ftan = tan (fov * (.5 * M_PI / 180.)); - GLdouble ymax = z_near * ftan; + GLdouble ftan = tanf (fov * GLfloat (.5 * M_PI / 180.)); + GLdouble ymax = ftan; GLdouble xmax = ymax * aspect; - glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); + matrix perspective; + { + matrix &m = perspective; + + m(0,0) = 1.F / xmax; m(0,1) = 0; m(0,2) = 0; m(0,3) = 0; + m(1,0) = 0; m(1,1) = 1.F / ymax; m(1,2) = 0; m(1,3) = 0; + m(2,0) = 0; m(2,1) = 0; m(2,2) = -1e10F / z_far; m(2,3) = 0; + m(3,0) = 0; m(3,1) = 0; m(3,2) = -1.F; m(3,3) = 0; + + glLoadMatrixf (perspective); + } + //glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); + + glGetFloatv (GL_PROJECTION_MATRIX, perspective); perspfact = z_near / ymax * 0.5F * h; + glMatrixMode (GL_MODELVIEW); + glLoadIdentity (); + matrix &m = projection; + d = normalize (d);//D u = normalize (u);//D @@ -121,19 +157,22 @@ vec3 rx = cross (u, rz); vec3 ry = cross (rz, rx); - 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; + 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.F; diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); //printf ("diagfact = %f\n", diagfact); - diagfact = sqrtf (3.);//D WHY??? + //diagfact = sqrtf (3.);//D WHY??? glMultMatrixf (m); + + glTranslatef (-p.x, -p.y, -p.z); - glGetFloatv (GL_PROJECTION_MATRIX, m); + glGetFloatv (GL_MODELVIEW_MATRIX, m); + + m = perspective * 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) ); @@ -142,6 +181,7 @@ 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) ); +#if 0 { GLdouble frustlen = z_far - z_near; GLdouble fheight = frustlen * ftan; @@ -151,16 +191,13 @@ frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half)); } +#endif { GLdouble depth = h / ftan; - frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); + frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan)); } - - glMatrixMode (GL_MODELVIEW); - glLoadIdentity (); - glTranslatef (-p.x, -p.y, -p.z); } void view::begin () @@ -168,6 +205,7 @@ generation++; vislist.clear (); + postdepthlist.clear (); z_near = max (nz_near, 1.F); z_far = max (nz_far, z_near * 2.F); @@ -177,6 +215,8 @@ nz_near = 100.; nc_far = nz_far = 1.F; + + first_lighted = false; } void view::end () @@ -186,18 +226,21 @@ #define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) -void view::render (enum pass_type p, pass &data) +void view::render (pass_data &pass) { - pass_type = p; - pass_data = &data; + this->pass = &pass; + + stat1 = stat2 = 0; //D - switch (pass_type) + switch (pass.type) { case DEPTH: glColorMask (1, 1, 1, 1); glDepthMask (1); + glDisable (GL_BLEND); - glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glEnable (GL_STENCIL_TEST); // for depth-passes //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); @@ -205,15 +248,22 @@ //glEnable (GL_POLYGON_OFFSET_FILL); //glPolygonOffset (0, 5); + glEnable (GL_CULL_FACE); 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); + for (vector::iterator i = vislist.begin (); i != vislist.end (); ++i) + (*i)->draw_depth (*this); + + if (pass.type == DEPTH) + printf ("fps %f NF %f:%f vis %d,%d (%d,%d) CAM (%ld,%ld,%ld)\n", + timer.fps, z_near, z_far, vislist.size (), drawn.size (), stat1, stat2, orig.x, orig.y, orig.z);//D + break; case POSTDEPTH: @@ -221,9 +271,13 @@ glDepthFunc (GL_LESS); glColorMask (0, 0, 0, 0); glDepthMask (0); - glEnable (GL_DEPTH_CLAMP_NV); glDisable (GL_STENCIL_TEST); + glDisable (GL_CULL_FACE); + + glEnable (GL_DEPTH_CLAMP_NV); + occ_query_material.enable (*this); + // check occlusion queries for (vector::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) { @@ -231,48 +285,46 @@ i->recv->event (oq); } + occ_query_material.disable (*this); + occ_queries.clear (); + for (vector::iterator i = postdepthlist.begin (); i != postdepthlist.end (); ++i) + (*i)->draw_postdepth (*this); + + glDisable (GL_DEPTH_CLAMP_NV); + + first_lighted = true; + break; case LIGHTED: - glClear (GL_STENCIL_BUFFER_BIT); + if (first_lighted) + glDisable (GL_BLEND); + else + { + glBlendFunc (GL_ONE, GL_ONE); + glEnable (GL_BLEND); + } + + //glClear (GL_STENCIL_BUFFER_BIT); //glEnable (GL_STENCIL_TEST); + glEnable (GL_CULL_FACE); glEnable (GL_MINMAX); glDepthRange (0., 1. - DEPTH_OFFSET); glDepthFunc (GL_LESS); glColorMask (1, 1, 1, 1); glDepthMask (0); - glDisable (GL_DEPTH_CLAMP_NV); + + for (vector::iterator i = vislist.begin (); i != vislist.end (); ++i) + (*i)->draw_lighted (*this); + + first_lighted = false; break; } - for (vector::iterator i = vislist.begin (); i != vislist.end (); ++i) - (*i)->display (*this); - - if (pass_type == DEPTH) - 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 0 - if (pass == view::DEPTH) - { - glEnable (GL_DEPTH_CLAMP_NV); - glDepthMask (0); - glDepthFunc (GL_LESS); - - for (vector::iterator i = farlist.begin (); i != farlist.end (); ++i) - { - begin_occ_query (**i); - (*i)->draw_bbox (*this); - end_occ_query (); - } - - glDisable (GL_DEPTH_CLAMP_NV); - } -#endif } void light::enable () @@ -293,16 +345,16 @@ lightvec = xyz (lightpos - model_view_matrix * vin.vertex); camdist = max (1 - length (lightvec) / radius, 0); + + fsh (); } -shader::temp_3f linear_light::operator ()() +void linear_light::fsh () { using namespace shader::compile; - temp_3f res; - res = float3 (c.r / 255.F, c.g / 255.F, c.b / 255.F) * (min (intensity * camdist * 0.6F + 0.9F, 1.F)); - - return res; + sh_lightvec = lightvec; + sh_colour = float3 (c.r / 255.F, c.g / 255.F, c.b / 255.F) * (min (intensity * camdist * 100.F + 0.9F, 1.F)); }