Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8037 in orxonox.OLD for trunk/src/lib/graphics/shader.cc


Ignore:
Timestamp:
May 31, 2006, 4:52:34 PM (18 years ago)
Author:
bensch
Message:

trunk: merged the water back
merged with command
svn merge -r7798:HEAD https://svn.orxonox.net/orxonox/branches/water .

conflicts are all resolved

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/shader.cc

    r7221 r8037  
    2020#include "stdlibincl.h"
    2121#include "compiler.h"
    22 #include <stdio.h>
     22//#include <stdio.h>
     23#include <fstream>
     24
    2325#include "debug.h"
    2426
     
    2628
    2729
    28 #ifndef PARSELINELENGHT
    29 #define PARSELINELENGHT     512       //!< how many chars to read at once
     30#ifndef PARSELINELENGTH
     31#define PARSELINELENGTH     512       //!< how many chars to read at once
    3032#endif
    3133
     
    4042   this->setClassID(CL_SHADER, "Shader");
    4143
    42    this->fragmentShaderFile = "";
    43    this->vertexShaderFile = "";
    4444   this->shaderProgram = 0;
    4545   this->vertexShader = 0;
     
    4848   if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100)
    4949     {
    50        GLint status = 0;
    51 
    5250       this->shaderProgram = glCreateProgramObjectARB();
    5351
    5452       if (!vertexShaderFile.empty())
    55          this->loadShaderProgramm(SHADER_VERTEX, vertexShaderFile);
     53         this->loadShaderProgramm(Shader::Vertex, vertexShaderFile);
    5654       if (!fragmentShaderFile.empty())
    57          this->loadShaderProgramm(SHADER_FRAGMENT, fragmentShaderFile);
    58        glLinkProgramARB(this->shaderProgram);
    59        // link error checking
    60        glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status);
    61        if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
    62          this->printError(this->shaderProgram);
     55         this->loadShaderProgramm(Shader::Fragment, fragmentShaderFile);
     56
     57       this->linkShaderProgram();
     58
    6359    }
    6460   else
     
    7874
    7975  // delete what has to be deleted here
    80   this->deleteProgram(SHADER_VERTEX);
    81   this->deleteProgram(SHADER_FRAGMENT);
     76  this->deleteProgram(Shader::Vertex);
     77  this->deleteProgram(Shader::Fragment);
    8278
    8379  if (this->fragmentShader != 0)
     
    116112
    117113
    118 bool Shader::loadShaderProgramm(SHADER_TYPE type, const std::string& fileName)
     114bool Shader::loadShaderProgramm(Shader::Type type, const std::string& fileName)
    119115{
    120116  GLhandleARB shader = 0;
    121117
    122   if (type != SHADER_VERTEX && type != SHADER_FRAGMENT)
     118  if (type != Shader::Vertex && type != Shader::Fragment)
    123119    return false;
    124120  this->deleteProgram(type);
    125121
    126122
    127   std::vector<char*>* program = fileReadArray(fileName);
    128 
    129   if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader)
     123  std::string program;
     124  if (!readShader(fileName, program))
     125    return false;
     126
     127  if (type == Shader::Vertex && GLEW_ARB_vertex_shader)
    130128  {
    131129    this->vertexShaderFile = fileName;
     
    134132  }
    135133
    136   if (type == SHADER_FRAGMENT && GLEW_ARB_fragment_shader)
     134  if (type == Shader::Fragment && GLEW_ARB_fragment_shader)
    137135  {
    138136    this->fragmentShaderFile = fileName;
     
    144142  {
    145143    GLint status = 0;
    146     /// FIXME do it back
    147     //    glShaderSourceARB(shader, program->size(), (const std::string&)&(*program)[0], NULL);
     144    const char* prog = program.c_str();
     145
     146    glShaderSourceARB(shader, 1, &prog, NULL);
    148147    glCompileShaderARB(shader);
    149148    // checking on error.
     
    154153      glAttachObjectARB(this->shaderProgram, shader);
    155154  }
    156   for (unsigned int i=0; i< program->size(); i++)
    157     delete[] (*program)[i];
    158   delete program;
    159 }
    160 
    161 char* Shader::fileRead(const std::string& fileName)
    162 {
    163   FILE* fileHandle;
    164   char* content = NULL;
    165 
    166   int count = 0;
    167 
    168   if (fileName.empty())
    169     return NULL;
    170 
    171   fileHandle = fopen(fileName.c_str(), "rt");
    172 
    173   if (fileHandle == NULL)
    174     return NULL;
    175   fseek(fileHandle, 0, SEEK_END);
    176   count = ftell(fileHandle);
    177   rewind(fileHandle);
    178   if (count > 0) {
    179      content = new char[count+1];
    180      count = fread(content, sizeof(char), count, fileHandle);
    181      content[count] = '\0';
    182    }
    183    fclose(fileHandle);
    184  return content;
    185 }
    186 
    187 
    188 std::vector<char*>* Shader::fileReadArray(const std::string& fileName)
    189 {
    190   FILE*    stream;           //< The stream we use to read the file.
    191 
    192   if( (stream = fopen (fileName.c_str(), "rt")) == NULL)
    193   {
    194     PRINTF(1)("Shader could not open %s\n", fileName.c_str());
    195     return NULL;
    196   }
    197   std::vector<char*>* file = new std::vector<char*>;
    198 
    199   char lineBuffer[PARSELINELENGHT];
    200   char* addString;
    201   while(fgets (lineBuffer, PARSELINELENGHT, stream) != NULL)
    202   {
    203     addString = new char[strlen(lineBuffer)+1];
    204     strcpy(addString, lineBuffer);
    205     file->push_back(addString);
    206   }
    207   fclose(stream);
    208   return file;
     155}
     156
     157
     158void Shader::linkShaderProgram()
     159{
     160  GLint status = 0;
     161
     162  glLinkProgramARB(this->shaderProgram);
     163       // link error checking
     164  glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status);
     165  if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
     166    this->printError(this->shaderProgram);
     167}
     168
     169
     170bool Shader::readShader(const std::string& fileName, std::string& output)
     171{
     172  char lineBuffer[PARSELINELENGTH];
     173
     174  std::ifstream shader;
     175  shader.open(fileName.c_str());
     176  if (!shader.is_open())
     177    return false;
     178
     179
     180  while (!shader.eof())
     181  {
     182    shader.getline(lineBuffer, PARSELINELENGTH);
     183    output += lineBuffer;
     184    output += "\n";
     185  }
     186
     187
     188  shader.close();
     189  return true;
    209190}
    210191
     
    228209
    229210
    230 void Shader::deleteProgram(SHADER_TYPE type)
     211void Shader::deleteProgram(Shader::Type type)
    231212{
    232213  GLint status = 0;
    233   if (type == SHADER_VERTEX && this->vertexShader != 0)
     214  if (type == Shader::Vertex && this->vertexShader != 0)
    234215  {
    235216    this->vertexShaderFile = "";
     
    241222    this->vertexShader = 0;
    242223  }
    243   else if (type == SHADER_FRAGMENT && this->fragmentShader != 0)
     224  else if (type == Shader::Fragment && this->fragmentShader != 0)
    244225  {
    245226    this->fragmentShaderFile = "";
Note: See TracChangeset for help on using the changeset viewer.