--- libgender/material.C 2004/11/06 04:26:18 1.42 +++ libgender/material.C 2005/02/11 14:54:55 1.60 @@ -57,8 +57,7 @@ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); - glTexImage2D (GL_TEXTURE_2D, - 0, + glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); SDL_FreeSurface (image); /* No longer needed */ @@ -71,9 +70,9 @@ 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 ()) { @@ -81,40 +80,55 @@ shader::shader_builder::start (); - 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) + 1.F) - 1); - z (vout.position) = z (vout.position) - 1; + 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 (); shader::shader_builder::start (); - if (ctx.pass_data->l) + if (ctx.pass->l) { - ctx.pass_data->l->fsh (); - + ctx.pass->l->fsh (); fsh (ctx); } else - shader::compile::fout.frag_color = shader::compile::float4 (1., 0., 1., 1.); + // 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 (ctx); } void material::disable (view &ctx) @@ -128,8 +142,83 @@ { } -static shader::varying_3f normal; + +void mat_timed::enable (view &ctx) +{ + material::enable (ctx); + + time->set (timer::now - (int)timer::now); + sh_colour->set (vec3 (255, 0, 0) * (1.F / 255.F)); +} + +void mat_timed::disable (view &ctx) +{ + material::disable (ctx); +} + +void mat_timed::vsh (view &ctx) +{ + using namespace shader::compile; + std_vsh (); + + if (ctx.pass->l) + f_normal = normal_matrix * vin.normal; +} + +void mat_timed::fsh (view &ctx) +{ + using namespace shader::compile; + + if (ctx.pass->l) + { + temp_1f fac; + + fac = dot (normalize (f_normal), normalize (ctx.pass->l->sh_lightvec)); + + //fac = (fac / 100) * (mod (time, 100)) ; + fac = time; + + xyz (fout.frag_color) = sin (time * 3.14159265*2);//ctx.pass->l->sh_colour * sh_colour * fac; + } +} + +void mat_gouraud_shaded::enable (view &ctx) +{ + material::enable (ctx); + + sh_colour->set (vec3 (c.r, c.g, c.b) * (1.F / 255.F)); +} + +void mat_gouraud_shaded::disable (view &ctx) +{ + material::disable (ctx); +} + +void mat_gouraud_shaded::vsh (view &ctx) +{ + using namespace shader::compile; + std_vsh (); + + if (ctx.pass->l) + f_normal = normal_matrix * vin.normal; +} + +void mat_gouraud_shaded::fsh (view &ctx) +{ + using namespace shader::compile; + + if (ctx.pass->l) + { + temp_1f fac; + + fac = dot (normalize (f_normal), normalize (ctx.pass->l->sh_lightvec)); + + xyz (fout.frag_color) = ctx.pass->l->sh_colour * sh_colour * fac; + } +} + static shader::varying_2f texcoord; +static shader::varying_3f normal; void test_material::vsh (view &ctx) { @@ -137,7 +226,7 @@ std_vsh (); - if (ctx.pass_data->l) + if (ctx.pass->l) { texcoord = xy (vin.tex_coord[0]); normal = normal_matrix * vin.normal; @@ -148,7 +237,7 @@ { using namespace shader::compile; - if (ctx.pass_data->l) + if (ctx.pass->l) { temp_3f lc; temp_1f fac; @@ -164,10 +253,10 @@ normal = mat3 (rot1, rot2, rot3) * normal; - fac = dot (normalize (normal), normalize (ctx.pass_data->l->sh_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) * fac - * ctx.pass_data->l->sh_colour; + * ctx.pass->l->sh_colour; //xyz (fout.frag_color) = lc * fac; } } @@ -187,4 +276,6 @@ } test_material *testmat; +mat_gouraud_shaded *testmat2; +mat_timed *testmat3;