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