--- libgender/view.C 2004/10/08 17:40:26 1.35 +++ libgender/view.C 2005/08/10 02:14:50 1.90 @@ -1,14 +1,55 @@ #include -#include -#include +#include "opengl.h" -#include "oct.h" #include "view.h" +#include "oct.h" +#include "material.h" + +#include "entity.h" + +using namespace gl; + +skybox *world_skybox; + +pass_data pass_depth (0, DEPTH); +pass_data pass_postdepth (0, POSTDEPTH); + +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 + glGenQueries (1, &id); + + glBeginQuery (GL_SAMPLES_PASSED, id); + return id; +} + +inline void end_occ_query () +{ + glEndQuery (GL_SAMPLES_PASSED); +} + +static GLuint occ_query_result (GLuint id) +{ + GLuint count; + + glGetQueryObjectuiv (id, GL_QUERY_RESULT, &count); + occ_query_objects.push_back (id); + + return count; +} - static GLenum gl_error; view::view () -: gamma(1.0) +: gamma(1.0), nz_near (1.F), nz_far (2.F) { } @@ -16,6 +57,34 @@ { } +void view::begin_occ_query (recv_occ_query &recv, void *id) +{ + occ_queries.push_back (oq_data (&recv, ::begin_occ_query (), id)); +} + +void view::end_occ_query () +{ + ::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 ()) @@ -25,156 +94,288 @@ 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 (ctx.generation - vs->generation > 2) + clear_visibility (vs); + + vs->generation = ctx.generation; + } + } + + return vs; +} + void view::reset_projection () { renormalize (orig, p); glViewport (0, 0, w, h); - glMatrixMode (GL_PROJECTION); - glLoadIdentity (); - - gl_error = glGetError (); GLdouble aspect = (GLdouble)w/h; - GLdouble zNear = 1.; - GLdouble zFar = 500.; + GLdouble ftan = tanf (fov * GLfloat (.5 * M_PI / 180.)); + GLdouble ymax = ftan; + GLdouble xmax = ymax * aspect; + + 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) = -1e30F / 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; + + matrix view; + { + matrix &m = view; + + d = normalize (d);//D + u = normalize (u);//D + + vec3 rz = -d; + vec3 rx = cross (u, rz); + vec3 ry = cross (rz, rx); + + 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); + diagfact = sqrtf (3.F); + } + + //printf ("diagfact = %f\n", diagfact); + //diagfact = sqrtf (3.);//D WHY??? + // + view = view * matrix::translation (-p); + + matrix projection = perspective * view; + + { + matrix &m = projection; + + 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) ); + frustum.t = 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) ); + 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; + 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)); + } +#endif + + GLdouble depth = h / ftan; + frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan)); - gl_error = glGetError (); - GLdouble ymax = zNear * tan (fov * (M_PI / 360.0)); - glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar); - - 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); - - gl_error = glGetError (); - glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&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) ); - frustum.t = 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) ); - 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) ); + glMatrixMode (GL_PROJECTION); + glLoadMatrixf (projection); - gl_error = glGetError (); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); - glTranslatef (-p.x, -p.y, -p.z); - gl_error = glGetError (); } void view::begin () { - glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glDepthRange (0, 1. - (1. / pow (2., 16.))); + generation++; + + stat1 = stat2 = 0; //D - vismap.clear (); vislist.clear (); + postdepthlist.clear (); - generation++; + z_near = max (nz_near, 1.F); + z_far = max (nz_far, 1E10F);//D + c_far = nc_far; reset_projection (); - for (vector::iterator i = farlist.begin (); i != farlist.end (); ++i) - ; - farlist.clear (); + nz_near = 100.; + nc_far = nz_far = 1.F; - // 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); - - checklist.clear (); -#endif - - world.detect_visibility (*this); + first_lighted = false; } void view::end () { - vismap.clear (); + 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 + + 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 (pass_data &pass) +{ + this->pass = &pass; - if (mode == view::DEPTH) - { - glEnable (GL_POLYGON_OFFSET_FILL); - glPolygonOffset (0, 1); - glDepthFunc (GL_LESS); - glDisable (GL_LIGHTING); - glColorMask (0, 0, 0, 0); - cgGLEnableProfile (vsh_profile);// z-fighting?? - cgGLDisableProfile (fsh_profile); - } - else + switch (pass.type) { - glEnable (GL_MINMAX); - glDisable (GL_POLYGON_OFFSET_FILL); - glDepthFunc (GL_LESS); - glDepthMask (0); - cgGLEnableProfile (vsh_profile); - cgGLEnableProfile (fsh_profile); + case DEPTH: + glEnable (GL_CULL_FACE); + glDisable (GL_MINMAX); + glDepthRange (DEPTH_OFFSET, 1.); + glDepthFunc (GL_LESS); + glColorMask (1, 1, 1, 1); + glDisable (GL_BLEND); + glDepthMask (1); + + glClear (GL_DEPTH_BUFFER_BIT | (world_skybox ? 0 : GL_COLOR_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); + glEnable (GL_DEPTH_TEST); + glColorMask (0, 0, 0, 0); + + world.detect_visibility (*this); + + for (vector::iterator i = vislist.begin (); i != vislist.end (); ++i) + (*i)->draw_depth (*this); + + break; + + case POSTDEPTH: + glDepthRange (0., 1. - DEPTH_OFFSET); + glDepthFunc (GL_LESS); + glColorMask (0, 0, 0, 0); + glDepthMask (0); + 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) + { + //occ_query oq(*this, i->data, ::occ_query_result (i->id)); + //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: + 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); + + if (world_skybox) + { + glDisable (GL_DEPTH_TEST); + world_skybox->draw (*this);// + glEnable (GL_DEPTH_TEST); + } + + 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); - drawn.clear (); +} - if (mode == view::DEPTH || 1) - { - glEnable (GL_DEPTH_CLAMP_NV); - glDepthFunc (GL_LESS); - glEnable (GL_COLOR_MATERIAL); - glDisable (GL_LIGHTING); -#if 1 - cgGLDisableProfile (vsh_profile); - cgGLDisableProfile (fsh_profile); -#endif - glShadeModel (GL_FLAT); +void light::enable (view &ctx) +{ + lightpos->set (p + (orig - ctx.orig)); +} -#if 0 - static int count; count++; - if (farlist.size ()) - printf ("%d: size %d\n", count, farlist.size ()); -#endif +void light::disable (view &ctx) +{ +} - for (vector::iterator i = farlist.begin (); i != farlist.end (); ++i) - { - glColor3f (1,0,0); - (*i)->draw_bbox (*this); - } - - glEnable (GL_DEPTH_TEST); - glDisable (GL_DEPTH_CLAMP_NV); - glShadeModel (GL_SMOOTH); - } +void linear_light::enable (view &ctx) +{ + light::enable (ctx); +} + +void linear_light::disable (view &ctx) +{ + light::disable (ctx); +} - glColorMask (1, 1, 1, 0); - glDepthMask (1); +void linear_light::vsh () +{ + using namespace shader::compile; + temp_1f camdist; + + 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 (camdist * (intensity * 100.F) + 0.9F, 1.F)); }