Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/shader.cc @ 5315

Last change on this file since 5315 was 5285, checked in by bensch, 19 years ago

orxonox/trunk: nicer quit-modi
TextEngine is now deleted by GraphicsEngine
trying to fix errors in the Element2D deletion

File size: 5.4 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.h"
19
20#include "stdlibincl.h"
21#include "compiler.h"
22#include <stdio.h>
23#include "debug.h"
24
25
26#ifndef PARSELINELENGHT
27#define PARSELINELENGHT     512       //!< how many chars to read at once
28#endif
29
30using namespace std;
31
32
33/**
34 * standard constructor
35*/
36Shader::Shader (const char* vertexShaderFile, const char* fragmentShaderFile)
37{
38   this->setClassID(CL_SHADER, "Shader");
39
40   this->fragmentShaderFile = NULL;
41   this->vertexShaderFile = NULL;
42   this->shaderProgram = 0;
43   this->vertexShader = 0;
44   this->fragmentShader = 0;
45
46   if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100)
47     {
48       this->shaderProgram = glCreateProgramObjectARB();
49
50       if (vertexShaderFile != NULL)
51         this->loadShaderProgramm(SHADER_VERTEX, vertexShaderFile);
52       if (fragmentShaderFile != NULL)
53         this->loadShaderProgramm(SHADER_FRAGMENT, fragmentShaderFile);
54       try  {
55         glLinkProgramARB(this->shaderProgram); }
56       catch(GLenum errorCode)  {
57         this->printError(this->shaderProgram); }
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  // delete what has to be deleted here
72  this->deleteProgram(SHADER_VERTEX);
73  this->deleteProgram(SHADER_FRAGMENT);
74
75  if (this->fragmentShader != 0)
76    glDeleteObjectARB(this->fragmentShader);
77  if (this->vertexShader != 0)
78    glDeleteObjectARB(this->vertexShader);
79  if (this->shaderProgram != 0)
80    glDeleteObjectARB(this->shaderProgram);
81}
82
83
84bool Shader::loadShaderProgramm(SHADER_TYPE type, const char* fileName)
85{
86  GLenum shader = 0;
87
88  if (type != SHADER_VERTEX && type != SHADER_FRAGMENT)
89    return false;
90  this->deleteProgram(type);
91
92
93  const char* program = fileRead(fileName);
94  if (program == NULL)
95    return false;
96  if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader)
97  {
98    this->vertexShaderFile = new char[strlen(fileName)+1];
99    strcpy(this->vertexShaderFile, fileName);
100
101    shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
102  }
103
104  if (type == SHADER_FRAGMENT && GLEW_ARB_fragment_shader)
105  {
106    this->fragmentShaderFile = new char[strlen(fileName)+1];
107    strcpy(this->fragmentShaderFile, fileName);
108
109    shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
110  }
111
112  if (shader != 0)
113    {
114      glShaderSourceARB(shader, 1, (const GLcharARB**)&program, NULL);
115      try {
116        glCompileShaderARB(shader);
117      }
118      catch (...)
119        {
120          this->printError(shader);
121        }
122      glAttachObjectARB(this->shaderProgram, shader);
123      delete[] program;
124    }
125}
126
127char* Shader::fileRead(const char* fileName)
128{
129  FILE* fileHandle;
130  char* content = NULL;
131
132  int count = 0;
133
134  if (fileName == NULL)
135    return NULL;
136
137  fileHandle = fopen(fileName, "rt");
138
139  if (fileHandle == NULL)
140    return NULL;
141  fseek(fileHandle, 0, SEEK_END);
142  count = ftell(fileHandle);
143  rewind(fileHandle);
144  if (count > 0) {
145     content = new char[count+1];
146     count = fread(content, sizeof(char), count, fileHandle);
147     content[count] = '\0';
148   }
149   fclose(fileHandle);
150 return content;
151}
152
153void Shader::activateShader()
154{
155  if (likely (this->shaderProgram != 0))
156    glUseProgramObjectARB(this->shaderProgram);
157}
158
159void Shader::deactivateShader()
160{
161  glUseProgramObjectARB(0);
162}
163
164
165void Shader::deleteProgram(SHADER_TYPE type)
166{
167  if (type == SHADER_VERTEX && this->vertexShader != 0)
168  {
169    delete[] this->vertexShaderFile;
170    this->vertexShaderFile = NULL;
171    glDeleteObjectARB(this->vertexShader);
172    this->vertexShader = 0;
173  }
174  else if (type == SHADER_FRAGMENT && this->fragmentShader != 0)
175  {
176    delete[] this->fragmentShaderFile;
177    this->fragmentShaderFile = NULL;
178    glDeleteObjectARB(this->fragmentShader);
179    this->fragmentShader = 0;
180  }
181  else
182    return;
183}
184
185
186void Shader::printError(GLenum program)
187{
188  if (program == 0)
189    return;
190
191  int infologLength = 0;
192  int charsWritten  = 0;
193  char *infoLog;
194
195  glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB,
196                            &infologLength);
197
198  if (infologLength > 0)
199  {
200    infoLog = new char[infologLength+1];
201    glGetInfoLogARB(program, infologLength, &charsWritten, infoLog);
202    printf("%s\n", infoLog);
203    delete[] infoLog;
204  }
205}
206
207
208
209void Shader::debug() const
210{
211  PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram);
212  if (this->vertexShader != 0)
213  {
214/*    PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile);
215    if (this->vertexShaderSource != NULL)
216      for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++)
217        PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i));
218  }
219  if (this->fragmentShader != 0)
220  {
221    PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile);
222    if (this->fragmentShaderSource != NULL)
223      for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++)
224        PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/
225  }
226}
227
Note: See TracBrowser for help on using the repository browser.