Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/graphics/shader.h @ 9785

Last change on this file since 9785 was 9715, checked in by bensch, 18 years ago

renamed newclassid to classid and newobjectlist to objectlist

File size: 4.1 KB
RevLine 
[4838]1/*!
[5261]2 * @file shader.h
3 * @brief Definition of the Shader rendering class
[3245]4*/
[1853]5
[5261]6#ifndef _SHADER_H
7#define _SHADER_H
[1853]8
[3543]9#include "base_object.h"
[5261]10#include "glincl.h"
[7164]11#include <vector>
[1853]12
[5261]13
[8037]14// FORWARD DECLARATION
15
16
17
18//! A class for ...
19class Shader : public BaseObject
[5261]20{
[9715]21  ObjectListDeclaration(Shader);
[8037]22public:
23  class Uniform
24  {
25  public:
26    Uniform(const Shader* shader, const std::string& location) { this->uniform = glGetUniformLocationARB(shader->getProgram(), location.c_str()) ; }
27    Uniform(const Shader& shader, const std::string& location) { this->uniform = glGetUniformLocation(shader.getProgram(), location.c_str()) ; };
28    Uniform(GLhandleARB shaderProgram, const std::string& location) { this->uniform = glGetUniformLocation(shaderProgram, location.c_str()) ; };
[5261]29
[8037]30    void set(float v0) const { glUniform1f(this->uniform, v0); }
31    void set(float v0, float v1) const { glUniform2f(this->uniform, v0, v1); }
32    void set(float v0, float v1, float v2) const { glUniform3f(this->uniform, v0, v1, v2); }
33    void set(float v0, float v1, float v2, float v3) const { glUniform4f(this->uniform, v0, v1, v2, v3); }
[5261]34
[8037]35    void set(int v0) const { glUniform1i(this->uniform, v0); }
36    void set(int v0, int v1) const { glUniform2i(this->uniform, v0, v1); }
37    void set(int v0, int v1, int v2) const { glUniform3i(this->uniform, v0, v1, v2); }
38    void set(int v0, int v1, int v2, int v3) const { glUniform4i(this->uniform, v0, v1, v2, v3); }
[3543]39
[8037]40    void setV(unsigned int count, float* vv) const
41    {
42      switch (count)
43      {
44        case 1:
[9685]45        glUniform1fv(this->uniform, 1, vv);
46        break;
[8037]47        case 2:
[9685]48        glUniform2fv(this->uniform, 2, vv);
49        break;
[8037]50        case 3:
[9685]51        glUniform3fv(this->uniform, 3, vv);
52        break;
[8037]53        case 4:
[9685]54        glUniform4fv(this->uniform, 4, vv);
55        break;
[8037]56      }
57    }
58    void setV(unsigned int count, int* vv) const
59    {
60      switch (count)
61      {
62        case 1:
[9685]63        glUniform1iv(this->uniform, 1, vv);
64        break;
[8037]65        case 2:
[9685]66        glUniform2iv(this->uniform, 2, vv);
67        break;
[8037]68        case 3:
[9685]69        glUniform3iv(this->uniform, 3, vv);
70        break;
[8037]71        case 4:
[9685]72        glUniform4iv(this->uniform, 4, vv);
73        break;
[8037]74      }
75    }
[9685]76  private:
[8037]77    GLint uniform;
78  };
[3543]79
[8037]80  typedef enum
81  {
82    None       = 0,
83    Fragment   = 1,
84    Vertex     = 2,
[1853]85
[8037]86    Program    = 4,
87  }
88  Type;
89
90public:
[7221]91  Shader(const std::string& vertexShaderFile = "", const std::string& fragmentShaderFile = "");
[5261]92  virtual ~Shader();
[7221]93  static Shader* getShader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile);
[5323]94  static bool unload(Shader* shader);
[1853]95
[8037]96
97  static bool checkShaderAbility();
[5266]98  void activateShader();
[8255]99  void bindShader(const char* name, const float* value, size_t size);
[5266]100  static void deactivateShader();
[3245]101
[9685]102Shader::Uniform getUniform(const std::string& location) { return Shader::Uniform(this, location); }
[5266]103
[8037]104  bool loadShaderProgramm(Shader::Type type, const std::string& fileName);
105  void deleteProgram(Shader::Type type);
[5317]106
[8037]107  void linkShaderProgram();
108
109
110  bool readShader(const std::string& fileName, std::string& output);
111
112
113inline static bool shaderActive() { return (Shader::storedShader != NULL)? true : false; };
[5323]114  inline static Shader* getActiveShader() { return Shader::storedShader; };
[5318]115  inline static void suspendShader() { Shader* currShader = storedShader; if (storedShader!= NULL) { Shader::deactivateShader(); Shader::storedShader = currShader;} };
[5317]116  inline static void restoreShader() { if (storedShader != NULL) storedShader->activateShader(); storedShader = NULL; };
117
118
[8037]119
120
[9685]121GLhandleARB getProgram() const { return this->shaderProgram; }
[8037]122  GLhandleARB getVertexS() const { return this->vertexShader; }
123  GLhandleARB getFragmentS() const { return this->fragmentShader; }
124
[5262]125  void debug() const;
126
[8037]127  static void printError(GLhandleARB program);
[5262]128
[5317]129
[8037]130private:
131  std::string            fragmentShaderFile;
132  std::string            vertexShaderFile;
133
134  GLhandleARB            shaderProgram;
135
136  GLhandleARB            vertexShader;
137  GLhandleARB            fragmentShader;
138
139
140  static Shader*         storedShader;
[1853]141};
142
[5261]143#endif /* _SHADER_H */
Note: See TracBrowser for help on using the repository browser.