Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

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