Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/graphics/shader_data.cc @ 9806

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

try with the shader

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