这一次讲HLSL文件。
1
//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION,
float3 vNormal : NORMAL,
float2 vTexCoord0 : TEXCOORD0,
uniform int nNumLights,
uniform bool bTexture,
uniform bool bAnimate )
首先是顶点着色器。题外话,uniform说明该变量在着色器外赋值初始化,本例子中就是在下面的技术框架中初始化。
// Animation the vertex based on time and the vertex's object space position
if( bAnimate )
vAnimatedPos += float4(vNormal, 0) * (sin(g_fTime+5.5)+0.5)*5;
// Transform the position from object space to homogeneous projection space
Output.Position = mul(vAnimatedPos, g_mWorldViewProjection);
改变顶点位置,这里就是为什么程序中的人不停变肥变瘦的原因。嗯,其实重点是跟变换矩阵相乘,将顶点变换到视野中。
// Transform the normal from object space to world space
vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld)); // normal (world space)
// Compute simple directional lighting equation
float3 vTotalLightDiffuse = float3(0,0,0);
for(int i=0; i<nNumLights; i++ )
vTotalLightDiffuse += g_LightDiffuse[i] * max(0,dot(vNormalWorldSpace, g_LightDir[i]));
Output.Diffuse.rgb = g_MaterialDiffuseColor * vTotalLightDiffuse +
g_MaterialAmbientColor * g_LightAmbient;
Output.Diffuse.a = 1.0f;
计算光照。注意计算光照需要把顶点法线向量转换到世界坐标中,要跟光照所在坐标系一样。嗯,很简单的计算光照,就不说了~
2
//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderScenePS( VS_OUTPUT In,
uniform bool bTexture )
{
PS_OUTPUT Output;
// Lookup mesh texture and modulate it with diffuse
if( bTexture )
Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;
else
Output.RGBColor = In.Diffuse;
return Output;
}
像素着色器就很简单了,主要是tex2d函数,在纹理上取样。
3这个就是效果框架,一个4个~
//--------------------------------------------------------------------------------------
// Renders scene to render target
//--------------------------------------------------------------------------------------
technique RenderSceneWithTexture1Light
{
pass P0
{
VertexShader = compile vs_2_0 RenderSceneVS( 1, true, true );
PixelShader = compile ps_2_0 RenderScenePS( true ); // trivial pixel shader (could use FF instead if desired)
}
}
technique RenderSceneWithTexture2Light
{
pass P0
{
VertexShader = compile vs_2_0 RenderSceneVS( 2, true, true );
PixelShader = compile ps_2_0 RenderScenePS( true ); // trivial pixel shader (could use FF instead if desired)
}
}
technique RenderSceneWithTexture3Light
{
pass P0
{
VertexShader = compile vs_2_0 RenderSceneVS( 3, true, true );
PixelShader = compile ps_2_0 RenderScenePS( true ); // trivial pixel shader (could use FF instead if desired)
}
}
technique RenderSceneNoTexture
{
pass P0
{
VertexShader = compile vs_2_0 RenderSceneVS( 1, false, false );
PixelShader = compile ps_2_0 RenderScenePS( false ); // trivial pixel shader (could use FF instead if desired)
}
}