texture texSphereMap;
matrix matWorld;
matrix matViewProject;
vector vecPosition;

technique Sphere
{
pass P0
{
	VertexShader =
		decl {                 }
		{
            vs.1.1
            def c64, 0.25f, 0.5f, 1.0f, -1.0f

            dcl_position v0
            dcl_normal v1

            // r0: camera-space position
            // r1: camera-space normal
            // r2: camera-space vertex-eye vector
            // r3: camera-space reflection vector
            // r4: texture coordinates

            // Transform position and normal into camera-space
            m4x4 r0, v0, c0
            m3x3 r1.xyz, v1, c0
            mov r1.w, c64.z

            // Compute normalized view vector
            add r2, c8, -r0
            dp3 r3, r2, r2
            rsq r3, r3.w
            mul r2, r2, r3

            // Compute camera-space reflection vector
            dp3 r3, r1, r2
            mul r1, r1, r3
            add r1, r1, r1
            add r3, r1, -r2

            // Compute sphere-map texture coords
            mad r4.w, -r3.z, c64.y, c64.y
            rsq r4, r4.w
            mul r4, r3, r4
            mad r4, r4, c64.x, c64.y

            // Project position
            m4x4 oPos, r0, c4
            mul oT0.xy, r4.xy, c64.zw
            mov oT0.zw, c64.z
        };
        
    VertexShaderConstant4[0] = <matWorld>;
    VertexShaderConstant4[4] = <matViewProject>;
    VertexShaderConstant1[8] = <vecPosition>;

    // Pixel state
    Texture[0] = <texSphereMap>;
    AddressU[0] = Wrap;
    AddressV[0] = Wrap;
    MinFilter[0] = Linear;
    MagFilter[0] = Linear;
    ColorOp[0] = SelectArg1;
    ColorArg1[0] = Texture;

}
}