aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/media/SampleRenderer/4/shaders/include/fragment_entry.cg
blob: 9cf25342f2e5babd1773c11862fec39d87386a69 (plain) (blame)
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
#ifndef FRAGMENT_ENTRY_CG
#define FRAGMENT_ENTRY_CG

#include <config.cg>

BEGIN_CBUFFER(cbShade)
CONST_TYPE float shadeMode;
END_CBUFFER(cbShade)

#if !defined(ENABLE_VFACE)
#define ENABLE_VFACE 0
#endif

// define the platform specific semantic for the facing register
#if defined(RENDERER_PS3)
#define FACE_SEMANTIC FACE
#else
#define FACE_SEMANTIC VFACE
#endif

#if defined(PASS_UNLIT)
	Fragment fmain(FragmentParameters params)
	{
		Fragment fout;
		SurfaceMaterial material = computeSurfaceMaterial(params);
		fout.color = half4(material.diffuseColor, material.alpha);
		return fout;
	}
#elif defined(PASS_AMBIENT_LIGHT) || defined(PASS_POINT_LIGHT) || defined(PASS_DIRECTIONAL_LIGHT) || defined(PASS_SPOT_LIGHT) || defined(PASS_SPOT_LIGHT_NO_SHADOW)
	Fragment fmain(FragmentParameters params
	#if ENABLE_VFACE
		,float vface : FACE_SEMANTIC
	#endif
	)
	{
	#if !ENABLE_VFACE
		float vface = 1.0;
	#endif
	#if ENABLE_VFACE_SCALE
		vface *= g_vfaceScale;
	#endif
		
		Fragment fout;
		float3x3 tangentBasis = float3x3(params.worldSpaceTangent, params.worldSpaceBinormal, params.worldSpaceNormal);
		SurfaceMaterial material = computeSurfaceMaterial(params);
		
		float3 bumpNormal = mul(material.tangentSpaceNormal, tangentBasis);

#if defined(NO_SUPPORT_DDX_DDY)
//#if !defined(GLSL_COMPILER)
		params.worldSpaceNormal = sign(vface) * normalize(bumpNormal);	
//#endif
#else
		// emulate flat shading by taking the derivative of position with respective to screen dx and dy and taking the cross product
		float3 faceNormal  = cross(ddy(params.worldSpacePosition.xyz), ddx(params.worldSpacePosition.xyz));

# ifdef RENDERER_PS3
		// ddy flipped on ps3
		faceNormal *= -1.0;
# endif		
		
		// select bump or face normal based on shade mode
		params.worldSpaceNormal = sign(vface) * normalize(lerp(bumpNormal, faceNormal, shadeMode));
#endif // NO_SUPPORT_DDX_DDY
		
		Lighting        lighting = computeLighting(params, material);
		float3 diffuseColor  = material.diffuseColor  * lighting.diffuseColor;
		float3 emissiveColor  = material.emissiveColor + material.specularColor * lighting.specularColor;
		fout.color = half4(diffuseColor + emissiveColor, material.alpha);
		//fout.color = half4(params.worldSpaceNormal, 1); // renders normals.
	
		// Fog
		//float depth = length(params.worldSpacePosition - g_eyePosition);
		//float fogBlend = min(1.0f, depth/g_fogColorAndDistance.w);
		//fout.color.xyz = half3(fout.color.xyz * (1.0f-fogBlend) + g_fogColorAndDistance.xyz * fogBlend);
		
		return fout;
	}
#elif defined(PASS_NORMALS)
	Fragment fmain(FragmentParameters params)
	{
		Fragment fout;
		SurfaceMaterial material = computeSurfaceMaterial(params);
		fout.color = half4(0,1,0,material.alpha);
		return fout;
	}
#elif defined(PASS_DEPTH)
	Fragment fmain(FragmentParameters params)
	{
		Fragment fout;
		SurfaceMaterial material = computeSurfaceMaterial(params);
		float depth = length(params.worldSpacePosition - g_eyePosition);
		fout.color = half4((half)depth,(half)depth,(half)depth,material.alpha);
		return fout;
	}
#elif defined(PASS_DEFERRED)
	DeferredFragment fmain(FragmentParameters params)
	{
		DeferredFragment fout;
		SurfaceMaterial material = computeSurfaceMaterial(params);
		fout.diffuseColor                     = half4(material.diffuseColor,   0);
		fout.emissiveColor                    = half4(material.emissiveColor,  0);
		fout.worldSpaceNormalAndSpecularPower = half4(params.worldSpaceNormal, material.specularPower);
		return fout;
	}
#endif
	
#endif