Commit 2887d21a authored by Jan Möbius's avatar Jan Möbius

Merge branch 'TwoSidedLighting_ShaderGen' into 'master'

Two sided lighting shader gen

See merge request !93
parents 2d95847e e00cd322
......@@ -28,12 +28,20 @@ vec3 LitPointLight(vec3 vPosition,
vec3 cLight = cLightAmbient * g_cAmbient;
// diffuse
float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
#ifdef TWO_SIDED_LIGHTING
float ldotn = abs(clamp(dot(vLightDir, vNormal),-1.0,1.0));
#else
float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
#endif
cLight += ldotn * cLightDiffuse * g_cDiffuse;
// specular
vec3 h = normalize(vLightDir - vec3(0,0,-1)); // half vector between light and view direction
#ifdef TWO_SIDED_LIGHTING
float hdotn = max(abs(dot(h, vNormal)), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
#else
float hdotn = max(dot(h, vNormal), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
#endif
cLight += (pow(hdotn, SHININESS) * cLightSpecular) * g_cSpecular;
......@@ -55,12 +63,20 @@ vec3 LitDirLight(vec3 vPosition,
vec3 cLight = cLightAmbient * g_cAmbient;
// diffuse
float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
#ifdef TWO_SIDED_LIGHTING
float ldotn = abs(clamp(dot(vLightDir, vNormal),-1.0,1.0));
#else
float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
#endif
cLight += ldotn * cLightDiffuse * g_cDiffuse;
// specular
vec3 h = normalize(vLightDir - vec3(0,0,-1)); // half vector between light and view direction
float hdotn = max(dot(h, vNormal), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
#ifdef TWO_SIDED_LIGHTING
float hdotn = max(abs(dot(h, vNormal)), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
#else
float hdotn = max(dot(h, vNormal), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
#endif
cLight += (pow(hdotn, SHININESS) * cLightSpecular) * g_cSpecular;
return cLight;
......@@ -88,15 +104,21 @@ vec3 LitSpotLight(vec3 vPosition,
vec3 cLight = cLightAmbient * g_cAmbient;
// diffuse
float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
#ifdef TWO_SIDED_LIGHTING
float ldotn = abs(clamp(dot(vLightDir, vNormal),-1.0,1.0));
#else
float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
#endif
cLight += ldotn * cLightDiffuse * g_cDiffuse;
// specular
vec3 h = normalize(vLightDir - vec3(0,0,-1)); // half vector between light and view direction
float hdotn = max(dot(h, vNormal), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
#ifdef TWO_SIDED_LIGHTING
float hdotn = max(abs(dot(h, vNormal)), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
#else
float hdotn = max(dot(h, vNormal), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
#endif
cLight += (pow(hdotn, SHININESS) * cLightSpecular) * g_cSpecular;
// attenuate
float fAtten = vLightAtten.x + vLightAtten.y * fLen + vLightAtten.z * fLen2;
......
......@@ -1039,6 +1039,9 @@ void ShaderProgGenerator::initGenDefines(ShaderGenerator* _gen)
std::cout << __FUNCTION__ << " -> unknown shade mode: " << desc_.shadeMode << std::endl;
}
if (desc_.twoSidedLighting)
_gen->addDefine("TWO_SIDED_LIGHTING 1");
if (desc_.textured())
_gen->addDefine("SG_TEXTURED 1");
......@@ -2761,9 +2764,8 @@ QString ShaderGenDesc::toString() const
resStrm << "}";
}
}
resStrm << "\nshaderDesc.shadeMode: " << shadeModeString[shadeMode];
resStrm << "\nshaderDesc.twoSidedLighting: " << (twoSidedLighting ? "Yes" : "No");
resStrm << "\nshaderDesc.vertexColors: " << vertexColors;
resStrm << "\nshaderDesc.textured(): " << textured();
for (std::map<size_t,TextureType>::const_iterator iter = textureTypes_.begin(); iter != textureTypes_.end();++iter)
......
......@@ -93,6 +93,7 @@ public:
ShaderGenDesc() :
version(150),
numLights(0),
twoSidedLighting(false),
shadeMode(SG_SHADE_UNLIT),
vertexColors(false),
vertexTemplateFile(""),
......@@ -131,6 +132,7 @@ public:
// version = _rhs.version;
//
// numLights = _rhs.numLights;
// twoSidedLighting = _rhs.twoSidedLighting;
//
// std::copy(_rhs.lightTypes,_rhs.lightTypes+SG_MAX_SHADER_LIGHTS,lightTypes);
// textureTypes_ = _rhs.textureTypes_;
......@@ -157,6 +159,8 @@ public:
int numLights;
ShaderGenLightType lightTypes[SG_MAX_SHADER_LIGHTS];
bool twoSidedLighting;
ShaderGenShadeMode shadeMode;
bool vertexColors;
......@@ -280,6 +284,9 @@ public:
if (numLights != _rhs.numLights)
return false;
if ( twoSidedLighting != _rhs.twoSidedLighting )
return false;
if (shadeMode != _rhs.shadeMode)
return false;
......
......@@ -754,6 +754,9 @@ void ACG::SceneGraph::MeshNodeT<Mesh>::getRenderObjects( IRenderer* _renderer, G
// enable / disable lighting
ro.shaderDesc.numLights = props->lighting() ? 0 : -1;
// Enable/Disable twoSided Lighting
ro.shaderDesc.twoSidedLighting = _state.twosided_lighting();
// TODO: better handling of attribute sources in shader gen
switch (props->lightStage())
{
......
......@@ -116,7 +116,7 @@ void RenderObjectHighlighter::update() {
HighlightingRule rule;
// Define rule
rule.pattern = QRegExp("#define.*");
rule.pattern = QRegExp("(#define|#ifdef|#else|#endif|#ifndef).*");
rule.format = defineFormat_;
highlightingRules_.append(rule);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment