mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-07-05 16:51:35 +00:00
Add bump mapping support
This commit is contained in:
parent
bbca1f3d1d
commit
19a7245251
6 changed files with 43 additions and 11 deletions
|
@ -46,12 +46,10 @@ void NiTexturingProperty::read(NIFStream *nif)
|
||||||
for (unsigned int i = 0; i < numTextures; i++)
|
for (unsigned int i = 0; i < numTextures; i++)
|
||||||
{
|
{
|
||||||
textures[i].read(nif);
|
textures[i].read(nif);
|
||||||
// Ignore these at the moment
|
|
||||||
if (i == 5 && textures[5].inUse) // Bump map settings
|
if (i == 5 && textures[5].inUse) // Bump map settings
|
||||||
{
|
{
|
||||||
/*float lumaScale =*/ nif->getFloat();
|
envMapLumaBias = nif->getVector2();
|
||||||
/*float lumaOffset =*/ nif->getFloat();
|
bumpMapMatrix = nif->getVector4();
|
||||||
/*const Vector4 *lumaMatrix =*/ nif->getVector4();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,6 +93,9 @@ public:
|
||||||
|
|
||||||
std::vector<Texture> textures;
|
std::vector<Texture> textures;
|
||||||
|
|
||||||
|
osg::Vec2f envMapLumaBias;
|
||||||
|
osg::Vec4f bumpMapMatrix;
|
||||||
|
|
||||||
void read(NIFStream *nif);
|
void read(NIFStream *nif);
|
||||||
void post(NIFFile *nif);
|
void post(NIFFile *nif);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1536,6 +1536,10 @@ namespace NifOsg
|
||||||
{
|
{
|
||||||
// Set this texture to Off by default since we can't render it with the fixed-function pipeline
|
// Set this texture to Off by default since we can't render it with the fixed-function pipeline
|
||||||
stateset->setTextureMode(texUnit, GL_TEXTURE_2D, osg::StateAttribute::OFF);
|
stateset->setTextureMode(texUnit, GL_TEXTURE_2D, osg::StateAttribute::OFF);
|
||||||
|
osg::Matrix2 bumpMapMatrix(texprop->bumpMapMatrix.x(), texprop->bumpMapMatrix.y(),
|
||||||
|
texprop->bumpMapMatrix.z(), texprop->bumpMapMatrix.w());
|
||||||
|
stateset->addUniform(new osg::Uniform("bumpMapMatrix", bumpMapMatrix));
|
||||||
|
stateset->addUniform(new osg::Uniform("envMapLumaBias", texprop->envMapLumaBias));
|
||||||
}
|
}
|
||||||
else if (i == Nif::NiTexturingProperty::DecalTexture)
|
else if (i == Nif::NiTexturingProperty::DecalTexture)
|
||||||
{
|
{
|
||||||
|
@ -1559,7 +1563,7 @@ namespace NifOsg
|
||||||
texture2d->setName("diffuseMap");
|
texture2d->setName("diffuseMap");
|
||||||
break;
|
break;
|
||||||
case Nif::NiTexturingProperty::BumpTexture:
|
case Nif::NiTexturingProperty::BumpTexture:
|
||||||
texture2d->setName("normalMap");
|
texture2d->setName("bumpMap");
|
||||||
break;
|
break;
|
||||||
case Nif::NiTexturingProperty::GlowTexture:
|
case Nif::NiTexturingProperty::GlowTexture:
|
||||||
texture2d->setName("emissiveMap");
|
texture2d->setName("emissiveMap");
|
||||||
|
|
|
@ -75,7 +75,7 @@ namespace Shader
|
||||||
return newStateSet.get();
|
return newStateSet.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* defaultTextures[] = { "diffuseMap", "normalMap", "emissiveMap", "darkMap", "detailMap", "envMap", "specularMap", "decalMap" };
|
const char* defaultTextures[] = { "diffuseMap", "normalMap", "emissiveMap", "darkMap", "detailMap", "envMap", "specularMap", "decalMap", "bumpMap" };
|
||||||
bool isTextureNameRecognized(const std::string& name)
|
bool isTextureNameRecognized(const std::string& name)
|
||||||
{
|
{
|
||||||
for (unsigned int i=0; i<sizeof(defaultTextures)/sizeof(defaultTextures[0]); ++i)
|
for (unsigned int i=0; i<sizeof(defaultTextures)/sizeof(defaultTextures[0]); ++i)
|
||||||
|
@ -130,6 +130,14 @@ namespace Shader
|
||||||
diffuseMap = texture;
|
diffuseMap = texture;
|
||||||
else if (texName == "specularMap")
|
else if (texName == "specularMap")
|
||||||
specularMap = texture;
|
specularMap = texture;
|
||||||
|
else if (texName == "bumpMap")
|
||||||
|
{
|
||||||
|
mRequirements.back().mShaderRequired = true;
|
||||||
|
if (!writableStateSet)
|
||||||
|
writableStateSet = getWritableStateSet(node);
|
||||||
|
// Bump maps are off by default as well
|
||||||
|
writableStateSet->setTextureMode(unit, GL_TEXTURE_2D, osg::StateAttribute::ON);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
Log(Debug::Error) << "ShaderVisitor encountered unknown texture " << texture;
|
Log(Debug::Error) << "ShaderVisitor encountered unknown texture " << texture;
|
||||||
|
|
|
@ -42,6 +42,13 @@ uniform sampler2D specularMap;
|
||||||
varying vec2 specularMapUV;
|
varying vec2 specularMapUV;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if @bumpMap
|
||||||
|
uniform sampler2D bumpMap;
|
||||||
|
varying vec2 bumpMapUV;
|
||||||
|
uniform vec2 envMapLumaBias;
|
||||||
|
uniform mat2 bumpMapMatrix;
|
||||||
|
#endif
|
||||||
|
|
||||||
varying float depth;
|
varying float depth;
|
||||||
|
|
||||||
#define PER_PIXEL_LIGHTING (@normalMap || @forcePPL)
|
#define PER_PIXEL_LIGHTING (@normalMap || @forcePPL)
|
||||||
|
@ -132,20 +139,24 @@ void main()
|
||||||
gl_FragData[0].xyz += texture2D(emissiveMap, emissiveMapUV).xyz;
|
gl_FragData[0].xyz += texture2D(emissiveMap, emissiveMapUV).xyz;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if @envMap
|
#if @envMap
|
||||||
|
vec2 texCoordGen = envMapUV;
|
||||||
|
float envLuma = 1.0;
|
||||||
|
|
||||||
#if @normalMap
|
#if @normalMap
|
||||||
// if using normal map + env map, take advantage of per-pixel normals for texCoordGen
|
// if using normal map + env map, take advantage of per-pixel normals for texCoordGen
|
||||||
vec3 viewVec = normalize(passViewPos.xyz);
|
vec3 viewVec = normalize(passViewPos.xyz);
|
||||||
vec3 r = reflect( viewVec, viewNormal );
|
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) );
|
float m = 2.0 * sqrt( r.x*r.x + r.y*r.y + (r.z+1.0)*(r.z+1.0) );
|
||||||
vec2 texCoordGen = vec2(r.x/m + 0.5, r.y/m + 0.5);
|
texCoordGen = vec2(r.x/m + 0.5, r.y/m + 0.5);
|
||||||
gl_FragData[0].xyz += texture2D(envMap, texCoordGen).xyz * envMapColor.xyz;
|
|
||||||
#else
|
|
||||||
gl_FragData[0].xyz += texture2D(envMap, envMapUV).xyz * envMapColor.xyz;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if @bumpMap
|
||||||
|
vec4 bumpTex = texture2D(bumpMap, bumpMapUV);
|
||||||
|
texCoordGen += bumpTex.rg * bumpMapMatrix;
|
||||||
|
envLuma = clamp(bumpTex.b * envMapLumaBias.x + envMapLumaBias.y, 0.0, 1.0);
|
||||||
|
#endif
|
||||||
|
gl_FragData[0].xyz += texture2D(envMap, texCoordGen).xyz * envMapColor.xyz * envLuma;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if @specularMap
|
#if @specularMap
|
||||||
|
|
|
@ -29,6 +29,10 @@ varying vec4 passTangent;
|
||||||
varying vec2 envMapUV;
|
varying vec2 envMapUV;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if @bumpMap
|
||||||
|
varying vec2 bumpMapUV;
|
||||||
|
#endif
|
||||||
|
|
||||||
#if @specularMap
|
#if @specularMap
|
||||||
varying vec2 specularMapUV;
|
varying vec2 specularMapUV;
|
||||||
#endif
|
#endif
|
||||||
|
@ -91,6 +95,10 @@ void main(void)
|
||||||
passTangent = gl_MultiTexCoord7.xyzw;
|
passTangent = gl_MultiTexCoord7.xyzw;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if @bumpMap
|
||||||
|
bumpMapUV = (gl_TextureMatrix[@bumpMapUV] * gl_MultiTexCoord@bumpMapUV).xy;
|
||||||
|
#endif
|
||||||
|
|
||||||
#if @specularMap
|
#if @specularMap
|
||||||
specularMapUV = (gl_TextureMatrix[@specularMapUV] * gl_MultiTexCoord@specularMapUV).xy;
|
specularMapUV = (gl_TextureMatrix[@specularMapUV] * gl_MultiTexCoord@specularMapUV).xy;
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue