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 | //CG_PROFILE_FP40;;
|
---|
71 | cgGLSetOptimalOptions(sCgFragmentProfile);
|
---|
72 | // get the optimal vertex profile
|
---|
73 | sCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
|
---|
74 | cgGLSetOptimalOptions(sCgVertexProfile);
|
---|
75 |
|
---|
76 | // set textures to auto load
|
---|
77 | cgGLSetManageTextureParameters(sCgContext, false);
|
---|
78 |
|
---|
79 | ///////////////
|
---|
80 | //-- load the defaul programs for mrt (used if not defined by the user)
|
---|
81 |
|
---|
82 | ShaderProgram *vertexProgMrt, *fragmentProgMrt, *fragmentProgMrtTex;
|
---|
83 |
|
---|
84 | vertexProgMrt = CreateVertexProgram("mrt", "vtx", "defaultVertexMrt");
|
---|
85 | fragmentProgMrt = CreateFragmentProgram("mrt", "frag", "defaultFragmentMrt");
|
---|
86 | fragmentProgMrtTex = CreateFragmentProgram("mrt", "fragtex", "defaultFragmentTexMrt");
|
---|
87 |
|
---|
88 |
|
---|
89 | // provide the current view matrix:
|
---|
90 | // this is an automatic parameter that is updated each frame
|
---|
91 | fragmentProgMrt->AddParameter("viewMatrix", 0);
|
---|
92 | fragmentProgMrtTex->AddParameter("viewMatrix", 0);
|
---|
93 |
|
---|
94 |
|
---|
95 | // vertex program
|
---|
96 | vertexProgMrt->AddParameter("viewMatrix", 0);
|
---|
97 | // needed for for ssao
|
---|
98 | vertexProgMrt->AddParameter("modelMatrix", 1);
|
---|
99 | vertexProgMrt->AddParameter("oldModelMatrix", 2);
|
---|
100 |
|
---|
101 |
|
---|
102 |
|
---|
103 | //////////////////////////
|
---|
104 | //-- hack: use hardcoded tree animation (should be provided with a material script!)
|
---|
105 |
|
---|
106 | ShaderProgram *treeAnimProg, *treeAnimProgMrt;
|
---|
107 |
|
---|
108 | treeAnimProg = CreateVertexProgram("treeanimation", "animateVtx", "treeAnimation");
|
---|
109 | treeAnimProgMrt = CreateVertexProgram("treeanimation", "animateVtxMrt", "treeAnimationMrt");
|
---|
110 |
|
---|
111 | treeAnimProg->AddParameter("timer", 0);
|
---|
112 | treeAnimProg->AddParameter("windDir", 1);
|
---|
113 | treeAnimProg->AddParameter("windStrength", 2);
|
---|
114 | treeAnimProg->AddParameter("minMaxPos", 3);
|
---|
115 | treeAnimProg->AddParameter("frequency", 4);
|
---|
116 | treeAnimProg->AddParameter("lightDir", 5);
|
---|
117 |
|
---|
118 | treeAnimProgMrt->AddParameter("timer", 0);
|
---|
119 | treeAnimProgMrt->AddParameter("windDir", 1);
|
---|
120 | treeAnimProgMrt->AddParameter("windStrength", 2);
|
---|
121 | treeAnimProgMrt->AddParameter("minMaxPos", 3);
|
---|
122 | treeAnimProgMrt->AddParameter("frequency", 4);
|
---|
123 | treeAnimProgMrt->AddParameter("oldTimer", 5);
|
---|
124 |
|
---|
125 |
|
---|
126 | //ShaderProgram *normalMappingVertexMrt, *normalMappingFragmentMrt;
|
---|
127 |
|
---|
128 | //normalMappingVertexMrt = CreateFragmentProgram("normalMapping", "vtx", "normalMappingVertexMrt");
|
---|
129 | //normalMappingFragmentMrt = CreateFragmentProgram("normalMapping", "frag", "normalMappingFragmentMrt");
|
---|
130 |
|
---|
131 |
|
---|
132 | cout << "cg initialization successful" << endl;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | void ShaderManager::DelSingleton()
|
---|
137 | {
|
---|
138 | DEL_PTR(sShaderManager);
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | ShaderManager::~ShaderManager()
|
---|
143 | {
|
---|
144 | CLEAR_CONTAINER(mShaders);
|
---|
145 |
|
---|
146 | if (sCgContext)
|
---|
147 | cgDestroyContext(sCgContext);
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | ShaderProgram *ShaderManager::CreateFragmentProgram(const std::string &filename,
|
---|
152 | const std::string &funcName,
|
---|
153 | const std::string &name)
|
---|
154 | {
|
---|
155 | return CreateShaderProgram(filename, funcName, name, true);
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | ShaderProgram *ShaderManager::CreateVertexProgram(const std::string &filename,
|
---|
160 | const std::string &funcName,
|
---|
161 | const std::string &name)
|
---|
162 | {
|
---|
163 | return CreateShaderProgram(filename, funcName, name, false);
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | void ShaderManager::EnableFragmentProfile()
|
---|
168 | {
|
---|
169 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | void ShaderManager::EnableVertexProfile()
|
---|
174 | {
|
---|
175 | cgGLEnableProfile(sCgVertexProfile);
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | void ShaderManager::DisableFragmentProfile()
|
---|
180 | {
|
---|
181 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | void ShaderManager::DisableVertexProfile()
|
---|
186 | {
|
---|
187 | cgGLDisableProfile(sCgVertexProfile);
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | ShaderProgram *ShaderManager::CreateShaderProgram(const std::string &filename,
|
---|
192 | const std::string &funcName,
|
---|
193 | const std::string &name,
|
---|
194 | bool isFragment)
|
---|
195 | {
|
---|
196 | const string fullName = "src/shaders/" + filename + ".cg";
|
---|
197 |
|
---|
198 | ShaderProgram *p =
|
---|
199 | new ShaderProgram(sCgContext, fullName,
|
---|
200 | isFragment ? sCgFragmentProfile : sCgVertexProfile, funcName);
|
---|
201 |
|
---|
202 | if (GetShaderProgram(name))
|
---|
203 | {
|
---|
204 | cerr << "Program name " << name << "already chosen" << endl;
|
---|
205 |
|
---|
206 | DEL_PTR(p);
|
---|
207 | return p;
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (!p->IsValid())
|
---|
211 | {
|
---|
212 | cerr << "Program " << funcName << " in " << fullName << " failed to load" << endl;
|
---|
213 |
|
---|
214 | DEL_PTR(p);
|
---|
215 | return p;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | // everything ok
|
---|
220 | cout << "Program " << funcName << " in " << fullName << " loaded" << endl;
|
---|
221 |
|
---|
222 | mShaders.push_back(p);
|
---|
223 | mShaderMap[name] = p;
|
---|
224 |
|
---|
225 | return p;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | ShaderProgram *ShaderManager::GetShaderProgram(const std::string &name)
|
---|
230 | {
|
---|
231 | ShaderMap::const_iterator it = mShaderMap.find(name);
|
---|
232 |
|
---|
233 | if (it == mShaderMap.end())
|
---|
234 | return NULL;
|
---|
235 |
|
---|
236 | return (*it).second;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | }
|
---|
241 |
|
---|