Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5262 in orxonox.OLD for trunk


Ignore:
Timestamp:
Sep 26, 2005, 11:33:55 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: file-parsing works

Location:
trunk/src/lib
Files:
4 edited

Legend:

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

    r5261 r5262  
    3131
    3232
     33#include "shader.h"
     34
    3335/**
    3436 *  standard constructor
    35    @todo this constructor is not jet implemented - do it
    36 */
     37 */
    3738GraphicsEngine::GraphicsEngine ()
    3839{
     
    6061  this->hwVersion = NULL;
    6162  this->hwExtensions = NULL;
     63
     64  Shader shader("test.txt");
     65  shader.debug();
     66  exit(-1);
    6267}
    6368
  • trunk/src/lib/graphics/shader.cc

    r5261 r5262  
    1818#include "shader.h"
    1919
     20#include "array.h"
     21#include "stdlibincl.h"
     22#include <stdio.h>
     23#include "debug.h"
     24
     25
     26#ifndef PARSELINELENGHT
     27#define PARSELINELENGHT     512       //!< how many chars to read at once
     28#endif
     29
    2030using namespace std;
    2131
     
    2434 * standard constructor
    2535*/
    26 Shader::Shader (SHADER_TYPE type, ...)
     36Shader::Shader (const char* vertexShaderFile, const char* fragmentShaderFile)
    2737{
    2838   this->setClassID(CL_SHADER, "Shader");
     
    3545   this->vertexShader = 0;
    3646   this->fragmentShader = 0;
     47
     48   if (vertexShaderFile != NULL)
     49     this->loadShaderProgramm(SHADER_VERTEX, vertexShaderFile);
     50   if (fragmentShaderFile != NULL)
     51     this->loadShaderProgramm(SHADER_FRAGMENT, fragmentShaderFile);
    3752}
    3853
     
    4459{
    4560  // delete what has to be deleted here
    46   delete[] this->fragmentShaderFile;
    47   delete[] this->vertexShaderFile;
    48   delete[] this->fragmentShaderSource;
    49   delete[] this->vertexShaderSource;
    50 
     61  this->deleteProgram(SHADER_VERTEX);
     62  this->deleteProgram(SHADER_FRAGMENT);
    5163  // DELETE THE PROGRAMS
    5264/*  this->shaderProgram = 0;
     
    5870bool Shader::loadShaderProgramm(SHADER_TYPE type, const char* fileName)
    5971{
    60   if (type != SHADER_VERTEX || type != SHADER_FRAGMENT)
     72  if (type != SHADER_VERTEX && type != SHADER_FRAGMENT)
    6173    return false;
     74  this->deleteProgram(type);
     75
     76  FILE*    stream;           //< The stream we use to read the file.
     77
     78  if( (stream = fopen (fileName, "r")) == NULL)
     79  {
     80    PRINTF(1)("Shader could not open %s\n", fileName);
     81    return false;
     82  }
     83  Array<char*>* program = new Array<char*>;
     84  if (type == SHADER_VERTEX)
     85  {
     86    this->vertexShaderFile = new char[strlen(fileName)+1];
     87    strcpy(this->vertexShaderFile, fileName);
     88    this->vertexShaderSource = program;
     89  }
     90  else
     91  {
     92    this->fragmentShaderFile = new char[strlen(fileName)+1];
     93    strcpy(this->fragmentShaderFile, fileName);
     94    this->fragmentShaderSource = program;
     95  }
     96
     97  char lineBuffer[PARSELINELENGHT];
     98  char* addString;
     99  while( !feof( stream))
     100  {
     101      // get next line
     102    fgets (lineBuffer, PARSELINELENGHT, stream);
     103    if (strchr(lineBuffer, '\n'))
     104    {
     105      addString = new char[strlen(lineBuffer)];
     106      strncpy(addString, lineBuffer, strlen (lineBuffer)-1);
     107      addString[strlen(lineBuffer)-1] ='\0';
     108    }
     109    else
     110    {
     111      addString = new char[strlen(lineBuffer)+1];
     112      strcpy(addString, lineBuffer);
     113    }
     114    program->addEntry(addString);
     115
     116    printf(lineBuffer);
     117  }
     118  fclose(stream);
     119
     120  program->finalizeArray();
    62121
    63122
     123//  this->shaderProgram = glCreateProgramObjectARB();
     124  this->vertexShader =1;
    64125}
    65126
     
    68129
    69130}
     131
     132
     133void Shader::deleteProgram(SHADER_TYPE type)
     134{
     135  Array<char*>* deleteArray = NULL;
     136  if (type == SHADER_VERTEX)
     137  {
     138    deleteArray = this->vertexShaderSource;
     139    this->vertexShaderSource = NULL;
     140    delete[] this->vertexShaderFile;
     141    this->vertexShaderFile = NULL;
     142  }
     143  else if (type == SHADER_FRAGMENT)
     144  {
     145    deleteArray = this->fragmentShaderSource;
     146    this->fragmentShaderSource = NULL;
     147    delete[] this->fragmentShaderFile;
     148    this->fragmentShaderFile = NULL;
     149  }
     150  else
     151    return;
     152
     153  if (deleteArray == NULL)
     154    return;
     155  else
     156  {
     157    deleteArray->finalizeArray();
     158    for (unsigned int i = 0; i < deleteArray->getCount(); i++)
     159    {
     160      delete[] deleteArray->getEntry(i);
     161    }
     162    delete deleteArray;
     163  }
     164}
     165
     166void Shader::debug() const
     167{
     168  PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram);
     169  if (this->vertexShader != 0)
     170  {
     171    PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile);
     172    if (this->vertexShaderSource != NULL)
     173      for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++)
     174        PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i));
     175  }
     176  if (this->fragmentShader != 0)
     177  {
     178    PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile);
     179    if (this->fragmentShaderSource != NULL)
     180      for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++)
     181        PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));
     182  }
     183
     184
     185}
     186
  • trunk/src/lib/graphics/shader.h

    r5261 r5262  
    2020
    2121// FORWARD DECLARATION
    22 
     22template<class T> class Array;
    2323
    2424
     
    2727
    2828 public:
    29   Shader(SHADER_TYPE type, ...);
     29  Shader(const char* vertexShaderFile = NULL, const char* fragmentShaderFile = NULL);
    3030  virtual ~Shader();
    3131
    3232  bool loadShaderProgramm(SHADER_TYPE type, const char* fileName);
    3333  bool activateShader();
     34  void deleteProgram(SHADER_TYPE type);
     35
     36  void debug() const;
     37
     38  private:
    3439
    3540 private:
    3641   char*                  fragmentShaderFile;
    3742   char*                  vertexShaderFile;
    38    char*                  fragmentShaderSource;
    39    char*                  vertexShaderSource;
     43   Array<char*>*          fragmentShaderSource;
     44   Array<char*>*          vertexShaderSource;
    4045   GLenum                 shaderProgram;
    4146   GLenum                 vertexShader;
  • trunk/src/lib/util/array.h

    r5100 r5262  
    1515
    1616/*!
    17   \file array.h
    18   \brief Contains the Array Class that handles arrays of classes.
     17  @file array.h
     18  @brief Contains the Array Class that handles arrays of classes.
    1919  this class creates a Array of a semi-Dynamic length.
    2020  beware, that after finalizing the array may not be resized again.
     
    9090{
    9191  PRINTF(5)("deleting array\n");
    92   Entry* walker = this->firstEntry;
    93   Entry* previous;
    94   while (walker)
     92  if (!this->finalized)
    9593  {
    96     previous = walker;
    97     walker = walker->next;
    98     delete previous;
     94    Entry* walker = this->firstEntry;
     95    Entry* previous;
     96    while (walker)
     97    {
     98      previous = walker;
     99      walker = walker->next;
     100      delete previous;
     101    }
    99102  }
    100   if (finalized)
     103  if (this->finalized)
    101104    delete[] this->array;
    102105}
     
    109112void Array<T>::finalizeArray ()
    110113{
     114  if (this->finalized)
     115    return;
    111116  PRINTF(5)("Finalizing array. Length: %i\n", entryCount);
    112117  if (!(this->array = new T [this->entryCount]))
    113118    PRINTF(1)("could not allocate %i data Blocks\n", this->entryCount);
    114119  Entry* walker = this->firstEntry;
    115   for (int i=0; i<this->entryCount; i++)
     120  for (int i=0; i < this->entryCount; i++)
    116121  {
    117122    this->array[i] = walker->value;
Note: See TracChangeset for help on using the changeset viewer.