Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/branches/Shader_HS18/programs/Cg/Example_CelShading.cg @ 12115

Last change on this file since 12115 was 12115, checked in by wiesep, 5 years ago

Changed folder structure, deletet some unused files and cleaned up code

File size: 1.8 KB
Line 
1
2/* Cel shading vertex program for single-pass rendering
3   In this program, we want to calculate the diffuse and specular
4   ramp components, and the edge factor (for doing simple outlining)
5   For the outlining to look good, we need a pretty well curved model.
6*/
7void main_vp(float4 position    : POSITION,
8                         float3 normal          : NORMAL,
9                         // outputs
10                         out float4 oPosition : POSITION,
11                         out float  diffuse              : TEXCOORD0,
12                         out float  specular     : TEXCOORD1,
13                         out float  edge                 : TEXCOORD2,
14                         // parameters
15                         uniform float3 lightPosition, // object space
16                         uniform float3 eyePosition,   // object space
17                         uniform float4  shininess,
18                         uniform float4x4 worldViewProj)
19{
20        // calculate output position
21        oPosition = mul(worldViewProj, position);
22
23        // calculate light vector
24        float3 N = normalize(normal);
25        float3 L = normalize(lightPosition - position.xyz);
26       
27        // Calculate diffuse component
28        diffuse = max(dot(N, L) , 0);
29
30        // Calculate specular component
31        float3 E = normalize(eyePosition - position.xyz);
32        float3 H = normalize(L + E);
33        specular = pow(max(dot(N, H), 0), shininess);
34        // Mask off specular if diffuse is 0
35        if (diffuse == 0) specular = 0;
36
37        // Edge detection, dot eye and normal vectors
38        edge = max(dot(N, E), 0);
39}
40
41void main_fp( float4 position   : POSITION,
42       float diffuseIn  : TEXCOORD0,
43                         float specularIn       : TEXCOORD1,
44                         float edge             : TEXCOORD2,
45                         
46                         out float4 colour      : COLOR,
47                         
48                         uniform float4 diffuse,
49                         uniform float4 specular,
50                         
51                         uniform sampler1D diffuseRamp,
52                         uniform sampler1D specularRamp,
53                         uniform sampler1D edgeRamp)
54{
55        // Step functions from textures
56        diffuseIn = tex1D(diffuseRamp, diffuseIn).x;
57        specularIn = tex1D(specularRamp, specularIn).x;
58        edge = tex1D(edgeRamp, edge).x;
59
60        colour = edge * ((diffuse * diffuseIn) +
61                                        (specular * specularIn));
62}
63                         
64                         
Note: See TracBrowser for help on using the repository browser.