Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10114 was 10114, checked in by patrick, 17 years ago

merged network back to trunk

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