hello kowal,
i have seen loads of examples on the web about bump map / normal map and how to use them, but the thing is that everyone already has a normal map of that texture to apply to the object and in the shader…in my case it´s a little bit diferent because, i´m going to render to a texture , using FBO, so everyframe will be a texture so i need to find the normals @ each texture , so later i can combine with other textures to compose the final image…i have made some improvements on the shaders and got preety good results let me show you:
Normal Map:
Texture + Normal Map:
Vertex Shader:
uniform vec3 LightPosition; //light position
uniform vec3 tangent; //tangent
varying vec3 n;
varying vec3 TBN_EyeDir; //Eye direction in tangent space
varying vec3 TBN_LightDir; //light position in tangent space
varying vec3 TBN_HalfVector; // Half vector in tangent space
varying mat3 TBN; //TBN matrix
void main( void )
{
//transformation of the vertex to clip space
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
// Texture coordinates
// Multiplication with texture matrix can be omitted if default (identity matrix) is used. To
// use the texture matrix comment the first and uncomment the second line.
gl_TexCoord[0] = gl_MultiTexCoord0;
// gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
// gl_FrontColor = gl_Color;
/* transformation of the vertex to eye space
Vertex coords from eye position */
vec4 eV = gl_ModelViewMatrix * gl_Vertex;
// pos in 3d space
TBN_EyeDir = vec3(eV)/eV.w;
/* Calculate TBN matrix
// Tangent space vectors (TBN)
// The binormal can either be passed as an attribute or calculated as cross(normal, tangent). */
vec3 t = gl_NormalMatrix * tangent;
n = gl_NormalMatrix * gl_Normal;
vec3 b = cross( t, n);
TBN = mat3( t, b , n );
/* Light Dir to tangent space */
vec3 v=LightPosition-TBN_EyeDir;// Light direction from vertex, For positional lights lights
v.x=dot(v,t);
v.y=dot(v,b);
v.z=dot(v,n);
TBN_LightDir=normalize(v);
/* Eye Dir to tangent space
// Eye direction from vertex, for half vector
// If eye position is at (0, 0, 0), -mvVertex points to eye position from vertex. Otherwise
// direction to eye is: eyePosition - mvVertex */
v= -TBN_EyeDir;
v.x=dot(v,t);
v.y=dot(v,b);
v.z=dot(v,n);
TBN_EyeDir=normalize(v);
// Half-vector for specular highlights
TBN_HalfVector = normalize(TBN_LightDir + TBN_EyeDir);
}
Fragment Shader:
uniform sampler2D tex;
varying vec3 TBN_EyeDir; //Eye direction
varying vec3 TBN_LightDir; //light position
varying vec3 TBN_HalfVector; // Half vector in tangent space
varying mat3 TBN; //TBN matrix
void main( void )
{
// Base colour from texture
vec4 baseColour = texture2D(tex, gl_TexCoord[0].xy);
// Uncompress normal from normal map texture
// vec3 normal = normalize(texture2D(tex, gl_TexCoord[0].xy).xyz * 2.0 - 1.0);
// Depending on the normal map's format, the normal's Y direction may have to be inverted to
// achieve the correct result. This depends - for example - on how the normal map has been
// created or how it was loaded by the engine. If the shader output seems wrong, uncomment
// this line:
// normal.y = -normal.y;
/**/ vec2 c= texture2D( tex, gl_TexCoord[0].xy).rg;
vec2 p= (fract(c) - vec2(0.5));
vec3 normDelta=vec3(p.x,p.y,1.0);
vec3 normal;
normal = TBN * normalize( (normDelta* 2.0) - 1.0 );
// vec3 normal = normalize(texture2D(tex, gl_TexCoord[0].xy).xyz * 2.0 - 1.0);
// normal = TBN * normal;
// Ambient
vec4 ambient = (0.1,0.1,0.1,1.0) * baseColour;
// Diffuse
// Normalize interpolated direction to light
vec3 tbnNormDirToLight = normalize(TBN_LightDir);
// Full strength if normal points directly at light
float diffuseIntensity = max(dot(tbnNormDirToLight, normal), 0.0);
vec4 diffuse = (0.3,0.3,0.3,1.0) * baseColour * diffuseIntensity;
// Specular
vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
// Only calculate specular light if light reaches the fragment.
if (diffuseIntensity > 0.0) {
// Colour of specular reflection
// vec4 specularColour = texture2D(specularMap, gl_TexCoord[0].xy);
vec4 specularColour = vec4(0.2, 0.2, 0.2, 1.0);
// Specular strength, Blinn�Phong shading model
float specularModifier = max(dot(normal, normalize(TBN_HalfVector)), 0.0);
specular = specular * specularColour * pow(specularModifier, 5.0);
}
// Sum of all lights
gl_FragColor = clamp(ambient + diffuse + specular, 0.0, 1.0);
// gl_FragColor =vec4(normal, 1.0 );
i have been running both shaders mine and yours to see the results and they both look preety good let me know what do you think, does it look good or not?
ps:if you ever come to portugal, let me know , we´ll go for a beer