bump map Vertex shader
Code: Select all
#version 100
precision mediump int;
precision mediump float;
void FFP_VertexFog_Linear(in mat4 mWorldViewProj, in vec4 pos, in vec4 fogParams, out float oFogFactor)
{
vec4 vOutPos = mWorldViewProj * pos;
float distance = abs(vOutPos.w);
float fogFactor = (fogParams.z - distance) * fogParams.w;
oFogFactor = clamp(fogFactor, 0.0, 1.0);
}
uniform vec4 lightPosition; // object space
uniform vec3 eyePosition; // object space
uniform mat4 worldViewProj;
uniform vec4 gFogParams;
attribute vec4 position;
attribute vec3 normal;
attribute vec3 tangent;
attribute vec2 uv0;
varying vec2 vUV0;
varying vec3 vTSLightDir;
varying vec3 vTSHalfAngle;
varying float vEyeDist;
varying float oFogFactor;
void main()
{
// calculate output position
gl_Position = worldViewProj * position;
vUV0 = uv0;
vEyeDist = sqrt( (position.x - eyePosition.x) * (position.x - eyePosition.x) +
(position.y - eyePosition.y) * (position.y - eyePosition.y) +
(position.z - eyePosition.z) * (position.z - eyePosition.z) );
/*
value of lightPosition:
For Point lights: (pos.x, pos.y, pos.z, 1.0f) => lightDir = lightPosition.xyz - position
For directional lights: (-dir.x, -dir.y, -dir.z, 0.0f) => lightDir = lightPosition.xyz = (-dir.x, -dir.y, -dir.z)
So the following expression to compute the lightDir is correct
*/
vec3 lightDir = normalize(lightPosition.xyz - (position * lightPosition.w).xyz);
// Calculate the binormal (NB we assume both normal and tangent are already normalised)
vec3 binormal = cross(normal, tangent);
// Form a rotation matrix out of the vectors
mat3 rotation = mat3(tangent, binormal, normal);
// Transform the light vector according to this matrix
//combine the tangent and normal
vTSLightDir = rotation * lightDir;
// Calculate half-angle in tangent space
vec3 eyeDir = normalize(eyePosition - position.xyz);
vec3 halfAngle = normalize(eyeDir + lightDir);
//combine the direction of light and eye relative to the point
vTSHalfAngle = rotation * halfAngle;
FFP_VertexFog_Linear(worldViewProj, position, gFogParams, oFogFactor);
}
// mat3 rotation = mat3(vec3(tangent[0], binormal[0], normal[0]),
// vec3(tangent[1], binormal[1], normal[1]),
// vec3(tangent[2], binormal[2], normal[2]));
bump map Fragment shader
Code: Select all
#version 100
precision mediump int;
precision mediump float;
vec3 expand(vec3 v)
{
return (v - 0.5) * 2.0;
}
uniform vec4 lightDiffuse;
uniform vec4 lightSpecular;
uniform sampler2D diffuseMap;
uniform sampler2D normalMap;
uniform vec4 fogColour;
varying vec2 vUV0;
varying vec3 vTSLightDir;
varying vec3 vTSHalfAngle;
varying float vEyeDist;
varying float oFogFactor;
void main()
{
//get bump map vector, again expand from range-compressed
vec3 normal = expand(texture2D(normalMap, vUV0).xyz); //range: [-1, 1]
vec4 diffuseTex = texture2D(diffuseMap, vUV0);
//lightDir affects the diffuse part
vec3 lightDir = normalize(vTSLightDir);
vec4 diffusePart = lightDiffuse * clamp(dot(normal, lightDir), 0.0, 1.0);
//float diffuseFactor = pow(clamp(dot(normal, lightDir), 0.0, 1.0), 2.0);
//vec4 diffusePart = lightDiffuse * diffuseFactor;
// Pre-raise the specular exponent to the eight power
// the direction between eye direction and original light direction affects the specular part
vec3 halfAngle = normalize(vTSHalfAngle);
float specularFactor = pow(clamp(dot(normal, halfAngle), 0.0, 1.0), 12.0);
vec4 specularPart = lightSpecular * specularFactor;
// Calculate dot product for diffuse
vec4 baseColor = vec4(diffuseTex.xyz + diffusePart.xyz + specularPart.xyz, 1);
gl_FragColor = mix(fogColour, baseColor, oFogFactor);
//gl_FragColor = vec4(diffuseTex.xyz + diffusePart.xyz + specularPart.xyz, 1);
}
Bump map material
Code: Select all
material redBoardSpecularBumpMaterial
{
// This is the preferred technique which uses both vertex and
// fragment programs, supports coloured lights
technique
{
pass
{
iteration once_per_light
alpha_to_coverage off
colour_write on
cull_hardware clockwise
depth_check on
depth_func less_equal
depth_write on
illumination_stage
light_clip_planes off
light_scissor off
lighting off
normalise_normals off
polygon_mode solid
scene_blend one zero
scene_blend_op add
shading gouraud
transparent_sorting on
// Vertex program reference: BumpMappingSpecularVp.glsles
vertex_program_ref BumpMap/BumpMapVPSpecularGLSLES
{
param_named_auto lightPosition light_position_object_space 0
param_named_auto eyePosition camera_position_object_space
param_named_auto worldViewProj worldviewproj_matrix
param_named_auto gFogParams fog_params
}
// Fragment program: BumpMappingSpecularFp.glsles
fragment_program_ref BumpMap/BumpMapFPSpecularGLSLES
{
param_named_auto lightDiffuse light_diffuse_colour 0
param_named_auto lightSpecular light_specular_colour 0
param_named_auto fogColour fog_colour
}
texture_unit diffuseMap
{
texture env_diffuse.tex
tex_address_mode wrap
scale 1.0 1.0
tex_coord_set 0
colour_op modulate
}
texture_unit normalmap
{
texture env_norm.tex
tex_address_mode wrap
scale 1.0 1.0
tex_coord_set 0
colour_op modulate
}
}
}
}