matrix  mWVP;
matrix  mW;
vector  pOL;
vector  pOV;
vector  cLgt;
vector  cnst;

DWord cAmbient = 0x40404040;
DWord cLight  = 0xffffffff;
DWord cBars   = 0x20202020;

vertexshader v1Light = 
	decl
	{
		stream 0;
		float    v0[3];  // Position
		D3DCOLOR v3;     // Normal
		float    v7[2];  // Tex coord 0
		D3DCOLOR v8;     // Binormal
		D3DCOLOR v9;     // Tangent
	}
	asm
	{
        ; Constants:
        ; 0      - Ambient light color
        ; 1      - Color of the bars
        ; 16..19 - Composite World-View-Projection Matrix
        ; 24..27 - World Matrix
        ; 31     - World Space Light Position
        ; 32     - Object Space Light Position
        ; 33     - {1/light range, light range, 1/attenuation2, attenuation2}
        ; 34     - Object Space Eye Position
        ; 95     - {0.0, 0.5, 1.0, 2.0}

        vs.1.0
        def c8, 0, 0, 0, 80.0f

        m4x4 oPos, v0, c16

        ;convert from D3DColor to -1 to 1 range
        mad  r4, v3, c95.wwwz, -c95.zzzx ; Normal = (v3*2.0)-1.0

        ; Viewer direction
        sub     r1, c34, v0     ;vViewer = pViewer - pPos
        dp3		r11.x, r1, r1	;magnitude
        rsq		r11.y, r11.x	;1/length
        mul		r1, r1, r11.y	;normalized

        ; Light direction
        sub     r2, c32, v0     ;vViewer = pViewer - pLight
        dp3		r11.x, r2, r2	;magnitude
        rsq		r11.y, r11.x	;1/length
        mul		r2, r2, r11.y	;normalized

        ; Ambient
        mov     r5, c0
        mul     r5, r5, c1

        ; Halfway vector
        add     r3.xyz, r1.xyz, r2.xyz ; Halfway = vV + vL
        dp3		r11.x, r3, r3	       ;magnitude
        rsq		r11.y, r11.x	       ;1/length
        mul		r3.xyz, r3, r11.y	   ;normalized

        ; Lighting
        dp3     r0.x, r4, r2    ;N.L
        dp3     r0.y, r4, r3    ;N.H
        mov     r0.w, c8.w      ;power
        lit     r6, r0          ;lighting
        mul     r7, r6.y, c1    ;Color*Diffuse
        mad     oD0, r7, c2.xyz, r5 ;Diffuse
        mul     oD1, r6.z, c2.xyz     ;Specular
	};

String Bars_desc = "Vertex Diffuse + Specular";

technique Bars
{
    pass P0
    {
        VertexShaderConstant[0]  = <cAmbient>;
        VertexShaderConstant[1]  = <cBars>;
        VertexShaderConstant[2]  = <cLight>;
        VertexShaderConstant[16] = <mWVP>;  // World*View*Proj Matrix
        VertexShaderConstant[32] = <pOL>;   // Object Space Light
        VertexShaderConstant[34] = <pOV>;   // Object Space Viewer Position
        VertexShaderConstant[95] = <cnst>;  // Common constants

        vertexshader = <v1Light>;

        ColorOp[0]   = SelectArg1;
        ColorArg1[0] = Diffuse;
        AlphaOp[0]   = SelectArg1;
        AlphaArg1[0] = Diffuse;

        SpecularEnable = True;
    }
}
