libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moShaderGLSL.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moShaderGLSL.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright (C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28 
29 
30  Description:
31  Class for GLSL shaders.
32 
33 *******************************************************************************/
34 
35 #include "moShaderGLSL.h"
36 
38 {
40 
41  m_VertexShader = 0;
42  m_FragmentShader = 0;
43  m_ProgramObject = 0;
44 }
45 
47 {
48  Finish();
49 }
50 
52 {
53  return moShader::Init();
54 }
55 
57 {
58  if (m_VertexShader != 0)
59  {
60  glDetachObjectARB(m_ProgramObject, m_VertexShader);
61  m_VertexShader = 0;
62  }
63  if (m_FragmentShader != 0)
64  {
65  glDetachObjectARB(m_ProgramObject, m_FragmentShader);
66  m_FragmentShader = 0;
67  }
68 
69  if(m_ProgramObject != 0)
70  {
71  glDeleteObjectARB(m_ProgramObject);
72  m_ProgramObject = 0;
73  }
74 
75  return moShader::Finish();
76 }
77 
78 void moShaderGLSL::CreateVertShader(const moText& vert_source)
79 {
80  m_ProgramObject = glCreateProgramObjectARB();
81  compileVertShader(vert_source);
82  linkProgram();
83 }
84 
85 void moShaderGLSL::CreateFragShader(const moText& frag_source)
86 {
87  m_ProgramObject = glCreateProgramObjectARB();
88  compileFragShader(frag_source);
89  linkProgram();
90 }
91 
92 void moShaderGLSL::CreateShader( const moText& vert_source, const moText& frag_source)
93 {
94  try {
95  m_ProgramObject = glCreateProgramObjectARB();
96  compileVertShader(vert_source);
97  compileFragShader(frag_source);
98  linkProgram();
99  } catch(...) {
100  cout << "error shader" << endl;
101  }
102 }
103 
104 void moShaderGLSL::LoadVertShader(const moText& vert_filename)
105 {
106  moText vert_source = LoadShaderSource(vert_filename);
107  CreateVertShader(vert_source);
108 }
109 
110 void moShaderGLSL::LoadFragShader(const moText& frag_filename)
111 {
112  moText frag_source = LoadShaderSource(frag_filename);
113  CreateFragShader(frag_source);
114 }
115 
116 void moShaderGLSL::LoadShader(const moText& vert_filename, const moText& frag_filename)
117 {
118  moText vert_source = LoadShaderSource(vert_filename);
119  moText frag_source = LoadShaderSource(frag_filename);
120  CreateShader(vert_source, frag_source);
121 }
122 
124 {
126  glUseProgramObjectARB(m_ProgramObject);
127 }
128 
130 {
131  glUseProgramObjectARB(0);
133 }
134 
136 {
138 }
139 
141 {
143 }
144 
146 {
147  //MODebug2->Message("uName:"+uName);
148  return glGetUniformLocationARB(m_ProgramObject, uName);
149 }
150 
152 {
153  return glGetAttribLocationARB(m_ProgramObject, aName);
154 }
155 
156 void moShaderGLSL::compileVertShader(const moText& vert_source)
157 {
158  m_VertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
159  const char *source = (const char *)vert_source;
160  const char **psource = &source;
161  glShaderSourceARB(m_VertexShader, 1, psource, NULL);
162  glCompileShaderARB(m_VertexShader);
163 
164  int IsCompiled_FS;
165  int maxLength;
166  char *vertexInfoLog;
167 
168  glGetShaderiv(m_VertexShader, GL_COMPILE_STATUS, &IsCompiled_FS);
169 
170  glGetShaderiv(m_VertexShader, GL_INFO_LOG_LENGTH, &maxLength);
171 
172  /* The maxLength includes the NULL character */
173  vertexInfoLog = new char[maxLength];
174 
175  glGetShaderInfoLog(m_VertexShader, maxLength, &maxLength, vertexInfoLog);
176 
177 
178  if(IsCompiled_FS == MO_FALSE)
179  {
180  if (MODebug2 != NULL)
181  MODebug2->Error(moText("Vertex Shader compile error:")
182  + "("+GetName()+")"
183  + moText(vertexInfoLog) );
184 
185  /* Handle the error in an appropriate way such as displaying a message or writing to a log file. */
186  /* In this simple program, we'll just leave */
187 
188  //return;
189  } else {
190 MODebug2->Message(moText("Vertex Shader compile log:")
191  + "("+GetName()+")"
192  + moText(vertexInfoLog) );
193  }
194 delete [] vertexInfoLog;
195 
196  glAttachObjectARB(m_ProgramObject, m_VertexShader);
197 }
198 
199 void moShaderGLSL::compileFragShader(const moText& frag_source)
200 {
201  m_FragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
202  const char *source = (const char *)frag_source;
203  const char **psource = &source;
204  glShaderSourceARB(m_FragmentShader, 1, psource, NULL);
205  glCompileShaderARB(m_FragmentShader);
206 
207  int IsCompiled_FS;
208  int maxLength;
209  char *fragmentInfoLog;
210 
211  glGetShaderiv(m_FragmentShader, GL_COMPILE_STATUS, &IsCompiled_FS);
212  if(IsCompiled_FS == MO_FALSE)
213  {
214  glGetShaderiv(m_FragmentShader, GL_INFO_LOG_LENGTH, &maxLength);
215 
216  /* The maxLength includes the NULL character */
217  fragmentInfoLog = new char[maxLength];
218 
219  glGetShaderInfoLog(m_FragmentShader, maxLength, &maxLength, fragmentInfoLog);
220 
221  if (MODebug2 != NULL)
222  MODebug2->Error(moText("Fragment Shader compile error:")
223  + "("+GetName()+")"
224  + moText(fragmentInfoLog) );
225 
226  /* Handle the error in an appropriate way such as displaying a message or writing to a log file. */
227  /* In this simple program, we'll just leave */
228  delete [] fragmentInfoLog;
229  //return;
230  }
231 
232  glAttachObjectARB(m_ProgramObject, m_FragmentShader);
233 
234 
235 
236 }
237 
239 {
240  glLinkProgramARB(m_ProgramObject);
241  GLint progLinkSuccess;
242  glGetObjectParameterivARB(m_ProgramObject, GL_OBJECT_LINK_STATUS_ARB, &progLinkSuccess);
243  m_VertErrorCode = progLinkSuccess;
244  m_FragErrorCode = progLinkSuccess;
245  if (!progLinkSuccess)
246  if (MODebug2 != NULL) MODebug2->Error(moText("Shader program could not be linked: ") + this->GetName()
247  + " Type:" + IntToStr(GetType()) );
248 }
249 
250 void moShaderGLSL::printInfoLog(GLhandleARB obj)
251 {
252  GLint infologLength = 0;
253  GLsizei charsWritten = 0;
254  char *infoLog;
255 
256  glGetObjectParameterivARB(obj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &infologLength);
257 
258  if (infologLength > 0)
259  {
260  infoLog = (char *)malloc(infologLength);
261  glGetInfoLogARB(obj, infologLength, &charsWritten, infoLog);
262  moText msg = moText(infoLog);
263  if (MODebug2 != NULL) MODebug2->Log(msg);
264  free(infoLog);
265  }
266 }
267 
virtual MOboolean Init()
Definition: moShader.cpp:214
void compileVertShader(const moText &vert_source)
virtual MOboolean Finish()
void Error(moText p_text)
Anuncia y registra un error.
Definition: moAbstract.cpp:79
GLhandleARB m_VertexShader
Definition: moShaderGLSL.h:139
void printInfoLog(GLhandleARB obj)
GLhandleARB m_ProgramObject
Definition: moShaderGLSL.h:137
#define MOboolean
Definition: moTypes.h:385
MOint m_VertErrorCode
Definition: moShader.h:295
MOuint GetType()
Definition: moShader.h:249
virtual MOboolean Finish()
Definition: moShader.cpp:219
virtual moText LoadShaderSource(const moText &p_fn)
Definition: moShader.cpp:253
void PrintVertShaderLog()
virtual ~moShaderGLSL()
GLint GetUniformID(const moText &uName)
virtual void CreateFragShader(const moText &frag_source)
virtual void StopShader()
MOint m_FragErrorCode
Definition: moShader.h:296
virtual void StartShader()
Definition: moShader.cpp:228
clase de para manejar textos
Definition: moText.h:75
virtual void LoadVertShader(const moText &vert_filename)
void Log(moText p_text)
Escribe un mensaje en el archivo de registro (log)
Definition: moAbstract.cpp:123
moText0 moText
Definition: moText.h:291
GLhandleARB m_FragmentShader
Definition: moShaderGLSL.h:138
void compileFragShader(const moText &frag_source)
virtual void StopShader()
Definition: moShader.cpp:233
void linkProgram()
virtual void LoadShader(const moText &vert_filename, const moText &frag_filename)
virtual void LoadFragShader(const moText &frag_filename)
void SetType(MOuint p_type)
Definition: moShader.h:254
virtual void CreateShader(const moText &vert_source, const moText &frag_source)
static moDebug * MODebug2
Clase de impresión de errores para depuración
Definition: moAbstract.h:225
virtual MOboolean Init()
#define MO_FALSE
Definition: moTypes.h:369
LIBMOLDEO_API moText0 IntToStr(int a)
Definition: moText.cpp:1070
moText GetName()
Definition: moShader.h:259
void PrintFragShaderLog()
virtual void StartShader()
void Message(moText p_text)
Anuncia un mensaje al usuario además de guardarlo en el log de texto.
Definition: moAbstract.cpp:114
virtual void CreateVertShader(const moText &vert_source)
GLint GetAttribID(const moText &aName)