# hustruan

::  ::  ::  ::  ::

L是光源的位置，V是模型上顶点的位置，P是把模型上顶点投影到接受阴影面上的点。

1直线LV上的点P的坐标是其中t是个参数。

2其中P在接受阴影的平面上，把（1）带入平面方程，得到

3t 代入（1），得到

void MakePlanarShadowMatrix( float planeNormalX, float planeNormalY, float planeNormalZ, float planeDist, float lightX, float lightY, float lightZ, Matrix4f& shadowMatrix ){    float nDotl = planeNormalX * lightX + planeNormalY * lightY + planeNormalZ * lightZ ;    shadowMatrix.m[0][0] = nDotl + planeDist - planeNormalX * lightX;    shadowMatrix.m[0][1] = -lightX * planeNormalY;    shadowMatrix.m[0][2] = -lightX * planeNormalZ;    shadowMatrix.m[0][3] = -lightX * planeDist;    shadowMatrix.m[1][0] = -lightY * planeNormalX;    shadowMatrix.m[1][1] = nDotl + planeDist - lightY * planeNormalY;    shadowMatrix.m[1][2] = -lightY * planeNormalZ;    shadowMatrix.m[1][3] = -lightY * planeDist;    shadowMatrix.m[2][0] = -lightZ * planeNormalX;    shadowMatrix.m[2][1] = -lightZ * planeNormalY;    shadowMatrix.m[2][2] = nDotl + planeDist - lightZ * planeNormalZ;    shadowMatrix.m[2][3] = -lightZ * planeDist;    shadowMatrix.m[3][0] = -planeNormalX;    shadowMatrix.m[3][1] = -planeNormalY;    shadowMatrix.m[3][2] = -planeNormalZ;    shadowMatrix.m[3][3] = nDotl;}

1. Create CgContext
1. Create Effect
1. Get Technique From Effect
1. Get Parameter From Effect
1. Set Parameter
1. For each pass in technique, Draw

float4x4 WorldMatrix : World;float4x4 ViewMatrix : View;float4x4 ProjectionMatrix : Projection;float3 LightPositon;float3 LightColor;float3 AmbientLight = float3(0.2, 0.2, 0.2);int Shadow; // if 1, draw planar shadowsampler2D DiffuseMap = sampler_state {    MinFilter = Linear;    MagFilter = Linear;    WrapS = Wrap;    WrapT = Wrap;};struct VertexShaderInput {    float4 Pos    : POSITION;    float3 Normal : NORMAL;    float2 Tex    : TEXCOORD0;};struct VertexShaderOutput{    float4 Pos : POSITION;    float2 Tex : TEXCOORD0;    float3 Normal : TEXCOORD1;    float3 WorldPos : TEXCOORD2;};VertexShaderOutput VertexShaderFunction(VertexShaderInput input){    VertexShaderOutput output;    output.Pos = mul( WorldMatrix, input.Pos );    output.Pos = mul( ViewMatrix, output.Pos );    output.Pos = mul( ProjectionMatrix, output.Pos );    output.Tex = input.Tex;    output.WorldPos = mul(WorldMatrix, input.Pos);    output.Normal = mul((float3x3)WorldMatrix, input.Normal);    return output;}float4 PixelShaderFunction(VertexShaderOutput input) : COLOR{    float4 renderColor = float4(0, 0, 0, 1);    if(!Shadow)    {        float3 lightVec = normalize(input.WorldPos - LightPositon);        float diffuseFactor = saturate( dot(-lightVec, input.Normal) );        float3 diffuseMaterial = tex2D(DiffuseMap, input.Tex);        float3 diffuseColor = diffuseFactor * LightColor * diffuseMaterial;        float3 ambientColor = AmbientLight * diffuseMaterial;        renderColor = float4(diffuseColor + ambientColor, 1);    }        return renderColor;}technique TextureLightingTech{    pass    {        CullFaceEnable = true;        CullFace = Back;        //PolygonMode =  int2(Front, Line);        VertexProgram   = compile gp4vp VertexShaderFunction();        FragmentProgram = compile gp4fp PixelShaderFunction();    }}

glEnable( GL_POLYGON_OFFSET_FILL ); glPolygonOffset( -0.1f, 0.2f );glPolygonOffset( 0.0f, 0.0f );glDisable( GL_POLYGON_OFFSET_FILL );