From 937681121348b8ed38b20225f62951fd7d568b0c Mon Sep 17 00:00:00 2001 From: scrawl Date: Tue, 16 Feb 2016 23:30:23 +0100 Subject: [PATCH] Vertex lighting shader --- apps/openmw/mwrender/animation.cpp | 5 ++++ components/sceneutil/lightmanager.cpp | 12 ++++++++ components/shader/shadervisitor.cpp | 1 + files/shaders/objects_fragment.glsl | 4 +++ files/shaders/objects_vertex.glsl | 42 ++++++++++++++++++++++++++- 5 files changed, 63 insertions(+), 1 deletion(-) diff --git a/apps/openmw/mwrender/animation.cpp b/apps/openmw/mwrender/animation.cpp index abca3b266..65ac7d877 100644 --- a/apps/openmw/mwrender/animation.cpp +++ b/apps/openmw/mwrender/animation.cpp @@ -1063,6 +1063,9 @@ namespace MWRender osg::ref_ptr 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 diff --git a/components/sceneutil/lightmanager.cpp b/components/sceneutil/lightmanager.cpp index 64448e73e..ca1fc7eb8 100644 --- a/components/sceneutil/lightmanager.cpp +++ b/components/sceneutil/lightmanager.cpp @@ -56,6 +56,18 @@ namespace SceneUtil } state.applyModelViewMatrix(modelViewMatrix); + + /* + for (int i=0; i<8; ++i) + { + osg::ref_ptr 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: diff --git a/components/shader/shadervisitor.cpp b/components/shader/shadervisitor.cpp index 235e63d95..02ece3296 100644 --- a/components/shader/shadervisitor.cpp +++ b/components/shader/shadervisitor.cpp @@ -79,6 +79,7 @@ namespace Shader if (it->first.first == osg::StateAttribute::MATERIAL) { const osg::Material* mat = static_cast(it->second.first.get()); + mRequirements.back().mColorMaterial = (mat->getColorMode() != osg::Material::OFF); mRequirements.back().mVertexColorMode = mat->getColorMode(); } } diff --git a/files/shaders/objects_fragment.glsl b/files/shaders/objects_fragment.glsl index 288760229..07a524ba1 100644 --- a/files/shaders/objects_fragment.glsl +++ b/files/shaders/objects_fragment.glsl @@ -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); } diff --git a/files/shaders/objects_vertex.glsl b/files/shaders/objects_vertex.glsl index 47ae20572..679abda1d 100644 --- a/files/shaders/objects_vertex.glsl +++ b/files/shaders/objects_vertex.glsl @@ -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