[692] | 1 | /******************************************************************************
|
---|
| 2 | Copyright (c) W.J. van der Laan
|
---|
| 3 |
|
---|
| 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of
|
---|
| 5 | this software and associated documentation files (the "Software"), to deal in
|
---|
| 6 | the Software without restriction, including without limitation the rights to use,
|
---|
| 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
---|
| 8 | Software, and to permit persons to whom the Software is furnished to do so, subject
|
---|
| 9 | to the following conditions:
|
---|
| 10 |
|
---|
| 11 | The above copyright notice and this permission notice shall be included in all copies
|
---|
| 12 | or substantial portions of the Software.
|
---|
| 13 |
|
---|
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
---|
| 15 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
---|
| 16 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
| 17 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
---|
| 18 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE
|
---|
| 19 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
| 20 | ******************************************************************************/
|
---|
| 21 |
|
---|
| 22 | #include "LightMaterialGenerator.h"
|
---|
| 23 |
|
---|
| 24 | #include "OgreStringConverter.h"
|
---|
| 25 | #include "OgreException.h"
|
---|
| 26 | #include "OgreMaterialManager.h"
|
---|
| 27 |
|
---|
| 28 | #include "OgrePass.h"
|
---|
| 29 | #include "OgreTechnique.h"
|
---|
| 30 |
|
---|
| 31 | #include "OgreHighLevelGpuProgram.h"
|
---|
| 32 | #include "OgreHighLevelGpuProgramManager.h"
|
---|
| 33 |
|
---|
| 34 | #include "MLight.h"
|
---|
| 35 |
|
---|
| 36 | using namespace Ogre;
|
---|
| 37 |
|
---|
| 38 | class LightMaterialGeneratorHLSL: public MaterialGenerator::Impl
|
---|
| 39 | {
|
---|
| 40 | public:
|
---|
| 41 | LightMaterialGeneratorHLSL(const String &baseName):
|
---|
| 42 | mBaseName(baseName)
|
---|
| 43 | {}
|
---|
| 44 | typedef MaterialGenerator::Perm Perm;
|
---|
| 45 |
|
---|
| 46 | virtual Ogre::GpuProgramPtr generateVertexShader(Perm permutation)
|
---|
| 47 | {
|
---|
| 48 | if(permutation & MLight::MI_QUAD)
|
---|
| 49 | {
|
---|
| 50 | return HighLevelGpuProgramManager::getSingleton().getByName("DeferredShading/post/hlsl/vs");
|
---|
| 51 | }
|
---|
| 52 | else
|
---|
| 53 | {
|
---|
| 54 | return HighLevelGpuProgramManager::getSingleton().getByName("DeferredShading/post/hlsl/LightMaterial_vs");
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | virtual Ogre::GpuProgramPtr generateFragmentShader(Perm permutation)
|
---|
| 58 | {
|
---|
| 59 | bool isAttenuated = permutation & MLight::MI_ATTENUATED;
|
---|
| 60 | bool isSpecular = permutation & MLight::MI_SPECULAR;
|
---|
| 61 | // bool isShadowed = perm&4;
|
---|
| 62 |
|
---|
| 63 | /// Create name
|
---|
| 64 | String name=mBaseName+StringConverter::toString(permutation)+"_ps";
|
---|
| 65 | /// Create shader
|
---|
| 66 | std::stringstream shader;
|
---|
| 67 | shader <<
|
---|
| 68 | "sampler Tex0: register(s0);\n"
|
---|
| 69 | "sampler Tex1: register(s1);\n"
|
---|
| 70 | "float4x4 worldView;\n"
|
---|
| 71 | // Attributes of light
|
---|
| 72 | "float4 lightDiffuseColor;\n"
|
---|
| 73 | "float4 lightSpecularColor;\n"
|
---|
| 74 | "float4 lightFalloff;\n"
|
---|
| 75 | "float4 main(float2 texCoord: TEXCOORD0, float3 projCoord: TEXCOORD1) : COLOR\n"
|
---|
| 76 | "{\n"
|
---|
| 77 | " float4 a0 = tex2D(Tex0, texCoord); \n"// Attribute 0: Diffuse color+shininess
|
---|
| 78 | " float4 a1 = tex2D(Tex1, texCoord); \n"// Attribute 1: Normal+depth
|
---|
| 79 | // Attributes
|
---|
| 80 | " float3 colour = a0.rgb;\n"
|
---|
| 81 | " float alpha = a0.a;" // Specularity
|
---|
| 82 | " float distance = a1.w;" // Distance from viewer (w)
|
---|
| 83 | " float3 normal = a1.xyz;\n"
|
---|
| 84 | // Calculate position of texel in view space
|
---|
| 85 | " float3 position = projCoord*distance;\n"
|
---|
| 86 | // Extract position in view space from worldView matrix
|
---|
| 87 | " float3 lightPos = float3(worldView[0][3],worldView[1][3],worldView[2][3]);\n"
|
---|
| 88 | // Calculate light direction and distance
|
---|
| 89 | " float3 lightVec = lightPos - position;\n"
|
---|
| 90 | " float len_sq = dot(lightVec, lightVec);\n"
|
---|
| 91 | " float len = sqrt(len_sq);\n"
|
---|
| 92 | " float3 lightDir = lightVec/len;\n"
|
---|
| 93 | /// Calculate attenuation
|
---|
| 94 | " float attenuation = dot(lightFalloff, float3(1, len, len_sq));\n"
|
---|
| 95 | /// Calculate diffuse colour
|
---|
| 96 | " float3 light_diffuse = max(0,dot(lightDir, normal)) * lightDiffuseColor;\n"
|
---|
| 97 | /// Calculate specular component
|
---|
| 98 | " float3 viewDir = -normalize(position);\n"
|
---|
| 99 | " float3 h = normalize(viewDir + lightDir);\n"
|
---|
| 100 | " float3 light_specular = pow(dot(normal, h),32) * lightSpecularColor;\n"
|
---|
| 101 | // Accumulate total lighting for this fragment
|
---|
| 102 | " float3 total_light_contrib;\n"
|
---|
| 103 | " total_light_contrib = light_diffuse;\n";
|
---|
| 104 | if(isSpecular)
|
---|
| 105 | {
|
---|
| 106 | /// Calculate specular contribution
|
---|
| 107 | shader <<
|
---|
| 108 | " total_light_contrib += alpha * light_specular;\n";
|
---|
| 109 | }
|
---|
| 110 | if(isAttenuated)
|
---|
| 111 | {
|
---|
| 112 | shader <<
|
---|
| 113 | " return float4(total_light_contrib*colour/attenuation, 0);\n";
|
---|
| 114 | }
|
---|
| 115 | else
|
---|
| 116 | {
|
---|
| 117 | shader <<
|
---|
| 118 | " return float4(total_light_contrib*colour, 0);\n";
|
---|
| 119 | }
|
---|
| 120 | shader <<
|
---|
| 121 | "}\n";
|
---|
| 122 |
|
---|
| 123 | /// Create shader object
|
---|
| 124 | HighLevelGpuProgramPtr program = HighLevelGpuProgramManager::getSingleton().createProgram(
|
---|
| 125 | name, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
---|
| 126 | "hlsl", GPT_FRAGMENT_PROGRAM);
|
---|
| 127 | program->setSource(shader.str());
|
---|
| 128 | program->setParameter("target","ps_2_0");
|
---|
| 129 | program->setParameter("entry_point","main");
|
---|
| 130 | /// Set up default parameters
|
---|
| 131 | GpuProgramParametersSharedPtr params = program->getDefaultParameters();
|
---|
| 132 | params->setNamedAutoConstant("worldView", GpuProgramParameters::ACT_WORLDVIEW_MATRIX, 0);
|
---|
| 133 | params->setNamedAutoConstant("lightDiffuseColor", GpuProgramParameters::ACT_CUSTOM, 1);
|
---|
| 134 | if(isSpecular)
|
---|
| 135 | params->setNamedAutoConstant("lightSpecularColor", GpuProgramParameters::ACT_CUSTOM, 2);
|
---|
| 136 | if(isAttenuated)
|
---|
| 137 | params->setNamedAutoConstant("lightFalloff", GpuProgramParameters::ACT_CUSTOM, 3);
|
---|
| 138 |
|
---|
| 139 | return HighLevelGpuProgramManager::getSingleton().getByName(program->getName());
|
---|
| 140 | }
|
---|
| 141 | virtual Ogre::MaterialPtr generateTemplateMaterial(Perm permutation)
|
---|
| 142 | {
|
---|
| 143 | if(permutation & MLight::MI_QUAD)
|
---|
| 144 | {
|
---|
| 145 | return MaterialManager::getSingleton().getByName("DeferredShading/LightMaterialQuad");
|
---|
| 146 | }
|
---|
| 147 | else
|
---|
| 148 | {
|
---|
| 149 | return MaterialManager::getSingleton().getByName("DeferredShading/LightMaterial");
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | protected:
|
---|
| 153 | String mBaseName;
|
---|
| 154 | };
|
---|
| 155 |
|
---|
| 156 | class LightMaterialGeneratorGLSL: public MaterialGenerator::Impl
|
---|
| 157 | {
|
---|
| 158 | public:
|
---|
| 159 | LightMaterialGeneratorGLSL(const String &baseName):
|
---|
| 160 | mBaseName(baseName)
|
---|
| 161 | {}
|
---|
| 162 | typedef MaterialGenerator::Perm Perm;
|
---|
| 163 |
|
---|
| 164 | virtual Ogre::GpuProgramPtr generateVertexShader(Perm permutation)
|
---|
| 165 | {
|
---|
| 166 | if(permutation & MLight::MI_QUAD)
|
---|
| 167 | {
|
---|
| 168 | return HighLevelGpuProgramManager::getSingleton().getByName("DeferredShading/post/glsl/vs");
|
---|
| 169 | }
|
---|
| 170 | else
|
---|
| 171 | {
|
---|
| 172 | return HighLevelGpuProgramManager::getSingleton().getByName("DeferredShading/post/glsl/LightMaterial_vs");
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | /// This hack is needed for GLSL, don't ask me why
|
---|
| 176 | void setNamedAutoConstant(const GpuProgramParametersSharedPtr ¶ms,
|
---|
| 177 | const String &name, GpuProgramParameters::AutoConstantType acType, size_t extraInfo)
|
---|
| 178 | {
|
---|
| 179 | size_t index = params->getParamIndex(name);
|
---|
| 180 | params->setAutoConstant(index, acType, extraInfo);
|
---|
| 181 | size_t constantIndex = params->addConstantDefinition(
|
---|
| 182 | name, index, 0, GpuProgramParameters::ET_REAL);
|
---|
| 183 | // update constant definition auto settings
|
---|
| 184 | // since an autoconstant was just added, its the last one in the container
|
---|
| 185 | size_t autoIndex = params->getAutoConstantCount() - 1;
|
---|
| 186 | // setup autoState which will allocate the proper amount of storage required by constant entries
|
---|
| 187 | params->setConstantDefinitionAutoState(constantIndex, true, autoIndex);
|
---|
| 188 | }
|
---|
| 189 | void setNamedIntConstant(const GpuProgramParametersSharedPtr ¶ms,
|
---|
| 190 | const String &name, int x)
|
---|
| 191 | {
|
---|
| 192 | int intBuffer[4];
|
---|
| 193 | intBuffer[0] = x;
|
---|
| 194 | params->setConstant(params->getParamIndex(name), intBuffer, 1);
|
---|
| 195 | }
|
---|
| 196 | virtual Ogre::GpuProgramPtr generateFragmentShader(Perm permutation)
|
---|
| 197 | {
|
---|
| 198 | bool isAttenuated = permutation & MLight::MI_ATTENUATED;
|
---|
| 199 | bool isSpecular = permutation & MLight::MI_SPECULAR;
|
---|
| 200 | // bool isShadowed = perm&4;
|
---|
| 201 |
|
---|
| 202 | /// Create name
|
---|
| 203 | String name=mBaseName+StringConverter::toString(permutation)+"_ps";
|
---|
| 204 | /// Create shader
|
---|
| 205 | std::stringstream shader;
|
---|
| 206 | shader <<
|
---|
| 207 | "uniform sampler2D tex0;\n"
|
---|
| 208 | "uniform sampler2D tex1;\n"
|
---|
| 209 | "varying vec2 texCoord;\n"
|
---|
| 210 | "varying vec3 projCoord;\n"
|
---|
| 211 | /// World view matrix to get object position in view space
|
---|
| 212 | "uniform mat4 worldView;\n"
|
---|
| 213 | /// Attributes of light
|
---|
| 214 | "uniform vec3 lightDiffuseColor;\n"
|
---|
| 215 | "uniform vec3 lightSpecularColor;\n"
|
---|
| 216 | "uniform vec3 lightFalloff;\n"
|
---|
| 217 | "void main()\n"
|
---|
| 218 | "{\n"
|
---|
| 219 | " vec4 a0 = texture2D(tex0, texCoord);\n" // Attribute 0: Diffuse color+shininess
|
---|
| 220 | " vec4 a1 = texture2D(tex1, texCoord);\n" // Attribute 1: Normal+depth
|
---|
| 221 | /// Attributes
|
---|
| 222 | " vec3 colour = a0.rgb;\n"
|
---|
| 223 | " float alpha = a0.a;\n" // Specularity
|
---|
| 224 | " float distance = a1.w;\n" // Distance from viewer (w)
|
---|
| 225 | " vec3 normal = a1.xyz;\n"
|
---|
| 226 | /// Calculate position of texel in view space
|
---|
| 227 | " vec3 position = projCoord*distance;\n"
|
---|
| 228 | /// Extract position in view space from worldView matrix
|
---|
| 229 | " vec3 lightPos = vec3(worldView[3][0],worldView[3][1],worldView[3][2]);\n"
|
---|
| 230 | /// Calculate light direction and distance
|
---|
| 231 | " vec3 lightVec = lightPos - position;\n"
|
---|
| 232 | " float len_sq = dot(lightVec, lightVec);\n"
|
---|
| 233 | " float len = sqrt(len_sq);\n"
|
---|
| 234 | " vec3 lightDir = lightVec/len;\n"
|
---|
| 235 | /// Calculate attenuation
|
---|
| 236 | " float attenuation = dot(lightFalloff, vec3(1, len, len_sq));\n"
|
---|
| 237 | /// Calculate diffuse colour
|
---|
| 238 | " vec3 light_diffuse = max(0.0,dot(lightDir, normal)) * lightDiffuseColor;\n"
|
---|
| 239 | /// Calculate specular component
|
---|
| 240 | " vec3 viewDir = -normalize(position);\n"
|
---|
| 241 | " vec3 h = normalize(viewDir + lightDir);\n"
|
---|
| 242 | " vec3 light_specular = pow(dot(normal, h),32.0) * lightSpecularColor;\n"
|
---|
| 243 | /// Calcalate total lighting for this fragment
|
---|
| 244 | " vec3 total_light_contrib;\n"
|
---|
| 245 | " total_light_contrib = light_diffuse;\n";
|
---|
| 246 | if(isSpecular)
|
---|
| 247 | {
|
---|
| 248 | shader<<
|
---|
| 249 | " total_light_contrib += alpha * light_specular;\n";
|
---|
| 250 | }
|
---|
| 251 | if(isAttenuated)
|
---|
| 252 | {
|
---|
| 253 | shader<<
|
---|
| 254 | " gl_FragColor = vec4(total_light_contrib*colour/attenuation, 0);\n";
|
---|
| 255 | }
|
---|
| 256 | else
|
---|
| 257 | {
|
---|
| 258 | shader<<
|
---|
| 259 | " gl_FragColor = vec4(total_light_contrib*colour, 0);\n";
|
---|
| 260 | }
|
---|
| 261 | shader<<
|
---|
| 262 | "}\n";
|
---|
| 263 |
|
---|
| 264 | /// Create shader object
|
---|
| 265 | HighLevelGpuProgramPtr program = HighLevelGpuProgramManager::getSingleton().createProgram(
|
---|
| 266 | name, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
---|
| 267 | "glsl", GPT_FRAGMENT_PROGRAM);
|
---|
| 268 | program->setSource(shader.str());
|
---|
| 269 | /// Set up default parameters
|
---|
| 270 | GpuProgramParametersSharedPtr params = program->getDefaultParameters();
|
---|
| 271 |
|
---|
| 272 | setNamedAutoConstant(params, "worldView", GpuProgramParameters::ACT_WORLDVIEW_MATRIX, 0);
|
---|
| 273 | setNamedAutoConstant(params, "lightDiffuseColor", GpuProgramParameters::ACT_CUSTOM, 1);
|
---|
| 274 | if(isSpecular)
|
---|
| 275 | setNamedAutoConstant(params, "lightSpecularColor", GpuProgramParameters::ACT_CUSTOM, 2);
|
---|
| 276 | if(isAttenuated)
|
---|
| 277 | setNamedAutoConstant(params, "lightFalloff", GpuProgramParameters::ACT_CUSTOM, 3);
|
---|
| 278 |
|
---|
| 279 | setNamedIntConstant(params, "tex0", 0);
|
---|
| 280 | setNamedIntConstant(params, "tex1", 1);
|
---|
| 281 | //params->setNamedConstant("tex0", 0);
|
---|
| 282 | //params->setNamedConstant("tex1", 1);
|
---|
| 283 |
|
---|
| 284 | return HighLevelGpuProgramManager::getSingleton().getByName(program->getName());
|
---|
| 285 | }
|
---|
| 286 | virtual Ogre::MaterialPtr generateTemplateMaterial(Perm permutation)
|
---|
| 287 | {
|
---|
| 288 | if(permutation & MLight::MI_QUAD)
|
---|
| 289 | {
|
---|
| 290 | return MaterialManager::getSingleton().getByName("DeferredShading/LightMaterialQuad");
|
---|
| 291 | }
|
---|
| 292 | else
|
---|
| 293 | {
|
---|
| 294 | return MaterialManager::getSingleton().getByName("DeferredShading/LightMaterial");
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | protected:
|
---|
| 298 | String mBaseName;
|
---|
| 299 | };
|
---|
| 300 |
|
---|
| 301 |
|
---|
| 302 | LightMaterialGenerator::LightMaterialGenerator(const Ogre::String &language)
|
---|
| 303 | {
|
---|
| 304 | bitNames.push_back("Quad"); // MI_QUAD
|
---|
| 305 | bitNames.push_back("Attenuated"); // MI_ATTENUATED
|
---|
| 306 | bitNames.push_back("Specular"); // MI_SPECULAR
|
---|
| 307 |
|
---|
| 308 | vsMask = 0x00000001;
|
---|
| 309 | fsMask = 0x00000006;
|
---|
| 310 | matMask = 0x00000001;
|
---|
| 311 |
|
---|
| 312 | materialBaseName = "DeferredShading/LightMaterial/";
|
---|
| 313 | if(language=="hlsl")
|
---|
| 314 | mImpl = new LightMaterialGeneratorHLSL("DeferredShading/LightMaterial/hlsl/");
|
---|
| 315 | else
|
---|
| 316 | mImpl = new LightMaterialGeneratorGLSL("DeferredShading/LightMaterial/glsl/");
|
---|
| 317 | }
|
---|