Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9818 in orxonox.OLD for branches/new_class_id/src/lib


Ignore:
Timestamp:
Sep 25, 2006, 12:10:07 PM (18 years ago)
Author:
bensch
Message:

Switching to new Shader layout, with Shader and ShaderData. Shaders do not render for the time being

Location:
branches/new_class_id/src/lib/graphics
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/new_class_id/src/lib/graphics/Makefile.am

    r9806 r9818  
    99                light.cc \
    1010                shader.cc \
     11                shader_data.cc \
    1112                \
    1213                render2D/render_2d.cc \
  • branches/new_class_id/src/lib/graphics/shader.cc

    r9806 r9818  
    7474
    7575
    76 
     76Shader::Shader()
     77  : data(new ShaderData)
     78{
     79 
     80}
    7781
    7882/**
     
    8084*/
    8185Shader::Shader (const std::string& vertexShaderFile, const std::string& fragmentShaderFile)
     86  : data(new ShaderData)
    8287{
    83   this->registerObject(this, Shader::_objectList);
    84   this->shaderProgram = 0;
    85   this->vertexShader = 0;
    86   this->fragmentShader = 0;
     88  if (!Shader::checkShaderAbility())
     89    PRINTF(2)("Your system does not support shaders\n");
     90  else
     91    this->load(vertexShaderFile, fragmentShaderFile);
    8792
    88   if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100)
    89   {
    90     this->shaderProgram = glCreateProgramObjectARB();
    91 
    92     if (!vertexShaderFile.empty())
    93       this->loadShaderProgramm(Shader::Vertex, vertexShaderFile);
    94     if (!fragmentShaderFile.empty())
    95       this->loadShaderProgramm(Shader::Fragment, fragmentShaderFile);
    96 
    97     this->linkShaderProgram();
    98 
    99   }
    100   else
    101   {
    102     PRINTF(2)("Shaders are not supported on your hardware\n");
    103   }
    10493}
    10594
     95const Shader* Shader::storedShader = NULL;
    10696
    107 /**
    108  * standard deconstructor
    109  */
    110 Shader::~Shader ()
     97void Shader::activateShader() const
    11198{
    112   if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB))
    113     Shader::deactivateShader();
    114 
    115   // delete what has to be deleted here
    116   this->deleteProgram(Shader::Vertex);
    117   this->deleteProgram(Shader::Fragment);
    118 
    119   if (this->fragmentShader != 0)
     99  if (likely (this->getProgram() != 0))
    120100  {
    121     glDetachObjectARB(this->shaderProgram, this->fragmentShader);
    122     glDeleteObjectARB(this->fragmentShader);
    123   }
    124   if (this->vertexShader != 0)
    125   {
    126     glDetachObjectARB(this->shaderProgram, this->vertexShader);
    127     glDeleteObjectARB(this->vertexShader);
    128   }
    129   if (this->shaderProgram != 0)
    130   {
    131     GLint status = 0;
    132     //glLinkProgramARB(this->shaderProgram);
    133     glDeleteObjectARB(this->shaderProgram);
    134     // link error checking
    135     glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_DELETE_STATUS_ARB, &status);
    136     if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
    137       this->printError(this->shaderProgram);
    138   }
    139 }
    140 
    141 Shader* Shader::storedShader = NULL;
    142 
    143 
    144 bool Shader::loadShaderProgramm(Shader::Type type, const std::string& fileName)
    145 {
    146   GLhandleARB shader = 0;
    147 
    148   if (type != Shader::Vertex && type != Shader::Fragment)
    149     return false;
    150   this->deleteProgram(type);
    151 
    152 
    153   std::string program;
    154   if (!readShader(fileName, program))
    155     return false;
    156 
    157   if (type == Shader::Vertex && GLEW_ARB_vertex_shader)
    158   {
    159     this->vertexShaderFile = fileName;
    160 
    161     shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
    162   }
    163 
    164   if (type == Shader::Fragment && GLEW_ARB_fragment_shader)
    165   {
    166     this->fragmentShaderFile = fileName;
    167 
    168     shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
    169   }
    170 
    171   if (shader != 0)
    172   {
    173     GLint status = 0;
    174     const char* prog = program.c_str();
    175 
    176     glShaderSourceARB(shader, 1, &prog, NULL);
    177     glCompileShaderARB(shader);
    178     // checking on error.
    179     glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
    180     if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
    181       this->printError(shader);
    182     else
    183       glAttachObjectARB(this->shaderProgram, shader);
    184   }
    185   return true;
    186 }
    187 
    188 
    189 void Shader::linkShaderProgram()
    190 {
    191   GLint status = 0;
    192 
    193   glLinkProgramARB(this->shaderProgram);
    194   // link error checking
    195   glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status);
    196   if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
    197     this->printError(this->shaderProgram);
    198 }
    199 
    200 
    201 bool Shader::readShader(const std::string& fileName, std::string& output)
    202 {
    203   char lineBuffer[PARSELINELENGTH];
    204 
    205   std::ifstream shader;
    206   shader.open(fileName.c_str());
    207   if (!shader.is_open())
    208     return false;
    209 
    210 
    211   while (!shader.eof())
    212   {
    213     shader.getline(lineBuffer, PARSELINELENGTH);
    214     output += lineBuffer;
    215     output += "\n";
    216   }
    217 
    218 
    219   shader.close();
    220   return true;
    221 }
    222 
    223 
    224 
    225 void Shader::activateShader()
    226 {
    227   if (likely (this->shaderProgram != 0))
    228   {
    229     glUseProgramObjectARB(this->shaderProgram);
     101    glUseProgramObjectARB(this->getProgram());
    230102    Shader::storedShader = this;
    231103  }
    232104}
    233 
    234 void Shader::bindShader(const char* name, const float* value, size_t size)
    235 {
    236   if (likely (this->shaderProgram != 0))
    237   {
    238     glUseProgramObjectARB(this->shaderProgram);
    239 
    240     unsigned int location = glGetUniformLocationARB(this->shaderProgram, name);
    241     /* This is EXPENSIVE and should be avoided. */
    242 
    243     if      (size == 1)  glUniform1fvARB(location, 1, value);
    244     else if (size == 2)  glUniform2fvARB(location, 1, value);
    245     else if (size == 3)  glUniform3fvARB(location, 1, value);
    246     else if (size == 4)  glUniform4fvARB(location, 1, value);
    247     else if (size == 9)  glUniformMatrix3fvARB(location, 1, false, value);
    248     else if (size == 16) glUniformMatrix4fvARB(location, 1, false, value);
    249 
    250   }
    251 }
    252 
    253105void Shader::deactivateShader()
    254106{
     
    256108    glUseProgramObjectARB(0);
    257109  Shader::storedShader = NULL;
    258 }
    259 
    260 
    261 void Shader::deleteProgram(Shader::Type type)
    262 {
    263   GLint status = 0;
    264   if (type == Shader::Vertex && this->vertexShader != 0)
    265   {
    266     this->vertexShaderFile = "";
    267     glDetachObjectARB(this->shaderProgram, this->vertexShader);
    268     glDeleteObjectARB(this->vertexShader);
    269     glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
    270     if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
    271       Shader::printError(this->vertexShader);
    272     this->vertexShader = 0;
    273   }
    274   else if (type == Shader::Fragment && this->fragmentShader != 0)
    275   {
    276     this->fragmentShaderFile = "";
    277     glDetachObjectARB(this->shaderProgram, this->fragmentShader);
    278     glDeleteObjectARB(this->fragmentShader);
    279     glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
    280     if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
    281       Shader::printError(this->fragmentShader);
    282     this->fragmentShader = 0;
    283   }
    284   else
    285     return;
    286 }
    287 
    288 
    289 void Shader::printError(GLhandleARB program)
    290 {
    291   if (program == 0)
    292     return;
    293 
    294   int infologLength = 0;
    295   int charsWritten  = 0;
    296   char *infoLog;
    297 
    298   glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB,
    299                             &infologLength);
    300 
    301   if (infologLength > 0)
    302   {
    303     infoLog = new char[infologLength+1];
    304     glGetInfoLogARB(program, infologLength, &charsWritten, infoLog);
    305     printf("%s\n", infoLog);
    306     delete[] infoLog;
    307   }
    308110}
    309111
     
    319121}
    320122
    321 void Shader::debug() const
    322 {
    323   PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram);
    324   if (this->vertexShader != 0)
    325   {
    326     /*    PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile);
    327         if (this->vertexShaderSource != NULL)
    328           for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++)
    329             PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i));
    330       }
    331       if (this->fragmentShader != 0)
    332       {
    333         PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile);
    334         if (this->fragmentShaderSource != NULL)
    335           for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++)
    336             PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/
    337   }
    338 }
    339123
  • branches/new_class_id/src/lib/graphics/shader.h

    r9806 r9818  
    88
    99#include "base_object.h"
    10 #include "glincl.h"
    11 #include <vector>
    12 
     10#include "shader_data.h"
    1311
    1412// FORWARD DECLARATION
     
    2422  {
    2523  public:
    26     Uniform(const Shader* shader, const std::string& location) { this->uniform = glGetUniformLocationARB(shader->getProgram(), location.c_str()) ; }
    2724    Uniform(const Shader& shader, const std::string& location) { this->uniform = glGetUniformLocation(shader.getProgram(), location.c_str()) ; };
    2825    Uniform(GLhandleARB shaderProgram, const std::string& location) { this->uniform = glGetUniformLocation(shaderProgram, location.c_str()) ; };
     
    4542  };
    4643
    47   //! The Type of Shader.
    48   typedef enum
    49   {
    50     None       = 0,     //!< No Type at all
    51     Fragment   = 1,     //!< Fragment Shader.
    52     Vertex     = 2,     //!< Vertex Shader.
    53     Program    = 4,     //!< Compiled Shader Programm.
    54   }  Type;
    5544
    5645public:
    57   Shader(const std::string& vertexShaderFile = "", const std::string& fragmentShaderFile = "");
    58   virtual ~Shader();
     46  Shader();
     47  Shader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile = "");
     48
     49  Shader& operator=(const Shader& shader) { this->data = shader.data; return *this; };
     50  const ShaderData::Pointer& dataPointer() const { return data; };
     51  void acquireData(const ShaderData::Pointer& pointer) { this->data = pointer; };
     52
     53  bool load(const std::string& vertexShaderFile, const std::string& fragmentShaderFile = "")
     54    { return this->data->load(vertexShaderFile, fragmentShaderFile); }
    5955
    6056
    61   Shader::Uniform getUniform(const std::string& location) { return Shader::Uniform(this, location); }
     57  Shader::Uniform getUniform(const std::string& location) { return Shader::Uniform(*this, location); }
    6258
    63   GLhandleARB getProgram() const { return this->shaderProgram; }
    64   GLhandleARB getVertexS() const { return this->vertexShader; }
    65   GLhandleARB getFragmentS() const { return this->fragmentShader; }
     59  GLhandleARB getProgram() const { return this->data->getProgram(); }
     60  GLhandleARB getVertexS() const { return this->data->getVertexS(); }
     61  GLhandleARB getFragmentS() const { return this->data->getFragmentS(); }
     62
     63  void activateShader() const;
     64  static void deactivateShader();
     65
     66  void debug() const { this->data->debug(); };
     67
    6668
    6769  static bool checkShaderAbility();
    68   void activateShader();
    69   static void deactivateShader();
    70 
    71   void debug() const;
    7270
    7371  inline static bool isShaderActive() { return (Shader::storedShader != NULL) ? true : false; };
    74   inline static Shader* getActiveShader() { return Shader::storedShader; };
    75   inline static void suspendShader() { Shader* currShader = storedShader; if (storedShader!= NULL) { Shader::deactivateShader(); Shader::storedShader = currShader;} };
     72  inline static const Shader* getActiveShader() { return Shader::storedShader; };
     73
     74  inline static void suspendShader() { const Shader* currShader = storedShader; if (storedShader!= NULL) { Shader::deactivateShader(); Shader::storedShader = currShader;} };
    7675  inline static void restoreShader() { if (storedShader != NULL) storedShader->activateShader(); storedShader = NULL; };
    7776
    78   static void printError(GLhandleARB program);
     77private:
     78  ShaderData::Pointer    data;
    7979
    80 private:
    81   void bindShader(const char* name, const float* value, size_t size);
    82   void linkShaderProgram();
    83   bool readShader(const std::string& fileName, std::string& output);
    84   bool loadShaderProgramm(Shader::Type type, const std::string& fileName);
    85   void deleteProgram(Shader::Type type);
    86 
    87 private:
    88   std::string            fragmentShaderFile;
    89   std::string            vertexShaderFile;
    90 
    91   GLhandleARB            shaderProgram;
    92 
    93   GLhandleARB            vertexShader;
    94   GLhandleARB            fragmentShader;
    95 
    96 
    97   static Shader*         storedShader;
     80  static const Shader*         storedShader;
    9881};
    9982
  • branches/new_class_id/src/lib/graphics/shader_data.cc

    r9806 r9818  
    3838 * standard constructor
    3939*/
    40 Shader::Shader (const std::string& vertexShaderFile, const std::string& fragmentShaderFile)
    41 {
    42   this->registerObject(this, Shader::_objectList);
     40ShaderData::ShaderData ()
     41{
     42  this->registerObject(this, ShaderData::_objectList);
    4343  this->shaderProgram = 0;
    4444  this->vertexShader = 0;
    4545  this->fragmentShader = 0;
    46 
     46}
     47
     48
     49/// TODO fix that shaders are unloaded first. then loaded
     50bool ShaderData::load(const std::string& vertexShaderFile, const std::string& fragmentShaderFile)
     51{
    4752  if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100)
    4853  {
     
    5055
    5156    if (!vertexShaderFile.empty())
    52       this->loadShaderProgramm(Shader::Vertex, vertexShaderFile);
     57      this->loadShaderProgramm(ShaderData::Vertex, vertexShaderFile);
    5358    if (!fragmentShaderFile.empty())
    54       this->loadShaderProgramm(Shader::Fragment, fragmentShaderFile);
     59      this->loadShaderProgramm(ShaderData::Fragment, fragmentShaderFile);
    5560
    5661    this->linkShaderProgram();
     
    6267  }
    6368}
    64 
    6569
    6670/**
    6771 * standard deconstructor
    6872 */
    69 Shader::~Shader ()
     73ShaderData::~ShaderData ()
    7074{
    7175  if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB))
    72     Shader::deactivateShader();
     76    glUseProgramObjectARB(0);
    7377
    7478  // delete what has to be deleted here
    75   this->deleteProgram(Shader::Vertex);
    76   this->deleteProgram(Shader::Fragment);
     79  this->deleteProgram(ShaderData::Vertex);
     80  this->deleteProgram(ShaderData::Fragment);
    7781
    7882  if (this->fragmentShader != 0)
     
    98102}
    99103
    100 Shader* Shader::storedShader = NULL;
    101 
    102 
    103 bool Shader::loadShaderProgramm(Shader::Type type, const std::string& fileName)
     104bool ShaderData::loadShaderProgramm(ShaderData::Type type, const std::string& fileName)
    104105{
    105106  GLhandleARB shader = 0;
    106107
    107   if (type != Shader::Vertex && type != Shader::Fragment)
     108  if (type != ShaderData::Vertex && type != ShaderData::Fragment)
    108109    return false;
    109110  this->deleteProgram(type);
     
    114115    return false;
    115116
    116   if (type == Shader::Vertex && GLEW_ARB_vertex_shader)
     117  if (type == ShaderData::Vertex && GLEW_ARB_vertex_shader)
    117118  {
    118119    this->vertexShaderFile = fileName;
     
    121122  }
    122123
    123   if (type == Shader::Fragment && GLEW_ARB_fragment_shader)
     124  if (type == ShaderData::Fragment && GLEW_ARB_fragment_shader)
    124125  {
    125126    this->fragmentShaderFile = fileName;
     
    146147
    147148
    148 void Shader::linkShaderProgram()
     149void ShaderData::linkShaderProgram()
    149150{
    150151  GLint status = 0;
     
    158159
    159160
    160 bool Shader::readShader(const std::string& fileName, std::string& output)
     161bool ShaderData::readShader(const std::string& fileName, std::string& output)
    161162{
    162163  char lineBuffer[PARSELINELENGTH];
     
    180181}
    181182
    182 
    183 
    184 void Shader::activateShader()
    185 {
    186   if (likely (this->shaderProgram != 0))
    187   {
    188     glUseProgramObjectARB(this->shaderProgram);
    189     Shader::storedShader = this;
    190   }
    191 }
    192 
    193 void Shader::bindShader(const char* name, const float* value, size_t size)
     183void ShaderData::bindShader(const char* name, const float* value, size_t size)
    194184{
    195185  if (likely (this->shaderProgram != 0))
     
    210200}
    211201
    212 void Shader::deactivateShader()
    213 {
    214   if (storedShader != NULL)
    215     glUseProgramObjectARB(0);
    216   Shader::storedShader = NULL;
    217 }
    218 
    219 
    220 void Shader::deleteProgram(Shader::Type type)
     202void ShaderData::deleteProgram(ShaderData::Type type)
    221203{
    222204  GLint status = 0;
    223   if (type == Shader::Vertex && this->vertexShader != 0)
     205  if (type == ShaderData::Vertex && this->vertexShader != 0)
    224206  {
    225207    this->vertexShaderFile = "";
     
    228210    glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
    229211    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
    230       Shader::printError(this->vertexShader);
     212      ShaderData::printError(this->vertexShader);
    231213    this->vertexShader = 0;
    232214  }
    233   else if (type == Shader::Fragment && this->fragmentShader != 0)
     215  else if (type == ShaderData::Fragment && this->fragmentShader != 0)
    234216  {
    235217    this->fragmentShaderFile = "";
     
    238220    glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
    239221    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
    240       Shader::printError(this->fragmentShader);
     222      ShaderData::printError(this->fragmentShader);
    241223    this->fragmentShader = 0;
    242224  }
     
    246228
    247229
    248 void Shader::printError(GLhandleARB program)
     230void ShaderData::printError(GLhandleARB program)
    249231{
    250232  if (program == 0)
     
    267249}
    268250
    269 bool Shader::checkShaderAbility()
    270 {
    271   if (GLEW_ARB_shader_objects &&
    272       GLEW_ARB_shading_language_100 &&
    273       GLEW_ARB_vertex_shader &&
    274       GLEW_ARB_fragment_shader)
    275     return true;
    276   else
    277     return false;
    278 }
    279 
    280 void Shader::debug() const
    281 {
    282   PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram);
     251void ShaderData::debug() const
     252{
     253  PRINT(3)("ShaderData info: (SHADER: %d)\n", this->shaderProgram);
    283254  if (this->vertexShader != 0)
    284255  {
  • branches/new_class_id/src/lib/graphics/shader_data.h

    r9806 r9818  
    99#include "base_object.h"
    1010
    11 #include "shader_types.h"
     11#include "glincl.h"
    1212#include <vector>
    13 
     13#include "count_pointer.h"
    1414
    1515// FORWARD DECLARATION
     
    2121{
    2222  ObjectListDeclaration(ShaderData);
     23  //! The Type of Shader.
     24  typedef enum  {
     25    None       = 0,     //!< No Type at all
     26    Fragment   = 1,     //!< Fragment Shader.
     27    Vertex     = 2,     //!< Vertex Shader.
     28    Program    = 4,     //!< Compiled Shader Programm.
     29  }  Type;
     30
     31 public:
     32  typedef CountPointer<ShaderData> Pointer;
    2333public:
    24   ShaderData(const std::string& vertexShaderFile = "", const std::string& fragmentShaderFile = "");
     34  ShaderData();
    2535  virtual ~ShaderData();
    2636
    27   Shaders::Uniform getUniform(const std::string& location) { return Shader::Uniform(this, location); }
     37  bool load(const std::string& vertexShaderFile = "", const std::string& fragmentShaderFile = "");
     38
    2839
    2940  GLhandleARB getProgram() const { return this->shaderProgram; }
     
    3849  void linkShaderProgram();
    3950  bool readShader(const std::string& fileName, std::string& output);
    40   bool loadShaderProgramm(Shader::Type type, const std::string& fileName);
    41   void deleteProgram(Shader::Type type);
     51  bool loadShaderProgramm(ShaderData::Type type, const std::string& fileName);
     52  void deleteProgram(ShaderData::Type type);
     53
     54  static void printError(GLhandleARB program);
    4255
    4356private:
     57
    4458  std::string            fragmentShaderFile;
    4559  std::string            vertexShaderFile;
Note: See TracChangeset for help on using the changeset viewer.