Thank you abrodersen and sqrt for your replies.
@abrodersen:
Thanks for the info, will save me some headaches when (and if) I’ll have to work with non uniform scaling
@sqrt:
I know there’s something wrong but I cannot tell you what… Maybe is the kind of perspective, maybe that texture fits better with a vertical plane. Or maybe my shaders are wrong ( this theory is far more reliable ).
btw here is my code:
vp:
varying vec3 LightDir;
varying vec3 EyeDir;
varying vec3 HalfVector;
varying float LightDistance;
varying vec2 TexCoords;
attribute vec3 Tangent;
//////////////////////////////////////////////////////////////////////////////
// Transforms a vector from eye space to tangent space
// Parameters:
// t -> Tangent to the vertex
// b -> Binormal to the vertex
// n -> Normal to the vertex
// vector -> Vector to transform (will be normalized)
// Output:
// Vector in tangent space
vec3 TransformToTangentSpace( in vec3 t , in vec3 b , in vec3 n , in vec3 vector )
{
vec3 v;
vec3 temp = normalize( vector );
v.x = dot ( vector , t);
v.y = dot ( vector , b);
v.z = dot ( vector , n);
return normalize(v);
}
void main()
{
vec3 lPos; // Light position ( eye space )
vec3 halfVec; // Light half vector
vec3 vertPos; // Vertex position ( eye space )
vec3 eyePosition = vec3( 0.0 , 0.0 , 0.0 );
vec3 eyeDirection = vec3( 0.0 , 0.0 , -1.0 );
//Put the vertex in the position passed
gl_Position = ftransform();
vec3 n = normalize ( gl_NormalMatrix * gl_Normal);
vec3 t = normalize ( gl_NormalMatrix * Tangent );
vec3 b = cross (n, t);
vertPos = vec3( gl_ModelViewMatrix * gl_Vertex );
lPos = gl_LightSource[0].position.xyz - vertPos;
LightDistance = length( lPos );
LightDir = TransformToTangentSpace( t , b, n , lPos );
halfVec = gl_LightSource[0].halfVector.xyz;
HalfVector = TransformToTangentSpace( t , b, n , halfVec );
EyeDir = TransformToTangentSpace( t , b, n , eyeDirection );
TexCoords = gl_MultiTexCoord1.xy;
}
fp:
uniform sampler2D Texture_Normal;
uniform sampler2D Texture_Color;
uniform sampler2D Texture_HeightMap;
varying vec3 HalfVector;
varying vec3 EyeDir;
varying vec3 LightDir;
varying vec2 TexCoords;
varying float LightDistance;
const float specularCoeff = 0.6;
//////////////////////////////////////////////////////////////////////////////
// Computes the diffuse coefficent for the current light
// Parameters:
// lightIdx -> Index of the light to use
// d -> Distance form the light from the point ( interpolated from vp )
// Output:
// the diffuse coefficent for the current light
vec4 ComputeLightDiffuse( in int lightIdx , in float d)
{
float attenuation = 1.0 / ( gl_LightSource[ lightIdx ].constantAttenuation +
gl_LightSource[ lightIdx ].linearAttenuation * d +
gl_LightSource[ lightIdx ].quadraticAttenuation * d * d );
vec4 diffuse = gl_LightSource[ lightIdx ].diffuse * attenuation;
return diffuse;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Expands a normal read from the bumpmap texture into a normalized vector
// Parameters:
// packedNormal -> Bump normal value read from the bumpmap texture
// Output:
// Normal unpacked
vec3 UnpackNormal( in vec4 packedNormal )
{
return ( vec3( packedNormal ) - 0.5) * 2.0;
}
void main()
{
vec3 lDir; // Normalized light direction in tangent space
vec3 eDir; // Normalized eye direction in tangent space
vec3 hVec; // Light half vector
vec2 uvCoord; // Texture coords
float height; // height offset for parallax mapping
vec3 bumpNorm; // normal to the pixel ( read from the normal map )
vec4 decalCol; // decal color ( read from color map )
vec4 diffuse; // Light diffuse coefficent
float nDotL; // Dot product between the light direction in tangent space and the pixel normal
float spec; // Specular factor
vec3 finalCol; // Final fragment color after the computations
lDir = normalize ( LightDir );
eDir = normalize ( EyeDir );
hVec = normalize ( HalfVector );
uvCoord = TexCoords;
// Parallax mapping...
height = texture2D (Texture_HeightMap, uvCoord ).r;
height = height * 0.04 - 0.02;
uvCoord = uvCoord + ( eDir.xy * height );
bumpNorm = UnpackNormal( texture2D( Texture_Normal, uvCoord ) );
decalCol = texture2D( Texture_Color, uvCoord );
// compute distance between the surface and the light position
diffuse = ComputeLightDiffuse( 0 , LightDistance );
//Find the dot product between the light direction and the normal
nDotL = max( dot( bumpNorm, lDir ), 0.0 );
// Specular factor
spec = max( dot( HalfVector , bumpNorm ), 0.0) ;
spec = pow( spec, 128.0 ) * specularCoeff;
finalCol = nDotL * decalCol.rgb;
finalCol = min( finalCol + spec , 1.0 );
gl_FragColor = vec4( finalCol , decalCol.a ) * diffuse;
}
if you want to try the .exe you can download the latest version from http://download.yousendit.com/A6D8DCFD26C2FF4F
Extract it in a folder and edit the config.cfg file so that the paths are correct.
You can find the shaders in the data\ folder.
Thank you