cgfx 쉐이더 이벡트 Template 파일
fx composer 1.8로 기본 쉐이더 effect를 만들면 DirectX에서 쓸 수 있는 기본 *.fx 파일이 나온다. 이와 동일하게 Cg 1.5에서 사용할 수 있는 기본 cgfx 파일의 템플릿을 만들어 보았다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
//-----------------------------------------------------------------------------
// 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();
}
} |