Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/shader_data.cc @ 10688

Last change on this file since 10688 was 10143, checked in by bensch, 18 years ago

Shader fixing

File size: 7.3 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
[9806]18#include "shader_data.h"
[1853]19
[5262]20#include "stdlibincl.h"
[5273]21#include "compiler.h"
[8037]22//#include <stdio.h>
23#include <fstream>
24
[5262]25#include "debug.h"
26
27
[8037]28#ifndef PARSELINELENGTH
29#define PARSELINELENGTH     512       //!< how many chars to read at once
[5262]30#endif
31
[1853]32
[9806]33ObjectListDefinition(ShaderData);
[1856]34
[3245]35/**
[4838]36 * standard constructor
[3245]37*/
[9818]38ShaderData::ShaderData ()
[3365]39{
[9818]40  this->registerObject(this, ShaderData::_objectList);
[9806]41  this->shaderProgram = 0;
42  this->vertexShader = 0;
43  this->fragmentShader = 0;
[9818]44}
[5262]45
[9818]46
47/// TODO fix that shaders are unloaded first. then loaded
48bool ShaderData::load(const std::string& vertexShaderFile, const std::string& fragmentShaderFile)
49{
[9806]50  if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100)
51  {
52    this->shaderProgram = glCreateProgramObjectARB();
[5263]53
[9806]54    if (!vertexShaderFile.empty())
[9818]55      this->loadShaderProgramm(ShaderData::Vertex, vertexShaderFile);
[9806]56    if (!fragmentShaderFile.empty())
[9818]57      this->loadShaderProgramm(ShaderData::Fragment, fragmentShaderFile);
[8037]58
[9806]59    this->linkShaderProgram();
[8037]60
[9806]61  }
62  else
63  {
64    PRINTF(2)("Shaders are not supported on your hardware\n");
[10114]65    return true;
[9806]66  }
[10143]67
[10114]68  return true;
[3365]69}
[1853]70
[3245]71/**
[4838]72 * standard deconstructor
[5318]73 */
[9818]74ShaderData::~ShaderData ()
[3543]75{
[10143]76  if (shaderProgram == 0)
77    return;
78
79
[5322]80  if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB))
[9818]81    glUseProgramObjectARB(0);
[5318]82
[3543]83  // delete what has to be deleted here
[9818]84  this->deleteProgram(ShaderData::Vertex);
85  this->deleteProgram(ShaderData::Fragment);
[5263]86
[5273]87  if (this->fragmentShader != 0)
[5322]88  {
89    glDetachObjectARB(this->shaderProgram, this->fragmentShader);
[5273]90    glDeleteObjectARB(this->fragmentShader);
[5322]91  }
[5273]92  if (this->vertexShader != 0)
[5322]93  {
94    glDetachObjectARB(this->shaderProgram, this->vertexShader);
[5273]95    glDeleteObjectARB(this->vertexShader);
[5322]96  }
[5273]97  if (this->shaderProgram != 0)
[5321]98  {
99    GLint status = 0;
[5322]100    //glLinkProgramARB(this->shaderProgram);
[5273]101    glDeleteObjectARB(this->shaderProgram);
[9806]102    // link error checking
[5321]103    glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_DELETE_STATUS_ARB, &status);
104    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
105      this->printError(this->shaderProgram);
106  }
[3543]107}
[5261]108
[9818]109bool ShaderData::loadShaderProgramm(ShaderData::Type type, const std::string& fileName)
[5261]110{
[5319]111  GLhandleARB shader = 0;
[5285]112
[9818]113  if (type != ShaderData::Vertex && type != ShaderData::Fragment)
[5261]114    return false;
[5262]115  this->deleteProgram(type);
[5261]116
117
[8037]118  std::string program;
119  if (!readShader(fileName, program))
120    return false;
[5318]121
[9818]122  if (type == ShaderData::Vertex && GLEW_ARB_vertex_shader)
[5262]123  {
[7221]124    this->vertexShaderFile = fileName;
[5262]125
[5269]126    shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
[5263]127  }
[5262]128
[9818]129  if (type == ShaderData::Fragment && GLEW_ARB_fragment_shader)
[5263]130  {
[7221]131    this->fragmentShaderFile = fileName;
[5266]132
[5269]133    shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
[5263]134  }
[5273]135
136  if (shader != 0)
[5319]137  {
[5320]138    GLint status = 0;
[8037]139    const char* prog = program.c_str();
140
141    glShaderSourceARB(shader, 1, &prog, NULL);
[5320]142    glCompileShaderARB(shader);
143    // checking on error.
144    glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
145    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
146      this->printError(shader);
147    else
148      glAttachObjectARB(this->shaderProgram, shader);
[5319]149  }
[8316]150  return true;
[5261]151}
152
[8037]153
[9818]154void ShaderData::linkShaderProgram()
[5261]155{
[8037]156  GLint status = 0;
[5266]157
[8037]158  glLinkProgramARB(this->shaderProgram);
[9806]159  // link error checking
[8037]160  glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status);
161  if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
162    this->printError(this->shaderProgram);
163}
[5266]164
165
[9818]166bool ShaderData::readShader(const std::string& fileName, std::string& output)
[8037]167{
168  char lineBuffer[PARSELINELENGTH];
[5266]169
[8037]170  std::ifstream shader;
171  shader.open(fileName.c_str());
172  if (!shader.is_open())
173    return false;
[5266]174
[5318]175
[8037]176  while (!shader.eof())
[5318]177  {
[8037]178    shader.getline(lineBuffer, PARSELINELENGTH);
179    output += lineBuffer;
180    output += "\n";
[5318]181  }
182
[8037]183
184  shader.close();
185  return true;
[5318]186}
187
[9818]188void ShaderData::bindShader(const char* name, const float* value, size_t size)
[5266]189{
[5273]190  if (likely (this->shaderProgram != 0))
[5317]191  {
[5273]192    glUseProgramObjectARB(this->shaderProgram);
[5262]193
[9806]194    unsigned int location = glGetUniformLocationARB(this->shaderProgram, name);
195    /* This is EXPENSIVE and should be avoided. */
[8255]196
[9806]197    if      (size == 1)  glUniform1fvARB(location, 1, value);
198    else if (size == 2)  glUniform2fvARB(location, 1, value);
199    else if (size == 3)  glUniform3fvARB(location, 1, value);
200    else if (size == 4)  glUniform4fvARB(location, 1, value);
201    else if (size == 9)  glUniformMatrix3fvARB(location, 1, false, value);
202    else if (size == 16) glUniformMatrix4fvARB(location, 1, false, value);
[8255]203
[9806]204  }
[8255]205}
206
[9818]207void ShaderData::deleteProgram(ShaderData::Type type)
[5266]208{
[5335]209  GLint status = 0;
[9818]210  if (type == ShaderData::Vertex && this->vertexShader != 0)
[5262]211  {
[7221]212    this->vertexShaderFile = "";
[5321]213    glDetachObjectARB(this->shaderProgram, this->vertexShader);
[5263]214    glDeleteObjectARB(this->vertexShader);
[5320]215    glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
216    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
[9818]217      ShaderData::printError(this->vertexShader);
[5263]218    this->vertexShader = 0;
[5262]219  }
[9818]220  else if (type == ShaderData::Fragment && this->fragmentShader != 0)
[5262]221  {
[7221]222    this->fragmentShaderFile = "";
[5321]223    glDetachObjectARB(this->shaderProgram, this->fragmentShader);
[5263]224    glDeleteObjectARB(this->fragmentShader);
[5320]225    glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
226    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
[9818]227      ShaderData::printError(this->fragmentShader);
[5263]228    this->fragmentShader = 0;
[5262]229  }
230  else
231    return;
232}
233
[5264]234
[9818]235void ShaderData::printError(GLhandleARB program)
[5264]236{
[5273]237  if (program == 0)
238    return;
239
[5267]240  int infologLength = 0;
241  int charsWritten  = 0;
242  char *infoLog;
243
244  glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB,
245                            &infologLength);
246
247  if (infologLength > 0)
248  {
[5283]249    infoLog = new char[infologLength+1];
[5267]250    glGetInfoLogARB(program, infologLength, &charsWritten, infoLog);
[5269]251    printf("%s\n", infoLog);
[5283]252    delete[] infoLog;
[5267]253  }
[5264]254}
255
[9818]256void ShaderData::debug() const
[5333]257{
[9818]258  PRINT(3)("ShaderData info: (SHADER: %d)\n", this->shaderProgram);
[5262]259  if (this->vertexShader != 0)
260  {
[9806]261    /*    PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile);
262        if (this->vertexShaderSource != NULL)
263          for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++)
264            PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i));
265      }
266      if (this->fragmentShader != 0)
267      {
268        PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile);
269        if (this->fragmentShaderSource != NULL)
270          for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++)
271            PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/
[5262]272  }
273}
274
Note: See TracBrowser for help on using the repository browser.