/*
This file is part of Caelum.
See http://www.ogre3d.org/wiki/index.php/Caelum 

Copyright (c) 2008 Caelum team. See Contributors.txt for details.

Caelum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Caelum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Caelum. If not, see <http://www.gnu.org/licenses/>.
*/

// Entry point for vertex program.
void MainVP
(
		in float4 iPosition : POSITION,
		in float2 iTexCoord : TEXCOORD0,
    	in float4 iNormal : NORMAL,
		
		uniform float4x4 worldviewproj_matrix,
        uniform float4x4 inverse_transpose_worldview_matrix,

		out float4 oPosition : POSITION,
        out float2 oTexCoord : TEXCOORD0,
    	out float3 oNormal : TEXCOORD1
) {
	oPosition = mul(worldviewproj_matrix, iPosition);
    oTexCoord = iTexCoord;
	oNormal = normalize(mul(inverse_transpose_worldview_matrix, iNormal).xyz);
}

sampler texture1 : register(s0);
sampler texture2 : register(s1);

// Entry point for fragment program.
void MainFP
(
	in float2 in_texcoord : TEXCOORD0,
	in float3 in_normal : TEXCOORD1,

	uniform float2 texture1_scale,
	uniform float2 texture2_scale,
	uniform float texture_lerp_factor,

	uniform float4 derived_scene_colour,
	uniform float4 light_position_view_space_0,
	uniform float4 derived_light_diffuse_colour_0,
	uniform float4 light_position_view_space_1,
	uniform float4 derived_light_diffuse_colour_1,
			
	out float4 out_color : COLOR
) {
    // Renormalize after lerp.
    float3 normal = normalize(in_normal);

    // Accumulate two lights
    float4 light_colour = derived_scene_colour;

    float diffuse_factor_0 = max(0, dot(normal, light_position_view_space_0));
    light_colour += diffuse_factor_0 * derived_light_diffuse_colour_0;

    float diffuse_factor_1 = max(0, dot(normal, light_position_view_space_1));
    light_colour += diffuse_factor_1 * derived_light_diffuse_colour_1;

    // Texture fetches
    float4 col1 = tex2D(texture1, in_texcoord * texture1_scale);
    float4 col2 = tex2D(texture2, in_texcoord * texture2_scale);

    out_color = col1 * col2 * light_colour;
    //out_color = float4(normal, 1);
}