Skip to main content
Offset Bump Ambient Lightmap         Offset/Bump with added ambient and optional lightmap

Table of contents

Hello, after some working on shaders, i have modified the offset sample to work with ambient light and lightmaps. It works only for 1 light, but includes specular. Updating to more lights should be easy. It is in CG code, so you can use it with DirectX and OpenGL.

Image Sample
Image Sample


It also shows how to use LOD for Materials, using a simple lightmapped texture for low-end cards.

The Material

Copy to clipboard
// Bump map with Parallax offset vertex program, support for this is required vertex_program SingleBumpVP cg { source singlebump.cg entry_point main_vp profiles vs_1_1 arbvp1 } // Bump map with parallax fragment program fragment_program SingleBumpFP cg { source singlebump.cg default_params { param_named lightmapactive float2 1.0 1.0 } entry_point main_fp profiles ps_2_0 arbfp1 } material vg/singlebump { lod_distances 400 // This is the preferred technique which uses both vertex and // fragment programs, supports coloured lights technique { // do the lighting and bump mapping with parallax pass // NB we don't do decal texture here because this is repeated per light pass { // Vertex program reference vertex_program_ref SingleBumpVP { param_named_auto lightPosition light_position_object_space 0 param_named_auto eyePosition camera_position_object_space param_named_auto worldViewProj worldviewproj_matrix } // Fragment program fragment_program_ref SingleBumpFP { param_named_auto lightAmbient ambient_light_colour 0 param_named_auto lightDiffuse light_diffuse_colour 0 param_named_auto lightSpecular light_specular_colour 0 // 1 active, 0 deactive. The second parameter will be the lightmap force (still not implemented) param_named lightmapactive float2 0.0 0.0 // Parallax Height scale and bias param_named scaleBias float4 0.0 -0.0 1 0 } // NORMAL + HEIGHT MAP texture_unit { texture_alias normalmap texture cam_ronda_c_88_bump.bmp tex_coord_set 0 } // DIFFUSE texture_unit { texture_alias diffusemap texture cam_ronda_c_88.bmp tex_coord_set 1 } // LIGHTMAP texture_unit { texture_alias lightmap texture Caja01MapaSombras.bmp tex_coord_set 2 } } } // Simple no-shader fallback technique { lod_index 1 pass { // Base diffuse texture map texture_unit { texture_alias diffusemap texture cam_ronda_c_88.bmp tex_coord_set 0 } // LIGHTMAP texture_unit { texture_alias lightmap texture white.bmp tex_coord_set 2 colour_op modulate } } } }

The CG PROGRAM

Copy to clipboard
/* 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, float2 uvlm : TEXCOORD2, // 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, // out float2 ouvlm : TEXCOORD4, // parameters uniform float4 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; // lightmap uv direct ouvlm = uvlm; // calculate tangent space light vector // Get object space light direction float3 lightDir = normalize(lightPosition.xyz - (position * lightPosition.w)); 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; } // THE LIGTHTMAPACTIVE IS: //lightmapactive[0] if active = 1.0 //lightmapactive[1] multiplier (still not implemented) void main_fp(float2 uv : TEXCOORD0, float3 lightDir : TEXCOORD1, float3 eyeDir : TEXCOORD2, float3 halfAngle : TEXCOORD3, float2 uvlm : TEXCOORD4, uniform float2 lightmapactive, uniform float3 lightAmbient, uniform float3 lightDiffuse, uniform float3 lightSpecular, uniform float4 scaleBias, uniform sampler2D diffuseMap, uniform sampler2D normalHeightMap, uniform sampler2D lightMap, 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)), 32) * lightSpecular; float3 difusa = (diffuse * saturate(dot(normal, lightDir)) * lightDiffuse); float3 ambiental = diffuse*lightAmbient; float3 col = ambiental + difusa + specular; // MAX DIFFUSE if (col[0] > diffuse[0]) col[0] = diffuse[0]; if (col[1] > diffuse[1]) col[1] = diffuse[1]; if (col[2] > diffuse[2]) col[2] = diffuse[2]; // LIGHTMAP if (lightmapactive[0] == 1.0) { float3 lm = tex2D(lightMap,uvlm).xyz; col = col * lm; } // SPECULAR col = col + specular; oColor = float4(col, 1); }