Add enchanted item glow to the shader

move
scrawl 8 years ago
parent 268594dcf1
commit 5e12a1b4ef

@ -52,8 +52,8 @@ namespace
class GlowUpdater : public SceneUtil::StateSetUpdater
{
public:
GlowUpdater(osg::Vec4f color, const std::vector<osg::ref_ptr<osg::Texture2D> >& textures)
: mTexUnit(1) // FIXME: might not always be 1
GlowUpdater(int texUnit, osg::Vec4f color, const std::vector<osg::ref_ptr<osg::Texture2D> >& textures)
: mTexUnit(texUnit)
, mColor(color)
, mTextures(textures)
{
@ -1055,17 +1055,27 @@ namespace MWRender
osg::ref_ptr<osg::Image> image = mResourceSystem->getImageManager()->getImage(stream.str());
osg::ref_ptr<osg::Texture2D> tex (new osg::Texture2D(image));
tex->setName("envMap");
tex->setWrap(osg::Texture::WRAP_S, osg::Texture2D::REPEAT);
tex->setWrap(osg::Texture::WRAP_T, osg::Texture2D::REPEAT);
mResourceSystem->getSceneManager()->applyFilterSettings(tex);
textures.push_back(tex);
}
osg::ref_ptr<GlowUpdater> glowupdater (new GlowUpdater(glowColor, textures));
int texUnit = 1; // FIXME: might not always be 1
osg::ref_ptr<GlowUpdater> glowupdater (new GlowUpdater(texUnit, glowColor, textures));
node->addUpdateCallback(glowupdater);
// TODO: regenerate shader
// allowedToModifyStatesets = false
// set a texture now so that the ShaderVisitor can find it
osg::ref_ptr<osg::StateSet> writableStateSet = NULL;
if (!node->getStateSet())
writableStateSet = node->getOrCreateStateSet();
else
writableStateSet = osg::clone(node->getStateSet(), osg::CopyOp::SHALLOW_COPY);
writableStateSet->setTextureAttributeAndModes(texUnit, textures.front(), osg::StateAttribute::ON);
writableStateSet->addUniform(new osg::Uniform("envMapColor", glowColor));
mResourceSystem->getSceneManager()->recreateShaders(node);
}
// TODO: Should not be here

@ -152,7 +152,7 @@ namespace Shader
writableStateSet = getWritableStateSet(node);
ShaderManager::DefineMap defineMap;
const char* defaultTextures[] = { "diffuseMap", "normalMap", "emissiveMap", "darkMap", "detailMap" };
const char* defaultTextures[] = { "diffuseMap", "normalMap", "emissiveMap", "darkMap", "detailMap", "envMap" };
for (unsigned int i=0; i<sizeof(defaultTextures)/sizeof(defaultTextures[0]); ++i)
{
defineMap[defaultTextures[i]] = "0";

@ -26,6 +26,12 @@ varying vec2 normalMapUV;
varying vec3 viewTangent;
#endif
#if @envMap
uniform sampler2D envMap;
varying vec2 envMapUV;
uniform vec4 envMapColor;
#endif
varying float depth;
#define PER_PIXEL_LIGHTING (@normalMap || @forcePPL)
@ -35,7 +41,7 @@ varying vec4 lighting;
#else
varying vec3 passViewPos;
varying vec3 passViewNormal;
varying vec4 passColour;
varying vec4 passColor;
#endif
#include "lighting.glsl"
@ -73,13 +79,17 @@ void main()
#if !PER_PIXEL_LIGHTING
gl_FragData[0] *= lighting;
#else
gl_FragData[0] *= doLighting(passViewPos, normalize(viewNormal), passColour);
gl_FragData[0] *= doLighting(passViewPos, normalize(viewNormal), passColor);
#endif
#if @emissiveMap
gl_FragData[0].xyz += texture2D(emissiveMap, emissiveMapUV).xyz;
#endif
#if @envMap
gl_FragData[0].xyz += texture2D(envMap, envMapUV).xyz * envMapColor.xyz;
#endif
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);
}

@ -21,6 +21,10 @@ varying vec2 normalMapUV;
varying vec3 viewTangent;
#endif
#if @envMap
varying vec2 envMapUV;
#endif
varying float depth;
#define PER_PIXEL_LIGHTING (@normalMap || @forcePPL)
@ -30,7 +34,7 @@ varying vec4 lighting;
#else
varying vec3 passViewPos;
varying vec3 passViewNormal;
varying vec4 passColour;
varying vec4 passColor;
#endif
#include "lighting.glsl"
@ -44,6 +48,13 @@ void main(void)
gl_ClipVertex = viewPos;
vec3 viewNormal = normalize((gl_NormalMatrix * gl_Normal).xyz);
#if @envMap
vec3 viewVec = normalize(viewPos.xyz);
vec3 r = reflect( viewVec, viewNormal );
float m = 2.0 * sqrt( r.x*r.x + r.y*r.y + (r.z+1.0)*(r.z+1.0) );
envMapUV = vec2(r.x/m + 0.5, r.y/m + 0.5);
#endif
#if @diffuseMap
diffuseMapUV = (gl_TextureMatrix[@diffuseMapUV] * gl_MultiTexCoord@diffuseMapUV).xy;
#endif
@ -70,6 +81,6 @@ void main(void)
#else
passViewPos = viewPos.xyz;
passViewNormal = viewNormal;
passColour = gl_Color;
passColor = gl_Color;
#endif
}

Loading…
Cancel
Save