--- libgender/material.C 2004/10/29 16:02:22 1.16 +++ libgender/material.C 2005/02/11 14:54:55 1.60 @@ -5,47 +5,25 @@ #include "opengl.h" #include "material.h" +#include "view.h" #include "util.h" -material::~material () -{ -} - -void -simple_material::enable () -{ - glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse); - glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular); - glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission); - glMaterialf (GL_FRONT, GL_SHININESS, shininess); -} - -void -simple_material::disable () -{ -} - GLuint texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) { GLuint name; - int w, h; SDL_Surface *image; SDL_Rect area; Uint32 saved_flags; Uint8 saved_alpha; /* Use the surface width and height expanded to powers of 2 */ - //w = power_of_two (surface->w); - //h = power_of_two (surface->h); - w = power_of_two (surface->w); - h = power_of_two (surface->h); - tex2oord[0] = 0.0f; /* Min X */ - tex2oord[1] = 0.0f; /* Min Y */ - tex2oord[2] = (GLfloat) surface->w / w; /* Max X */ - tex2oord[3] = (GLfloat) surface->h / h; /* Max Y */ + tex2oord[0] = 0.F; /* Min X */ + tex2oord[1] = 0.F; /* Min Y */ + tex2oord[2] = 1.F; /* Max X */ + tex2oord[3] = 1.F; /* Max Y */ - image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, + image = SDL_CreateRGBSurface (SDL_SWSURFACE, surface->w, surface->h, 32, #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else @@ -79,55 +57,225 @@ 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, - GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); + 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 */ return name; } +material::~material () +{ +} + +void material::enable (view &ctx) +{ + pass_data::matmap_t &matmap = ctx.pass->matmap; + + pass_data::matmap_t::iterator i = matmap.find (this); + + if (i == matmap.end ()) + { + string vsh_src, fsh_src; + + shader::shader_builder::start (); + + if (ctx.pass->l) + ctx.pass->l->vsh (); + + vsh (ctx); + + // compute logarithmic depth - see the frustum matrix in view.C + { + using namespace shader::compile; + + 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->l) + { + ctx.pass->l->fsh (); + fsh (ctx); + } + 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_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) +{ +} + test_material::test_material () -: tex ("textures/osama.jpg"), texvar (tex.name) +//: tex ("textures/osama.jpg"), texvar (tex.name) +: tex ("textures/rockwall.jpg"), texvar (tex.name) +, norm ("textures/rockwall_normal.jpg"), normvar (norm.name) { - using namespace shader; +} - p.vsh->start (); - temp_4f lightpos; - temp_4f wpos; +void mat_timed::enable (view &ctx) +{ + material::enable (ctx); - //lightpos = shader::vec4 (0, 10, 0, 1); - vout.position = shader::gl.model_view_projection_matrix * vin.vertex; - vout.tex_coord[0] = vin.tex_coord[0] + 1; - //vout.tex_coord[1] = normalize (lightpos - wpos); - //vout.tex_coord[2] = normalize (wpos); - //vout.tex_coord[3] = normalize (xyz (gl.model_view_matrix_inverse_transpose) * vin.normal); - //vout.tex_coord[4] = normalize (xyz (gl.projection_matrix_inverse_transpose) - wpos); + time->set (timer::now - (int)timer::now); + sh_colour->set (vec3 (255, 0, 0) * (1.F / 255.F)); +} - p.vsh->end (); - p.vsh->compile (); +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 (); - fragment_shader fsh; + 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) +{ + using namespace shader::compile; + + std_vsh (); - p.fsh->start (); + if (ctx.pass->l) + { + texcoord = xy (vin.tex_coord[0]); + normal = normal_matrix * vin.normal; + } +} + +void test_material::fsh (view &ctx) +{ + using namespace shader::compile; + + if (ctx.pass->l) + { + temp_3f lc; + temp_1f fac; + + temp_3f rot1, rot2, rot3; - xyz(fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]); + yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F); - p.fsh->end (); - p.fsh->compile (); - p.link (); + //rot1 = normal_matrix * rot1; + + rot2 = float3 (x(rot1), z(rot1), -y(rot1)); + rot3 = float3 (y(rot1), -x(rot1), z(rot1)); + + normal = mat3 (rot1, rot2, rot3) * normal; + + 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->l->sh_colour; + //xyz (fout.frag_color) = lc * fac; + } } -void test_material::enable () +void test_material::enable (view &ctx) { - p.enable (); + material::enable (ctx); texvar->enable (); + normvar->enable (); } -void test_material::disable () +void test_material::disable (view &ctx) { + normvar->disable (); texvar->disable (); - p.disable (); + material::disable (ctx); } +test_material *testmat; +mat_gouraud_shaded *testmat2; +mat_timed *testmat3; +