// Geometric Tools, Inc.
// http://www.geometrictools.com
// Copyright (c) 1998-2005.  All Rights Reserved
//
// The Wild Magic Library (WM3) source code is supplied under the terms of
// the license agreement
//     http://www.geometrictools.com/License/WildMagic3License.pdf
// and may not be copied or disclosed except in accordance with the terms
// of that agreement.

#include "Wm3FresnelVShader.h"
using namespace Wm3;

WM3_IMPLEMENT_RTTI(Wm3,FresnelVShader,VertexShader);
WM3_IMPLEMENT_STREAM(FresnelVShader);
WM3_IMPLEMENT_DEFAULT_STREAM(FresnelVShader,VertexShader);
WM3_IMPLEMENT_DEFAULT_NAME_ID(FresnelVShader,VertexShader);

//----------------------------------------------------------------------------
FresnelVShader::FresnelVShader ()
{
    // shader program (load the type of the current renderer)
    m_acProgram = ms_aacProgram[Renderer::GetType()];

    // shader constants
    int iType, iOption, iReg, iRegQuantity;
    ShaderConstant* pkConst;

    // model-to-view * projection matrix (4x4)
    iType = ShaderConstant::SC_TRANSFORM_MVP;
    iOption = ShaderConstant::SCO_MATRIX;
    iReg = 0;
    iRegQuantity = 4;
    pkConst = new ShaderConstant(iType,iOption,iReg,iRegQuantity);
    m_kConstant.Append(pkConst);

    // model-to-world matrix (4x4)
    iType = ShaderConstant::SC_TRANSFORM_M;
    iOption = ShaderConstant::SCO_MATRIX;
    iReg = 4;
    iRegQuantity = 4;
    pkConst = new ShaderConstant(iType,iOption,iReg,iRegQuantity);
    m_kConstant.Append(pkConst);

    // camera position vector (x,y,z,1)
    iType = ShaderConstant::SC_CAMERA_POSITION;
    iOption = ShaderConstant::SCO_NONE;
    iReg = 8;
    iRegQuantity = 1;
    pkConst = new ShaderConstant(iType,iOption,iReg,iRegQuantity);
    m_kConstant.Append(pkConst);

    // numerical constant vector (register 9)
    iType = ShaderConstant::SC_NUMERICAL_CONSTANT;
    iOption = ShaderConstant::SCO_NONE;
    iReg = 9;
    iRegQuantity = 1;
    pkConst = new ShaderConstant(iType,iOption,iReg,iRegQuantity);
    pkConst->SetRegisterData(0,0.000000f,1.00000f,2.00000f,0.500000f);
    m_kConstant.Append(pkConst);

}
//----------------------------------------------------------------------------
FresnelVShader::~FresnelVShader ()
{
}
//----------------------------------------------------------------------------
const char* FresnelVShader::ms_aacProgram[Renderer::RT_QUANTITY] =
{
"!!ARBvp1.0\n"
"# ARB_vertex_program generated by NVIDIA Cg compiler\n"
"# cgc version 1.1.0003, build date Mar  4 2003  12:32:10\n"
"# command line args: -q -profile arbvp1 -entry vmain\n"
"# nv30vp backend compiling 'vmain' program\n"
"PARAM c9 = { 0, 1, 2, 0.5 };\n"
"#vendor NVIDIA Corporation\n"
"#version 1.0.02\n"
"#profile arbvp1\n"
"#program vmain\n"
"#semantic vmain.Wm3TransformMVP\n"
"#semantic vmain.Wm3TransformM\n"
"#semantic vmain.Wm3CameraPosition\n"
"#var float4 iPosition : $vin.POSITION : POSITION : 0 : 1\n"
"#var float3 iNormal : $vin.NORMAL : NORMAL : 1 : 1\n"
"#var float4 oPosition : $vout.POSITION : POSITION : 2 : 1\n"
"#var float3 oNormal : $vout.TEXCOORD0 : TEXCOORD0 : 3 : 1\n"
"#var float3 oEyeDir : $vout.TEXCOORD1 : TEXCOORD1 : 4 : 1\n"
"#var float4x4 Wm3TransformMVP :  : c[0], 4 : 5 : 1\n"
"#var float4x4 Wm3TransformM :  : c[4], 4 : 6 : 1\n"
"#var float3 Wm3CameraPosition :  : c[8] : 7 : 1\n"
"TEMP R0;\n"
"ATTRIB v18 = vertex.normal;\n"
"ATTRIB v16 = vertex.position;\n"
"PARAM c8 = program.local[8];\n"
"PARAM c4[4] = { program.local[4..7] };\n"
"PARAM c0[4] = { program.local[0..3] };\n"
"	DP4 result.position.x, c0[0], v16;\n"
"	DP4 result.position.y, c0[1], v16;\n"
"	DP4 result.position.z, c0[2], v16;\n"
"	DP4 result.position.w, c0[3], v16;\n"
"	DP3 R0.x, c4[0].xyzx, v18.xyzx;\n"
"	DP3 R0.y, c4[1].xyzx, v18.xyzx;\n"
"	DP3 R0.z, c4[2].xyzx, v18.xyzx;\n"
"	MAD result.texcoord[0].xyz, c9.w, R0.xyzx, c9.w;\n"
"	DP4 R0.x, c4[0], v16;\n"
"	DP4 R0.y, c4[1], v16;\n"
"	DP4 R0.z, c4[2], v16;\n"
"	ADD R0.yzw, R0.xxyz, -c8.xxyz;\n"
"	DP3 R0.x, R0.yzwy, R0.yzwy;\n"
"	RSQ R0.x, R0.x;\n"
"	MUL R0.xyz, R0.x, R0.yzwy;\n"
"	MAD result.texcoord[1].xyz, c9.w, R0.xyzx, c9.w;\n"
"END\n"
"# 17 instructions\n"
"# 1 temp registers\n"
"# End of program\n"
,
"vs_1_1\n"
"// DX9 Vertex shader generated by NVIDIA Cg compiler\n"
"// cgc version 1.1.0003, build date Mar  4 2003  12:32:10\n"
"// command line args: -q -profile vs_1_1 -entry vmain -profileopts dcls -strict\n"
"// nv30vp backend compiling 'vmain' program\n"
"def c9, 0, 1, 2, 0.5\n"
"//vendor NVIDIA Corporation\n"
"//version 1.0.02\n"
"//profile vs_1_1\n"
"//program vmain\n"
"//semantic vmain.Wm3TransformMVP\n"
"//semantic vmain.Wm3TransformM\n"
"//semantic vmain.Wm3CameraPosition\n"
"//var float4 iPosition : $vin.POSITION : POSITION : 0 : 1\n"
"//var float3 iNormal : $vin.NORMAL : NORMAL : 1 : 1\n"
"//var float4 oPosition : $vout.POSITION : POSITION : 2 : 1\n"
"//var float3 oNormal : $vout.TEXCOORD0 : TEXCOORD0 : 3 : 1\n"
"//var float3 oEyeDir : $vout.TEXCOORD1 : TEXCOORD1 : 4 : 1\n"
"//var float4x4 Wm3TransformMVP :  : c[0], 4 : 5 : 1\n"
"//var float4x4 Wm3TransformM :  : c[4], 4 : 6 : 1\n"
"//var float3 Wm3CameraPosition :  : c[8] : 7 : 1\n"
"//const c[9] = 0 1 2 0.5\n"
"dcl_normal v3\n"
"dcl_position v0\n"
"	dp4 oPos.x, c0, v0\n"
"	dp4 oPos.y, c1, v0\n"
"	dp4 oPos.z, c2, v0\n"
"	dp4 oPos.w, c3, v0\n"
"	dp3 r0.x, c4.xyz, v3.xyz\n"
"	dp3 r0.y, c5.xyz, v3.xyz\n"
"	dp3 r0.z, c6.xyz, v3.xyz\n"
"	mad oT0.xyz, c9.w, r0.xyz, c9.w\n"
"	dp4 r0.x, c4, v0\n"
"	dp4 r0.y, c5, v0\n"
"	dp4 r0.z, c6, v0\n"
"	add r0.yzw, r0.xxyz, -c8.xxyz\n"
"	dp3 r0.x, r0.yzw, r0.yzw\n"
"	rsq r0.x, r0.x\n"
"	mul r0.xyz, r0.x, r0.yzw\n"
"	mad oT1.xyz, c9.w, r0.xyz, c9.w\n"
"// 17 instructions\n"
"// 1 temp registers\n"
"// End of program\n"
};



