1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| /* Bump mapping with Parallax offset vertex program
In this program, we want to calculate the tangent space light end eye vectors
which will get passed to the fragment program to produce the per-pixel bump map
with parallax offset effect.
*/
/* Vertex program that moves light and eye vectors into texture tangent space at vertex */
void main_vp(float4 position : POSITION,
float3 normal : NORMAL,
float2 uv : TEXCOORD0,
float3 tangent : TEXCOORD1,
// outputs
out float4 oPosition : POSITION,
out float2 oUv : TEXCOORD0,
out float3 oLightDir : TEXCOORD1, // tangent space
out float3 oEyeDir : TEXCOORD2, // tangent space
out float3 oHalfAngle : TEXCOORD3, //
// parameters
uniform float3 lightPosition, // object space
uniform float3 eyePosition, // object space
uniform float4x4 worldViewProj)
{
// calculate output position
oPosition = mul(worldViewProj, position);
// pass the main uvs straight through unchanged
oUv = uv;
// calculate tangent space light vector
// Get object space light direction
float3 lightDir = lightPosition - position.xyz;
float3 eyeDir = eyePosition - position.xyz;
// Calculate the binormal (NB we assume both normal and tangent are
// already normalised)
// NB looks like nvidia cross params are BACKWARDS to what you'd expect
// this equates to NxT, not TxN
float3 binormal = cross(tangent, normal);
// Form a rotation matrix out of the vectors
float3x3 rotation = float3x3(tangent, binormal, normal);
// Transform the light vector according to this matrix
lightDir = normalize(mul(rotation, lightDir));
eyeDir = normalize(mul(rotation, eyeDir));
oLightDir = lightDir;
oEyeDir = eyeDir;
oHalfAngle = normalize(eyeDir + lightDir);
}
// General functions
// Expand a range-compressed vector
float3 expand(float3 v)
{
return (v - 0.5) * 2;
}
void main_fp(float2 uv : TEXCOORD0,
float3 lightDir : TEXCOORD1,
float3 eyeDir : TEXCOORD2,
float3 halfAngle : TEXCOORD3,
uniform float3 lightDiffuse,
uniform float3 lightSpecular,
uniform float4 scaleBias,
uniform sampler2D normalHeightMap,
uniform sampler2D diffuseMap,
uniform sampler2D glossMap,//LA GLOSS MAP
out float4 oColor : COLOR)
{
// get the height using the tex coords
float height = tex2D(normalHeightMap, uv).a;
// scale and bias factors
float scale = scaleBias.x;
float bias = scaleBias.y;
// calculate displacement
float displacement = (height * scale) + bias;
float3 uv2 = float3(uv, 1);
// calculate the new tex coord to use for normal and diffuse
float2 newTexCoord = ((eyeDir * displacement) + uv2).xy;
// get the new normal and diffuse values
float3 normal = expand(tex2D(normalHeightMap, newTexCoord).xyz);
float3 diffuse = tex2D(diffuseMap, newTexCoord).xyz;
float3 specular = pow(saturate(dot(normal, halfAngle)),16) *lightSpecular*tex2D(glossMap, uv).xyz;//voila c'est comme ça que je calcule la spécularité
//float3 specular = saturate(dot(normal, halfAngle))* lightSpecular;
float3 col = (diffuse * saturate(dot(normal, lightDir)) * lightDiffuse + specular);
oColor = float4(col, 1);
} |
Partager