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
moShader.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moShader.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  Andres Colubri
29 
30  Description:
31  Base class for GLSL and Cg shaders.
32 
33 *******************************************************************************/
34 
35 #include "moShader.h"
36 
37 #include "moArray.h"
38 moDefineDynamicArray(moShaderArray)
39 
40 //===========================================
41 //
42 // moTexturedGrid
43 //
44 //===========================================
45 
47 {
48  m_num_layers = 0;
49  m_size_x = 0;
50  m_size_y = 0;
51 }
52 
54 {
55  Finish();
56 }
57 
58 MOboolean moTexturedGrid::Init(MOint p_size_x, MOint p_size_y, MOint p_num_layers, const MOfloat p_grid_dx[], const MOfloat p_grid_dy[])
59 {
60  m_size_x = p_size_x;
61  m_size_y = p_size_y;
62 
63  m_num_layers = p_num_layers;
64  for (int i = 0; i < m_num_layers; i++)
65  {
66  m_grid_dx[i] = p_grid_dx[i];
67  m_grid_dy[i] = p_grid_dy[i];
68  }
69  return true;
70 }
71 
72 // The format of the grid parameters is the following:
73 // nx ny
74 // dx0 dy0
75 // dx1 dy1
76 // ...
77 // dxn dyn
78 // there nx and ny are the number of grid quads along x and y, and dxi, dyi
79 // are the grid spacings on each layer (the layer 0 represents the grid itself,
80 // the rest of the layers represent the texture coordinates).
82 {
83  moParam* param;
84  MOuint count = p_cfg->GetParam(p_param_idx).GetValue().GetSubValueCount();
85 
86  p_cfg->SetCurrentParamIndex(p_param_idx);
87  p_cfg->FirstValue();
88  for (MOuint i = 0; i < count; i++)
89  {
90  param = &p_cfg->GetParam();
91  if (i == 0)
92  {
93  m_size_x = param->GetValue().GetSubValue(0).Int() + 1;
94  m_size_y = param->GetValue().GetSubValue(1).Int() + 1;
95  }
96  else
97  {
98  // Reading layers.
99  m_grid_dx[i - 1] = param->GetValue().GetSubValue(0).Float();
100  m_grid_dy[i - 1] = param->GetValue().GetSubValue(1).Float();
101 
102  }
103  p_cfg->NextValue();
104  }
105 
106  // if there is only one parameter line, it should contain the
107  // grid dimensions. The 0th layer is default to the grid spacing
108  // corresponding to 1/(number of grid quads)
109  if (count == 1)
110  {
111  m_num_layers = 1;
112  m_grid_dx[0] = 1.0 / float(m_size_x - 1);
113  m_grid_dy[0] = 1.0 / float(m_size_y - 1);
114  }
115  else m_num_layers = count - 1;
116  return true;
117 }
118 
120 {
121  m_num_layers = 1;
122  m_size_x = 2;
123  m_size_y = 2;
124  m_grid_dx[0] = 1.0;
125  m_grid_dy[0] = 1.0;
126 }
127 
129  return true;
130 }
131 
133 {
134  if (layer < m_num_layers)
135  {
136  x = i * m_grid_dx[layer];
137  y = j * m_grid_dy[layer];
138  }
139  else
140  {
141  // If there are not layers defined below the 0th, then the texture coordinates
142  // are set identical to the grid coordinates.
143  x = i * m_grid_dx[0];
144  y = j * m_grid_dy[0];
145  }
146 }
147 
149 {
150  float x0, y0, x1, y1;
151 
152  for (int j = 0; j < m_size_y - 1; j++)
153  {
154 #ifndef OPENGLESV2
155  glBegin(GL_QUAD_STRIP);
156  for (int i = 0; i < m_size_x; i++)
157  {
158  GetPoint(0, i, j, x0, y0);
159  GetPoint(0, i, j + 1, x1, y1);
160 
161  x0 *= w; y0 *= h;
162  x1 *= w; y1 *= h;
163 
164  SetTexCoord(i, j, l);
165  glVertex2f(x0, y0);
166 
167  SetTexCoord(i, j + 1, l);
168  glVertex2f(x1, y1);
169  }
170  glEnd();
171 #endif
172  }
173 }
174 
176 {
177  float s, t;
178  for (int k = 1; k <= l; k++)
179  {
180  GetPoint(k, i, j, s, t);
181 #ifndef OPENGLESV2
182  glMultiTexCoord2fARB(GL_TEXTURE0_ARB + k - 1, s, t);
183 #endif
184  }
185 }
186 
188 {
189  Finish();
190  Init(p_src_grid.m_size_x, p_src_grid.m_size_y, p_src_grid.m_num_layers, p_src_grid.m_grid_dx, p_src_grid.m_grid_dy);
191  return *this;
192 }
193 
194 //===========================================
195 //
196 // moShader
197 //
198 //===========================================
199 
201 {
202  m_Active = 0;
203  m_VertErrorCode = 0;
204  m_FragErrorCode = 0;
205 
206  m_name = moText("");
207 }
208 
210 {
211  Finish();
212 }
213 
215 {
216  return moAbstract::Init();
217 }
218 
220 {
221  m_VertErrorCode = 0;
222  m_FragErrorCode = 0;
223 
224  if (ShaderActive()) StopShader();
225  return true;
226 }
227 
229 {
230  m_Active = 1;
231 }
232 
234 {
235  m_Active = 0;
236 }
237 
239 {
240  return m_Active;
241 }
242 
244 {
245  return m_VertErrorCode;
246 }
247 
249 {
250  return m_FragErrorCode;
251 }
252 
254 {
255  FILE *src_file = fopen(p_fn, "rt");
256  moText src_text;
257 
258  if (src_file)
259  {
260  char *content = NULL;
261  int count = 0;
262 
263  fseek(src_file, 0, SEEK_END);
264  count = ftell(src_file);
265  fseek(src_file, 0, SEEK_SET);
266  if (count > 0)
267  {
268  content = (char *)malloc(sizeof(char) * (count+1));
269  count = fread(content,sizeof(char), count, src_file);
270  content[count] = '\0';
271  }
272  fclose(src_file);
273 
274  src_text = moText(content);
275  }
276  else
277  {
278  moText errstr = "Shader source file ";
279  errstr += p_fn + moText(" not found") ;
280  if (MODebug2 != NULL) MODebug2->Error(errstr);
281  src_text = moText("");
282  }
283 
284  return src_text;
285 }
286 
virtual MOboolean Init()
Definition: moShader.cpp:214
MOint m_num_layers
Definition: moShader.h:148
void Error(moText p_text)
Anuncia y registra un error.
Definition: moAbstract.cpp:79
MOint m_size_x
Definition: moShader.h:144
moValueBase & GetSubValue(MOint p_indexsubvalue=0)
Definition: moValue.h:539
void Draw(MOint w, MOint h, MOint l)
Definition: moShader.cpp:148
MOboolean m_Active
Definition: moShader.h:294
bool NextValue()
Selecciona el próximo valor del parámetro actual.
Definition: moConfig.cpp:1545
moText m_name
Definition: moShader.h:290
#define MOboolean
Definition: moTypes.h:385
MOint m_VertErrorCode
Definition: moShader.h:295
virtual MOboolean Finish()
Definition: moShader.cpp:219
moTexturedGrid & operator=(const moTexturedGrid &p_src_grid)
Definition: moShader.cpp:187
moDefineDynamicArray(moShaderArray) moTexturedGrid
Definition: moShader.cpp:38
virtual moText LoadShaderSource(const moText &p_fn)
Definition: moShader.cpp:253
MOint FragErrorCode()
Definition: moShader.cpp:248
virtual ~moShader()
Definition: moShader.cpp:209
virtual MOboolean Init()
Inicializa el objeto.
Definition: moAbstract.cpp:141
MOfloat m_grid_dy[MO_MAX_TEXTURE_UNITS+1]
Definition: moShader.h:150
virtual MOboolean Finish()
Definition: moShader.cpp:128
MOint m_FragErrorCode
Definition: moShader.h:296
virtual void StartShader()
Definition: moShader.cpp:228
#define MOfloat
Definition: moTypes.h:403
clase de para manejar textos
Definition: moText.h:75
MOfloat m_grid_dx[MO_MAX_TEXTURE_UNITS+1]
Definition: moShader.h:149
MOfloat Float() const
Definition: moValue.cpp:835
moText0 moText
Definition: moText.h:291
#define MOint
Definition: moTypes.h:388
virtual void StopShader()
Definition: moShader.cpp:233
void Set1QuadGrid()
Definition: moShader.cpp:119
void SetTexCoord(MOint i, MOint j, MOint l)
Definition: moShader.cpp:175
static moDebug * MODebug2
Clase de impresión de errores para depuración
Definition: moAbstract.h:225
MOint m_size_y
Definition: moShader.h:145
moValue & GetValue(MOint i=-1)
Definition: moParam.cpp:1204
void GetPoint(MOint layer, MOint i, MOint j, MOfloat &x, MOfloat &y)
Definition: moShader.cpp:132
moParam & GetParam(MOint p_paramindex=-1)
Devuelve el parámetro por índice.
Definition: moConfig.cpp:984
virtual MOboolean ShaderActive()
Definition: moShader.cpp:238
virtual ~moTexturedGrid()
Definition: moShader.cpp:53
#define MOuint
Definition: moTypes.h:387
bool SetCurrentParamIndex(int)
Selecciona el parámetro por el índice.
Definition: moConfig.cpp:1497
MOint Int() const
Definition: moValue.cpp:773
MOuint GetSubValueCount()
Definition: moValue.h:545
bool FirstValue()
Selecciona el primer valor del parámetro actual.
Definition: moConfig.cpp:1532
MOint VertErrorCode()
Definition: moShader.cpp:243
almacena la configuración de los parámetros de un objeto en un archivo XML
Definition: moConfig.h:193