--- libgender/material.C 2004/11/05 19:55:15 1.39 +++ libgender/material.C 2005/02/07 07:37:00 1.56 @@ -71,44 +71,65 @@ void material::enable (view &ctx) { - pass::matmap_t &matmap = ctx.pass_data->matmap; + pass_data::matmap_t &matmap = ctx.pass->matmap; - pass::matmap_t::iterator i = matmap.find (this); + pass_data::matmap_t::iterator i = matmap.find (this); if (i == matmap.end ()) { string vsh_src, fsh_src; shader::shader_builder::start (); - vsh (ctx); - if (ctx.pass_data->l) - ctx.pass_data->l->vsh (); + if (ctx.pass->l) + ctx.pass->l->vsh (); + + vsh (ctx); + // compute logarithmic depth - see the frustum matrix in view.C { using namespace shader::compile; - z (vout.position) = log2 (z (vout.position) / w (vout.position)) * w (vout.position); + temp_1f lz; + + // TODO: negative z is not calculated in an acceptable way, clipping does horrible things(?) + lz = z (vout.position); +#if 0 + lz = (log (max (lz, 0) + 1) / log (1e30)) - 1; +#else + lz = ifelse (lz <= 0, + 0, + log (lz + 1) / log (1e30) + ) - 1; +#endif + z (vout.position) = lz * w (vout.position); } vsh_src = shader::shader_builder::stop (); - if (ctx.pass_data->l || 1) + shader::shader_builder::start (); + + if (ctx.pass->l) { - shader::shader_builder::start (); - shader::compile::fout.frag_color = shader::compile::float4 (1., 1., 0., 1.); + ctx.pass->l->fsh (); fsh (ctx); - - fsh_src = shader::shader_builder::stop (); } + else + // many drivers need both vertex and fragment shader, 'caboom!' otherwise + shader::compile::fout.frag_color = shader::compile::float4 (0., 0., 0., 0.); + + fsh_src = shader::shader_builder::stop (); shader::program_object po = shader::get_program (vsh_src, fsh_src); - matmap.insert (pass::matmap_t::value_type (this, po)); + matmap.insert (pass_data::matmap_t::value_type (this, po)); po->enable (); } else i->second->enable (); + + if (ctx.pass->l) + ctx.pass->l->enable (); } void material::disable (view &ctx) @@ -122,20 +143,42 @@ { } -static shader::varying_3f normal, lightvec; +static shader::varying_3f normal; static shader::varying_2f texcoord; +void test_material2::vsh (view &ctx) +{ + using namespace shader::compile; + std_vsh (); + + if (ctx.pass->l) + { + normal = normal_matrix * vin.normal; + } +} + +void test_material2::fsh (view &ctx) +{ + using namespace shader::compile; + + if (ctx.pass->l) + { + temp_1f fac; + fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec)); + xyz (fout.frag_color) = ctx.pass->l->sh_colour * fac;//normalize (ctx.pass->l->sh_lightvec); + } +} + void test_material::vsh (view &ctx) { using namespace shader::compile; std_vsh (); - if (ctx.pass_data->l) + if (ctx.pass->l) { texcoord = xy (vin.tex_coord[0]); normal = normal_matrix * vin.normal; - lightvec = xyz (ctx.pass_data->l->lightpos - model_view_matrix * vin.vertex); } } @@ -143,13 +186,11 @@ { using namespace shader::compile; - if (ctx.pass_data->l) + if (ctx.pass->l) { temp_3f lc; temp_1f fac; - lc = (*ctx.pass_data->l)(); - temp_3f rot1, rot2, rot3; yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F); @@ -161,9 +202,10 @@ normal = mat3 (rot1, rot2, rot3) * normal; - fac = dot (normalize (normal), normalize (lightvec)); + fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec)); fac = max (pow (max (fac, 0.0), 6), 0.3); - xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * lc * fac; + xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac + * ctx.pass->l->sh_colour; //xyz (fout.frag_color) = lc * fac; } } @@ -183,4 +225,5 @@ } test_material *testmat; +test_material2 *testmat2;