#ifndef MATERIAL_H #define MATERIAL_H #include "util.h" struct texture_execption { texture_execption (string s) : msg(s) { } string msg; }; inline int power_of_two (int input) { int value = 1; while (value < input) { value <<= 1; } return value; } struct material { virtual void begin () = 0; virtual void end () = 0; virtual ~material (); }; struct simple_material : material { colour diffuse, specular, emission; GLfloat shininess; void begin (); void end (); simple_material () : diffuse(1, 0, 1, 1) , specular(1, 0, 1, 1) , emission(1, 0, 1, 1) , shininess(1.) { } }; struct texture { SDL_Surface *image; GLuint texture_pxls; GLfloat texcoord[4]; GLuint load_texture (SDL_Surface *surface, GLfloat *texcoord); texture(const char *f) { image = IMG_Load (f); if (!image) throw (texture_execption ("Couldn't load " + (string) f + ": " + (string) IMG_GetError ())); texture_pxls = load_texture (image, (GLfloat*)&texcoord); if (texture_pxls == 0) throw (texture_execption ("Couldn't make GL Texture, failed to create new RGB SWSurface")); } }; extern CGcontext cgc; struct shader { CGprogram vsh, fsh; CGparameter lightpos; // mv, mvp CGprofile vsh_profile, fsh_profile; CGparameter g_Texture; // the texture parameter void check_cg_error (void) { CGerror err = cgGetError (); if (err != CG_NO_ERROR) { printf("CG error: %s\n", cgGetErrorString (err)); exit(1); } } shader (const char *vshp, const char *fshp) { vsh_profile = CG_PROFILE_ARBVP1; //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30; //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40; fsh_profile = CG_PROFILE_ARBFP1; //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30; //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40; vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, vshp, vsh_profile, 0, 0); check_cg_error (); cgGLLoadProgram (vsh); check_cg_error (); fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, fshp, fsh_profile, 0, 0); check_cg_error (); cgGLLoadProgram (fsh); check_cg_error (); cgGLBindProgram (vsh); cgGLBindProgram (fsh); // mv = cgGetNamedParameter (vsh, "WorldProj"); // mvp = cgGetNamedParameter (vsh, "WorldViewProj"); //lightpos = cgGetNamedParameter (vsh, "LightPos"); //cgGLSetParameter4f (lightpos, 10, 10, 0, 1); //camera.p.x, camera.p.y, camera.p.z, 1); check_cg_error (); } }; struct osama_material : material, texture, shader { osama_material () : shader ("vsh.cg", "fsh.cg"), texture ("textures/osama.jpg") { g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;) cgGLSetTextureParameter(g_Texture, texture_pxls); // Bind the texture number 999 to g_Texture } void begin (); void end (); }; void init_shaders (); #endif