Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/heathaze/src/lib/graphics/shader_data.cc @ 10764

Last change on this file since 10764 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
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_data.h"
19
20#include "stdlibincl.h"
21#include "compiler.h"
22//#include <stdio.h>
23#include <fstream>
24
25#include "debug.h"
26
27
28#ifndef PARSELINELENGTH
29#define PARSELINELENGTH     512       //!< how many chars to read at once
30#endif
31
32
33ObjectListDefinition(ShaderData);
34
35/**
36 * standard constructor
37*/
38ShaderData::ShaderData ()
39{
40  this->registerObject(this, ShaderData::_objectList);
41  this->shaderProgram = 0;
42  this->vertexShader = 0;
43  this->fragmentShader = 0;
44}
45
46
47/// TODO fix that shaders are unloaded first. then loaded
48bool ShaderData::load(const std::string& vertexShaderFile, const std::string& fragmentShaderFile)
49{
50  if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100)
51  {
52    this->shaderProgram = glCreateProgramObjectARB();
53
54    if (!vertexShaderFile.empty())
55      this->loadShaderProgramm(ShaderData::Vertex, vertexShaderFile);
56    if (!fragmentShaderFile.empty())
57      this->loadShaderProgramm(ShaderData::Fragment, fragmentShaderFile);
58
59    this->linkShaderProgram();
60
61  }
62  else
63  {
64    PRINTF(2)("Shaders are not supported on your hardware\n");
65  }
66}
67
68/**
69 * standard deconstructor
70 */
71ShaderData::~ShaderData ()
72{
73  if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB))
74    glUseProgramObjectARB(0);
75
76  // delete what has to be deleted here
77  this->deleteProgram(ShaderData::Vertex);
78  this->deleteProgram(ShaderData::Fragment);
79
80  if (this->fragmentShader != 0)
81  {
82    glDetachObjectARB(this->shaderProgram, this->fragmentShader);
83    glDeleteObjectARB(this->fragmentShader);
84  }
85  if (this->vertexShader != 0)
86  {
87    glDetachObjectARB(this->shaderProgram, this->vertexShader);
88    glDeleteObjectARB(this->vertexShader);
89  }
90  if (this->shaderProgram != 0)
91  {
92    GLint status = 0;
93    //glLinkProgramARB(this->shaderProgram);
94    glDeleteObjectARB(this->shaderProgram);
95    // link error checking
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  }
100}
101
102bool ShaderData::loadShaderProgramm(ShaderData::Type type, const std::string& fileName)
103{
104  GLhandleARB shader = 0;
105
106  if (type != ShaderData::Vertex && type != ShaderData::Fragment)
107    return false;
108  this->deleteProgram(type);
109
110
111  std::string program;
112  if (!readShader(fileName, program))
113    return false;
114
115  if (type == ShaderData::Vertex && GLEW_ARB_vertex_shader)
116  {
117    this->vertexShaderFile = fileName;
118
119    shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
120  }
121
122  if (type == ShaderData::Fragment && GLEW_ARB_fragment_shader)
123  {
124    this->fragmentShaderFile = fileName;
125
126    shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
127  }
128
129  if (shader != 0)
130  {
131    GLint status = 0;
132    const char* prog = program.c_str();
133
134    glShaderSourceARB(shader, 1, &prog, NULL);
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);
142  }
143  return true;
144}
145
146
147void ShaderData::linkShaderProgram()
148{
149  GLint status = 0;
150
151  glLinkProgramARB(this->shaderProgram);
152  // link error checking
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}
157
158
159bool ShaderData::readShader(const std::string& fileName, std::string& output)
160{
161  char lineBuffer[PARSELINELENGTH];
162
163  std::ifstream shader;
164  shader.open(fileName.c_str());
165  if (!shader.is_open())
166    return false;
167
168
169  while (!shader.eof())
170  {
171    shader.getline(lineBuffer, PARSELINELENGTH);
172    output += lineBuffer;
173    output += "\n";
174  }
175
176
177  shader.close();
178  return true;
179}
180
181void ShaderData::bindShader(const char* name, const float* value, size_t size)
182{
183  if (likely (this->shaderProgram != 0))
184  {
185    glUseProgramObjectARB(this->shaderProgram);
186
187    unsigned int location = glGetUniformLocationARB(this->shaderProgram, name);
188    /* This is EXPENSIVE and should be avoided. */
189
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);
196
197  }
198}
199
200void ShaderData::deleteProgram(ShaderData::Type type)
201{
202  GLint status = 0;
203  if (type == ShaderData::Vertex && this->vertexShader != 0)
204  {
205    this->vertexShaderFile = "";
206    glDetachObjectARB(this->shaderProgram, this->vertexShader);
207    glDeleteObjectARB(this->vertexShader);
208    glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
209    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
210      ShaderData::printError(this->vertexShader);
211    this->vertexShader = 0;
212  }
213  else if (type == ShaderData::Fragment && this->fragmentShader != 0)
214  {
215    this->fragmentShaderFile = "";
216    glDetachObjectARB(this->shaderProgram, this->fragmentShader);
217    glDeleteObjectARB(this->fragmentShader);
218    glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
219    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
220      ShaderData::printError(this->fragmentShader);
221    this->fragmentShader = 0;
222  }
223  else
224    return;
225}
226
227
228void ShaderData::printError(GLhandleARB program)
229{
230  if (program == 0)
231    return;
232
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  {
242    infoLog = new char[infologLength+1];
243    glGetInfoLogARB(program, infologLength, &charsWritten, infoLog);
244    printf("%s\n", infoLog);
245    delete[] infoLog;
246  }
247}
248
249void ShaderData::debug() const
250{
251  PRINT(3)("ShaderData info: (SHADER: %d)\n", this->shaderProgram);
252  if (this->vertexShader != 0)
253  {
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));*/
265  }
266}
267
Note: See TracBrowser for help on using the repository browser.