| 1 |
#ifndef __SHADERS_H__ |
| 2 |
#define __SHADERS_H__ |
| 3 |
|
| 4 |
#include <GL/glew.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <set> |
| 7 |
|
| 8 |
#include "gl_object.h" |
| 9 |
|
| 10 |
class Shader : public GLObject, public Bindable |
| 11 |
{ |
| 12 |
public: |
| 13 |
typedef enum |
| 14 |
{ |
| 15 |
VERTEX, |
| 16 |
FRAGMENT |
| 17 |
} ShaderType; |
| 18 |
|
| 19 |
Shader(void) : GLObject(), Bindable() |
| 20 |
{ |
| 21 |
this->flags = 0; |
| 22 |
this->flags |= SOURCE_DIRTY; |
| 23 |
this->compiled = false; |
| 24 |
} |
| 25 |
|
| 26 |
void Gen(void) |
| 27 |
{ |
| 28 |
this->Del(); |
| 29 |
GLenum t; |
| 30 |
switch (this->Type()) |
| 31 |
{ |
| 32 |
case Shader::VERTEX : t = GL_VERTEX_SHADER; break; |
| 33 |
case Shader::FRAGMENT : t = GL_FRAGMENT_SHADER; break; |
| 34 |
default: return; |
| 35 |
}; |
| 36 |
this->objectID = glCreateShader(t); |
| 37 |
} |
| 38 |
|
| 39 |
void Del(void) |
| 40 |
{ |
| 41 |
if (this->objectID == 0) return; |
| 42 |
glDeleteShader(this->objectID); |
| 43 |
this->objectID = 0; |
| 44 |
} |
| 45 |
|
| 46 |
virtual ShaderType Type(void) const = 0; |
| 47 |
|
| 48 |
void SetSource(const char * src) |
| 49 |
{ |
| 50 |
this->flags |= SOURCE_DIRTY; |
| 51 |
this->compiled = false; |
| 52 |
this->source = src; |
| 53 |
|
| 54 |
const char * pSrc = this->source.c_str(); |
| 55 |
glShaderSource(this->objectID, 1, &pSrc, 0); |
| 56 |
} |
| 57 |
|
| 58 |
bool LoadSource(const char * fileName) |
| 59 |
{ |
| 60 |
this->flags |= SOURCE_DIRTY; |
| 61 |
this->compiled = false; |
| 62 |
FILE * f = fopen(fileName, "rb"); |
| 63 |
if (f == 0) |
| 64 |
{ |
| 65 |
this->source = ""; |
| 66 |
return false; |
| 67 |
} |
| 68 |
fseek(f, 0, SEEK_END); |
| 69 |
const size_t sz = (size_t)ftell(f); |
| 70 |
rewind(f); |
| 71 |
char * buff = new char[sz + 1]; |
| 72 |
fread(buff, sizeof(char), sz, f); |
| 73 |
fclose(f); |
| 74 |
buff[sz] = '\0'; |
| 75 |
|
| 76 |
this->source = buff; |
| 77 |
delete [] buff; |
| 78 |
|
| 79 |
const char * pSrc = this->source.c_str(); |
| 80 |
glShaderSource(this->objectID, 1, &pSrc, 0); |
| 81 |
|
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
bool Compile(void) |
| 86 |
{ |
| 87 |
glCompileShader(this->objectID); |
| 88 |
GLint cm = 0; |
| 89 |
glGetShaderiv(this->objectID, GL_COMPILE_STATUS, &cm); |
| 90 |
this->compiled = (cm != GL_FALSE); |
| 91 |
this->flags = 0; |
| 92 |
return this->compiled; |
| 93 |
} |
| 94 |
|
| 95 |
bool IsCompiled(void) |
| 96 |
{ |
| 97 |
return this->compiled; |
| 98 |
} |
| 99 |
|
| 100 |
std::string InfoLog(void) |
| 101 |
{ |
| 102 |
GLint len = 0; |
| 103 |
glGetShaderiv(this->objectID, GL_INFO_LOG_LENGTH, &len); |
| 104 |
char * ch = new char[len + 1]; |
| 105 |
glGetShaderInfoLog(this->objectID, len, &len, ch); |
| 106 |
std::string infoLog = ch; |
| 107 |
delete [] ch; |
| 108 |
return infoLog; |
| 109 |
} |
| 110 |
|
| 111 |
protected: |
| 112 |
enum |
| 113 |
{ |
| 114 |
SOURCE_DIRTY |
| 115 |
}; |
| 116 |
|
| 117 |
std::string source; |
| 118 |
unsigned int flags; |
| 119 |
bool compiled; |
| 120 |
|
| 121 |
void DoBind(void) |
| 122 |
{ |
| 123 |
} |
| 124 |
|
| 125 |
void DoUnbind(void) |
| 126 |
{ |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
class VertexShader : public Shader |
| 131 |
{ |
| 132 |
public: |
| 133 |
VertexShader(void) : Shader() |
| 134 |
{ |
| 135 |
} |
| 136 |
|
| 137 |
ShaderType Type(void) const |
| 138 |
{ |
| 139 |
return Shader::VERTEX; |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
class FragmentShader : public Shader |
| 144 |
{ |
| 145 |
public: |
| 146 |
FragmentShader(void) : Shader() |
| 147 |
{ |
| 148 |
} |
| 149 |
|
| 150 |
ShaderType Type(void) const |
| 151 |
{ |
| 152 |
return Shader::FRAGMENT; |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
#if 0 |
| 157 |
class Program; |
| 158 |
|
| 159 |
class Uniform |
| 160 |
{ |
| 161 |
friend class Program; |
| 162 |
|
| 163 |
public: |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
const std::string & Name(void) const |
| 201 |
{ |
| 202 |
return this->name; |
| 203 |
} |
| 204 |
|
| 205 |
virtual GLenum Type(void) const = 0; |
| 206 |
|
| 207 |
protected: |
| 208 |
Program * prog; |
| 209 |
GLint location; |
| 210 |
std::string name; |
| 211 |
|
| 212 |
Uniform(Program * prog, GLint location, const std::string & name) |
| 213 |
{ |
| 214 |
this->prog = prog; |
| 215 |
this->location = location; |
| 216 |
this->name = name; |
| 217 |
} |
| 218 |
|
| 219 |
virtual void Apply(void) = 0; |
| 220 |
}; |
| 221 |
|
| 222 |
class Uniform1b : public Uniform; |
| 223 |
{ |
| 224 |
public: |
| 225 |
|
| 226 |
void SetValue(GLboolean x) |
| 227 |
{ |
| 228 |
this->value[0] = x; |
| 229 |
} |
| 230 |
|
| 231 |
GLboolean GetValue(void) const |
| 232 |
{ |
| 233 |
return this->value[0]; |
| 234 |
} |
| 235 |
|
| 236 |
protected: |
| 237 |
Program * prog; |
| 238 |
GLboolean value[1]; |
| 239 |
|
| 240 |
Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) |
| 241 |
{ |
| 242 |
this->value = GL_FALSE; |
| 243 |
} |
| 244 |
}; |
| 245 |
|
| 246 |
class Uniform2b : public Uniform; |
| 247 |
{ |
| 248 |
public: |
| 249 |
|
| 250 |
void SetValue(GLboolean x, GLboolean y) |
| 251 |
{ |
| 252 |
this->value[0] = x; |
| 253 |
this->value[1] = y; |
| 254 |
} |
| 255 |
|
| 256 |
GLboolean GetValueX(void) const |
| 257 |
{ |
| 258 |
return this->value[0]; |
| 259 |
} |
| 260 |
|
| 261 |
GLboolean GetValueY(void) const |
| 262 |
{ |
| 263 |
return this->value[1]; |
| 264 |
} |
| 265 |
|
| 266 |
protected: |
| 267 |
Program * prog; |
| 268 |
GLboolean value[2]; |
| 269 |
|
| 270 |
Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) |
| 271 |
{ |
| 272 |
this->value[0] = GL_FALSE; |
| 273 |
this->value[1] = GL_FALSE; |
| 274 |
} |
| 275 |
}; |
| 276 |
|
| 277 |
class Uniform3b : public Uniform; |
| 278 |
{ |
| 279 |
public: |
| 280 |
|
| 281 |
void SetValue(GLboolean x, GLboolean y, GLboolean z) |
| 282 |
{ |
| 283 |
this->value[0] = x; |
| 284 |
this->value[1] = y; |
| 285 |
this->value[2] = z; |
| 286 |
} |
| 287 |
|
| 288 |
GLboolean GetValueX(void) const |
| 289 |
{ |
| 290 |
return this->value[0]; |
| 291 |
} |
| 292 |
|
| 293 |
GLboolean GetValueY(void) const |
| 294 |
{ |
| 295 |
return this->value[1]; |
| 296 |
} |
| 297 |
|
| 298 |
GLboolean GetValueZ(void) const |
| 299 |
{ |
| 300 |
return this->value[2]; |
| 301 |
} |
| 302 |
|
| 303 |
protected: |
| 304 |
Program * prog; |
| 305 |
GLboolean value[2]; |
| 306 |
|
| 307 |
Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) |
| 308 |
{ |
| 309 |
this->value[0] = GL_FALSE; |
| 310 |
this->value[1] = GL_FALSE; |
| 311 |
} |
| 312 |
}; |
| 313 |
|
| 314 |
class Uniform1i : public Uniform; |
| 315 |
{ |
| 316 |
public: |
| 317 |
|
| 318 |
void SetValue(GLint v) |
| 319 |
{ |
| 320 |
this->value = v; |
| 321 |
} |
| 322 |
|
| 323 |
GLint GetValue(void) const |
| 324 |
{ |
| 325 |
return this->value; |
| 326 |
} |
| 327 |
|
| 328 |
protected: |
| 329 |
Program * prog; |
| 330 |
GLint value; |
| 331 |
|
| 332 |
Uniform(Program * prog, GLint location, const std::string & name) : Uniform(prog, location, name) |
| 333 |
{ |
| 334 |
this->value = 0; |
| 335 |
} |
| 336 |
}; |
| 337 |
#endif |
| 338 |
|
| 339 |
class Program : public GLObject, public Bindable |
| 340 |
{ |
| 341 |
public: |
| 342 |
|
| 343 |
Program(void) |
| 344 |
{ |
| 345 |
this->linked = false; |
| 346 |
} |
| 347 |
|
| 348 |
void Gen(void) |
| 349 |
{ |
| 350 |
this->Del(); |
| 351 |
this->objectID = glCreateProgram(); |
| 352 |
} |
| 353 |
|
| 354 |
void Del(void) |
| 355 |
{ |
| 356 |
if (this->objectID == 0) return; |
| 357 |
glDeleteProgram(this->objectID); |
| 358 |
this->objectID = 0; |
| 359 |
} |
| 360 |
|
| 361 |
void Attach(Shader * shd) |
| 362 |
{ |
| 363 |
this->shaders.insert(shd); |
| 364 |
this->linked = false; |
| 365 |
glAttachShader(this->objectID, shd->ObjectID()); |
| 366 |
} |
| 367 |
|
| 368 |
void Detach(Shader * shd) |
| 369 |
{ |
| 370 |
this->shaders.erase(shd); |
| 371 |
this->linked = false; |
| 372 |
glDetachShader(this->objectID, shd->ObjectID()); |
| 373 |
} |
| 374 |
|
| 375 |
GLsizei AttachedShaders(void) const |
| 376 |
{ |
| 377 |
return ((GLsizei)(this->shaders.size())); |
| 378 |
} |
| 379 |
|
| 380 |
Shader * AttachedShader(int i) |
| 381 |
{ |
| 382 |
Shader * shd = 0; |
| 383 |
int cnt = 0; |
| 384 |
for (std::set<Shader *>::iterator it=this->shaders.begin(); (cnt < i) && (it!=this->shaders.end()); ++it) |
| 385 |
{ |
| 386 |
shd = (*it); |
| 387 |
} |
| 388 |
return shd; |
| 389 |
} |
| 390 |
|
| 391 |
bool Link(void) |
| 392 |
{ |
| 393 |
for (std::set<Shader *>::iterator it=this->shaders.begin(); it!=this->shaders.end(); ++it) |
| 394 |
{ |
| 395 |
Shader * shd = (*it); |
| 396 |
if (!shd->IsCompiled()) |
| 397 |
{ |
| 398 |
shd->Compile(); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
glLinkProgram(this->objectID); |
| 403 |
|
| 404 |
GLint cm = 0; |
| 405 |
glGetProgramiv(this->objectID, GL_LINK_STATUS, &cm); |
| 406 |
this->linked = (cm != GL_FALSE); |
| 407 |
|
| 408 |
return this->linked; |
| 409 |
} |
| 410 |
|
| 411 |
bool IsLinked(void) const |
| 412 |
{ |
| 413 |
return this->linked; |
| 414 |
} |
| 415 |
|
| 416 |
std::string InfoLog(void) |
| 417 |
{ |
| 418 |
GLint len = 0; |
| 419 |
glGetProgramiv(this->objectID, GL_INFO_LOG_LENGTH, &len); |
| 420 |
char * ch = new char[len + 1]; |
| 421 |
glGetProgramInfoLog(this->objectID, len, &len, ch); |
| 422 |
std::string infoLog = ch; |
| 423 |
delete [] ch; |
| 424 |
return infoLog; |
| 425 |
} |
| 426 |
|
| 427 |
void Uniform(const char * name, GLint x) |
| 428 |
{ |
| 429 |
const GLint loc = glGetUniformLocation(this->objectID, name); |
| 430 |
glUniform1i(loc, x); |
| 431 |
} |
| 432 |
|
| 433 |
void Uniform(const char * name, GLint x, GLint y) |
| 434 |
{ |
| 435 |
const GLint loc = glGetUniformLocation(this->objectID, name); |
| 436 |
glUniform2i(loc, x, y); |
| 437 |
} |
| 438 |
|
| 439 |
void Uniform(const char * name, GLint x, GLint y, GLint z) |
| 440 |
{ |
| 441 |
const GLint loc = glGetUniformLocation(this->objectID, name); |
| 442 |
glUniform3i(loc, x, y, z); |
| 443 |
} |
| 444 |
|
| 445 |
void Uniform(const char * name, GLint x, GLint y, GLint z, GLint w) |
| 446 |
{ |
| 447 |
const GLint loc = glGetUniformLocation(this->objectID, name); |
| 448 |
glUniform4i(loc, x, y, z, w); |
| 449 |
} |
| 450 |
|
| 451 |
void Uniform(const char * name, GLfloat x) |
| 452 |
{ |
| 453 |
const GLint loc = glGetUniformLocation(this->objectID, name); |
| 454 |
glUniform1f(loc, x); |
| 455 |
} |
| 456 |
|
| 457 |
void Uniform(const char * name, GLfloat x, GLfloat y) |
| 458 |
{ |
| 459 |
const GLint loc = glGetUniformLocation(this->objectID, name); |
| 460 |
glUniform2f(loc, x, y); |
| 461 |
} |
| 462 |
|
| 463 |
void Uniform(const char * name, GLfloat x, GLfloat y, GLfloat z) |
| 464 |
{ |
| 465 |
const GLint loc = glGetUniformLocation(this->objectID, name); |
| 466 |
glUniform3f(loc, x, y, z); |
| 467 |
} |
| 468 |
|
| 469 |
void Uniform(const char * name, GLfloat x, GLfloat y, GLfloat z, GLfloat w) |
| 470 |
{ |
| 471 |
const GLint loc = glGetUniformLocation(this->objectID, name); |
| 472 |
glUniform4f(loc, x, y, z, w); |
| 473 |
} |
| 474 |
|
| 475 |
protected: |
| 476 |
std::set<Shader *> shaders; |
| 477 |
bool linked; |
| 478 |
|
| 479 |
void DoBind(void) |
| 480 |
{ |
| 481 |
if (!this->IsLinked()) |
| 482 |
{ |
| 483 |
this->Link(); |
| 484 |
} |
| 485 |
glUseProgram(this->objectID); |
| 486 |
} |
| 487 |
|
| 488 |
void DoUnbind(void) |
| 489 |
{ |
| 490 |
glUseProgram(0); |
| 491 |
} |
| 492 |
}; |
| 493 |
|
| 494 |
class ProgramVF : public Bindable |
| 495 |
{ |
| 496 |
public: |
| 497 |
Program prog; |
| 498 |
VertexShader vshd; |
| 499 |
FragmentShader fshd; |
| 500 |
|
| 501 |
ProgramVF(void) : Bindable() |
| 502 |
{ |
| 503 |
} |
| 504 |
|
| 505 |
void SetSources(const char * vsrc, const char * fsrc) |
| 506 |
{ |
| 507 |
this->vshd.SetSource(vsrc); |
| 508 |
this->fshd.SetSource(fsrc); |
| 509 |
this->prog.Attach(&(this->vshd)); |
| 510 |
this->prog.Attach(&(this->fshd)); |
| 511 |
} |
| 512 |
|
| 513 |
void LoadSources(const char * vfile, const char * ffile) |
| 514 |
{ |
| 515 |
this->vshd.LoadSource(vfile); |
| 516 |
this->fshd.LoadSource(ffile); |
| 517 |
this->prog.Attach(&(this->vshd)); |
| 518 |
this->prog.Attach(&(this->fshd)); |
| 519 |
} |
| 520 |
|
| 521 |
protected: |
| 522 |
void DoBind(void) |
| 523 |
{ |
| 524 |
this->prog.Bind(); |
| 525 |
} |
| 526 |
|
| 527 |
void DoUnbind(void) |
| 528 |
{ |
| 529 |
this->prog.Unbind(); |
| 530 |
} |
| 531 |
}; |
| 532 |
|
| 533 |
#endif |