| 1 |
#ifndef MATERIAL_H |
| 2 |
#define MATERIAL_H |
| 3 |
|
| 4 |
#include <vector> |
| 5 |
#include <string> |
| 6 |
#include <sstream> |
| 7 |
|
| 8 |
using namespace std; |
| 9 |
|
| 10 |
#include "opengl.h" |
| 11 |
#include "util.h" |
| 12 |
#include "shader.h" |
| 13 |
|
| 14 |
inline void std_vsh () |
| 15 |
{ |
| 16 |
using namespace shader::compile; |
| 17 |
|
| 18 |
vout.position = ftransform (); |
| 19 |
} |
| 20 |
|
| 21 |
inline void std_vsh (const shader::gluvar &vertex) |
| 22 |
{ |
| 23 |
using namespace shader::compile; |
| 24 |
|
| 25 |
vout.position = model_view_projection_matrix * vertex; |
| 26 |
} |
| 27 |
|
| 28 |
struct material |
| 29 |
{ |
| 30 |
// the vertex shader fragment. it should check ctx.pass_data->light and |
| 31 |
// only generate varying data when light is non-null. it should also use |
| 32 |
// std_vsh () whenever possible. |
| 33 |
|
| 34 |
virtual void vsh (view &ctx) = 0; |
| 35 |
|
| 36 |
// the fragment shader fragment :). it should check ctx.pass_data->light |
| 37 |
// and, if null, best do nothing. if non-null, it should call |
| 38 |
// light->value to get the per-pixel light colour and apply it to the |
| 39 |
// surface as neccessary. |
| 40 |
|
| 41 |
virtual void fsh (view &ctx) = 0; |
| 42 |
|
| 43 |
virtual void enable (view &ctx); |
| 44 |
virtual void disable (view &ctx); |
| 45 |
|
| 46 |
virtual ~material (); |
| 47 |
}; |
| 48 |
|
| 49 |
struct texture_execption |
| 50 |
{ |
| 51 |
texture_execption (string s) : msg(s) { } |
| 52 |
string msg; |
| 53 |
}; |
| 54 |
|
| 55 |
struct texture |
| 56 |
{ |
| 57 |
SDL_Surface *image; |
| 58 |
GLuint name; |
| 59 |
GLfloat texcoord[4]; |
| 60 |
|
| 61 |
GLuint load_texture (SDL_Surface *surface, GLfloat *texcoord, int flags); |
| 62 |
|
| 63 |
enum { |
| 64 |
DISABLE_MIPMAP = 1, |
| 65 |
CLAMP = 2, |
| 66 |
}; |
| 67 |
|
| 68 |
texture (const char *f, int flags = 0) |
| 69 |
{ |
| 70 |
image = IMG_Load (f); |
| 71 |
|
| 72 |
if (!image) |
| 73 |
throw (texture_execption ("Couldn't load " + (string) f + ": " + (string) IMG_GetError ())); |
| 74 |
|
| 75 |
name = load_texture (image, (GLfloat*)&texcoord, flags); |
| 76 |
if (name == 0) |
| 77 |
throw (texture_execption ("Couldn't make GL Texture, failed to create new RGB SWSurface")); |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
///////////////////////////////////////////////////////////////////////////// |
| 82 |
|
| 83 |
struct test_material : material |
| 84 |
{ |
| 85 |
texture tex, norm; |
| 86 |
shader::sampler_2d texvar, normvar; |
| 87 |
|
| 88 |
void vsh (view &ctx); |
| 89 |
void fsh (view &ctx); |
| 90 |
|
| 91 |
void enable (view &ctx); |
| 92 |
void disable (view &ctx); |
| 93 |
|
| 94 |
test_material (); |
| 95 |
}; |
| 96 |
|
| 97 |
struct skybox_material : material |
| 98 |
{ |
| 99 |
shader::sampler_2d tex; |
| 100 |
|
| 101 |
void vsh (view &ctx); |
| 102 |
void fsh (view &ctx); |
| 103 |
|
| 104 |
void enable (view &ctx); |
| 105 |
void disable (view &ctx); |
| 106 |
|
| 107 |
skybox_material (); |
| 108 |
}; |
| 109 |
|
| 110 |
struct mat_timed : material |
| 111 |
{ |
| 112 |
shader::uniform_1f time; |
| 113 |
|
| 114 |
void enable (view &ctx); |
| 115 |
void disable (view &ctx); |
| 116 |
|
| 117 |
void vsh (view &ctx); |
| 118 |
void fsh (view &ctx); |
| 119 |
|
| 120 |
protected: |
| 121 |
shader::varying_3f f_normal; |
| 122 |
shader::uniform_3f sh_colour; |
| 123 |
}; |
| 124 |
|
| 125 |
struct mat_gouraud_shaded : material |
| 126 |
{ |
| 127 |
// texture tex, norm; |
| 128 |
// shader::sampler_2d texvar, normvar; |
| 129 |
colour c; |
| 130 |
|
| 131 |
void vsh (view &ctx); |
| 132 |
void fsh (view &ctx); |
| 133 |
|
| 134 |
void enable (view &ctx); |
| 135 |
void disable (view &ctx); |
| 136 |
|
| 137 |
mat_gouraud_shaded (const colour &c) : c(c) { } |
| 138 |
|
| 139 |
protected: |
| 140 |
shader::uniform_3f sh_colour; |
| 141 |
shader::varying_3f f_normal; |
| 142 |
}; |
| 143 |
|
| 144 |
struct mat_debug : material { |
| 145 |
void vsh (view &ctx); |
| 146 |
void fsh (view &ctx); |
| 147 |
|
| 148 |
void enable (view &ctx); |
| 149 |
void disable (view &ctx); |
| 150 |
|
| 151 |
mat_debug () { } |
| 152 |
}; |
| 153 |
|
| 154 |
extern test_material *testmat; |
| 155 |
extern mat_gouraud_shaded *testmat2; |
| 156 |
extern mat_timed *testmat3; |
| 157 |
extern mat_debug *debugmat; |
| 158 |
|
| 159 |
#endif |
| 160 |
|
| 161 |
|