#include #include #include #include "opengl.h" #include "material.h" #include "view.h" #include "util.h" GLuint texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) { GLuint name; SDL_Surface *image; SDL_Rect area; Uint32 saved_flags; Uint8 saved_alpha; /* Use the surface width and height expanded to powers of 2 */ 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, surface->w, surface->h, 32, #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); if (image == NULL) return 0; /* Save the alpha blending attributes */ saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); saved_alpha = surface->format->alpha; if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) SDL_SetAlpha (surface, 0, 0); /* Copy the surface into the GL texture image */ area.x = 0; area.y = 0; area.w = surface->w; area.h = surface->h; SDL_BlitSurface (surface, &area, image, &area); /* Restore the alpha blending attributes */ if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) SDL_SetAlpha (surface, saved_flags, saved_alpha); /* Create an OpenGL texture for the image */ glGenTextures (1, &name); glBindTexture (GL_TEXTURE_2D, name); 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, 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::matmap_t &matmap = ctx.pass_data->matmap; pass::matmap_t::iterator i = matmap.find (this); if (i == matmap.end ()) { string vsh_src, fsh_src; shader::shader_builder::start (); if (ctx.pass_data->l) ctx.pass_data->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 lz = z (vout.position); z (vout.position) = (log2 (lz) * (2 / log2 (1e10)) - 1) * w (vout.position); } vsh_src = shader::shader_builder::stop (); shader::shader_builder::start (); if (ctx.pass_data->l) { ctx.pass_data->l->fsh (); fsh (ctx); } else shader::compile::fout.frag_color = shader::compile::float4 (1., 0., 1., 1.); 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)); po->enable (); } else i->second->enable (); if (ctx.pass_data->l) ctx.pass_data->l->enable (); } void material::disable (view &ctx) { } test_material::test_material () //: tex ("textures/osama.jpg"), texvar (tex.name) : tex ("textures/rockwall.jpg"), texvar (tex.name) , norm ("textures/rockwall_normal.jpg"), normvar (norm.name) { } static shader::varying_3f normal; static shader::varying_2f texcoord; void test_material::vsh (view &ctx) { using namespace shader::compile; std_vsh (); if (ctx.pass_data->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_data->l) { temp_3f lc; temp_1f fac; temp_3f rot1, rot2, rot3; yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F); //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_data->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; //xyz (fout.frag_color) = lc * fac; } } void test_material::enable (view &ctx) { material::enable (ctx); texvar->enable (); normvar->enable (); } void test_material::disable (view &ctx) { normvar->disable (); texvar->disable (); material::disable (ctx); } test_material *testmat;