Accurate per-pixel cube mapping with normal map influence         Using lambert diffuse model and phong highlight specular


Title says it all.
Single pass, 2 lights (fixed), per pixel, lambert diffuse, phong specular, and reflective cubemap CG shader. All channels influenced by the normal map, even the ambient!
Mesh is required to have embedded NORMAL, TANGENT and BINORMAL info.

Beautiful smooth diffuse, specular and reflections were the target. Good for close ups.
This should be an expensive shader (everything is done in the fragment).


Low cubemap reflection.

Normals in the shade.

High cubemap reflection.

Low poly sphere on close up!


  • uvDecalScale: To tile the diffuse (decal) texture.
  • uvNormalScale: To tile the normal and specular texture.
  • ambient: Brighten/Tint up the diffuse.
  • diffuseColor: Base color of the object.
  • reflectivity: Linear interpolation between the diffuse and the spheremap.
  • specularPower0: Phong coefficient of light 0.
  • specularPower1: Phong coefficient of light 1.
  • specularMultiplier0: FInal multiplier of specular for light 0. To manipulate specular intensity.
  • specularMultiplier1: FInal multiplier of specular for light 1.
  • useSpecular: Enable/Disable specular highlights.

CG shader

//	Title: 
//	Single Pass, 2 Lights, per-pixel CG shader with Lambert diffuse, Phong specular, and CubeMap reflection.
//	Every channel is normal perturbated.
//	Precision over performace.
//	Requires meshes to have implicit NORMAL, TANGENT and BINORMAL

//	Author: Alberto Toglia (ogre user:toglia)

//	License: AS IS; Use it for whatever reason (commercial, open source...), modify it as you like. Don't bother letting me know. ;)
float3 expand(float3 v)
	return (v - 0.5) * 2;

void main_vs(
	float4 position:			POSITION,
	float2 uv:				TEXCOORD0,
	float4 normal:				NORMAL,	
	float4 binormal:			BINORMAL,
	float4 tangent: 			TANGENT,		
	out float4 oPosition_W:			POSITION,
	out float2 oUV:				TEXCOORD0,	
	out float4 oPosition_OS:		TEXCOORD1,		
	out float4 oNormal_OS:			TEXCOORD2,
	out float4 oBinormal_OS:		TEXCOORD3,
	out float4 oTangent_OS:			TEXCOORD4,
	uniform float4x4 worldViewProjMatrix
	oPosition_W = mul(worldViewProjMatrix, position);	
	oUV = uv;	
	oPosition_OS = position;
	oNormal_OS = normal;
	oBinormal_OS = binormal;
	oTangent_OS = tangent;

float4 main_ps(
	float2 uv:				TEXCOORD0,	
	float4 position_OS:			TEXCOORD1,
	float4 normal_OS:			TEXCOORD2,
	float4 binormal_OS:			TEXCOORD3,
	float4 tangent_OS:			TEXCOORD4,	
	uniform float2 uvDecalScale,
	uniform float2 uvNormalScale,
	uniform float4 ambient,
	uniform float4 diffuseColor,
	uniform float reflectivity,	
	uniform float specularPower0,
	uniform float specularPower1,
	uniform float specularMultiplier0,
	uniform float specularMultiplier1,	
	uniform float useSpecular,
	uniform float4x4 worldMatrix,	
	uniform float3 cameraPosition_WS,
	uniform float3 cameraPosition_OS,	
	uniform	float4 lightPosition_OS[2],
	uniform float4 lightDiffuse[2],
	uniform float4 lightSpecular[2],
	uniform sampler2D DiffuseMap:				TEXUNIT0,
	uniform sampler2D NormalMap:				TEXUNIT1,
	uniform sampler2D SpecularMap:				TEXUNIT2,
	uniform samplerCUBE CubeMap:				TEXUNIT3
) : COLOR {	
	//Expanding the texture to make it signed; generally comes unsigned.
	float3 normalTexture = expand(tex2D(NormalMap, uv*uvNormalScale).xyz);	
	//Normal, Binormal and Tangent in object space.
	//Normalization made in the fragment shader to get nicer results; although its more expensive here (per-pixel).
	float3 N_OS = normalize(;
	float3 B_OS = normalize(;
	float3 T_OS = normalize(;
	//Normal, Binormal and Tangent in world space.
	float3 N_WS = mul((float3x3)worldMatrix,;
	float3 B_WS = mul((float3x3)worldMatrix,;
	float3 T_WS = mul((float3x3)worldMatrix,;
	//Making the rotation matrices
	float3x3 rotation_2_TS = float3x3(T_OS, B_OS, N_OS);
	float3x3 rotation_2_WS = float3x3(T_WS, B_WS, N_WS);	
	//Putting the final normal vector (object normal + texture normal) in tangent space.
	//This vector is used to influence the phong lighting with the normal map.
	float3 normal_TS = normalize(normalize(mul(rotation_2_TS,N_OS))+normalize(normalTexture));
	//Putting the position in world space; Later will be used to calculate the camera direction in world space.
	//World space values needed for the cube map reflection.
	float3 position_WS = mul(float3x3(worldMatrix),float3(position_OS));
	//Putting the final normal vector in world space.
	//This vector is used to influence the cube map reflection with the normal map.	
	float3 normal_WS = normalize(mul(normalTexture,rotation_2_WS));
	//Calculating the camera direction in tangent space for the diffuse and specular math.
	//First step
	float3 cameraDirection_OS = normalize(cameraPosition_OS -;
	//Second step
	float3 cameraDirection_TS = normalize(mul(rotation_2_TS, cameraDirection_OS));
	//We need the camera direction in world space for the cube map reflection.
	float3 cameraDirection_WS = mul((float3x3)worldMatrix,cameraDirection_OS);

	//Calculating the light direction in tangent space for the diffuse and specular math.
	//First step
	float3 lightDirection0_OS = normalize(lightPosition_OS[0].xyz - ( * lightPosition_OS[0].w));
	float3 lightDirection1_OS = normalize(lightPosition_OS[1].xyz - ( * lightPosition_OS[1].w));	
	//Second step
	float3 lightDirection0_TS = mul(rotation_2_TS,lightDirection0_OS);
	float3 lightDirection1_TS = mul(rotation_2_TS,lightDirection1_OS);
	//Simple lambert diffuse model N.L
	float light0 = saturate(dot(N_OS,lightDirection0_OS));
	float light1 = saturate(dot(N_OS,lightDirection1_OS));

	//Normal channel for the lambert diffuse.
	float normal0 = saturate(dot(normalTexture, lightDirection0_TS));
	float normal1 = saturate(dot(normalTexture, lightDirection1_TS));		
	//Sample the diffuse map.
	float4 diffuseTexture = tex2D(DiffuseMap, uv);
	//Tint diffuse with light information.
	float4 diffuse0 = light0*lightDiffuse[0]*normal0;	
	float4 diffuse1 = light1*lightDiffuse[1]*normal1;
	//Cube map sampling with normals in world space	
	float3 cameraReflection_TS = reflect(-cameraDirection_WS,normal_WS);
	float4 reflectedColor = texCUBE(CubeMap, cameraReflection_TS);
	//Sample the specular map
	float4 specularTexture = tex2D(SpecularMap, uv*uvNormalScale);
	//Linear interpolation between the final diffuse and the reflection cube map
	float4 diffuse = lerp((diffuse0+diffuse1),reflectedColor,reflectivity);
	//This is CRAZY!!! Not very mathematical, but it looks good.
	//Why you want this? When pushing ambient values, although the shadows are brigther no normal perturbation is visible in the shade.	
	//How to simulate ambient normals? Calculate the normal as if the light source was the camera.
	float ambientNormal = saturate(dot(normalTexture,cameraDirection_TS));	
		//Normal_TS is already influenced by the normal texture so we have to do everything in Tangent Space.
		float3 lightReflection0_TS = reflect(-lightDirection0_TS,normal_TS);
		float3 lightReflection1_TS = reflect(-lightDirection1_TS,normal_TS);		
		//Phong specular model R.V
		float RdotV0 = saturate(dot(lightReflection0_TS,cameraDirection_TS));
		float RdotV1 = saturate(dot(lightReflection1_TS,cameraDirection_TS));
		//Tint the specular with the light specular color.
		float4 specular0 = pow(RdotV0,specularPower0)*lightSpecular[0]*specularMultiplier0;
		float4 specular1 = pow(RdotV1,specularPower1)*lightSpecular[1]*specularMultiplier1;
		return (ambient*ambientNormal*diffuseTexture)+diffuse*diffuseColor*diffuseTexture+(specular0+specular1)*specularTexture;		
		return (ambient*ambientNormal*diffuseTexture)+diffuse*diffuseColor*diffuseTexture;

Ogre Material Definition


vertex_program ppPhongNormalCubeMap_vp cg
	entry_point main_vs
	profiles vs_1_1 arbvp1

fragment_program ppPhongNormalCubeMap_fp cg
	entry_point main_ps
	profiles ps_2_0 arbfp1
		param_named_auto	worldMatrix				world_matrix

		param_named_auto	cameraPosition_OS			camera_position_object_space				

		param_named_auto 	lightPosition_OS			light_position_object_space_array 2
		param_named_auto	lightDiffuse				light_diffuse_colour_array 2
		param_named_auto	lightSpecular				light_specular_colour_array 2

material ppPhongNormalCubeMap
			vertex_program_ref ppPhongNormalCubeMap_vp
				param_named_auto	worldViewProjMatrix	worldviewproj_matrix

			fragment_program_ref ppPhongNormalCubeMap_fp
				//UV scale for decal (diffuse) texture.
				param_named uvDecalScale float2 1 1
				//UV scale for normal and specular map. 
				param_named uvNormalScale float2 2.5 2.5				
				param_named ambient float4 0.0 0.0 0.0 1.0
				param_named diffuseColor float4 0.75 0.8 1 1.0
				//Linear interpolation between the diffuse and the cubemap
				param_named reflectivity float 1
				//Phong exponent
				param_named specularPower0 float 5
				param_named specularPower1 float 5
				//In some cases its necesarry to lower the brightness of the speculararity (different than the power)
				param_named specularMultiplier0 float 0.75
				param_named specularMultiplier1 float 0.75
				//Turn on or off the phong specular higlight; Higher than 0 is considered ON
				param_named useSpecular float 1
				texture_alias DiffuseMap
				texture_alias NormalMap
				texture_alias SpecularMap
				texture_alias ReflectionMap
				tex_address_mode clamp

material ballMaterial : ppPhongNormalCubeMap
	set_texture_alias DiffuseMap
	set_texture_alias NormalMap
	set_texture_alias SpecularMap
	set_texture_alias ReflectionMap


  • Decal

I did it myself. Its 100% free!

  • Normal

The original leather texture is not mine, it was taken from here. Converted to normal map using crazybump software.
I'm not sure what license it has.

  • Specular

Generated with the same leather texture with crazybump.

  • Cubemap

Note that this is a cube cross (just for showing purposes), Can be converted to dds format using CubeMapGen.
Taken from the humus texture library.