fx composer 1.8로 기본 쉐이더 effect를 만들면 DirectX에서 쓸 수 있는 기본 *.fx 파일이 나온다. 이와 동일하게 Cg 1.5에서 사용할 수 있는 기본 cgfx 파일의 템플릿을 만들어 보았다.
//-----------------------------------------------------------------------------
// newEffect.cgfx
// new cgfx effect template
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Effect Info
//-----------------------------------------------------------------------------
string Info
<
string Sampler01 = "TextureSampler";
> = "newEffect.cgfx";
//-----------------------------------------------------------------------------
// Matrices
//-----------------------------------------------------------------------------
/* before
float4x4 worldViewProj : WorldViewProjection;
float4x4 world : World;
float4x4 worldInverseTranspose : WorldInverseTranspose;
float4x4 viewInverse : ViewInverse;
*/
float4x4 modelView : state.matrix.modelview;
float4x4 modelViewInvTrans : state.matrix.modelview.Invtrans;
float4x4 modelViewProj : state.matrix.mvp;
float4x4 modelViewInv : state.matrix.modelview.Inverse;
//-----------------------------------------------------------------------------
// Variables
//-----------------------------------------------------------------------------
float4 lightDir : Direction
<
string Object = "DirectionalLight";
string Space = "World";
> = {1.0f, -1.0f, 1.0f, 0.0f};
float4 lightColor : Diffuse
<
string UIName = "Diffuse Light Color";
string Object = "DirectionalLight";
> = {1.0f, 1.0f, 1.0f, 1.0f};
float4 lightAmbient : Ambient
<
string UIWidget = "Ambient Light Color";
string Space = "material";
> = {0.0f, 0.0f, 0.0f, 1.0f};
float4 materialDiffuse : Diffuse
<
string UIWidget = "Surface Color";
string Space = "material";
> = {1.0f, 1.0f, 1.0f, 1.0f};
float4 materialSpecular : Specular
<
string UIWidget = "Surface Specular";
string Space = "material";
> = {1.0f, 1.0f, 1.0f, 1.0f};
float shininess : SpecularPower
<
string UIWidget = "slider";
float UIMin = 1.0;
float UIMax = 128.0;
float UIStep = 1.0;
string UIName = "specular power";
> = 30.0;
//-----------------------------------------------------------------------------
// Samplers
//-----------------------------------------------------------------------------
texture diffuseTexture : Diffuse
<
string ResourceName = "wall.dds";
string ResourceType = "2D";
>;
sampler2D TextureSampler = sampler_state
{
texture = <diffuseTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
//-----------------------------------------------------------------------------
// In-Out Structure
//-----------------------------------------------------------------------------
struct vertexInput
{
float3 position : POSITION;
float3 normal : NORMAL;
float3 texCoordDiffuse : TEXCOORD0;
};
struct vertexOutput
{
float4 hPosition : POSITION;
float3 texCoordDiffuse : TEXCOORD0;
float4 diffAmbColor : COLOR0;
float4 specCol : COLOR1;
};
//-----------------------------------------------------------------------------
// Vertex Shader
//-----------------------------------------------------------------------------
vertexOutput VS_newEffect(vertexInput IN)
{
vertexOutput OUT;
// OUT.hPosition = mul( float4(IN.position.xyz , 1.0) , worldViewProj);
OUT.hPosition = mul( modelViewProj, float4(IN.position.xyz , 1.0));
OUT.texCoordDiffuse = IN.texCoordDiffuse;
//calculate our vectors N, E, L, and H
// float3 worldEyePos = viewInverse[3].xyz;
float3 worldEyePos = modelViewInv[3].xyz;
// float3 worldVertPos = mul(IN.position, world).xyz;
float3 worldVertPos = mul(modelView, float4(IN.position, 1.0f)).xyz;
// float4 N = mul(IN.normal, worldInverseTranspose); //normal vector
float3 N = mul(modelViewInvTrans, float4(IN.normal, 1.0f)).xyz; //normal vector
float3 E = normalize(worldEyePos - worldVertPos); //eye vector
float3 L = normalize( -lightDir.xyz); //light vector
float3 H = normalize(E + L); //half angle vector
//calculate the diffuse and specular contributions
float diff = max(0 , dot(N,L));
float spec = pow( max(0 , dot(N,H) ) , shininess );
if( diff <= 0 )
{
spec = 0;
}
//output diffuse
float4 ambColor = materialDiffuse * lightAmbient;
float4 diffColor = materialDiffuse * diff * lightColor ;
OUT.diffAmbColor = diffColor + ambColor;
//output specular
float4 specColor = materialSpecular * lightColor * spec;
OUT.specCol = specColor;
/**/
return OUT;
}
//-----------------------------------------------------------------------------
// Fragment Shader
//-----------------------------------------------------------------------------
float4 FS_newEffect( vertexOutput IN): COLOR
{
float4 diffuseTexture; // = float4(1, 0.5, 0, 1);
diffuseTexture = tex2D(TextureSampler, IN.texCoordDiffuse);
return IN.diffAmbColor * diffuseTexture + IN.specCol;
}
//-----------------------------------------------------------------------------
// Technique
//-----------------------------------------------------------------------------
technique T1
{
pass p0
{
DepthTestEnable = true;
VertexShader = compile arbvp1 VS_newEffect();
PixelShader = compile arbfp1 FS_newEffect();
}
}
Download: neweffect.cgfx