Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5266 in orxonox.OLD for trunk/src/lib


Ignore:
Timestamp:
Sep 27, 2005, 4:10:26 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: Shaders now work :)

Location:
trunk/src/lib/graphics
Files:
3 edited

Legend:

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

    r5265 r5266  
    2929
    3030using namespace std;
    31 
    32 
    33 #include "shader.h"
    3431
    3532/**
     
    274271  PRINTF(4)("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
    275272
    276   Shader* shader = new Shader("test.txt", "shader.cg");
    277 //  delete shader;
    278   shader->activateShader();
    279 //  exit(-1);
    280 
    281273}
    282274
     
    301293      //    return -1;
    302294    }
     295    glMatrixMode(GL_PROJECTION_MATRIX);
     296    glLoadIdentity();
    303297    glViewport(0,0,width,height);                                                   // Reset The Current Viewport
    304298}
  • trunk/src/lib/graphics/shader.cc

    r5264 r5266  
    1818#include "shader.h"
    1919
    20 
    21 #include "array.h"
    2220#include "stdlibincl.h"
    2321#include <stdio.h>
     
    4139   this->fragmentShaderFile = NULL;
    4240   this->vertexShaderFile = NULL;
    43    this->fragmentShaderSource = NULL;
    44    this->vertexShaderSource = NULL;
    45    this->fragmentShaderLengths = NULL;
    46    this->vertexShaderLenghts = NULL;
    4741   this->shaderProgram = 0;
    4842   this->vertexShader = 0;
     
    5044
    5145   if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100)
    52      this->shaderProgram = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
     46     this->shaderProgram = glCreateProgramObjectARB();
    5347
    5448   if (vertexShaderFile != NULL)
     
    8175  this->deleteProgram(type);
    8276
    83   FILE*    stream;           //< The stream we use to read the file.
    8477
    85   if( (stream = fopen (fileName, "r")) == NULL)
    86   {
    87     PRINTF(1)("Shader could not open %s\n", fileName);
    88     return false;
    89   }
    90   Array<char*>* program = new Array<char*>;
    91   Array<int>* lengths = new Array<int>;
    92   if (type == SHADER_VERTEX)
     78  const char* program = fileRead(fileName);
     79
     80  if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader)
    9381  {
    9482    this->vertexShaderFile = new char[strlen(fileName)+1];
    9583    strcpy(this->vertexShaderFile, fileName);
    96     this->vertexShaderSource = program;
    97     this->vertexShaderLenghts = lengths;
    98   }
    99   else
    100   {
    101     this->fragmentShaderFile = new char[strlen(fileName)+1];
    102     strcpy(this->fragmentShaderFile, fileName);
    103     this->fragmentShaderSource = program;
    104     this->fragmentShaderLengths = lengths;
    105   }
    10684
    107   char lineBuffer[PARSELINELENGHT];
    108   char* addString;
    109   while( !feof( stream))
    110   {
    111       // get next line
    112     fgets (lineBuffer, PARSELINELENGHT, stream);
    113 //     if (strchr(lineBuffer, '\n'))
    114 //     {
    115 //       addString = new char[strlen(lineBuffer)];
    116 //       strncpy(addString, lineBuffer, strlen (lineBuffer)-1);
    117 //       addString[strlen(lineBuffer)-1] ='\0';
    118 //     }
    119 //     else
    120     {
    121       addString = new char[strlen(lineBuffer)+1];
    122       strcpy(addString, lineBuffer);
    123     }
    124     program->addEntry(addString);
    125     lengths->addEntry(strlen(addString));
    126   }
    127   fclose(stream);
    128 
    129   program->finalizeArray();
    130   lengths->finalizeArray();
    131 
    132   if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader)
    133   {
    13485    this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
    135     glShaderSourceARB(this->vertexShader, program->getCount(), (const GLcharARB**)program->getArray(), lengths->getArray());
     86    glShaderSourceARB(this->vertexShader, 1, (const GLcharARB**)&program, NULL);
    13687    glCompileShaderARB(this->vertexShader);
    13788    glAttachObjectARB(this->shaderProgram, this->vertexShader);
     
    14192  if (type == SHADER_FRAGMENT && GLEW_ARB_fragment_shader)
    14293  {
     94    this->fragmentShaderFile = new char[strlen(fileName)+1];
     95    strcpy(this->fragmentShaderFile, fileName);
     96
    14397    this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
    144     glShaderSourceARB(this->fragmentShader, program->getCount(), (const GLcharARB**)program->getArray(), lengths->getArray());
     98    glShaderSourceARB(this->fragmentShader, 1, (const GLcharARB**)&program, NULL);
    14599    glCompileShaderARB(this->fragmentShader);
    146100    glAttachObjectARB(this->shaderProgram, this->fragmentShader);
    147101    this->printError(this->fragmentShader);
    148102  }
     103
     104  delete[] program;
    149105}
    150106
    151 bool Shader::activateShader()
     107
     108char* Shader::fileRead(const char* fileName)
     109{
     110  FILE* fileHandle;
     111  char* content = NULL;
     112
     113  int count = 0;
     114
     115  if (fileName != NULL) {
     116    fileHandle = fopen(fileName, "rt");
     117
     118    if (fileHandle != NULL) {
     119
     120      fseek(fileHandle, 0, SEEK_END);
     121      count = ftell(fileHandle);
     122      rewind(fileHandle);
     123
     124      if (count > 0) {
     125        content = new char[count+1];
     126        count = fread(content, sizeof(char), count, fileHandle);
     127        content[count] = '\0';
     128      }
     129      fclose(fileHandle);
     130    }
     131  }
     132  return content;
     133}
     134
     135void Shader::activateShader()
    152136{
    153137  glUseProgramObjectARB(this->shaderProgram);
     138}
     139
     140void Shader::deactivateShader()
     141{
     142  glUseProgramObjectARB(0);
    154143}
    155144
     
    157146void Shader::deleteProgram(SHADER_TYPE type)
    158147{
    159   Array<char*>* deleteArray = NULL;
    160148  if (type == SHADER_VERTEX)
    161149  {
    162     deleteArray = this->vertexShaderSource;
    163     this->vertexShaderSource = NULL;
    164     delete this->vertexShaderLenghts;
    165     this->vertexShaderLenghts = NULL;
    166150    delete[] this->vertexShaderFile;
    167151    this->vertexShaderFile = NULL;
     
    171155  else if (type == SHADER_FRAGMENT)
    172156  {
    173     deleteArray = this->fragmentShaderSource;
    174     delete this->fragmentShaderLengths;
    175     this->fragmentShaderLengths = NULL;
    176     this->fragmentShaderSource = NULL;
    177157    delete[] this->fragmentShaderFile;
    178158    this->fragmentShaderFile = NULL;
     
    182162  else
    183163    return;
    184 
    185   if (deleteArray == NULL)
    186     return;
    187   else
    188   {
    189     deleteArray->finalizeArray();
    190     for (unsigned int i = 0; i < deleteArray->getCount(); i++)
    191     {
    192       delete[] deleteArray->getEntry(i);
    193     }
    194     delete deleteArray;
    195   }
    196164}
    197165
     
    214182  if (this->vertexShader != 0)
    215183  {
    216     PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile);
     184/*    PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile);
    217185    if (this->vertexShaderSource != NULL)
    218186      for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++)
     
    224192    if (this->fragmentShaderSource != NULL)
    225193      for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++)
    226         PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));
     194        PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/
    227195  }
    228196
  • trunk/src/lib/graphics/shader.h

    r5264 r5266  
    2020
    2121// FORWARD DECLARATION
    22 template<class T> class Array;
    2322
    2423
     
    3130
    3231  bool loadShaderProgramm(SHADER_TYPE type, const char* fileName);
    33   bool activateShader();
     32  void activateShader();
     33  static void deactivateShader();
    3434  void deleteProgram(SHADER_TYPE type);
     35
     36  char* fileRead(const char* fileName);
    3537
    3638  void printError(GLenum program);
     
    4244   char*                  fragmentShaderFile;
    4345   char*                  vertexShaderFile;
    44    Array<char*>*          fragmentShaderSource;
    45    Array<char*>*          vertexShaderSource;
    46    Array<int>*            fragmentShaderLengths;
    47    Array<int>*            vertexShaderLenghts;
    4846   GLenum                 shaderProgram;
    4947   GLenum                 vertexShader;
Note: See TracChangeset for help on using the changeset viewer.