Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/shader.cc @ 5262

Last change on this file since 5262 was 5262, checked in by bensch, 19 years ago

orxonox/trunk: file-parsing works

File size: 4.4 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "shader.h"
19
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
30using namespace std;
31
32
33/**
34 * standard constructor
35*/
36Shader::Shader (const char* vertexShaderFile, const char* fragmentShaderFile)
37{
38   this->setClassID(CL_SHADER, "Shader");
39
40   this->fragmentShaderFile = NULL;
41   this->vertexShaderFile = NULL;
42   this->fragmentShaderSource = NULL;
43   this->vertexShaderSource = NULL;
44   this->shaderProgram = 0;
45   this->vertexShader = 0;
46   this->fragmentShader = 0;
47
48   if (vertexShaderFile != NULL)
49     this->loadShaderProgramm(SHADER_VERTEX, vertexShaderFile);
50   if (fragmentShaderFile != NULL)
51     this->loadShaderProgramm(SHADER_FRAGMENT, fragmentShaderFile);
52}
53
54
55/**
56 * standard deconstructor
57*/
58Shader::~Shader ()
59{
60  // delete what has to be deleted here
61  this->deleteProgram(SHADER_VERTEX);
62  this->deleteProgram(SHADER_FRAGMENT);
63  // DELETE THE PROGRAMS
64/*  this->shaderProgram = 0;
65  this->vertexShader = 0;
66  this->fragmentShader = 0;*/
67}
68
69
70bool Shader::loadShaderProgramm(SHADER_TYPE type, const char* fileName)
71{
72  if (type != SHADER_VERTEX && type != SHADER_FRAGMENT)
73    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();
121
122
123//  this->shaderProgram = glCreateProgramObjectARB();
124  this->vertexShader =1;
125}
126
127bool Shader::activateShader()
128{
129
130}
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
Note: See TracBrowser for help on using the repository browser.