libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo es el conjunto de objetos y funciones, que permiten ejecutar las operaciones básicas de la plataforma Moldeo, y que compone su núcleo.
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Amigas 'defines' Grupos Páginas
moShaderManager.cpp
Ir a la documentación de este archivo.
1 /*******************************************************************************
2 
3  moShaderManager.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 *******************************************************************************/
31 
32 #include <moShaderManager.h>
33 #include <moTextureFilterIndex.h>
34 #include <moDataManager.h>
35 
36 
37 
38 
39 
40 
41 
42 
102 {
103  if (m_pResourceManager){
104  m_glmanager = m_pResourceManager->GetGLMan();
105  m_fbmanager = m_pResourceManager->GetFBMan();
106  } else return false;
107 
108  if (!m_glmanager || !m_fbmanager) return false;
109 
110  if (m_pTextureFilterIndex==NULL)
111  m_pTextureFilterIndex = new moTextureFilterIndex();
112 
113  m_shaders_array.Init(0, NULL);
114 
117 
118  if (m_Config.IsConfigLoaded()) {
119  m_pTextureFilterIndex->Init( &m_Config, 0, m_glmanager, m_fbmanager, this, m_pResourceManager->GetTextureMan(), m_pResourceManager->GetRenderMan());
120  } else {
121  m_pTextureFilterIndex->Init( m_glmanager, m_fbmanager, this, m_pResourceManager->GetTextureMan(), m_pResourceManager->GetRenderMan());
122  }
123 #ifndef OPENGLESV2
124  if ( m_glmanager->GetGLMajorVersion() >=2 ) {
125 #endif
126  if (!m_RenderShader.Initialized()) {
127  MODebug2->Message("moShaderManager::Init > Creating basic Render Shader...");
128  m_RenderShader.Init();
129  m_RenderShader.CreateShader(
130  moText(
131  "attribute vec4 position;\n"
132  "attribute vec3 colors;\n"
133  "attribute vec2 t_coordedge;\n"
134  "attribute vec2 t_coord;\n"
135  "attribute vec3 normal;\n"
136  "uniform mat4 projmatrix;\n"
137  "varying vec3 colorVarying;\n"
138  "varying vec3 v_normal;\n"
139  "varying vec2 v_texcoord;\n"
140  "varying vec2 v_texcoordedge;\n"
141  "\n"
142  "void main() {\n"
143  " colorVarying = colors;\n"
144  " v_normal = normal;\n"
145  " v_texcoord = t_coord;\n"
146  " v_texcoordedge = t_coordedge;\n"
147  " gl_Position = projmatrix*position;\n"
148  "}\n"
149  ),
150  moText(
151  #ifdef OPENGLESV2
152  "precision mediump float;"
153  #endif
154  "varying vec3 colorVarying;\n"
155  "uniform sampler2D t_image;\n"
156  "varying vec3 v_normal;\n"
157  "varying vec2 v_texcoord;\n"
158  "varying vec2 v_texcoordedge;\n"
159  "uniform float wseg;\n"
160  "uniform float hseg;\n"
161  "uniform float wireframe_width;"
162  "uniform vec3 a_light;\n"
163  "uniform vec3 color;\n"
164  "uniform float opacity;\n"
165  "\n"
166  "void main() {\n"
167  " vec4 texcolor = texture2D( t_image, v_texcoord );\n"
168  //" vec4 texcolor = vec4( abs(v_normal.x), abs(v_normal.y), abs(v_normal.z), 1.0);\n"///NORMALES COLORIDAS
169  //" vec4 texcolor = vec4( 1.0, 1.0, 1.0, 1.0);\n"
170  " vec2 vector_to_borderC = vec2( 1.0, 1.0 ) - v_texcoord;\n"
171  " vec2 vector_to_borderB = vec2( 1.0, 0.0 ) - v_texcoord;\n"
172  " vec2 vector_to_borderA = vec2( 0.0, 0.0 ) - v_texcoord;\n"
173  " vec2 vector_to_borderD = vec2( 0.0, 1.0 ) - v_texcoord;\n"
174  " float distance_to_borderX = v_texcoord.x;\n"
175  " float intensity = 0.6+0.4*abs(max( 0.0, dot( v_normal, -1.0*a_light )) );\n"
176  //" float intensity = abs(a_light.y);\n"
177  " float dX = floor( v_texcoord.x * wseg );\n"
178  " float dY = floor( v_texcoord.y * hseg );\n"
179  " float distance_to_borderXd = abs( v_texcoord.x - dX / wseg );\n"
180  " float distance_to_borderY = v_texcoord.y;\n"
181  " float distance_to_borderYd = abs( v_texcoord.y - dY / hseg );\n"
182  " float distance_to_borderA = length(vector_to_borderA);\n"
183  " float distance_to_borderB = length(vector_to_borderB);\n"
184  " float distance_to_borderC = length(vector_to_borderC);\n"
185  " float distance_to_borderD = length(vector_to_borderD);\n"
186  " vec4 wirecolor = vec4( 0.0, 1.0, 0.0, 1.0);\n"
187  " vec4 wirecolorA = vec4( 1.0, 0.0, 0.0, 1.0);\n"
188  " vec4 wirecolorB = vec4( 0.0, 1.0, 0.0, 1.0);\n"
189  " vec4 wirecolorC = vec4( 0.0, 0.0, 1.0, 1.0);\n"
190  " vec4 wirecolorD = vec4( 1.0, 1.0, 1.0, 1.0);\n"
191  " if (distance_to_borderA<(wireframe_width*10.0)) texcolor = wirecolorA;\n"
192  " if (distance_to_borderB<(wireframe_width*10.0)) texcolor = wirecolorB;\n"
193  " if (distance_to_borderC<(wireframe_width*10.0)) texcolor = wirecolorC;\n"
194  " if (distance_to_borderD<(wireframe_width*10.0)) texcolor = wirecolorD;\n"
195  " if (distance_to_borderXd<wireframe_width) texcolor = wirecolor;\n"
196  " if (distance_to_borderYd<wireframe_width) texcolor = wirecolor;\n"
197  " if (distance_to_borderXd>(-wireframe_width+1.0/wseg)) texcolor = wirecolor;\n"
198  " if (distance_to_borderYd>(-wireframe_width+1.0/hseg)) texcolor = wirecolor;\n"
199  //" vec4 mulcolor = vec4( colorVarying, 1.0 );\n"
200  " vec4 mulcolor = intensity*vec4( 1.0*color.r, 1.0*color.g, 1.0*color.b, 1.0*opacity );\n"
201  " gl_FragColor = vec4( mulcolor.x*texcolor.x, mulcolor.y*texcolor.y, mulcolor.z*texcolor.z, mulcolor.w*texcolor.w );\n"
202  "}\n"
203  )
204  );
205 
206  m_RenderShader.PrintVertShaderLog();
207  m_RenderShader.PrintFragShaderLog();
208 
209  m_RenderShaderPositionIndex = m_RenderShader.GetAttribID(moText("position"));
210  m_RenderShaderColorsIndex = m_RenderShader.GetAttribID(moText("colors"));
211  m_RenderShaderTexCoordIndex = m_RenderShader.GetAttribID(moText("t_coord"));
212  m_RenderShaderTexCoordEdgeIndex = m_RenderShader.GetAttribID(moText("t_coordedge"));
213  m_RenderShaderNormalIndex = m_RenderShader.GetAttribID(moText("normal"));
214 
215  m_RenderShaderColorIndex = m_RenderShader.GetUniformID(moText("color"));
216  m_RenderShaderOpacityIndex = m_RenderShader.GetUniformID(moText("opacity"));
217  m_RenderShaderTextureIndex = m_RenderShader.GetUniformID(moText("t_image"));
218  m_RenderShaderProjectionMatrixIndex = m_RenderShader.GetUniformID("projmatrix");
219  m_RenderShaderWireframeWidthIndex = m_RenderShader.GetUniformID(moText("wireframe_width"));
220  m_RenderShaderTexWSegmentsIndex = m_RenderShader.GetUniformID(moText("wseg"));
221  m_RenderShaderTexHSegmentsIndex = m_RenderShader.GetUniformID(moText("hseg"));
222  m_RenderShaderLightIndex = m_RenderShader.GetUniformID(moText("a_light"));
223 
225 
226  "moShaderManager::Init > m_RenderShader Attrib IDs,"
227  " position:"+IntToStr(m_RenderShaderPositionIndex)+""
228  " normal:"+IntToStr(m_RenderShaderNormalIndex)+""
229  " color:"+IntToStr(m_RenderShaderColorIndex)+""
230  " opacity:"+IntToStr(m_RenderShaderOpacityIndex)+""
231  " t_coord:"+IntToStr(m_RenderShaderTexCoordIndex)+""
232  " t_coordedge:"+IntToStr(m_RenderShaderTexCoordEdgeIndex)
233 
234  ));
235 
236  MODebug2->Message( moText("moShaderManager::Init > m_RenderShader Uniform IDs,")
237  +moText(" projmatrix:")+IntToStr(m_RenderShaderProjectionMatrixIndex)
238  +moText(" wireframe_width:")+IntToStr(m_RenderShaderWireframeWidthIndex)
239  +moText(" wseg:")+IntToStr(m_RenderShaderTexWSegmentsIndex)
240  +moText(" hseg:")+IntToStr(m_RenderShaderTexHSegmentsIndex)
241  +moText(" a_light:")+IntToStr(m_RenderShaderLightIndex)
242  +moText(" t_image:")+IntToStr(m_RenderShaderTextureIndex) );
243  }
244 #ifndef OPENGLESV2
245  }
246 #endif
247 
248  return (m_glmanager && m_fbmanager);
249 }
250 
252 {
255 
256  SetName("shadermanager");
257  SetLabelName("shadermanager");
258 
259  m_glmanager = NULL;
260  m_fbmanager = NULL;
261  m_pTextureFilterIndex = NULL;
262 }
263 
264 
265 
266 
268 {
269  if (m_pTextureFilterIndex) {
270  delete m_pTextureFilterIndex;
271  m_pTextureFilterIndex = NULL;
272  }
273 
274  m_shaders_array.Finish();
275 
276  m_glmanager = NULL;
277  m_fbmanager = NULL;
278 
279  return true;
280 }
281 
283 {
284  moShader* pshader;
285  for (MOuint i = 0; i < m_shaders_array.Count(); i++)
286  {
287  pshader = m_shaders_array[i];
288  if ((pshader != NULL) && (!stricmp(pshader->GetName(), p_name))) return i;
289  }
290  if (p_create_shader) return AddShader(p_name);
291  else return -1;
292 }
293 
295 {
296  moShader* pshader;
297  moShaderGLSL* pshader_glsl;
298 
299  if (p_type == MO_SHADER_GLSL)
300  {
301  pshader_glsl = new moShaderGLSL();
302  pshader_glsl->SetName(p_name);
303  pshader_glsl->Init();
304 
305  pshader = (moShader*)pshader_glsl;
306  m_shaders_array.Add(pshader);
307  return m_shaders_array.Count() - 1;
308  }
309  else if (p_type == MO_SHADER_CG)
310  {
311 #ifdef SHADER_CG
312  moShaderCG* pshader_cg;
313  pshader_cg = new moShaderCG();
314  pshader_cg->Init();
315  pshader_cg->SetName(p_name);
316  pshader = (moShader*)pshader_cg;
317  m_shaders_array.Add(pshader);
318  return m_shaders_array.Count() - 1;
319 #endif
320  return -1;
321  }
322 
323  return -1;
324 }
325 
327 {
328 
329  moConfig config;
330 
331  moText complete_fn = m_pResourceManager->GetDataMan()->GetDataPath() + moText("/");
332  complete_fn += p_filename;
333 
334 
335  if (config.LoadConfig(complete_fn) != MO_CONFIG_OK) {
336  moDebugManager::Error( moText("Couldn't load shader config :") + complete_fn );
337  return -1;
338  }
339 
340  moFile cfgFile( complete_fn );
341 
342  MOint type_idx = config.GetParamIndex("type");
343  if (type_idx == MO_PARAM_NOT_FOUND) {
344  moDebugManager::Error( moText("In shader config :")
345  + complete_fn
346  + moText(" type parameter not founded.")
347  );
348  return -1;
349  }
350 
351  MOint vertex_idx = config.GetParamIndex("vertex");
352  MOint fragment_idx = config.GetParamIndex("fragment");
353 
354  if ((vertex_idx == MO_PARAM_NOT_FOUND) && (fragment_idx == MO_PARAM_NOT_FOUND)) {
355  if (fragment_idx == MO_PARAM_NOT_FOUND)
356  moDebugManager::Error( moText("In shader config :")
357  + complete_fn
358  + moText(" fragment(pixel) shader parameter not founded.")
359  );
360  if (vertex_idx == MO_PARAM_NOT_FOUND)
361  moDebugManager::Error( moText("In shader config :")
362  + complete_fn
363  + moText(" vertex shader parameter not founded.")
364  );
365  return -1;
366  }
367 
368  moFile vertexFile;
369  moFile fragmentFile;
370 
371  moText vertex_fn;
372  moText fragment_fn;
373 
374  if (vertex_idx == MO_PARAM_NOT_FOUND) vertex_fn = moText("");
375  else
376  {
377  vertex_fn = cfgFile.GetPath() + moText("/");
378  vertex_fn = vertex_fn + config.GetParam(vertex_idx).GetValue().GetSubValue().Text();
379  vertexFile.SetCompletePath( vertex_fn );
380  if (!vertexFile.Exists()) {
381  vertex_fn = m_pResourceManager->GetDataMan()->GetDataPath() + moText("/");
382  vertex_fn = vertex_fn + config.GetParam(vertex_idx).GetValue().GetSubValue().Text();
383  }
384 
385  }
386 
387  if (fragment_idx == MO_PARAM_NOT_FOUND) fragment_fn = moText("");
388  else
389  {
390  fragment_fn = cfgFile.GetPath() + moText("/");
391  fragment_fn = fragment_fn + config.GetParam(fragment_idx).GetValue().GetSubValue().Text();
392  fragmentFile.SetCompletePath(fragment_fn);
393  if (!fragmentFile.Exists()) {
394  fragment_fn = m_pResourceManager->GetDataMan()->GetDataPath() + moText("/");
395  fragment_fn = fragment_fn + config.Text(fragment_idx);
396  }
397  }
398 
399  MOint grid_idx = config.GetParamIndex("grid");
400  moTexturedGrid tex_grid;
401  if (grid_idx == MO_PARAM_NOT_FOUND) tex_grid.Set1QuadGrid();
402  else tex_grid.Init(&config, grid_idx);
403 
404  int result = AddShader(config.GetParam(type_idx).GetValue().GetSubValue().Int(),
405  p_filename,
406  vertex_fn, fragment_fn, tex_grid);
407 
408  if ( result > -1 ) {
409  moShader* pshader = GetShader(result);
410  if (pshader) {
411  MODebug2->Message("Added shader, loading config: " + p_filename );
412  pshader->m_Config.LoadConfig( p_filename );
413  }
414  }
415 
416  return result;
417 }
418 
419 MOint moShaderManager::AddShader(MOuint p_type, moText p_name, moText p_vert_fn, moText p_frag_fn, moTexturedGrid p_tex_grid)
420 {
421  MOint idx = AddShader(p_type, p_name);
422  if (-1 < idx)
423  {
424  if (p_vert_fn == moText("")) GetShader(idx)->LoadFragShader(p_frag_fn);
425  else if (p_frag_fn == moText("")) GetShader(idx)->LoadVertShader(p_vert_fn);
426  else GetShader(idx)->LoadShader(p_vert_fn, p_frag_fn);
427  GetShader(idx)->SetGrid(p_tex_grid);
428  return idx;
429  }
430  else return -1;
431 }
432 
434 {
435  if (p_idx < (MOint)m_shaders_array.Count())
436  {
437  m_shaders_array.Remove(p_idx);
438  return true;
439  }
440  else return false;
441 }
442 
int GetGLMajorVersion()
Definition: moGLManager.h:383
MOboolean Initialized()
Pregunta si está inicializado.
Definition: moAbstract.cpp:153
moConfig m_Config
Definition: moShader.h:286
void SetName(const moText &p_name)
moValueBase & GetSubValue(MOint p_indexsubvalue=0)
Definition: moValue.h:539
void SetGrid(moTexturedGrid p_grid)
Definition: moShader.h:275
moRenderManager * GetRenderMan()
#define MOboolean
Definition: moTypes.h:385
static void Error(moText p_text)
Anuncia un error.
int GetParamIndex(moText p_paramname)
Devuelve el índice correspondiente al parámetro por nombre.
Definition: moConfig.cpp:1008
void SetResourceType(moResourceType p_restype)
virtual MOboolean Init(MOint p_size_x, MOint p_size_y, MOint p_num_layers, const MOfloat p_grid_dx[], const MOfloat p_grid_dy[])
Definition: moShader.cpp:58
virtual MOboolean Init()
virtual MOboolean Finish()
void PrintVertShaderLog()
GLint GetUniformID(const moText &uName)
moGLManager * GetGLMan()
void SetCompletePath(moText p_completepath)
Definition: moFile.cpp:482
MOboolean Exists()
Definition: moFile.cpp:436
virtual MOboolean Init()
clase de para manejar textos
Definition: moText.h:75
virtual void LoadShader(const moText &vert_filename, const moText &frag_filename)=0
moResourceManager * m_pResourceManager
Puntero al administrador de recursos.
Dispositivo de entrada/salida, típicamente, interfaces humanas de IO y datos ( teclado, mouse, tableta, tcp, udp, serial )
Definition: moTypes.h:532
moTypes MOint moText moParamIndex moParamReference int iRow int int i int i
Definition: all_f.js:18
MOboolean DeleteShader(MOint p_idx)
virtual MOboolean Init(moParam *p_param, moGLManager *p_glman, moFBManager *p_fbman, moShaderManager *p_shaman, moTextureManager *p_texman, moRenderManager *p_renderman)
moText0 moText
Definition: moText.h:291
moConfig m_Config
Configuración de parámetros del objeto.
int LoadConfig(moText p_filename)
Lee la configuracion de un archivo.
Definition: moConfig.cpp:402
moText GetPath()
Retreive full file name: return "myFileName.txt", extension is included.
Definition: moFile.cpp:533
#define MOint
Definition: moTypes.h:388
virtual void LoadVertShader(const moText &vert_filename)=0
#define MO_PARAM_NOT_FOUND
Definition: moConfig.h:41
void Set1QuadGrid()
Definition: moShader.cpp:119
moFBManager * GetFBMan()
moShader * GetShader(MOuint p_idx)
void SetLabelName(const moText &p_labelname)
MOboolean IsConfigLoaded()
Indica si ha sido cargado con éxito el archivo de configuración.
Definition: moConfig.cpp:279
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
moDataManager * GetDataMan()
moText Text()
Definition: moValue.cpp:539
virtual MOboolean Init()
moValue & GetValue(MOint i=-1)
Definition: moParam.cpp:1204
moParam & GetParam(MOint p_paramindex=-1)
Devuelve el parámetro por índice.
Definition: moConfig.cpp:984
moText GetDataPath()
#define MOuint
Definition: moTypes.h:387
LIBMOLDEO_API moText0 IntToStr(int a)
Definition: moText.cpp:1070
MOint GetShaderIndex(moText p_name, MOboolean p_create_shader)
moText GetName()
Definition: moShader.h:259
MOint Int() const
Definition: moValue.cpp:773
MOint AddShader(MOuint p_type, moText p_name)
void PrintFragShaderLog()
void SetName(const moText &p_name)
Definition: moShader.h:264
virtual void LoadFragShader(const moText &frag_filename)=0
void Message(moText p_text)
Anuncia un mensaje al usuario además de guardarlo en el log de texto.
Definition: moAbstract.cpp:114
#define MO_CONFIG_OK
Definition: moConfig.h:43
void SetType(moMoldeoObjectType p_type)
GLint GetAttribID(const moText &aName)
moTextureManager * GetTextureMan()
moText Text(moParamReference p_paramreference)
Acceso rápido a un valor de texto.
Definition: moConfig.cpp:1070
almacena la configuración de los parámetros de un objeto en un archivo XML
Definition: moConfig.h:193