#include #include #include #include "opengl.h" #include "material.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 */ image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, 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, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); SDL_FreeSurface (image); /* No longer needed */ return name; } test_material::test_material () : tex ("textures/osama.jpg"), texvar (tex.name) { using namespace shader; p.vsh->start (); temp_4f lightpos; temp_4f wpos; //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]; //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); p.vsh->end (); p.vsh->compile (); fragment_shader fsh; p.fsh->start (); xyz(fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]); p.fsh->end (); p.fsh->compile (); p.link (); } void test_material::enable () { p.enable (); texvar->enable (); } void test_material::disable () { texvar->disable (); p.disable (); }