1 | #include "Material.h"
|
---|
2 | #include "Texture.h"
|
---|
3 | #include "RenderState.h"
|
---|
4 | #include "ShaderProgram.h"
|
---|
5 | #include "ShaderManager.h"
|
---|
6 | #include "Matrix4x4.h"
|
---|
7 | #include <Cg/cg.h>
|
---|
8 | #include <Cg/cgGL.h>
|
---|
9 |
|
---|
10 |
|
---|
11 | #ifdef _CRT_SET
|
---|
12 | #define _CRTDBG_MAP_ALLOC
|
---|
13 | #include <stdlib.h>
|
---|
14 | #include <crtdbg.h>
|
---|
15 |
|
---|
16 | // redefine new operator
|
---|
17 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
18 | #define new DEBUG_NEW
|
---|
19 | #endif
|
---|
20 |
|
---|
21 |
|
---|
22 | using namespace std;
|
---|
23 |
|
---|
24 |
|
---|
25 | namespace CHCDemoEngine
|
---|
26 | {
|
---|
27 |
|
---|
28 |
|
---|
29 | // only instance of the shader manager
|
---|
30 | ShaderManager *ShaderManager::sShaderManager = NULL;
|
---|
31 |
|
---|
32 | /////////
|
---|
33 | //-- cg stuff
|
---|
34 |
|
---|
35 | static CGcontext sCgContext = NULL;
|
---|
36 |
|
---|
37 | static CGprofile sCgFragmentProfile;
|
---|
38 | static CGprofile sCgVertexProfile;
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 | static void cgErrorCallback()
|
---|
43 | {
|
---|
44 | CGerror lastError = cgGetError();
|
---|
45 |
|
---|
46 | if (lastError)
|
---|
47 | {
|
---|
48 | printf("Cg error, exiting...\n");
|
---|
49 |
|
---|
50 | //Debug << "\n" << cgGetErrorString(lastError) << endl;
|
---|
51 | //Debug << "\n" << cgGetLastListing(sCgContext) << endl;
|
---|
52 |
|
---|
53 | printf("%s\n\n", cgGetErrorString(lastError));
|
---|
54 | printf("%s\n", cgGetLastListing(sCgContext));
|
---|
55 |
|
---|
56 | exit(0);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | ShaderManager::ShaderManager()
|
---|
62 | {
|
---|
63 | // setup cg
|
---|
64 | cgSetErrorCallback(cgErrorCallback);
|
---|
65 | // create context.
|
---|
66 | sCgContext = cgCreateContext();
|
---|
67 | // get the optimal fragment profile
|
---|
68 | sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
|
---|
69 | //sCgFragmentProfile = CG_PROFILE_GPU_FP;
|
---|
70 | if (sCgFragmentProfile == CG_PROFILE_FP40)
|
---|
71 | cout << "cg profile: fp40" << endl;
|
---|
72 | else
|
---|
73 | cout << "cg profile " << sCgFragmentProfile << endl;
|
---|
74 |
|
---|
75 | cgGLSetOptimalOptions(sCgFragmentProfile);
|
---|
76 | // get the optimal vertex profile
|
---|
77 | sCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
|
---|
78 | cgGLSetOptimalOptions(sCgVertexProfile);
|
---|
79 |
|
---|
80 | // set textures to auto load
|
---|
81 | cgGLSetManageTextureParameters(sCgContext, false);
|
---|
82 |
|
---|
83 | ///////////////
|
---|
84 | //-- load the defaul programs for mrt (used if not defined by the user)
|
---|
85 |
|
---|
86 | ShaderProgram *vertexProgMrt, *fragmentProgMrt, *fragmentProgMrtTex;
|
---|
87 |
|
---|
88 | vertexProgMrt = CreateVertexProgram("mrt", "vtx", "defaultVertexMrt");
|
---|
89 | fragmentProgMrt = CreateFragmentProgram("mrt", "frag", "defaultFragmentMrt");
|
---|
90 | fragmentProgMrtTex = CreateFragmentProgram("mrt", "fragtex", "defaultFragmentTexMrt");
|
---|
91 |
|
---|
92 |
|
---|
93 | // provide the current view matrix:
|
---|
94 | // this is an automatic parameter that is updated each frame
|
---|
95 | fragmentProgMrt->AddParameter("viewMatrix", 0);
|
---|
96 | fragmentProgMrtTex->AddParameter("viewMatrix", 0);
|
---|
97 |
|
---|
98 |
|
---|
99 | // vertex program
|
---|
100 | vertexProgMrt->AddParameter("viewMatrix", 0);
|
---|
101 | // needed for for ssao
|
---|
102 | vertexProgMrt->AddParameter("modelMatrix", 1);
|
---|
103 | vertexProgMrt->AddParameter("oldModelMatrix", 2);
|
---|
104 |
|
---|
105 |
|
---|
106 |
|
---|
107 | //////////////////////////
|
---|
108 | //-- hack: use hardcoded tree animation (should be provided with a material script!)
|
---|
109 |
|
---|
110 | ShaderProgram *treeAnimProg, *treeAnimProgMrt;
|
---|
111 |
|
---|
112 | treeAnimProg = CreateVertexProgram("treeanimation", "animateVtx", "treeAnimation");
|
---|
113 | treeAnimProgMrt = CreateVertexProgram("treeanimation", "animateVtxMrt", "treeAnimationMrt");
|
---|
114 |
|
---|
115 | treeAnimProg->AddParameter("timer", 0);
|
---|
116 | treeAnimProg->AddParameter("windDir", 1);
|
---|
117 | treeAnimProg->AddParameter("windStrength", 2);
|
---|
118 | treeAnimProg->AddParameter("minMaxPos", 3);
|
---|
119 | treeAnimProg->AddParameter("frequency", 4);
|
---|
120 | treeAnimProg->AddParameter("lightDir", 5);
|
---|
121 |
|
---|
122 | treeAnimProgMrt->AddParameter("timer", 0);
|
---|
123 | treeAnimProgMrt->AddParameter("windDir", 1);
|
---|
124 | treeAnimProgMrt->AddParameter("windStrength", 2);
|
---|
125 | treeAnimProgMrt->AddParameter("minMaxPos", 3);
|
---|
126 | treeAnimProgMrt->AddParameter("frequency", 4);
|
---|
127 | treeAnimProgMrt->AddParameter("oldTimer", 5);
|
---|
128 |
|
---|
129 |
|
---|
130 | ShaderProgram *normalMappingVertexMrt, *normalMappingFragmentMrt;
|
---|
131 |
|
---|
132 | normalMappingVertexMrt = CreateVertexProgram("normalMapping", "vtx", "normalMappingVertexMrt");
|
---|
133 | normalMappingFragmentMrt = CreateFragmentProgram("normalMapping", "frag", "normalMappingFragmentMrt");
|
---|
134 |
|
---|
135 |
|
---|
136 | // provide the current view matrix:
|
---|
137 | // this is an automatic parameter that is updated each frame
|
---|
138 | normalMappingFragmentMrt->AddParameter("viewMatrix", 0);
|
---|
139 |
|
---|
140 | // vertex program
|
---|
141 | normalMappingVertexMrt->AddParameter("viewMatrix", 0);
|
---|
142 | // needed for for ssao
|
---|
143 | normalMappingVertexMrt->AddParameter("modelMatrix", 1);
|
---|
144 | normalMappingVertexMrt->AddParameter("oldModelMatrix", 2);
|
---|
145 |
|
---|
146 |
|
---|
147 | cout << "cg initialization successful" << endl;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | void ShaderManager::DelSingleton()
|
---|
152 | {
|
---|
153 | DEL_PTR(sShaderManager);
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | ShaderManager::~ShaderManager()
|
---|
158 | {
|
---|
159 | CLEAR_CONTAINER(mShaders);
|
---|
160 |
|
---|
161 | if (sCgContext)
|
---|
162 | cgDestroyContext(sCgContext);
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | ShaderProgram *ShaderManager::CreateFragmentProgram(const std::string &filename,
|
---|
167 | const std::string &funcName,
|
---|
168 | const std::string &name)
|
---|
169 | {
|
---|
170 | return CreateShaderProgram(filename, funcName, name, true);
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | ShaderProgram *ShaderManager::CreateVertexProgram(const std::string &filename,
|
---|
175 | const std::string &funcName,
|
---|
176 | const std::string &name)
|
---|
177 | {
|
---|
178 | return CreateShaderProgram(filename, funcName, name, false);
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | void ShaderManager::EnableFragmentProfile()
|
---|
183 | {
|
---|
184 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | void ShaderManager::EnableVertexProfile()
|
---|
189 | {
|
---|
190 | cgGLEnableProfile(sCgVertexProfile);
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | void ShaderManager::DisableFragmentProfile()
|
---|
195 | {
|
---|
196 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | void ShaderManager::DisableVertexProfile()
|
---|
201 | {
|
---|
202 | cgGLDisableProfile(sCgVertexProfile);
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | ShaderProgram *ShaderManager::CreateShaderProgram(const std::string &filename,
|
---|
207 | const std::string &funcName,
|
---|
208 | const std::string &name,
|
---|
209 | bool isFragment)
|
---|
210 | {
|
---|
211 | const string fullName = "src/shaders/" + filename + ".cg";
|
---|
212 |
|
---|
213 | ShaderProgram *p;
|
---|
214 |
|
---|
215 | if (isFragment)
|
---|
216 | {
|
---|
217 | p = new ShaderProgram(sCgContext, fullName, sCgFragmentProfile, funcName, ShaderProgram::FRAGMENT_PROGRAM);
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | p = new ShaderProgram(sCgContext, fullName, sCgVertexProfile, funcName, ShaderProgram::VERTEX_PROGRAM);
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (GetShaderProgram(name))
|
---|
225 | {
|
---|
226 | cerr << "Program name " << name << "already chosen" << endl;
|
---|
227 |
|
---|
228 | DEL_PTR(p);
|
---|
229 | return p;
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (!p->IsValid())
|
---|
233 | {
|
---|
234 | cerr << "Program " << funcName << " in " << fullName << " failed to load" << endl;
|
---|
235 |
|
---|
236 | DEL_PTR(p);
|
---|
237 | return p;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | // everything ok
|
---|
242 | cout << "Program " << funcName << " in " << fullName << " loaded" << endl;
|
---|
243 |
|
---|
244 | mShaders.push_back(p);
|
---|
245 | mShaderMap[name] = p;
|
---|
246 |
|
---|
247 | return p;
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | ShaderProgram *ShaderManager::GetShaderProgram(const std::string &name)
|
---|
252 | {
|
---|
253 | ShaderMap::const_iterator it = mShaderMap.find(name);
|
---|
254 |
|
---|
255 | if (it == mShaderMap.end())
|
---|
256 | return NULL;
|
---|
257 |
|
---|
258 | return (*it).second;
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | }
|
---|
263 |
|
---|