//--------------------------------------------------------------------------------------
// File: Pick.fx
//
// The effect file for the Pick sample.  
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------

const float colors = 256.0; // number of colors the display can show

//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4 g_MaterialAmbientColor:register(c10);      // Material's ambient color
float4 g_MaterialDiffuseColor:register(c11);      // Material's diffuse color
float3 g_LightDir;                  // Light's direction in world space
float4 g_LightDiffuse;              // Light's diffuse color

float    g_fTime;                   // App's time in seconds
float4x4 g_mWorld;                  // World matrix for object
float4x4 g_mWorldViewProjection;    // World * View * Projection matrix

texture g_MeshTexture;              // Color texture for mesh

float2 g_TexelSize : TEXELSIZE;

float4 vGravity = float4(0,-2.0,0,0);
float Layer     = 0; // 0 to 1 for the level


//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler MeshTextureSampler:register(s10) = 
sampler_state
{
    Texture = <g_MeshTexture>;
}
;


//--------------------------------------------------------------------------------------
// Vertex shader output structure
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Position   : POSITION;   // vertex position 
    float4 Diffuse    : COLOR0;     // vertex diffuse color (note that COLOR0 is clamped from 0..1)
    float2 TextureUV  : TEXCOORD0;  // vertex texture coords 
};


//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0 )
{
    VS_OUTPUT Output;
	float4 Pos = vPos;
	
	Pos.z -= cos( Pos.x/2.5);
	Pos.z -= cos( Pos.y/2.5);
	
	Output.Diffuse    = 0;
    Output.Position =vPos+ mul(Pos,g_mWorldViewProjection);
    Output.TextureUV = vTexCoord0; 
    
    return Output;    
}


//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT FullScreenVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0 )
{
    VS_OUTPUT Output;
	Output.Diffuse    = 0;
	
	vPos.xy = sign(vPos.xy);  

	Output.Position = float4(vPos.xy, 0, 1);  
	Output.Position.z = 1.0f;
	// Image-space  
	Output.TextureUV.x = 0.5 * (1 + vPos.x);  
	Output.TextureUV.y = 0.5 * (1 - vPos.y);  
	
    return Output;    
}


//--------------------------------------------------------------------------------------
// Pixel shader output structure
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};


//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderScenePS( VS_OUTPUT In ) 
{ 
    PS_OUTPUT Output;

  
	Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV);

    return Output;
}

//--------------------------------------------------------------------------------------
// MCAMBER
//--------------------------------------------------------------------------------------
float4 MCAmber_PS ( VS_OUTPUT  In ) : COLOR
{
	half3 ink = half3(0.55, 0.41, 0.0);
	half3 c11 = tex2D(MeshTextureSampler, In.TextureUV).xyz;
	half lct = floor(colors*length(c11))/colors;
	return half4(lct*ink,1);
}


const float3 dtt = float3(65536,255,1);



/*  GET_RESULT function                            */
/*  Copyright (c) 1999-2001 by Derek Liauw Kie Fa  */
/*  License: GNU-GPL                               */


int GET_RESULT(float A, float B, float C, float D)
{
	int x = 0; int y = 0; int r = 0;
	if (A == C) x+=1; else if (B == C) y+=1;
	if (A == D) x+=1; else if (B == D) y+=1;
	if (x <= 1) r+=1; 
	if (y <= 1) r-=1;
	return r;
} 


float reduce(half3 color)
{ 
	return dot(color,dtt);
}

float4 SuperEaglePS ( VS_OUTPUT VAR ) : COLOR
{
	// calculating offsets, coordinates

	half2 dx = half2( g_TexelSize.x, 0.0); 
	half2 dy = half2( 0.0, g_TexelSize.y);
	half2 g1 = half2( g_TexelSize.x,g_TexelSize.y);
	half2 g2 = half2(-g_TexelSize.x,g_TexelSize.y);	
	
	float2 pixcoord  = VAR.TextureUV/g_TexelSize;
	float2 fp        = frac(pixcoord);
	float2 pC4       = VAR.TextureUV-fp*g_TexelSize;
	float2 pC8       = pC4+g1;

	

	// Reading the texels

	half3 C0 = tex2D(MeshTextureSampler,pC4-g1).xyz; 
	half3 C1 = tex2D(MeshTextureSampler,pC4-dy).xyz;
	half3 C2 = tex2D(MeshTextureSampler,pC4-g2).xyz;
	half3 D3 = tex2D(MeshTextureSampler,pC4-g2+dx).xyz;
	half3 C3 = tex2D(MeshTextureSampler,pC4-dx).xyz;
	half3 C4 = tex2D(MeshTextureSampler,pC4   ).xyz;
	half3 C5 = tex2D(MeshTextureSampler,pC4+dx).xyz;
	half3 D4 = tex2D(MeshTextureSampler,pC8-g2).xyz;
	half3 C6 = tex2D(MeshTextureSampler,pC4+g2).xyz;
	half3 C7 = tex2D(MeshTextureSampler,pC4+dy).xyz;
	half3 C8 = tex2D(MeshTextureSampler,pC4+g1).xyz;
	half3 D5 = tex2D(MeshTextureSampler,pC8+dx).xyz;
	half3 D0 = tex2D(MeshTextureSampler,pC4+g2+dy).xyz;
	half3 D1 = tex2D(MeshTextureSampler,pC8+g2).xyz;
	half3 D2 = tex2D(MeshTextureSampler,pC8+dy).xyz;
	half3 D6 = tex2D(MeshTextureSampler,pC8+g1).xyz;

	float3 p00,p10,p01,p11;


	// reducing half3 to float
	
	float c0 = reduce(C0);float c1 = reduce(C1);
	float c2 = reduce(C2);float c3 = reduce(C3);
	float c4 = reduce(C4);float c5 = reduce(C5);
	float c6 = reduce(C6);float c7 = reduce(C7);
	float c8 = reduce(C8);float d0 = reduce(D0);
	float d1 = reduce(D1);float d2 = reduce(D2);
	float d3 = reduce(D3);float d4 = reduce(D4);
	float d5 = reduce(D5);float d6 = reduce(D6);



	/*              SuperEagle code               */
	/*  Copied from the Dosbox source code        */
	/*  Copyright (C) 2002-2007  The DOSBox Team  */
	/*  License: GNU-GPL                          */
	/*  Adapted by guest(r) on 16.4.2007          */

       
	if (c4 != c8) {
                if (c7 == c5) {
                        p01 = p10 = C7;
                        if ((c6 == c7) || (c5 == c2)) {
                                p00 = 0.25*(3.0*C7+C4);
                        } else {
                                p00 = 0.5*(C4+C5);
                        }

                        if ((c5 == d4) || (c7 == d1)) {
                                p11 = 0.25*(3.0*C7+C8);
                        } else {
                                p11 = 0.5*(C7+C8);
                        }
                } else {
                        p11 = 0.125*(6.0*C8+C7+C5);
                        p00 = 0.125*(6.0*C4+C7+C5);

                        p10 = 0.125*(6.0*C7+C4+C8);
                        p01 = 0.125*(6.0*C5+C4+C8);
                }
         } else {
                if (c7 != c5) {
                        p11 = p00 = C4;

                        if ((c1 == c4) || (c8 == d5)) {
                                p01 = 0.25*(3.0*C4+C5);
                        } else {
                                p01 = 0.5*(C4+C5);
                        }

                        if ((c8 == d2) || (c3 == c4)) {
                                p10 = 0.25*(3.0*C4+C7);
                        } else {
                                p10 = 0.5*(C7+C8);
                        }
                } else {
                        int r = 0;
                        r += GET_RESULT(c5,c4,c6,d1);
                        r += GET_RESULT(c5,c4,c3,c1);
                        r += GET_RESULT(c5,c4,d2,d5);
                        r += GET_RESULT(c5,c4,c2,d4);

                        if (r > 0) {
                                p01 = p10 = C7;
                                p00 = p11 = 0.5*(C4+C5);
                        } else if (r < 0) {
                                p11 = p00 = C4;
                                p01 = p10 = 0.5*(C4+C5);
                        } else {
                                p11 = p00 = C4;
                                p01 = p10 = C7;
                        }
                }
        }



	// Distributing the four products
	
	if (fp.x < 0.50)
		{ if (fp.y < 0.50) p10 = p00;}
	else
		{ if (fp.y < 0.50) p10 = p01; else p10 = p11;}

	return float4(p10,1); 
}

technique RenderModel//MCAmber
{
   pass P0
   {
     VertexShader = compile vs_2_0 FullScreenVS();
     PixelShader  = compile ps_2_0 SuperEaglePS();
   }  
}

//--------------------------------------------------------------------------------------
// Renders Background 
//--------------------------------------------------------------------------------------
technique RenderFullScreen
{
    pass P0
    {          
		VertexShader = compile vs_2_0 FullScreenVS();
        PixelShader  = compile ps_2_0 RenderScenePS(); 
    }
}

//--------------------------------------------------------------------------------------
// Renders scene 
//--------------------------------------------------------------------------------------
technique RenderModel_old//RenderModel
{
    pass P0
    {          
		VertexShader = compile vs_2_0 FullScreenVS();
        PixelShader  = compile ps_2_0 RenderScenePS(); 
    }
}
