| 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 |
|
|
#include "opengl.h" |
| 9 |
|
|
|
| 10 |
root |
1.1 |
#include "util.h" |
| 11 |
|
|
|
| 12 |
root |
1.5 |
namespace shader { |
| 13 |
|
|
|
| 14 |
|
|
using namespace std; |
| 15 |
|
|
|
| 16 |
root |
1.10 |
const int NAMELEN = 24; |
| 17 |
root |
1.5 |
|
| 18 |
|
|
class refcounted |
| 19 |
|
|
{ |
| 20 |
root |
1.6 |
template<class type> friend class ref; |
| 21 |
|
|
mutable int refcnt; |
| 22 |
|
|
void refcnt_inc () const { refcnt++; } |
| 23 |
|
|
void refcnt_dec () const { if (!--refcnt) delete this; }; |
| 24 |
root |
1.5 |
public: |
| 25 |
|
|
refcounted () : refcnt(0) { } |
| 26 |
|
|
virtual ~refcounted (); |
| 27 |
|
|
}; |
| 28 |
|
|
|
| 29 |
root |
1.6 |
template<class type> |
| 30 |
|
|
struct ref |
| 31 |
root |
1.5 |
{ |
| 32 |
root |
1.6 |
type *p; |
| 33 |
root |
1.5 |
|
| 34 |
root |
1.8 |
ref () |
| 35 |
|
|
{ |
| 36 |
|
|
p = new type; |
| 37 |
|
|
p->refcnt_inc (); |
| 38 |
|
|
} |
| 39 |
|
|
|
| 40 |
root |
1.6 |
ref (type &d) |
| 41 |
root |
1.5 |
{ |
| 42 |
|
|
d.refcnt_inc (); |
| 43 |
root |
1.6 |
p = &d; |
| 44 |
|
|
} |
| 45 |
|
|
|
| 46 |
|
|
ref (const ref &r) |
| 47 |
|
|
{ |
| 48 |
|
|
r.p->refcnt_inc (); |
| 49 |
|
|
p = r.p; |
| 50 |
root |
1.5 |
} |
| 51 |
|
|
|
| 52 |
root |
1.6 |
template<class type2> |
| 53 |
|
|
ref (const ref<type2> &r) |
| 54 |
root |
1.5 |
{ |
| 55 |
root |
1.6 |
r.p->refcnt_inc (); |
| 56 |
root |
1.5 |
p = r.p; |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
root |
1.6 |
~ref () |
| 60 |
root |
1.5 |
{ |
| 61 |
|
|
p->refcnt_dec (); |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
root |
1.6 |
ref &operator =(type &d) |
| 65 |
root |
1.5 |
{ |
| 66 |
root |
1.6 |
d.refcnt_inc (); |
| 67 |
|
|
p->refcnt_dec (); |
| 68 |
|
|
p = &d; |
| 69 |
|
|
return *this; |
| 70 |
root |
1.5 |
} |
| 71 |
|
|
|
| 72 |
root |
1.10 |
type *operator ->() const { return p; } |
| 73 |
root |
1.5 |
|
| 74 |
root |
1.10 |
operator type &() const { return *p; } |
| 75 |
root |
1.5 |
|
| 76 |
root |
1.6 |
template<class type2> |
| 77 |
|
|
bool operator ==(const ref<type2> b) const |
| 78 |
root |
1.5 |
{ |
| 79 |
root |
1.6 |
return (void *)p == (void *)b.p; |
| 80 |
root |
1.5 |
} |
| 81 |
|
|
}; |
| 82 |
root |
1.10 |
|
| 83 |
|
|
// ref with auto-construct |
| 84 |
|
|
template<class type> |
| 85 |
|
|
struct auto_ref0 : ref<type> |
| 86 |
|
|
{ |
| 87 |
|
|
auto_ref0 () : ref<type> (*new type ()) { } |
| 88 |
|
|
}; |
| 89 |
|
|
|
| 90 |
|
|
template<class type, typename arg1> |
| 91 |
|
|
struct auto_ref1 : ref<type> |
| 92 |
|
|
{ |
| 93 |
|
|
auto_ref1 (arg1 a) : ref<type> (*new type (a)) { } |
| 94 |
|
|
}; |
| 95 |
root |
1.5 |
|
| 96 |
root |
1.9 |
extern const char str_float []; |
| 97 |
|
|
extern const char str_vec2 []; |
| 98 |
|
|
extern const char str_vec3 []; |
| 99 |
|
|
extern const char str_vec4 []; |
| 100 |
|
|
extern const char str_mat2 []; |
| 101 |
|
|
extern const char str_mat3 []; |
| 102 |
|
|
extern const char str_mat4 []; |
| 103 |
|
|
|
| 104 |
|
|
extern const char str_sampler_1d []; |
| 105 |
|
|
extern const char str_sampler_1d_shadow []; |
| 106 |
|
|
extern const char str_sampler_2d []; |
| 107 |
|
|
extern const char str_sampler_2d_shadow []; |
| 108 |
|
|
extern const char str_sampler_2d_rect []; |
| 109 |
|
|
extern const char str_sampler_2d_rect_shadow []; |
| 110 |
|
|
extern const char str_sampler_3d []; |
| 111 |
|
|
extern const char str_sampler_3d_rect []; |
| 112 |
|
|
extern const char str_sampler_cube []; |
| 113 |
root |
1.8 |
|
| 114 |
root |
1.5 |
typedef ref<struct var_i> var; |
| 115 |
root |
1.6 |
typedef ref<struct uniform_i> uniform; |
| 116 |
root |
1.5 |
|
| 117 |
|
|
struct shader_builder |
| 118 |
|
|
{ |
| 119 |
root |
1.6 |
vector<uniform> refs; // uniform parameters |
| 120 |
|
|
vector<var> temps; // temporary variables |
| 121 |
|
|
vector<var> streams; // varying inputs & outputs |
| 122 |
root |
1.5 |
|
| 123 |
root |
1.6 |
ostringstream source; |
| 124 |
root |
1.5 |
|
| 125 |
|
|
template<typename type> |
| 126 |
|
|
shader_builder &operator <<(const type &t) |
| 127 |
|
|
{ |
| 128 |
root |
1.6 |
source << t; |
| 129 |
root |
1.5 |
return *this; |
| 130 |
|
|
} |
| 131 |
|
|
}; |
| 132 |
|
|
|
| 133 |
root |
1.6 |
struct fragment_i : refcounted |
| 134 |
root |
1.5 |
{ |
| 135 |
|
|
virtual void build (shader_builder &b) = 0; |
| 136 |
|
|
}; |
| 137 |
|
|
|
| 138 |
root |
1.6 |
typedef ref<fragment_i> fragment; |
| 139 |
|
|
|
| 140 |
root |
1.10 |
// a simple predeclared variable with unspecified type |
| 141 |
|
|
struct glvar_i : fragment_i |
| 142 |
|
|
{ |
| 143 |
|
|
const char *name; |
| 144 |
|
|
void build (shader_builder &b); |
| 145 |
|
|
glvar_i (const char *name) : name (name) { } |
| 146 |
|
|
}; |
| 147 |
|
|
|
| 148 |
|
|
typedef auto_ref1<glvar_i, const char *> glvar; |
| 149 |
|
|
|
| 150 |
root |
1.6 |
struct var_i : fragment_i |
| 151 |
root |
1.5 |
{ |
| 152 |
root |
1.10 |
static unsigned int next_id; |
| 153 |
root |
1.5 |
|
| 154 |
|
|
CGparameter param; |
| 155 |
root |
1.10 |
char name[NAMELEN]; |
| 156 |
root |
1.6 |
const char *typestr; |
| 157 |
root |
1.5 |
|
| 158 |
|
|
void build (shader_builder &b); |
| 159 |
root |
1.7 |
virtual void build_decl (ostringstream &b); |
| 160 |
root |
1.5 |
|
| 161 |
root |
1.6 |
var_i (CGtype cgtype, const char *typestr); |
| 162 |
root |
1.5 |
~var_i (); |
| 163 |
|
|
}; |
| 164 |
|
|
|
| 165 |
root |
1.6 |
struct uniform_i : var_i |
| 166 |
root |
1.5 |
{ |
| 167 |
root |
1.6 |
bool dirty; |
| 168 |
root |
1.5 |
|
| 169 |
|
|
virtual void update () = 0; |
| 170 |
|
|
|
| 171 |
root |
1.6 |
void build (shader_builder &b); |
| 172 |
|
|
void build_decl (ostringstream &b); |
| 173 |
root |
1.10 |
uniform_i (CGtype cgtype, const char *strtype); |
| 174 |
root |
1.5 |
}; |
| 175 |
|
|
|
| 176 |
root |
1.6 |
template<int dimension, typename gltype, CGtype cgtype, const char *strtype, void (*upd)(CGparameter, const gltype *)> |
| 177 |
|
|
struct uniform2_i : uniform_i |
| 178 |
root |
1.5 |
{ |
| 179 |
|
|
gltype data[dimension]; |
| 180 |
|
|
|
| 181 |
|
|
void update () |
| 182 |
|
|
{ |
| 183 |
|
|
if (dirty) |
| 184 |
|
|
{ |
| 185 |
root |
1.6 |
upd (param, data); |
| 186 |
root |
1.5 |
dirty = false; |
| 187 |
|
|
} |
| 188 |
|
|
} |
| 189 |
|
|
|
| 190 |
root |
1.6 |
uniform2_i () : uniform_i (cgtype, strtype) { } |
| 191 |
root |
1.5 |
}; |
| 192 |
|
|
|
| 193 |
root |
1.6 |
template<int dimension, const char *strtype, void (*update)(CGparameter, const GLfloat *)> |
| 194 |
|
|
struct uniform_f_i : uniform2_i<dimension, GLfloat, CG_FLOAT, strtype, update> |
| 195 |
root |
1.5 |
{ |
| 196 |
|
|
}; |
| 197 |
|
|
|
| 198 |
root |
1.6 |
struct uniform_1f_i : uniform_f_i<1, str_float, cgGLSetParameter1fv> { |
| 199 |
root |
1.5 |
void operator =(GLfloat v) |
| 200 |
|
|
{ |
| 201 |
|
|
data[0] = v; |
| 202 |
|
|
dirty = true; |
| 203 |
|
|
} |
| 204 |
|
|
}; |
| 205 |
|
|
|
| 206 |
root |
1.9 |
struct uniform_2f_i : uniform_f_i<2, str_vec2, cgGLSetParameter2fv> { }; |
| 207 |
root |
1.5 |
|
| 208 |
root |
1.9 |
struct uniform_3f_i : uniform_f_i<3, str_vec3, cgGLSetParameter3fv> { |
| 209 |
root |
1.5 |
void operator =(const vec3 &v) |
| 210 |
|
|
{ |
| 211 |
|
|
data[0] = v.x; |
| 212 |
|
|
data[1] = v.y; |
| 213 |
|
|
data[2] = v.z; |
| 214 |
|
|
dirty = true; |
| 215 |
|
|
} |
| 216 |
|
|
}; |
| 217 |
|
|
|
| 218 |
root |
1.9 |
struct uniform_4f_i : uniform_f_i<4, str_vec4, cgGLSetParameter4fv> { |
| 219 |
root |
1.10 |
#if 0 |
| 220 |
root |
1.5 |
void operator =(const gl::matrix &m) |
| 221 |
|
|
{ |
| 222 |
|
|
memcpy (data, m.data, 16 * sizeof (GLfloat)); |
| 223 |
|
|
dirty = true; |
| 224 |
|
|
} |
| 225 |
root |
1.10 |
#endif |
| 226 |
root |
1.5 |
}; |
| 227 |
|
|
|
| 228 |
root |
1.9 |
struct uniform_matrix_2f_i : uniform_f_i< 4, str_mat2, cgGLSetMatrixParameterfc> { }; |
| 229 |
|
|
struct uniform_matrix_3f_i : uniform_f_i< 9, str_mat3, cgGLSetMatrixParameterfc> { }; |
| 230 |
|
|
struct uniform_matrix_4f_i : uniform_f_i<16, str_mat4, cgGLSetMatrixParameterfc> { }; |
| 231 |
root |
1.5 |
|
| 232 |
|
|
typedef ref<uniform_1f_i> uniform_1f; |
| 233 |
|
|
typedef ref<uniform_2f_i> uniform_2f; |
| 234 |
|
|
typedef ref<uniform_3f_i> uniform_3f; |
| 235 |
|
|
typedef ref<uniform_4f_i> uniform_4f; |
| 236 |
root |
1.9 |
typedef ref<uniform_matrix_2f_i> uniform_matrix_2f; |
| 237 |
|
|
typedef ref<uniform_matrix_3f_i> uniform_matrix_3f; |
| 238 |
|
|
typedef ref<uniform_matrix_4f_i> uniform_matrix_4f; |
| 239 |
root |
1.5 |
|
| 240 |
root |
1.6 |
struct stream_i : var_i |
| 241 |
root |
1.5 |
{ |
| 242 |
root |
1.6 |
void build (shader_builder &b); |
| 243 |
|
|
void build_decl (ostringstream &b); |
| 244 |
|
|
stream_i (CGtype type, const char *strtype) : var_i (type, strtype) { } |
| 245 |
|
|
}; |
| 246 |
|
|
|
| 247 |
|
|
template<int dimension, GLenum gltype, CGtype cgtype, const char *strtype> |
| 248 |
|
|
struct varying_i : stream_i |
| 249 |
|
|
{ |
| 250 |
root |
1.10 |
varying_i (const char *glname) |
| 251 |
root |
1.6 |
: stream_i (cgtype, strtype) |
| 252 |
|
|
{ |
| 253 |
root |
1.10 |
strcpy (name, glname); |
| 254 |
root |
1.6 |
} |
| 255 |
root |
1.5 |
|
| 256 |
|
|
void set (GLsizei stride, GLvoid *ptr) |
| 257 |
|
|
{ |
| 258 |
|
|
cgGLSetParameterPointer (param, dimension, gltype, stride, ptr); |
| 259 |
|
|
} |
| 260 |
|
|
}; |
| 261 |
|
|
|
| 262 |
root |
1.6 |
template<int dimension, const char *strtype> |
| 263 |
|
|
struct varying_f_i : varying_i<dimension, GL_FLOAT, CG_FLOAT, strtype> |
| 264 |
root |
1.5 |
{ |
| 265 |
root |
1.10 |
varying_f_i (const char *glname) : varying_i<dimension, GL_FLOAT, CG_FLOAT, strtype> (glname) { } |
| 266 |
root |
1.5 |
|
| 267 |
|
|
void set (const gl::vertex_buffer_object &vb, GLint offset) |
| 268 |
|
|
{ |
| 269 |
root |
1.6 |
varying_i<dimension, GL_FLOAT, CG_FLOAT, strtype>::set (gl::format_stride (vb.format), (GLvoid *)(long)offset); |
| 270 |
root |
1.5 |
} |
| 271 |
|
|
}; |
| 272 |
|
|
|
| 273 |
root |
1.6 |
struct varying_1f_i : varying_f_i<1, str_float> |
| 274 |
root |
1.5 |
{ |
| 275 |
root |
1.10 |
varying_1f_i (const char *glname) : varying_f_i<1, str_float> (glname) { } |
| 276 |
root |
1.5 |
}; |
| 277 |
|
|
|
| 278 |
root |
1.9 |
struct varying_2f_i : varying_f_i<2, str_vec2> |
| 279 |
root |
1.5 |
{ |
| 280 |
root |
1.10 |
varying_2f_i (const char *glname) : varying_f_i<2, str_vec2> (glname) { } |
| 281 |
root |
1.5 |
|
| 282 |
|
|
void set_t (const gl::vertex_buffer_object &vb) |
| 283 |
|
|
{ |
| 284 |
|
|
set (vb, gl::format_offset_t (vb.format)); |
| 285 |
|
|
} |
| 286 |
|
|
}; |
| 287 |
|
|
|
| 288 |
root |
1.9 |
struct varying_3f_i : varying_f_i<3, str_vec3> |
| 289 |
root |
1.5 |
{ |
| 290 |
root |
1.10 |
varying_3f_i (const char *glname) : varying_f_i<3, str_vec3> (glname) { } |
| 291 |
root |
1.5 |
|
| 292 |
|
|
void set_p (const gl::vertex_buffer_object &vb) |
| 293 |
|
|
{ |
| 294 |
|
|
set (vb, gl::format_offset_p (vb.format)); |
| 295 |
|
|
} |
| 296 |
|
|
|
| 297 |
|
|
void set_n (const gl::vertex_buffer_object &vb) |
| 298 |
|
|
{ |
| 299 |
|
|
set (vb, gl::format_offset_n (vb.format)); |
| 300 |
|
|
} |
| 301 |
|
|
}; |
| 302 |
|
|
|
| 303 |
root |
1.9 |
struct varying_4f_i : varying_f_i<4, str_vec4> |
| 304 |
root |
1.6 |
{ |
| 305 |
root |
1.10 |
varying_4f_i (const char *glname) : varying_f_i<4, str_vec4> (glname) { } |
| 306 |
root |
1.6 |
}; |
| 307 |
|
|
|
| 308 |
root |
1.10 |
typedef auto_ref1<varying_1f_i, const char *> varying_1f; |
| 309 |
|
|
typedef auto_ref1<varying_2f_i, const char *> varying_2f; |
| 310 |
|
|
typedef auto_ref1<varying_3f_i, const char *> varying_3f; |
| 311 |
|
|
typedef auto_ref1<varying_4f_i, const char *> varying_4f; |
| 312 |
root |
1.6 |
|
| 313 |
|
|
extern struct vin |
| 314 |
|
|
{ |
| 315 |
|
|
static varying_3f position_3f; |
| 316 |
|
|
static varying_4f position_4f; |
| 317 |
|
|
static varying_3f normal_3f; |
| 318 |
root |
1.9 |
static varying_3f color_3f; |
| 319 |
|
|
static varying_4f color_4f; |
| 320 |
|
|
static varying_3f color2_3f; |
| 321 |
|
|
static varying_4f color2_4f; |
| 322 |
root |
1.6 |
static varying_1f psize_1f; |
| 323 |
root |
1.8 |
static varying_1f texcoord_1f[8]; |
| 324 |
|
|
static varying_2f texcoord_2f[8]; |
| 325 |
|
|
static varying_3f texcoord_3f[8]; |
| 326 |
|
|
static varying_4f texcoord_4f[8]; |
| 327 |
root |
1.10 |
} vin; |
| 328 |
root |
1.6 |
|
| 329 |
root |
1.10 |
extern struct vout |
| 330 |
|
|
{ |
| 331 |
|
|
// glstate builtin |
| 332 |
|
|
static glvar position; |
| 333 |
|
|
static glvar point_size; |
| 334 |
|
|
static glvar clip_vertex; |
| 335 |
|
|
|
| 336 |
|
|
// standard varying |
| 337 |
|
|
static glvar front_color; |
| 338 |
|
|
static glvar back_color; |
| 339 |
|
|
static glvar front_secondary_color; |
| 340 |
|
|
static glvar back_secondary_color; |
| 341 |
|
|
static glvar texcoord[8]; |
| 342 |
|
|
static glvar fog_frag_coord; |
| 343 |
|
|
} vout; |
| 344 |
root |
1.6 |
|
| 345 |
|
|
extern struct fin |
| 346 |
|
|
{ |
| 347 |
root |
1.10 |
// glstate builtin |
| 348 |
|
|
static glvar frag_coord; |
| 349 |
root |
1.6 |
|
| 350 |
root |
1.10 |
// standard varying |
| 351 |
|
|
static glvar color; |
| 352 |
|
|
static glvar secondary_color; |
| 353 |
|
|
static glvar texcoord[8]; |
| 354 |
|
|
static glvar fog_frag_coord; |
| 355 |
root |
1.6 |
} fin; |
| 356 |
|
|
|
| 357 |
|
|
extern struct fout |
| 358 |
|
|
{ |
| 359 |
root |
1.10 |
// glstate builtin |
| 360 |
|
|
static glvar frag_color; |
| 361 |
|
|
static glvar frag_depth; |
| 362 |
|
|
static glvar frag_data[2]; |
| 363 |
root |
1.6 |
} fout; |
| 364 |
|
|
|
| 365 |
|
|
// predefined globals |
| 366 |
root |
1.10 |
extern struct gl { |
| 367 |
|
|
static uniform_matrix_4f model_view_matrix, |
| 368 |
|
|
projection_matrix, |
| 369 |
|
|
model_view_projection_matrix, |
| 370 |
|
|
texture_matrix[8]; |
| 371 |
|
|
static uniform_matrix_4f model_view_matrix_inverse, |
| 372 |
|
|
projection_matrix_inverse, |
| 373 |
|
|
model_view_projection_matrix_inverse, |
| 374 |
|
|
texture_matrix_inverse[8]; |
| 375 |
|
|
static uniform_matrix_4f model_view_matrix_transpose, |
| 376 |
|
|
projection_matrix_transpose, |
| 377 |
|
|
model_view_projection_matrix_transpose, |
| 378 |
|
|
texture_matrix_transpose[8]; |
| 379 |
|
|
static uniform_matrix_4f model_view_matrix_inverse_transpose, |
| 380 |
|
|
projection_matrix_inverse_transpose, |
| 381 |
|
|
model_view_projection_matrix_inverse_transpose, |
| 382 |
|
|
texture_matrix_inverse_transpose[8]; |
| 383 |
|
|
|
| 384 |
|
|
static uniform_matrix_3f normal_matrix; |
| 385 |
|
|
|
| 386 |
|
|
static uniform_1f normal_scale; |
| 387 |
|
|
static uniform_1f depth_range_near, depth_range_far, depth_range_diff; |
| 388 |
|
|
|
| 389 |
|
|
static uniform_4f clip_plane[2];//TODO |
| 390 |
|
|
|
| 391 |
|
|
// point parameters TODO |
| 392 |
|
|
|
| 393 |
|
|
static uniform_4f front_material_emission, |
| 394 |
|
|
front_material_ambient, |
| 395 |
|
|
front_material_diffuse, |
| 396 |
|
|
front_material_specular, |
| 397 |
|
|
front_material_shininess; |
| 398 |
|
|
static uniform_4f back_material_emission, |
| 399 |
|
|
back_material_ambient, |
| 400 |
|
|
back_material_diffuse, |
| 401 |
|
|
back_material_specular, |
| 402 |
|
|
back_material_shininess; |
| 403 |
|
|
|
| 404 |
|
|
// light source parameters TODO |
| 405 |
|
|
|
| 406 |
|
|
static uniform_4f light_model_ambient; |
| 407 |
|
|
static uniform_4f front_light_model_product_scene_color; |
| 408 |
|
|
static uniform_4f back_light_model_product_scene_color; |
| 409 |
|
|
|
| 410 |
|
|
// light products TODO |
| 411 |
|
|
// texture env and gen TODO |
| 412 |
|
|
// fogparameters TODO |
| 413 |
|
|
|
| 414 |
|
|
// shader output are in vout and fout |
| 415 |
|
|
} gl; |
| 416 |
root |
1.6 |
|
| 417 |
|
|
struct temporary_i : var_i |
| 418 |
|
|
{ |
| 419 |
|
|
void build (shader_builder &b); |
| 420 |
|
|
|
| 421 |
root |
1.10 |
temporary_i (CGtype cgtype, const char *strtype); |
| 422 |
root |
1.6 |
}; |
| 423 |
|
|
|
| 424 |
|
|
template<CGtype cgtype, const char *strtype> |
| 425 |
|
|
struct temp_ref : ref<temporary_i> |
| 426 |
root |
1.5 |
{ |
| 427 |
root |
1.6 |
temp_ref () |
| 428 |
root |
1.7 |
: ref<temporary_i> (*new temporary_i (cgtype, strtype)) |
| 429 |
root |
1.6 |
{ |
| 430 |
|
|
} |
| 431 |
root |
1.5 |
}; |
| 432 |
|
|
|
| 433 |
root |
1.9 |
typedef temp_ref<CG_FLOAT, str_float> temp_1f; |
| 434 |
|
|
typedef temp_ref<CG_FLOAT, str_vec2> temp_2f; |
| 435 |
|
|
typedef temp_ref<CG_FLOAT, str_vec3> temp_3f; |
| 436 |
|
|
typedef temp_ref<CG_FLOAT, str_vec4> temp_4f; |
| 437 |
|
|
typedef temp_ref<CG_FLOAT, str_mat2> temp_matrix_2f; |
| 438 |
|
|
typedef temp_ref<CG_FLOAT, str_mat3> temp_matrix_3f; |
| 439 |
|
|
typedef temp_ref<CG_FLOAT, str_mat4> temp_matrix_4f; |
| 440 |
root |
1.5 |
|
| 441 |
root |
1.8 |
template<CGtype cgtype, const char *strtype> |
| 442 |
|
|
struct sampler_i : uniform_i |
| 443 |
|
|
{ |
| 444 |
|
|
GLuint texture; |
| 445 |
|
|
|
| 446 |
|
|
void update () |
| 447 |
|
|
{ |
| 448 |
|
|
if (dirty) |
| 449 |
|
|
{ |
| 450 |
|
|
cgGLSetTextureParameter (param, texture); |
| 451 |
|
|
dirty = false; |
| 452 |
|
|
} |
| 453 |
|
|
} |
| 454 |
|
|
|
| 455 |
|
|
void begin () |
| 456 |
|
|
{ |
| 457 |
|
|
cgGLEnableTextureParameter (texture); |
| 458 |
|
|
} |
| 459 |
|
|
|
| 460 |
|
|
sampler_i (GLuint texturename) : uniform_i (cgtype, strtype), texture (texturename) { } |
| 461 |
|
|
}; |
| 462 |
|
|
|
| 463 |
root |
1.9 |
struct sampler_1d_i : sampler_i<CG_SAMPLER1D, str_sampler_1d> |
| 464 |
root |
1.8 |
{ |
| 465 |
root |
1.9 |
sampler_1d_i (GLuint texturename) : sampler_i<CG_SAMPLER1D, str_sampler_1d> (texturename) { } |
| 466 |
root |
1.8 |
}; |
| 467 |
|
|
|
| 468 |
root |
1.9 |
struct sampler_1d_shadow_i : sampler_i<CG_SAMPLER1D, str_sampler_1d_shadow> |
| 469 |
root |
1.8 |
{ |
| 470 |
root |
1.9 |
sampler_1d_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLER1D, str_sampler_1d_shadow> (texturename) { } |
| 471 |
root |
1.8 |
}; |
| 472 |
|
|
|
| 473 |
root |
1.9 |
struct sampler_2d_i : sampler_i<CG_SAMPLER2D, str_sampler_2d> |
| 474 |
root |
1.8 |
{ |
| 475 |
root |
1.9 |
sampler_2d_i (GLuint texturename) : sampler_i<CG_SAMPLER2D, str_sampler_2d> (texturename) { } |
| 476 |
root |
1.8 |
}; |
| 477 |
|
|
|
| 478 |
root |
1.9 |
struct sampler_2d_shadow_i : sampler_i<CG_SAMPLER2D, str_sampler_2d_shadow> |
| 479 |
root |
1.5 |
{ |
| 480 |
root |
1.9 |
sampler_2d_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLER2D, str_sampler_2d_shadow> (texturename) { } |
| 481 |
root |
1.5 |
}; |
| 482 |
root |
1.8 |
|
| 483 |
root |
1.9 |
struct sampler_2d_rect_i : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect> |
| 484 |
root |
1.8 |
{ |
| 485 |
root |
1.9 |
sampler_2d_rect_i (GLuint texturename) : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect> (texturename) { } |
| 486 |
|
|
}; |
| 487 |
|
|
|
| 488 |
|
|
struct sampler_2d_rect_shadow_i : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect_shadow> |
| 489 |
|
|
{ |
| 490 |
|
|
sampler_2d_rect_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect_shadow> (texturename) { } |
| 491 |
|
|
}; |
| 492 |
|
|
|
| 493 |
|
|
struct sampler_3d_i : sampler_i<CG_SAMPLER3D, str_sampler_3d> |
| 494 |
|
|
{ |
| 495 |
|
|
sampler_3d_i (GLuint texturename) : sampler_i<CG_SAMPLER3D, str_sampler_3d> (texturename) { } |
| 496 |
|
|
}; |
| 497 |
|
|
|
| 498 |
|
|
struct sampler_3d_rect_i : sampler_i<CG_SAMPLER3D, str_sampler_3d_rect> |
| 499 |
|
|
{ |
| 500 |
|
|
sampler_3d_rect_i (GLuint texturename) : sampler_i<CG_SAMPLER3D, str_sampler_3d_rect> (texturename) { } |
| 501 |
|
|
}; |
| 502 |
|
|
|
| 503 |
|
|
struct sampler_cube_i : sampler_i<CG_SAMPLERCUBE, str_sampler_cube> |
| 504 |
|
|
{ |
| 505 |
|
|
sampler_cube_i (GLuint texturename) : sampler_i<CG_SAMPLERCUBE, str_sampler_cube> (texturename) { } |
| 506 |
root |
1.8 |
}; |
| 507 |
|
|
|
| 508 |
root |
1.10 |
typedef auto_ref1<sampler_1d_i, GLuint> sampler_1d; |
| 509 |
|
|
typedef auto_ref1<sampler_1d_shadow_i, GLuint> sampler_1d_shadow; |
| 510 |
|
|
typedef auto_ref1<sampler_2d_i, GLuint> sampler_2d; |
| 511 |
|
|
typedef auto_ref1<sampler_2d_shadow_i, GLuint> sampler_2d_shadow; |
| 512 |
|
|
typedef auto_ref1<sampler_2d_rect_i, GLuint> sampler_2d_rect; |
| 513 |
|
|
typedef auto_ref1<sampler_2d_rect_shadow_i, GLuint> sampler_2d_rect_shadow; |
| 514 |
|
|
typedef auto_ref1<sampler_3d_i, GLuint> sampler_3d; |
| 515 |
|
|
typedef auto_ref1<sampler_3d_rect_i, GLuint> sampler_3d_rect; |
| 516 |
|
|
typedef auto_ref1<sampler_cube_i, GLuint> sampler_cube; |
| 517 |
root |
1.5 |
|
| 518 |
root |
1.6 |
struct fragment_const_string_i : fragment_i |
| 519 |
|
|
{ |
| 520 |
|
|
const char *str; |
| 521 |
|
|
|
| 522 |
|
|
void build (shader_builder &b); |
| 523 |
|
|
|
| 524 |
|
|
fragment_const_string_i (const char *str) : str(str) { } |
| 525 |
|
|
}; |
| 526 |
|
|
|
| 527 |
|
|
struct fragment_vector_i : fragment_i, vector<fragment> |
| 528 |
|
|
{ |
| 529 |
|
|
void build (shader_builder &b); |
| 530 |
|
|
|
| 531 |
root |
1.10 |
#if 0 |
| 532 |
|
|
fragment_vector_i &operator <<(statement_i &f) |
| 533 |
|
|
{ |
| 534 |
|
|
push_back (*new fragment_const_string_i (" ")); |
| 535 |
|
|
|
| 536 |
|
|
for (vector<fragment>::iterator i = f.begin (); i != f.end (); i++) |
| 537 |
|
|
push_back (*i); |
| 538 |
|
|
|
| 539 |
|
|
push_back (*new fragment_const_string_i (";\n")); |
| 540 |
|
|
return *this; |
| 541 |
|
|
} |
| 542 |
|
|
#endif |
| 543 |
|
|
}; |
| 544 |
|
|
|
| 545 |
|
|
typedef ref<fragment_vector_i> fragment_vector; |
| 546 |
|
|
|
| 547 |
|
|
struct statement_i : fragment_vector_i |
| 548 |
|
|
{ |
| 549 |
|
|
void build (shader_builder &b); |
| 550 |
|
|
}; |
| 551 |
|
|
|
| 552 |
|
|
struct statement : ref<statement_i> |
| 553 |
|
|
{ |
| 554 |
root |
1.6 |
template<class fragment_i> |
| 555 |
root |
1.10 |
statement (ref<fragment_i> &f) |
| 556 |
|
|
: ref<statement_i> (*new statement_i) |
| 557 |
root |
1.6 |
{ |
| 558 |
root |
1.10 |
p->push_back (f); |
| 559 |
root |
1.6 |
} |
| 560 |
|
|
|
| 561 |
|
|
template<class fragment_i> |
| 562 |
root |
1.10 |
const statement &operator <<(ref<fragment_i> &f) const |
| 563 |
root |
1.6 |
{ |
| 564 |
root |
1.10 |
p->push_back (f); |
| 565 |
root |
1.6 |
return *this; |
| 566 |
|
|
} |
| 567 |
|
|
|
| 568 |
root |
1.10 |
const statement &operator <<(const char *s) const |
| 569 |
root |
1.6 |
{ |
| 570 |
root |
1.10 |
p->push_back (*new fragment_const_string_i (s)); |
| 571 |
root |
1.6 |
return *this; |
| 572 |
|
|
} |
| 573 |
|
|
}; |
| 574 |
|
|
|
| 575 |
root |
1.10 |
struct shader_object_i : fragment_vector_i |
| 576 |
|
|
{ |
| 577 |
|
|
GLenum type; |
| 578 |
|
|
GLuint id; // GLhandleARB, but 2.0 will use uint |
| 579 |
root |
1.6 |
|
| 580 |
root |
1.10 |
string source (); |
| 581 |
|
|
void compile (); |
| 582 |
root |
1.6 |
|
| 583 |
root |
1.10 |
shader_object_i (GLenum type); |
| 584 |
|
|
~shader_object_i (); |
| 585 |
root |
1.6 |
}; |
| 586 |
|
|
|
| 587 |
root |
1.10 |
template<GLenum type> |
| 588 |
|
|
struct shader_object : ref<shader_object_i> |
| 589 |
root |
1.6 |
{ |
| 590 |
root |
1.10 |
shader_object () |
| 591 |
|
|
: ref<shader_object_i> (*new shader_object_i (type)) |
| 592 |
|
|
{ |
| 593 |
|
|
} |
| 594 |
|
|
|
| 595 |
|
|
const statement &operator <<(const statement &f) |
| 596 |
|
|
{ |
| 597 |
|
|
p->push_back (f); |
| 598 |
|
|
return f; |
| 599 |
|
|
} |
| 600 |
root |
1.5 |
}; |
| 601 |
root |
1.6 |
|
| 602 |
root |
1.10 |
typedef shader_object<GL_VERTEX_SHADER_ARB> vertex_shader; |
| 603 |
|
|
typedef shader_object<GL_FRAGMENT_SHADER_ARB> fragment_shader; |
| 604 |
root |
1.6 |
|
| 605 |
|
|
void debdebdebdebug ();//D |
| 606 |
root |
1.5 |
} |
| 607 |
root |
1.3 |
|
| 608 |
|
|
struct texture_execption |
| 609 |
|
|
{ |
| 610 |
|
|
texture_execption (string s) : msg(s) { } |
| 611 |
|
|
string msg; |
| 612 |
|
|
}; |
| 613 |
|
|
|
| 614 |
|
|
inline int power_of_two (int input) |
| 615 |
|
|
{ |
| 616 |
|
|
int value = 1; |
| 617 |
|
|
|
| 618 |
|
|
while (value < input) |
| 619 |
|
|
{ |
| 620 |
|
|
value <<= 1; |
| 621 |
|
|
} |
| 622 |
|
|
return value; |
| 623 |
|
|
} |
| 624 |
|
|
|
| 625 |
root |
1.1 |
struct material |
| 626 |
|
|
{ |
| 627 |
root |
1.2 |
virtual void begin () = 0; |
| 628 |
|
|
virtual void end () = 0; |
| 629 |
root |
1.1 |
virtual ~material (); |
| 630 |
|
|
}; |
| 631 |
|
|
|
| 632 |
|
|
struct simple_material : material |
| 633 |
|
|
{ |
| 634 |
|
|
colour diffuse, specular, emission; |
| 635 |
|
|
GLfloat shininess; |
| 636 |
|
|
|
| 637 |
root |
1.2 |
void begin (); |
| 638 |
|
|
void end (); |
| 639 |
root |
1.1 |
|
| 640 |
|
|
simple_material () |
| 641 |
|
|
: diffuse(1, 0, 1, 1) |
| 642 |
|
|
, specular(1, 0, 1, 1) |
| 643 |
|
|
, emission(1, 0, 1, 1) |
| 644 |
|
|
, shininess(1.) |
| 645 |
|
|
{ |
| 646 |
|
|
} |
| 647 |
|
|
}; |
| 648 |
|
|
|
| 649 |
root |
1.3 |
struct texture |
| 650 |
|
|
{ |
| 651 |
|
|
SDL_Surface *image; |
| 652 |
|
|
GLuint texture_pxls; |
| 653 |
|
|
GLfloat texcoord[4]; |
| 654 |
|
|
|
| 655 |
|
|
GLuint load_texture (SDL_Surface *surface, GLfloat *texcoord); |
| 656 |
|
|
|
| 657 |
|
|
texture(const char *f) |
| 658 |
|
|
{ |
| 659 |
|
|
image = IMG_Load (f); |
| 660 |
|
|
|
| 661 |
|
|
if (!image) |
| 662 |
|
|
throw (texture_execption ("Couldn't load " + (string) f + ": " + (string) IMG_GetError ())); |
| 663 |
|
|
|
| 664 |
|
|
texture_pxls = load_texture (image, (GLfloat*)&texcoord); |
| 665 |
|
|
if (texture_pxls == 0) |
| 666 |
|
|
throw (texture_execption ("Couldn't make GL Texture, failed to create new RGB SWSurface")); |
| 667 |
|
|
} |
| 668 |
|
|
}; |
| 669 |
|
|
|
| 670 |
|
|
extern CGcontext cgc; |
| 671 |
|
|
|
| 672 |
root |
1.6 |
struct xshader |
| 673 |
root |
1.3 |
{ |
| 674 |
|
|
CGprogram vsh, fsh; |
| 675 |
|
|
CGparameter lightpos; |
| 676 |
|
|
// mv, mvp |
| 677 |
|
|
CGprofile vsh_profile, fsh_profile; |
| 678 |
root |
1.4 |
CGparameter g_Texture; // the texture parameter |
| 679 |
root |
1.3 |
|
| 680 |
|
|
void check_cg_error (void) |
| 681 |
|
|
{ |
| 682 |
|
|
CGerror err = cgGetError (); |
| 683 |
|
|
|
| 684 |
|
|
if (err != CG_NO_ERROR) |
| 685 |
|
|
{ |
| 686 |
|
|
printf("CG error: %s\n", cgGetErrorString (err)); |
| 687 |
|
|
exit(1); |
| 688 |
|
|
} |
| 689 |
|
|
} |
| 690 |
root |
1.6 |
xshader (const char *vshp, const char *fshp) |
| 691 |
root |
1.3 |
{ |
| 692 |
|
|
vsh_profile = CG_PROFILE_ARBVP1; |
| 693 |
|
|
//if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30; |
| 694 |
|
|
//if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40; |
| 695 |
|
|
fsh_profile = CG_PROFILE_ARBFP1; |
| 696 |
|
|
//if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30; |
| 697 |
|
|
//if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40; |
| 698 |
|
|
|
| 699 |
|
|
vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, vshp, vsh_profile, 0, 0); |
| 700 |
|
|
check_cg_error (); |
| 701 |
|
|
cgGLLoadProgram (vsh); |
| 702 |
|
|
check_cg_error (); |
| 703 |
|
|
|
| 704 |
|
|
fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, fshp, fsh_profile, 0, 0); |
| 705 |
|
|
check_cg_error (); |
| 706 |
|
|
cgGLLoadProgram (fsh); |
| 707 |
|
|
check_cg_error (); |
| 708 |
|
|
|
| 709 |
root |
1.4 |
cgGLBindProgram (vsh); |
| 710 |
|
|
cgGLBindProgram (fsh); |
| 711 |
root |
1.3 |
|
| 712 |
|
|
// mv = cgGetNamedParameter (vsh, "WorldProj"); |
| 713 |
|
|
// mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
| 714 |
root |
1.4 |
//lightpos = cgGetNamedParameter (vsh, "LightPos"); |
| 715 |
|
|
//cgGLSetParameter4f (lightpos, 10, 10, 0, 1); //camera.p.x, camera.p.y, camera.p.z, 1); |
| 716 |
|
|
|
| 717 |
|
|
check_cg_error (); |
| 718 |
root |
1.3 |
} |
| 719 |
|
|
}; |
| 720 |
|
|
|
| 721 |
root |
1.6 |
struct osama_material : material, texture, xshader { |
| 722 |
root |
1.3 |
osama_material () |
| 723 |
root |
1.6 |
: xshader ("vsh.cg", "fsh.cg"), texture ("textures/osama.jpg") |
| 724 |
root |
1.3 |
{ |
| 725 |
|
|
g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;) |
| 726 |
root |
1.4 |
cgGLSetTextureParameter(g_Texture, texture_pxls); // Bind the texture number 999 to g_Texture |
| 727 |
root |
1.3 |
} |
| 728 |
|
|
|
| 729 |
|
|
void begin (); |
| 730 |
|
|
void end (); |
| 731 |
|
|
|
| 732 |
|
|
}; |
| 733 |
|
|
|
| 734 |
|
|
void init_shaders (); |
| 735 |
root |
1.1 |
#endif |
| 736 |
|
|
|
| 737 |
|
|
|