--- libgender/view.C 2004/10/23 02:27:52 1.64 +++ libgender/view.C 2004/11/07 03:28:20 1.76 @@ -4,9 +4,13 @@ #include "view.h" #include "oct.h" +#include "material.h" using namespace gl; +pass_data pass_depth (0, DEPTH); +pass_data pass_postdepth (0, POSTDEPTH); + vector occ_query_objects; static GLuint begin_occ_query () @@ -19,22 +23,22 @@ occ_query_objects.pop_back (); } else - glGenQueriesARB (1, &id); + glGenQueries (1, &id); - glBeginQueryARB (GL_SAMPLES_PASSED, id); + glBeginQuery (GL_SAMPLES_PASSED, id); return id; } inline void end_occ_query () { - glEndQueryARB (GL_SAMPLES_PASSED); + glEndQuery (GL_SAMPLES_PASSED); } static GLuint occ_query_result (GLuint id) { GLuint count; - glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count); + glGetQueryObjectuiv (id, GL_QUERY_RESULT, &count); occ_query_objects.push_back (id); return count; @@ -59,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 ()) @@ -108,10 +130,30 @@ GLdouble ymax = z_near * ftan; GLdouble xmax = ymax * aspect; + matrix perspective; +#if 1 + { + 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) = 1; + m(3,0) = 0; m(3,1) = 0; m(3,2) = -1; m(3,3) = 0; + + glLoadMatrixf (perspective); + } +#else glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); +#endif + + 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 @@ -119,7 +161,6 @@ 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; @@ -130,9 +171,10 @@ 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) ); @@ -157,8 +199,7 @@ frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); } - glMatrixMode (GL_MODELVIEW); - glLoadIdentity (); + glTranslatef (-p.x, -p.y, -p.z); } void view::begin () @@ -173,7 +214,7 @@ reset_projection (); - nz_near = 1e37; + nz_near = 100.; nc_far = nz_far = 1.F; } @@ -184,17 +225,19 @@ #define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) -void view::render (enum pass p) +void view::render (pass_data &pass) { - pass = p; + this->pass = &pass; - switch (pass) + 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); @@ -202,6 +245,7 @@ //glEnable (GL_POLYGON_OFFSET_FILL); //glPolygonOffset (0, 5); + glEnable (GL_CULL_FACE); glDisable (GL_MINMAX); glDepthRange (DEPTH_OFFSET, 1.); glDepthFunc (GL_LESS); @@ -220,7 +264,10 @@ glDepthMask (0); glEnable (GL_DEPTH_CLAMP_NV); glDisable (GL_STENCIL_TEST); + glDisable (GL_CULL_FACE); + occ_query_material.enable (*this); + // check occlusion queries for (vector::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) { @@ -228,12 +275,17 @@ i->recv->event (oq); } + occ_query_material.disable (*this); + occ_queries.clear (); break; case LIGHTED: - glClear (GL_STENCIL_BUFFER_BIT); + glEnable (GL_CULL_FACE); + //glEnable (GL_BLEND);//TODO, only when not first lighted pass + glBlendFunc (GL_ONE, GL_ONE); + //glClear (GL_STENCIL_BUFFER_BIT); //glEnable (GL_STENCIL_TEST); glEnable (GL_MINMAX); glDepthRange (0., 1. - DEPTH_OFFSET); @@ -248,7 +300,7 @@ for (vector::iterator i = vislist.begin (); i != vislist.end (); ++i) (*i)->display (*this); - if (pass == LIGHTED) + 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 (); @@ -272,4 +324,34 @@ #endif } +void light::enable () +{ + lightpos->set (p); +} + +void light::disable () +{ +} + +static shader::varying_1f camdist; +static shader::varying_3f lightvec; + +void linear_light::vsh () +{ + using namespace shader::compile; + + lightvec = xyz (lightpos - model_view_matrix * vin.vertex); + camdist = max (1 - length (lightvec) / radius, 0); + + fsh (); +} + +void linear_light::fsh () +{ + using namespace shader::compile; + + 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)); +} +