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