Well I will leave that up to you sir and continue
In learning to use normal maps in glsl I am using the default Rendermonkey textured bump shader. I will list the default code, and my shader callback.
Code: Select all
// VERTEX SHADER //
uniform vec3 fvLightPosition;
uniform vec3 fvEyePosition;
varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
attribute vec3 rm_Binormal;
attribute vec3 rm_Tangent;
void main( void )
{
gl_Position = ftransform();
Texcoord = gl_MultiTexCoord0.xy;
vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;
vec3 fvViewDirection = fvEyePosition - fvObjectPosition.xyz;
vec3 fvLightDirection = fvLightPosition - fvObjectPosition.xyz;
vec3 fvNormal = gl_NormalMatrix * gl_Normal;
vec3 fvBinormal = gl_NormalMatrix * rm_Binormal;
vec3 fvTangent = gl_NormalMatrix * rm_Tangent ;
ViewDirection.x = dot( fvTangent, fvViewDirection );
ViewDirection.y = dot( fvBinormal, fvViewDirection );
ViewDirection.z = dot( fvNormal, fvViewDirection );
LightDirection.x = dot( fvTangent, fvLightDirection.xyz );
LightDirection.y = dot( fvBinormal, fvLightDirection.xyz );
LightDirection.z = dot( fvNormal, fvLightDirection.xyz );
}
Code: Select all
// FRAGMENT SHADER//
//uniform vec4 fvAmbient;
//uniform vec4 fvSpecular;
//uniform vec4 fvDiffuse;
uniform float fSpecularPower;
uniform sampler2D baseMap;
uniform sampler2D bumpMap;
varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
void main( void )
{
vec3 fvLightDirection = normalize( LightDirection );
vec3 fvNormal = normalize( ( texture2D( bumpMap, Texcoord ).xyz * 2.0 ) - 1.0 );
float fNDotL = dot( fvNormal, fvLightDirection );
vec3 fvReflection = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection );
vec3 fvViewDirection = normalize( ViewDirection );
float fRDotV = max( 0.0, dot( fvReflection, fvViewDirection ) );
vec4 fvBaseColor = texture2D( baseMap, Texcoord );
vec4 fvAmbient = vec4(0.1,0.1,0.1,0.1);
vec4 fvSpecular = vec4(0.8,0.8,0.7,0.7);
vec4 fvDiffuse = vec4(1.0,1.0,1.0,1.0);
float fvSpecularPower = 20.0f;
vec4 fvTotalAmbient = fvAmbient * fvBaseColor;
vec4 fvTotalDiffuse = fvDiffuse * fNDotL * fvBaseColor;
vec4 fvTotalSpecular = fvSpecular * ( pow( fRDotV, fSpecularPower ) );
gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular );
}
Code: Select all
virtual void OnSetConstants(irr::video::IMaterialRendererServices* services, irr::s32 userData)
{
// VERTEX SHADER CONSTANTS //
irr::core::vector3df fvLightPosition = Light::light->getAbsolutePosition();
services->setVertexShaderConstant("fvLightPosition", reinterpret_cast<irr::f32*>(&fvLightPosition),3);
irr::core::vector3dffvEyePosition=Camera::camera->getAbsolutePosition();
services->setVertexShaderConstant("fvEyePosition", reinterpret_cast<irr::f32*>(&fvEyePosition),3);
irr::video::S3DVertexTangents *tVerts = (irr::video::S3DVertexTangents*)Plane::tmesh->getMeshBuffer(0)->getVertices();
irr::core::vector3df rm_Tangent = tVerts->Tangent;
services->setVertexShaderConstant("rm_Tangent", reinterpret_cast<irr::f32*>(&rm_Tangent),3);
irr::core::vector3df rm_Binormal = tVerts->Binormal;
services->setVertexShaderConstant("rm_Binormal", reinterpret_cast<irr::f32*>(&rm_Binormal),3);
// FRAG SHADER CONSTANTS //
irr::f32 baseMap = 0;
services->setPixelShaderConstant("baseMap", &baseMap,1);
irr::f32 bumpMap = 1;
services->setPixelShaderConstant("bumpMap", &bumpMap,1);
irr::video::SColorf fvAmbient;
fvAmbient.set(0.1f,0.1f,0.1f,1);
services->setPixelShaderConstant("fvAmbient",reinterpret_cast<irr::f32*>(&fvAmbient),4);
irr::video::SColorf fvDiffuse;
fvDiffuse.set(0.8f,0.8f,0.7f,1);
services->setPixelShaderConstant("fvDiffuse", reinterpret_cast<irr::f32*>(&fvDiffuse),4);
irr::video::SColorf fvSpecular;
fvSpecular.set(1,1,1,1);
services->setPixelShaderConstant("fvSpecular",reinterpret_cast<irr::f32*>(&fvSpecular),4);
irr::f32fSpecularPower=20.0f; services->setPixelShaderConstant("fSpecularPower",reinterpret_cast<irr::f32*>(&fSpecularPower),1);
I want to learn this on my own, but I cant figure out what Im missing.
I changed fvLightPosition to gl_LightSource[0].position, and that seems to make the randomness at least react to the light, ...randomly.
Can you help me to make this code work? For me to learn from it?