Vertex lighting shader

move
scrawl 9 years ago
parent 044e0a829a
commit 9376811213

@ -1063,6 +1063,9 @@ namespace MWRender
osg::ref_ptr<GlowUpdater> glowupdater (new GlowUpdater(glowColor, textures));
node->addUpdateCallback(glowupdater);
// TODO: regenerate shader
// allowedToModifyStatesets = false
}
// TODO: Should not be here
@ -1243,6 +1246,8 @@ namespace MWRender
material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4f(1,1,1,1));
stateset->setAttributeAndModes(material, osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
// FIXME: regenerate shader (mVertexColorMode)
mObjectRoot->setStateSet(stateset);
}
else

@ -56,6 +56,18 @@ namespace SceneUtil
}
state.applyModelViewMatrix(modelViewMatrix);
/*
for (int i=0; i<8; ++i)
{
osg::ref_ptr<osg::Light> defaultLight (new osg::Light(i));
defaultLight->setAmbient(osg::Vec4());
defaultLight->setDiffuse(osg::Vec4());
defaultLight->setSpecular(osg::Vec4());
defaultLight->setConstantAttenuation(0.f);
state.setGlobalDefaultAttribute(defaultLight);
}
*/
}
private:

@ -79,6 +79,7 @@ namespace Shader
if (it->first.first == osg::StateAttribute::MATERIAL)
{
const osg::Material* mat = static_cast<const osg::Material*>(it->second.first.get());
mRequirements.back().mColorMaterial = (mat->getColorMode() != osg::Material::OFF);
mRequirements.back().mVertexColorMode = mat->getColorMode();
}
}

@ -7,6 +7,8 @@ varying vec2 diffuseMapUV;
varying float depth;
varying vec3 lighting;
void main()
{
#if @diffuseMap
@ -15,6 +17,8 @@ void main()
gl_FragData[0] = vec4(1.0, 1.0, 1.0, 1.0);
#endif
gl_FragData[0].xyz *= lighting;
float fogValue = clamp((depth - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0);
gl_FragData[0].xyz = mix(gl_FragData[0].xyz, gl_Fog.color.xyz, fogValue);
}

@ -5,7 +5,44 @@ varying vec2 diffuseMapUV;
#endif
varying float depth;
varying vec3 lighting;
#define MAX_LIGHTS 8
vec3 doLighting(vec4 vertex, vec3 normal, vec3 vertexColor)
{
vec3 viewPos = (gl_ModelViewMatrix * vertex).xyz;
vec3 viewNormal = normalize((gl_NormalMatrix * normal).xyz);
vec3 lightDir;
float d;
#if @colorMode == 2
vec3 diffuse = vertexColor;
vec3 ambient = vertexColor;
#else
vec3 diffuse = gl_FrontMaterial.diffuse.xyz;
vec3 ambient = gl_FrontMaterial.ambient.xyz;
#endif
vec3 lightResult = vec3(0.0, 0.0, 0.0);
for (int i=0; i<MAX_LIGHTS; ++i)
{
lightDir = gl_LightSource[i].position.xyz - (viewPos * gl_LightSource[i].position.w);
d = length(lightDir);
lightDir = normalize(lightDir);
lightResult.xyz += ambient * gl_LightSource[i].ambient.xyz;
lightResult.xyz += diffuse * gl_LightSource[i].diffuse.xyz * clamp(1.0 / (gl_LightSource[i].constantAttenuation + gl_LightSource[i].linearAttenuation * d + gl_LightSource[i].quadraticAttenuation * d * d), 0.0, 1.0)
* max(dot(viewNormal.xyz, lightDir), 0.0);
}
lightResult += gl_LightModel.ambient.xyz * ambient;
return lightResult;
}
void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
@ -14,4 +51,7 @@ void main(void)
#if @diffuseMap
diffuseMapUV = gl_MultiTexCoord@diffuseMapUV.xy;
#endif
lighting = doLighting(gl_Vertex, gl_Normal.xyz, gl_Color.xyz);
lighting = clamp(lighting, vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0));
}

Loading…
Cancel
Save