[3045] | 1 | #include "Material.h"
|
---|
| 2 | #include "Texture.h"
|
---|
| 3 | #include "RenderState.h"
|
---|
| 4 | #include "ShaderProgram.h"
|
---|
[3055] | 5 | #include "ShaderManager.h"
|
---|
[3113] | 6 | #include "Matrix4x4.h"
|
---|
[3045] | 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 |
|
---|
[3056] | 29 | // only instance of the shader manager
|
---|
| 30 | ShaderManager *ShaderManager::sShaderManager = NULL;
|
---|
| 31 |
|
---|
[3045] | 32 | /////////
|
---|
| 33 | //-- cg stuff
|
---|
| 34 |
|
---|
| 35 | static CGcontext sCgContext = NULL;
|
---|
| 36 |
|
---|
| 37 | static CGprofile sCgFragmentProfile;
|
---|
| 38 | static CGprofile sCgVertexProfile;
|
---|
[3055] | 39 |
|
---|
[3045] | 40 |
|
---|
| 41 |
|
---|
| 42 | static void cgErrorCallback()
|
---|
| 43 | {
|
---|
| 44 | CGerror lastError = cgGetError();
|
---|
| 45 |
|
---|
[3144] | 46 | if (lastError)
|
---|
[3045] | 47 | {
|
---|
[3113] | 48 | printf("Cg error, exiting...\n");
|
---|
| 49 |
|
---|
[3116] | 50 | //Debug << "\n" << cgGetErrorString(lastError) << endl;
|
---|
| 51 | //Debug << "\n" << cgGetLastListing(sCgContext) << endl;
|
---|
[3113] | 52 |
|
---|
| 53 | printf("%s\n\n", cgGetErrorString(lastError));
|
---|
[3045] | 54 | printf("%s\n", cgGetLastListing(sCgContext));
|
---|
| 55 |
|
---|
| 56 | exit(0);
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
[3055] | 61 | ShaderManager::ShaderManager()
|
---|
[3045] | 62 | {
|
---|
[3055] | 63 | // setup cg
|
---|
| 64 | cgSetErrorCallback(cgErrorCallback);
|
---|
| 65 | // create context.
|
---|
| 66 | sCgContext = cgCreateContext();
|
---|
| 67 | // get the optimal fragment profile
|
---|
| 68 | sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
|
---|
[3112] | 69 | //sCgFragmentProfile = CG_PROFILE_GPU_FP;
|
---|
[3251] | 70 | if (sCgFragmentProfile == CG_PROFILE_FP40)
|
---|
| 71 | cout << "cg profile: fp40" << endl;
|
---|
| 72 | else
|
---|
| 73 | cout << "cg profile " << sCgFragmentProfile << endl;
|
---|
| 74 |
|
---|
[3055] | 75 | cgGLSetOptimalOptions(sCgFragmentProfile);
|
---|
| 76 | // get the optimal vertex profile
|
---|
| 77 | sCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
|
---|
| 78 | cgGLSetOptimalOptions(sCgVertexProfile);
|
---|
[3045] | 79 |
|
---|
[3055] | 80 | // set textures to auto load
|
---|
| 81 | cgGLSetManageTextureParameters(sCgContext, false);
|
---|
[3056] | 82 |
|
---|
| 83 | ///////////////
|
---|
| 84 | //-- load the defaul programs for mrt (used if not defined by the user)
|
---|
[3045] | 85 |
|
---|
[3056] | 86 | ShaderProgram *vertexProgMrt, *fragmentProgMrt, *fragmentProgMrtTex;
|
---|
[3045] | 87 |
|
---|
[3056] | 88 | vertexProgMrt = CreateVertexProgram("mrt", "vtx", "defaultVertexMrt");
|
---|
| 89 | fragmentProgMrt = CreateFragmentProgram("mrt", "frag", "defaultFragmentMrt");
|
---|
| 90 | fragmentProgMrtTex = CreateFragmentProgram("mrt", "fragtex", "defaultFragmentTexMrt");
|
---|
| 91 |
|
---|
[3128] | 92 |
|
---|
[3113] | 93 | // provide the current view matrix:
|
---|
| 94 | // this is an automatic parameter that is updated each frame
|
---|
[3056] | 95 | fragmentProgMrt->AddParameter("viewMatrix", 0);
|
---|
| 96 | fragmentProgMrtTex->AddParameter("viewMatrix", 0);
|
---|
| 97 |
|
---|
[3114] | 98 |
|
---|
| 99 | // vertex program
|
---|
[3113] | 100 | vertexProgMrt->AddParameter("viewMatrix", 0);
|
---|
[3114] | 101 | // needed for for ssao
|
---|
[3113] | 102 | vertexProgMrt->AddParameter("modelMatrix", 1);
|
---|
| 103 | vertexProgMrt->AddParameter("oldModelMatrix", 2);
|
---|
| 104 |
|
---|
| 105 |
|
---|
[3045] | 106 |
|
---|
[3055] | 107 | //////////////////////////
|
---|
[3056] | 108 | //-- hack: use hardcoded tree animation (should be provided with a material script!)
|
---|
[3045] | 109 |
|
---|
[3056] | 110 | ShaderProgram *treeAnimProg, *treeAnimProgMrt;
|
---|
[3045] | 111 |
|
---|
[3056] | 112 | treeAnimProg = CreateVertexProgram("treeanimation", "animateVtx", "treeAnimation");
|
---|
| 113 | treeAnimProgMrt = CreateVertexProgram("treeanimation", "animateVtxMrt", "treeAnimationMrt");
|
---|
[3045] | 114 |
|
---|
[3056] | 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);
|
---|
[3045] | 121 |
|
---|
[3056] | 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);
|
---|
[3116] | 127 | treeAnimProgMrt->AddParameter("oldTimer", 5);
|
---|
[3056] | 128 |
|
---|
[3128] | 129 |
|
---|
[3144] | 130 | ShaderProgram *normalMappingVertexMrt, *normalMappingFragmentMrt;
|
---|
[3128] | 131 |
|
---|
[3144] | 132 | normalMappingVertexMrt = CreateVertexProgram("normalMapping", "vtx", "normalMappingVertexMrt");
|
---|
| 133 | normalMappingFragmentMrt = CreateFragmentProgram("normalMapping", "frag", "normalMappingFragmentMrt");
|
---|
[3128] | 134 |
|
---|
| 135 |
|
---|
[3146] | 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 |
|
---|
[3055] | 147 | cout << "cg initialization successful" << endl;
|
---|
[3045] | 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
[3055] | 151 | void ShaderManager::DelSingleton()
|
---|
[3045] | 152 | {
|
---|
[3055] | 153 | DEL_PTR(sShaderManager);
|
---|
| 154 | }
|
---|
[3045] | 155 |
|
---|
| 156 |
|
---|
[3055] | 157 | ShaderManager::~ShaderManager()
|
---|
| 158 | {
|
---|
| 159 | CLEAR_CONTAINER(mShaders);
|
---|
| 160 |
|
---|
| 161 | if (sCgContext)
|
---|
| 162 | cgDestroyContext(sCgContext);
|
---|
[3045] | 163 | }
|
---|
| 164 |
|
---|
| 165 |
|
---|
[3055] | 166 | ShaderProgram *ShaderManager::CreateFragmentProgram(const std::string &filename,
|
---|
| 167 | const std::string &funcName,
|
---|
| 168 | const std::string &name)
|
---|
[3045] | 169 | {
|
---|
[3055] | 170 | return CreateShaderProgram(filename, funcName, name, true);
|
---|
[3045] | 171 | }
|
---|
| 172 |
|
---|
| 173 |
|
---|
[3055] | 174 | ShaderProgram *ShaderManager::CreateVertexProgram(const std::string &filename,
|
---|
| 175 | const std::string &funcName,
|
---|
| 176 | const std::string &name)
|
---|
[3045] | 177 | {
|
---|
[3055] | 178 | return CreateShaderProgram(filename, funcName, name, false);
|
---|
[3045] | 179 | }
|
---|
| 180 |
|
---|
| 181 |
|
---|
[3055] | 182 | void ShaderManager::EnableFragmentProfile()
|
---|
[3045] | 183 | {
|
---|
[3055] | 184 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
[3045] | 185 | }
|
---|
| 186 |
|
---|
| 187 |
|
---|
[3055] | 188 | void ShaderManager::EnableVertexProfile()
|
---|
[3045] | 189 | {
|
---|
[3055] | 190 | cgGLEnableProfile(sCgVertexProfile);
|
---|
[3045] | 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
[3055] | 194 | void ShaderManager::DisableFragmentProfile()
|
---|
[3045] | 195 | {
|
---|
[3055] | 196 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
| 197 | }
|
---|
[3045] | 198 |
|
---|
| 199 |
|
---|
[3055] | 200 | void ShaderManager::DisableVertexProfile()
|
---|
| 201 | {
|
---|
| 202 | cgGLDisableProfile(sCgVertexProfile);
|
---|
[3045] | 203 | }
|
---|
| 204 |
|
---|
| 205 |
|
---|
[3055] | 206 | ShaderProgram *ShaderManager::CreateShaderProgram(const std::string &filename,
|
---|
| 207 | const std::string &funcName,
|
---|
| 208 | const std::string &name,
|
---|
| 209 | bool isFragment)
|
---|
[3045] | 210 | {
|
---|
| 211 | const string fullName = "src/shaders/" + filename + ".cg";
|
---|
[3055] | 212 |
|
---|
[3147] | 213 | ShaderProgram *p;
|
---|
[3045] | 214 |
|
---|
[3147] | 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 |
|
---|
[3057] | 224 | if (GetShaderProgram(name))
|
---|
| 225 | {
|
---|
| 226 | cerr << "Program name " << name << "already chosen" << endl;
|
---|
[3045] | 227 |
|
---|
| 228 | DEL_PTR(p);
|
---|
[3055] | 229 | return p;
|
---|
[3045] | 230 | }
|
---|
| 231 |
|
---|
| 232 | if (!p->IsValid())
|
---|
| 233 | {
|
---|
[3057] | 234 | cerr << "Program " << funcName << " in " << fullName << " failed to load" << endl;
|
---|
| 235 |
|
---|
[3045] | 236 | DEL_PTR(p);
|
---|
[3055] | 237 | return p;
|
---|
[3045] | 238 | }
|
---|
[3055] | 239 |
|
---|
[3057] | 240 |
|
---|
| 241 | // everything ok
|
---|
[3045] | 242 | cout << "Program " << funcName << " in " << fullName << " loaded" << endl;
|
---|
[3055] | 243 |
|
---|
| 244 | mShaders.push_back(p);
|
---|
| 245 | mShaderMap[name] = p;
|
---|
[3045] | 246 |
|
---|
| 247 | return p;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 |
|
---|
[3057] | 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;
|
---|
[3045] | 259 | }
|
---|
| 260 |
|
---|
[3057] | 261 |
|
---|
| 262 | }
|
---|
| 263 |
|
---|