Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/shared_lib/src/lib/graphics/shader.cc @ 7187

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

deleted unused stuff

File size: 8.1 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 "stdlibincl.h"
21#include "compiler.h"
22#include <stdio.h>
23#include "debug.h"
24
25#include "resource_manager.h"
26
27
28#ifndef PARSELINELENGHT
29#define PARSELINELENGHT     512       //!< how many chars to read at once
30#endif
31
32using namespace std;
33
34
35/**
36 * standard constructor
37*/
38Shader::Shader (const char* vertexShaderFile, const char* fragmentShaderFile)
39{
40   this->setClassID(CL_SHADER, "Shader");
41
42   this->fragmentShaderFile = NULL;
43   this->vertexShaderFile = NULL;
44   this->shaderProgram = 0;
45   this->vertexShader = 0;
46   this->fragmentShader = 0;
47
48   if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100)
49     {
50       GLint status = 0;
51
52       this->shaderProgram = glCreateProgramObjectARB();
53
54       if (vertexShaderFile != NULL)
55         this->loadShaderProgramm(SHADER_VERTEX, vertexShaderFile);
56       if (fragmentShaderFile != NULL)
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);
63    }
64   else
65     {
66       PRINTF(2)("Shaders are not supported on your hardware\n");
67     }
68}
69
70
71/**
72 * standard deconstructor
73 */
74Shader::~Shader ()
75{
76  if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB))
77    Shader::deactivateShader();
78
79  // delete what has to be deleted here
80  this->deleteProgram(SHADER_VERTEX);
81  this->deleteProgram(SHADER_FRAGMENT);
82
83  if (this->fragmentShader != 0)
84  {
85    glDetachObjectARB(this->shaderProgram, this->fragmentShader);
86    glDeleteObjectARB(this->fragmentShader);
87  }
88  if (this->vertexShader != 0)
89  {
90    glDetachObjectARB(this->shaderProgram, this->vertexShader);
91    glDeleteObjectARB(this->vertexShader);
92  }
93  if (this->shaderProgram != 0)
94  {
95    GLint status = 0;
96    //glLinkProgramARB(this->shaderProgram);
97    glDeleteObjectARB(this->shaderProgram);
98       // link error checking
99    glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_DELETE_STATUS_ARB, &status);
100    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
101      this->printError(this->shaderProgram);
102  }
103}
104
105Shader* Shader::getShader(const char* vertexShaderFile, const char* fragmentShaderFile)
106{
107  return (Shader*)ResourceManager::getInstance()->load(vertexShaderFile, SHADER,  RP_LEVEL, fragmentShaderFile);
108}
109
110bool Shader::unload(Shader* shader)
111{
112  return ResourceManager::getInstance()->unload(shader);
113}
114
115Shader* Shader::storedShader = NULL;
116
117
118bool Shader::loadShaderProgramm(SHADER_TYPE type, const char* fileName)
119{
120  GLhandleARB shader = 0;
121
122  if (type != SHADER_VERTEX && type != SHADER_FRAGMENT)
123    return false;
124  this->deleteProgram(type);
125
126
127  std::vector<char*>* program = fileReadArray(fileName);
128
129  if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader)
130  {
131    this->vertexShaderFile = new char[strlen(fileName)+1];
132    strcpy(this->vertexShaderFile, fileName);
133
134    shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
135  }
136
137  if (type == SHADER_FRAGMENT && GLEW_ARB_fragment_shader)
138  {
139    this->fragmentShaderFile = new char[strlen(fileName)+1];
140    strcpy(this->fragmentShaderFile, fileName);
141
142    shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
143  }
144
145  if (shader != 0)
146  {
147    GLint status = 0;
148    glShaderSourceARB(shader, program->size(), (const char**)&(*program)[0], NULL);
149    glCompileShaderARB(shader);
150    // checking on error.
151    glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
152    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
153      this->printError(shader);
154    else
155      glAttachObjectARB(this->shaderProgram, shader);
156  }
157  for (unsigned int i=0; i< program->size(); i++)
158    delete[] (*program)[i];
159  delete program;
160}
161
162char* Shader::fileRead(const char* fileName)
163{
164  FILE* fileHandle;
165  char* content = NULL;
166
167  int count = 0;
168
169  if (fileName == NULL)
170    return NULL;
171
172  fileHandle = fopen(fileName, "rt");
173
174  if (fileHandle == NULL)
175    return NULL;
176  fseek(fileHandle, 0, SEEK_END);
177  count = ftell(fileHandle);
178  rewind(fileHandle);
179  if (count > 0) {
180     content = new char[count+1];
181     count = fread(content, sizeof(char), count, fileHandle);
182     content[count] = '\0';
183   }
184   fclose(fileHandle);
185 return content;
186}
187
188
189std::vector<char*>* Shader::fileReadArray(const char* fileName)
190{
191  FILE*    stream;           //< The stream we use to read the file.
192
193  if( (stream = fopen (fileName, "rt")) == NULL)
194  {
195    PRINTF(1)("Shader could not open %s\n", fileName);
196    return NULL;
197  }
198  std::vector<char*>* file = new std::vector<char*>;
199
200  char lineBuffer[PARSELINELENGHT];
201  char* addString;
202  while(fgets (lineBuffer, PARSELINELENGHT, stream) != NULL)
203  {
204    addString = new char[strlen(lineBuffer)+1];
205    strcpy(addString, lineBuffer);
206    file->push_back(addString);
207  }
208  fclose(stream);
209  return file;
210}
211
212
213
214void Shader::activateShader()
215{
216  if (likely (this->shaderProgram != 0))
217  {
218    glUseProgramObjectARB(this->shaderProgram);
219    Shader::storedShader = this;
220  }
221}
222
223void Shader::deactivateShader()
224{
225 if (storedShader != NULL)
226   glUseProgramObjectARB(0);
227 Shader::storedShader = NULL;
228}
229
230
231void Shader::deleteProgram(SHADER_TYPE type)
232{
233  GLint status = 0;
234  if (type == SHADER_VERTEX && this->vertexShader != 0)
235  {
236    delete[] this->vertexShaderFile;
237    this->vertexShaderFile = NULL;
238    glDetachObjectARB(this->shaderProgram, this->vertexShader);
239    glDeleteObjectARB(this->vertexShader);
240    glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
241    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
242      Shader::printError(this->vertexShader);
243    this->vertexShader = 0;
244  }
245  else if (type == SHADER_FRAGMENT && this->fragmentShader != 0)
246  {
247    delete[] this->fragmentShaderFile;
248    this->fragmentShaderFile = NULL;
249    glDetachObjectARB(this->shaderProgram, this->fragmentShader);
250    glDeleteObjectARB(this->fragmentShader);
251    glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
252    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
253      Shader::printError(this->fragmentShader);
254    this->fragmentShader = 0;
255  }
256  else
257    return;
258}
259
260
261void Shader::printError(GLhandleARB program)
262{
263  if (program == 0)
264    return;
265
266  int infologLength = 0;
267  int charsWritten  = 0;
268  char *infoLog;
269
270  glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB,
271                            &infologLength);
272
273  if (infologLength > 0)
274  {
275    infoLog = new char[infologLength+1];
276    glGetInfoLogARB(program, infologLength, &charsWritten, infoLog);
277    printf("%s\n", infoLog);
278    delete[] infoLog;
279  }
280}
281
282bool Shader::checkShaderAbility()
283{
284  if (GLEW_ARB_shader_objects &&
285      GLEW_ARB_shading_language_100 &&
286      GLEW_ARB_vertex_shader &&
287      GLEW_ARB_fragment_shader)
288    return true;
289  else
290    return false;
291}
292
293void Shader::debug() const
294{
295  PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram);
296  if (this->vertexShader != 0)
297  {
298/*    PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile);
299    if (this->vertexShaderSource != NULL)
300      for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++)
301        PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i));
302  }
303  if (this->fragmentShader != 0)
304  {
305    PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile);
306    if (this->fragmentShaderSource != NULL)
307      for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++)
308        PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/
309  }
310}
311
Note: See TracBrowser for help on using the repository browser.