<Vertex Shader>
float4x4 gWorldMatrix;
float4x4 gViewMatrix;
float4x4 gProjectionMatrix;
float4 gWorldLightPosition; // World 상의 Light의 위치(위치는 곧 방향) //
struct VS_INPUT
{
float4 mPosition : POSITION;
float3 mNormal : NORMAL; // Vertex Shader에서 난반사를 표현하기 위해 Normal정보를 받는다 //
};
struct VS_OUTPUT
{
float4 mPosition : POSITION;
float3 mDiffuse : TEXCOORD1;
};
VS_OUTPUT vs_main(VS_INPUT Input)
{
VS_OUTPUT Output;
Output.mPosition = mul(Input.mPosition, gWorldMatrix);
// Vertex에서 보는 Light의 방향 //
float3 lightDir = Output.mPosition.xyz - gWorldLightPosition.xyz; // Light에서 Vertex방향으로 향함 //
lightDir = normalize(lightDir); // 정규화 //
// Normal을 World 공간으로 이동 //
float3 worldNormal = mul(Input.mNormal, (float3x3)gWorldMatrix);
worldNormal = normalize(worldNormal);
Output.mPosition = mul(Output.mPosition, gViewMatrix);
Output.mPosition = mul(Output.mPosition, gProjectionMatrix);
// dot은 실수 하나 이지만 float3에 넣으면 같은 성분으로 모두 채워짐 //
// -lightDir : Vertex에서 Light를 보는 방향을 변경 //
Output.mDiffuse = dot(-lightDir, worldNormal);
return Output;
}
<Pixel Shader>
struct PS_INPUT
{
float3 mDiffuse : TEXCOORD1; // Vertex Shader에서 받아온 난반사광 //
};
float4 ps_main(PS_INPUT Input) : COLOR
{
// saturate : 0이하는 0, 1이상은 1로 만들어줌 //
// dot은 -1까지 나오니 saturate 사용하자 //
float3 diffuse = saturate(Input.mDiffuse);
return float4(diffuse, 1); // float3를 float4로 변경 //
}
'Shader > HLSH Shader 공부' 카테고리의 다른 글
Lighting - Specular (0) | 2014.05.28 |
---|---|
TextureMapping (0) | 2014.05.22 |
Color (0) | 2014.05.15 |