Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9968 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 7.2 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");
65  }
[3365]66}
[1853]67
[3245]68/**
[4838]69 * standard deconstructor
[5318]70 */
[9818]71ShaderData::~ShaderData ()
[3543]72{
[5322]73  if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB))
[9818]74    glUseProgramObjectARB(0);
[5318]75
[3543]76  // delete what has to be deleted here
[9818]77  this->deleteProgram(ShaderData::Vertex);
78  this->deleteProgram(ShaderData::Fragment);
[5263]79
[5273]80  if (this->fragmentShader != 0)
[5322]81  {
82    glDetachObjectARB(this->shaderProgram, this->fragmentShader);
[5273]83    glDeleteObjectARB(this->fragmentShader);
[5322]84  }
[5273]85  if (this->vertexShader != 0)
[5322]86  {
87    glDetachObjectARB(this->shaderProgram, this->vertexShader);
[5273]88    glDeleteObjectARB(this->vertexShader);
[5322]89  }
[5273]90  if (this->shaderProgram != 0)
[5321]91  {
92    GLint status = 0;
[5322]93    //glLinkProgramARB(this->shaderProgram);
[5273]94    glDeleteObjectARB(this->shaderProgram);
[9806]95    // link error checking
[5321]96    glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_DELETE_STATUS_ARB, &status);
97    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
98      this->printError(this->shaderProgram);
99  }
[3543]100}
[5261]101
[9818]102bool ShaderData::loadShaderProgramm(ShaderData::Type type, const std::string& fileName)
[5261]103{
[5319]104  GLhandleARB shader = 0;
[5285]105
[9818]106  if (type != ShaderData::Vertex && type != ShaderData::Fragment)
[5261]107    return false;
[5262]108  this->deleteProgram(type);
[5261]109
110
[8037]111  std::string program;
112  if (!readShader(fileName, program))
113    return false;
[5318]114
[9818]115  if (type == ShaderData::Vertex && GLEW_ARB_vertex_shader)
[5262]116  {
[7221]117    this->vertexShaderFile = fileName;
[5262]118
[5269]119    shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
[5263]120  }
[5262]121
[9818]122  if (type == ShaderData::Fragment && GLEW_ARB_fragment_shader)
[5263]123  {
[7221]124    this->fragmentShaderFile = fileName;
[5266]125
[5269]126    shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
[5263]127  }
[5273]128
129  if (shader != 0)
[5319]130  {
[5320]131    GLint status = 0;
[8037]132    const char* prog = program.c_str();
133
134    glShaderSourceARB(shader, 1, &prog, NULL);
[5320]135    glCompileShaderARB(shader);
136    // checking on error.
137    glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
138    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
139      this->printError(shader);
140    else
141      glAttachObjectARB(this->shaderProgram, shader);
[5319]142  }
[8316]143  return true;
[5261]144}
145
[8037]146
[9818]147void ShaderData::linkShaderProgram()
[5261]148{
[8037]149  GLint status = 0;
[5266]150
[8037]151  glLinkProgramARB(this->shaderProgram);
[9806]152  // link error checking
[8037]153  glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status);
154  if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
155    this->printError(this->shaderProgram);
156}
[5266]157
158
[9818]159bool ShaderData::readShader(const std::string& fileName, std::string& output)
[8037]160{
161  char lineBuffer[PARSELINELENGTH];
[5266]162
[8037]163  std::ifstream shader;
164  shader.open(fileName.c_str());
165  if (!shader.is_open())
166    return false;
[5266]167
[5318]168
[8037]169  while (!shader.eof())
[5318]170  {
[8037]171    shader.getline(lineBuffer, PARSELINELENGTH);
172    output += lineBuffer;
173    output += "\n";
[5318]174  }
175
[8037]176
177  shader.close();
178  return true;
[5318]179}
180
[9818]181void ShaderData::bindShader(const char* name, const float* value, size_t size)
[5266]182{
[5273]183  if (likely (this->shaderProgram != 0))
[5317]184  {
[5273]185    glUseProgramObjectARB(this->shaderProgram);
[5262]186
[9806]187    unsigned int location = glGetUniformLocationARB(this->shaderProgram, name);
188    /* This is EXPENSIVE and should be avoided. */
[8255]189
[9806]190    if      (size == 1)  glUniform1fvARB(location, 1, value);
191    else if (size == 2)  glUniform2fvARB(location, 1, value);
192    else if (size == 3)  glUniform3fvARB(location, 1, value);
193    else if (size == 4)  glUniform4fvARB(location, 1, value);
194    else if (size == 9)  glUniformMatrix3fvARB(location, 1, false, value);
195    else if (size == 16) glUniformMatrix4fvARB(location, 1, false, value);
[8255]196
[9806]197  }
[8255]198}
199
[9818]200void ShaderData::deleteProgram(ShaderData::Type type)
[5266]201{
[5335]202  GLint status = 0;
[9818]203  if (type == ShaderData::Vertex && this->vertexShader != 0)
[5262]204  {
[7221]205    this->vertexShaderFile = "";
[5321]206    glDetachObjectARB(this->shaderProgram, this->vertexShader);
[5263]207    glDeleteObjectARB(this->vertexShader);
[5320]208    glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
209    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
[9818]210      ShaderData::printError(this->vertexShader);
[5263]211    this->vertexShader = 0;
[5262]212  }
[9818]213  else if (type == ShaderData::Fragment && this->fragmentShader != 0)
[5262]214  {
[7221]215    this->fragmentShaderFile = "";
[5321]216    glDetachObjectARB(this->shaderProgram, this->fragmentShader);
[5263]217    glDeleteObjectARB(this->fragmentShader);
[5320]218    glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
219    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
[9818]220      ShaderData::printError(this->fragmentShader);
[5263]221    this->fragmentShader = 0;
[5262]222  }
223  else
224    return;
225}
226
[5264]227
[9818]228void ShaderData::printError(GLhandleARB program)
[5264]229{
[5273]230  if (program == 0)
231    return;
232
[5267]233  int infologLength = 0;
234  int charsWritten  = 0;
235  char *infoLog;
236
237  glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB,
238                            &infologLength);
239
240  if (infologLength > 0)
241  {
[5283]242    infoLog = new char[infologLength+1];
[5267]243    glGetInfoLogARB(program, infologLength, &charsWritten, infoLog);
[5269]244    printf("%s\n", infoLog);
[5283]245    delete[] infoLog;
[5267]246  }
[5264]247}
248
[9818]249void ShaderData::debug() const
[5333]250{
[9818]251  PRINT(3)("ShaderData info: (SHADER: %d)\n", this->shaderProgram);
[5262]252  if (this->vertexShader != 0)
253  {
[9806]254    /*    PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile);
255        if (this->vertexShaderSource != NULL)
256          for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++)
257            PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i));
258      }
259      if (this->fragmentShader != 0)
260      {
261        PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile);
262        if (this->fragmentShaderSource != NULL)
263          for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++)
264            PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/
[5262]265  }
266}
267
Note: See TracBrowser for help on using the repository browser.