source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg @ 2964

Revision 2964, 3.3 KB checked in by mattausch, 16 years ago (diff)
Line 
1//--------------------------------------------------------------------------------------
2// Input and Output structs
3//--------------------------------------------------------------------------------------
4
5
6
7struct VertexInput
8{
9        float4 position: POSITION;
10        float3 normal: NORMAL;
11        float3 color: COLOR;
12        float2 texcoord: TEXCOORD0;
13};
14
15
16// vtx output
17struct vtxout
18{
19        float4 position: POSITION; // eye space
20        float4 texCoord: TEXCOORD0;   
21
22        float4 color: COLOR0; 
23        float4 worldPos: TEXCOORD1; // world position
24        float3 normal: TEXCOORD2;
25        float4 mypos: TEXCOORD3;
26};
27
28
29
30struct pixel
31{
32  float4 col: COLOR0;
33  float4 pos: COLOR1;
34  float4 norm: COLOR2;
35};
36
37// fragment input
38struct fragin
39{
40        float4 color: COLOR0; 
41        float4 position: POSITION; // eye space
42        float4 texCoord: TEXCOORD0;   
43
44        float4 projPos: WPOS;
45        float4 worldPos: TEXCOORD1; // world position
46        float3 normal: TEXCOORD2;
47        float4 mypos: TEXCOORD3;
48};
49//--------------------------------------------------------------------------------------
50// Vertex Shaders
51//--------------------------------------------------------------------------------------
52
53
54vtxout default_vs(VertexInput IN,
55                                  uniform float3 lightDir,
56                                  uniform float2 thetaSun,
57                                  uniform float3 zenithColor,
58                                  uniform float3 aColor,
59                                  uniform float3 bColor,
60                                  uniform float3 cColor,
61                                  uniform float3 dColor,
62                                  uniform float3 eColor,
63                                  uniform float4x4 ModelView)
64{
65        vtxout OUT;
66
67        OUT.position = mul(glstate.matrix.mvp, IN.position);
68
69
70        const float dotLN = dot(lightDir, IN.normal);   
71        const float cos2gamma = dotLN * dotLN;
72
73        const float gamma = acos(dotLN);   
74        const float theta = dot(float3(0.0, 0.0, 1.0), IN.normal);
75
76    float3 num = (1.0 + aColor * exp(bColor / theta)) * (1.0 + cColor * exp(dColor * gamma) + eColor * cos2gamma);   
77        float3 den = (1.0 + aColor * exp(bColor)) * (1.0 + cColor * exp(dColor * thetaSun.x) + eColor * thetaSun.y);   
78    float3 xyY = (num / den) * zenithColor;   
79   
80    float3 XYZ;                                                                                             
81                                                                                                           
82    XYZ.x = (xyY.x / xyY.y) * xyY.z;                                                                       
83        XYZ.y = xyY.z;                                                                                         
84        XYZ.z = ((1.0f - xyY.x - xyY.y) / xyY.y) * xyY.z;   
85       
86    const static float3x3 conv_Mat =
87                float3x3(3.240479, -1.537150, -0.498535,
88                         -0.969256, 1.875992,  0.041556,
89                             0.055648, -0.204043,  1.057311);
90
91        float3 hcol = mul(conv_Mat, XYZ);
92        OUT.color = float4(hcol, 1.0);
93
94        OUT.color.rgb *= 5e-5f;
95
96        //OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position);
97        OUT.worldPos = mul(ModelView, IN.position);
98
99        OUT.normal = IN.normal;
100        OUT.mypos = OUT.position;
101
102        return OUT;
103}
104
105
106pixel frag_skydome(fragin IN)
107{
108        pixel pix;
109
110        pix.col = IN.color;
111        pix.pos = IN.worldPos * 1e20;// * maxDepth;
112
113        pix.norm.xyz = IN.normal;
114       
115        // hack: squeeze some information about the ambient term into the target
116        pix.norm.w = 1;
117        pix.pos.w = IN.mypos.w;
118       
119        // the projected depth
120        pix.col.w = IN.mypos.z / IN.mypos.w;
121
122        return pix;
123}
Note: See TracBrowser for help on using the repository browser.