| 1 |
root |
1.1 |
#ifndef MATERIAL_H |
| 2 |
|
|
#define MATERIAL_H |
| 3 |
|
|
|
| 4 |
root |
1.5 |
#include <vector> |
| 5 |
|
|
#include <string> |
| 6 |
|
|
#include <sstream> |
| 7 |
|
|
|
| 8 |
root |
1.11 |
using namespace std; |
| 9 |
root |
1.5 |
|
| 10 |
root |
1.23 |
#include "opengl.h" |
| 11 |
root |
1.1 |
#include "util.h" |
| 12 |
root |
1.12 |
#include "shader.h" |
| 13 |
root |
1.3 |
|
| 14 |
root |
1.18 |
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 |
root |
1.16 |
{ |
| 23 |
|
|
using namespace shader::compile; |
| 24 |
|
|
|
| 25 |
|
|
vout.position = model_view_projection_matrix * vertex; |
| 26 |
|
|
} |
| 27 |
root |
1.3 |
|
| 28 |
root |
1.1 |
struct material |
| 29 |
|
|
{ |
| 30 |
root |
1.16 |
// 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 |
root |
1.1 |
virtual ~material (); |
| 47 |
|
|
}; |
| 48 |
|
|
|
| 49 |
root |
1.16 |
struct texture_execption |
| 50 |
|
|
{ |
| 51 |
|
|
texture_execption (string s) : msg(s) { } |
| 52 |
|
|
string msg; |
| 53 |
root |
1.1 |
}; |
| 54 |
|
|
|
| 55 |
root |
1.3 |
struct texture |
| 56 |
|
|
{ |
| 57 |
|
|
SDL_Surface *image; |
| 58 |
root |
1.12 |
GLuint name; |
| 59 |
root |
1.3 |
GLfloat texcoord[4]; |
| 60 |
|
|
|
| 61 |
root |
1.23 |
GLuint load_texture (SDL_Surface *surface, GLfloat *texcoord, int flags); |
| 62 |
|
|
|
| 63 |
|
|
enum { |
| 64 |
|
|
DISABLE_MIPMAP = 1, |
| 65 |
|
|
CLAMP = 2, |
| 66 |
|
|
}; |
| 67 |
root |
1.3 |
|
| 68 |
root |
1.23 |
texture (const char *f, int flags = 0) |
| 69 |
root |
1.3 |
{ |
| 70 |
|
|
image = IMG_Load (f); |
| 71 |
|
|
|
| 72 |
|
|
if (!image) |
| 73 |
|
|
throw (texture_execption ("Couldn't load " + (string) f + ": " + (string) IMG_GetError ())); |
| 74 |
|
|
|
| 75 |
root |
1.23 |
name = load_texture (image, (GLfloat*)&texcoord, flags); |
| 76 |
root |
1.12 |
if (name == 0) |
| 77 |
root |
1.3 |
throw (texture_execption ("Couldn't make GL Texture, failed to create new RGB SWSurface")); |
| 78 |
|
|
} |
| 79 |
|
|
}; |
| 80 |
|
|
|
| 81 |
root |
1.23 |
///////////////////////////////////////////////////////////////////////////// |
| 82 |
|
|
|
| 83 |
root |
1.16 |
struct test_material : material |
| 84 |
root |
1.3 |
{ |
| 85 |
root |
1.15 |
texture tex, norm; |
| 86 |
|
|
shader::sampler_2d texvar, normvar; |
| 87 |
root |
1.12 |
|
| 88 |
root |
1.16 |
void vsh (view &ctx); |
| 89 |
|
|
void fsh (view &ctx); |
| 90 |
|
|
|
| 91 |
root |
1.13 |
void enable (view &ctx); |
| 92 |
|
|
void disable (view &ctx); |
| 93 |
root |
1.16 |
|
| 94 |
|
|
test_material (); |
| 95 |
root |
1.3 |
}; |
| 96 |
root |
1.16 |
|
| 97 |
root |
1.23 |
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 |
root |
1.21 |
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 |
root |
1.20 |
struct mat_gouraud_shaded : material |
| 126 |
root |
1.19 |
{ |
| 127 |
|
|
// texture tex, norm; |
| 128 |
|
|
// shader::sampler_2d texvar, normvar; |
| 129 |
root |
1.20 |
colour c; |
| 130 |
root |
1.19 |
|
| 131 |
|
|
void vsh (view &ctx); |
| 132 |
|
|
void fsh (view &ctx); |
| 133 |
|
|
|
| 134 |
root |
1.20 |
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 |
root |
1.19 |
}; |
| 143 |
|
|
|
| 144 |
root |
1.22 |
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 |
root |
1.17 |
extern test_material *testmat; |
| 155 |
root |
1.20 |
extern mat_gouraud_shaded *testmat2; |
| 156 |
root |
1.21 |
extern mat_timed *testmat3; |
| 157 |
root |
1.22 |
extern mat_debug *debugmat; |
| 158 |
root |
1.3 |
|
| 159 |
root |
1.1 |
#endif |
| 160 |
|
|
|
| 161 |
|
|
|