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.
moShaderCG.cpp
Ir a la documentación de este archivo.
1 /*******************************************************************************
2 
3  moShaderCG.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 Cg shaders.
32 
33 *******************************************************************************/
34 
35 #include "moShaderCG.h"
36 
37 #ifdef SHADER_CG
38 
39 moShaderCG::moShaderCG() : moShader()
40 {
42 
43  m_cgContext = NULL;
44  m_cgFragmentShader = NULL;
45  m_cgVertexShader = NULL;
46 }
47 
48 moShaderCG::~moShaderCG()
49 {
50  Finish();
51 }
52 
53 MOboolean moShaderCG::Init()
54 {
55  return moShader::Init();
56 }
57 
58 MOboolean moShaderCG::Finish()
59 {
60  if (m_cgVertexShader != NULL)
61  {
62  cgDestroyProgram(m_cgVertexShader);
63  m_cgVertexShader = NULL;
64  }
65  if (m_cgFragmentShader != NULL)
66  {
67  cgDestroyProgram(m_cgFragmentShader);
68  m_cgFragmentShader = NULL;
69  }
70 
71  if(m_cgContext != NULL)
72  {
73  cgDestroyContext(m_cgContext);
74  m_cgContext = NULL;
75  }
76 
77  return moShader::Finish();
78 }
79 
80 void moShaderCG::CreateVertShader(moText vert_source)
81 {
82  m_cgContext = cgCreateContext();
83  if (m_cgContext) {
84  compileVertShader(vert_source);
85  } else {
86  MODebug2->Error( moText("moShaderCG:: Couldn't create CG Context") );
87  }
88 }
89 
90 void moShaderCG::CreateFragShader(moText frag_source)
91 {
92  m_cgContext = cgCreateContext();
93  if (m_cgContext) {
94  compileFragShader(frag_source);
95  } else {
96  MODebug2->Error( moText("moShaderCG:: Couldn't create CG Context") );
97  }
98 }
99 
100 void moShaderCG::CreateShader(moText vert_source, moText frag_source)
101 {
102  m_cgContext = cgCreateContext();
103  if (m_cgContext) {
104  compileVertShader(vert_source);
105  compileFragShader(frag_source);
106  } else {
107  MODebug2->Error( moText("moShaderCG:: Couldn't create CG Context") );
108  }
109 }
110 
111 void moShaderCG::LoadVertShader(moText vert_filename)
112 {
113  moText vert_source = LoadShaderSource(vert_filename);
114  CreateVertShader(vert_source);
115 }
116 
117 void moShaderCG::LoadFragShader(moText frag_filename)
118 {
119  moText frag_source = LoadShaderSource(frag_filename);
120  CreateFragShader(frag_source);
121 }
122 
123 void moShaderCG::LoadShader(moText vert_filename, moText frag_filename)
124 {
125  moText vert_source = LoadShaderSource(vert_filename);
126  moText frag_source = LoadShaderSource(frag_filename);
127  CreateShader(vert_source, frag_source);
128 }
129 #include "moDebugManager.h"
130 
131 static void checkForCgError( moText situation )
132 {
133  CGerror error;
134  const char *string = cgGetLastErrorString(&error);
135 
136  if (error != CG_NO_ERROR) {
137  moDebugManager::Error( situation
138  + (" ")
139  + moText(string) );
140  if (error == CG_COMPILER_ERROR) {
141  //moDebugManager::Error( moText( cgGetLastListing(m_cgContext) ) );
142  }
143  //exit(1);
144  }
145 }
146 
147 
148 void moShaderCG::StartShader()
149 {
151 
152  if (m_cgVertexShader != NULL)
153  {
154  cgGLBindProgram(m_cgVertexShader);
155  checkForCgError("Binding vertex program");
156  cgGLEnableProfile(m_cgVertexProfile);
157  checkForCgError("enabling vertex profile");
158  }
159  if (m_cgFragmentShader != NULL)
160  {
161  cgGLBindProgram(m_cgFragmentShader);
162  checkForCgError("Binding fragment program");
163  cgGLEnableProfile(m_cgFragmentProfile);
164  checkForCgError("enabling fragments profile");
165  }
166 
167 
168 }
169 
170 void moShaderCG::StopShader()
171 {
172  if (m_cgVertexShader != NULL) cgGLDisableProfile(m_cgVertexProfile);
173  if (m_cgFragmentShader != NULL) cgGLDisableProfile(m_cgFragmentProfile);
174 
176 }
177 
178 CGparameter moShaderCG::GetVertParameter(moText pName)
179 {
180  return cgGetNamedParameter(m_cgVertexShader, pName);
181 }
182 
183 CGparameter moShaderCG::GetFragParameter(moText pName)
184 {
185  return cgGetNamedParameter(m_cgFragmentShader, pName);
186 }
187 
188 void moShaderCG::compileVertShader(moText vert_source)
189 {
190  m_cgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
191  if (m_cgVertexProfile == CG_PROFILE_UNKNOWN)
192  {
193  if (MODebug != NULL) MODebug2->Error(moText("Vertex profile not found"));
194  m_VertErrorCode = -1;
195  return;
196  }
197  cgGLSetOptimalOptions(m_cgVertexProfile);
198  m_cgVertexShader = cgCreateProgram(m_cgContext, CG_SOURCE, vert_source, m_cgVertexProfile, NULL, NULL);
199  if (m_cgVertexShader == NULL)
200  {
201  CGerror Error = cgGetError();
202  const char* errorString = cgGetErrorString(Error);
203  if (MODebug2 != NULL)
204  {
205  MODebug2->Error(moText("Cg error!"));
206  MODebug2->Error(
207  moText("Vertex shader [")
208  + vert_source
209  + moText("] could not be created: ")
210  + moText(errorString));
211  MODebug2->Error(moText(cgGetLastListing(m_cgContext)));
212  }
213  m_VertErrorCode = 1;
214  }
215  else
216  {
217  cgGLLoadProgram(m_cgVertexShader);
218  m_VertErrorCode = 0;
219  }
220 }
221 
222 void moShaderCG::compileFragShader(moText frag_source)
223 {
224  m_cgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
225  if (m_cgFragmentProfile == CG_PROFILE_UNKNOWN)
226  {
227  MODebug2->Error(moText("Fragment profile not found"));
228  m_FragErrorCode = -1;
229  return;
230  }
231  cgGLSetOptimalOptions(m_cgFragmentProfile);
232  m_cgFragmentShader = cgCreateProgram(m_cgContext, CG_SOURCE, frag_source, m_cgFragmentProfile, NULL, NULL);
233  if (m_cgFragmentShader == NULL)
234  {
235  CGerror Error = cgGetError();
236  const char* errorString = cgGetErrorString(Error);
237  if (MODebug != NULL)
238  {
239  MODebug2->Error(moText("Cg error!"));
240  MODebug2->Error(
241  moText("Fragment shader [")
242  + frag_source
243  + moText("] could not be created: ")
244  + moText(errorString));
245  MODebug2->Error(moText(cgGetLastListing(m_cgContext)));
246 
247  }
248  m_FragErrorCode = 1;
249  }
250  else
251  {
252  cgGLLoadProgram(m_cgFragmentShader);
253  m_FragErrorCode = 0;
254  }
255 }
256 
257 #endif
virtual MOboolean Init()
Definition: moShader.cpp:214
void Error(moText p_text)
Anuncia y registra un error.
Definition: moAbstract.cpp:79
#define MOboolean
Definition: moTypes.h:385
static void Error(moText p_text)
Anuncia un error.
virtual void CreateShader(const moText &vert_source, const moText &frag_source)=0
MOint m_VertErrorCode
Definition: moShader.h:330
virtual MOboolean Finish()
Definition: moShader.cpp:219
virtual void CreateFragShader(const moText &frag_source)=0
virtual moText LoadShaderSource(const moText &p_fn)
Definition: moShader.cpp:263
MOint m_FragErrorCode
Definition: moShader.h:331
virtual void StartShader()
Definition: moShader.cpp:228
clase de para manejar textos
Definition: moText.h:75
virtual void compileFragShader(const moText &frag_source)=0
moText0 moText
Definition: moText.h:291
virtual void CreateVertShader(const moText &vert_source)=0
virtual void StopShader()
Definition: moShader.cpp:233
void SetType(MOuint p_type)
Definition: moShader.h:288
static moDebug * MODebug2
Clase de impresión de errores para depuración.
Definition: moAbstract.h:225
virtual void compileVertShader(const moText &vert_source)=0
static moTextHeap * MODebug
Lista de textos.
Definition: moAbstract.h:226