source: GTP/trunk/App/Demos/Illum/Shark3D/version164x12u/IllumDemo/src/res/levelutil/shader/prog/ogl_glsl/bump_ogl_glsl_fs1x0.s3d_shadercode @ 2196

Revision 2196, 6.4 KB checked in by szirmay, 17 years ago (diff)
Line 
1///////////////////////////////////////////////////////////////////////////////
2//
3//      ##  ######
4//       ######  ###
5//  ## ###############        Shark 3D Engine (www.shark3d.com)
6//   ########## # # #
7//    ########                Copyright (c) 1996-2006 Spinor GmbH.
8//   ######### # # #          All rights reserved.
9//  ##   ##########
10//      ##
11//
12///////////////////////////////////////////////////////////////////////////////
13
14@include "levelutil/shader/prog/ogl_glsl/" \
15        "include_shmap_filter_ogl_glsl_vs1x0.s3d_shadercode_run"
16
17///////////////////////////////////////////////////////////////////////////////
18
19@define S3D_NORMALIZE(v) (v)
20
21///////////////////////////////////////////////////////////////////////////////
22
23uniform sampler2D tex0;
24uniform sampler2D normalSamp;
25
26///////////////////////////////////////////////////////////////////////////////
27
28@ifdef S3D_LIGHT_BRIGHT
29uniform sampler2D lightBrightSamp;
30@endif
31@ifdef S3D_LIGHT_PROJ
32uniform sampler2D lightProjSamp;
33@endif
34@ifdef S3D_LIGHT_SHMAP
35uniform sampler2D lightShmapSamp;
36@endif
37
38///////////////////////////////////////////////////////////////////////////////
39
40uniform vec4 lightAmbient;
41uniform vec4 lightDiffuse;
42uniform vec4 lightSpecular;
43
44@ifdef S3D_LIGHT_BRIGHT
45uniform vec4 lightBrightRcpSize;
46@endif
47
48@ifdef S3D_LIGHT_SHMAP
49uniform vec4 lightShmapSize;
50uniform vec4 lightShmapRcpSize;
51@endif
52
53///////////////////////////////////////////////////////////////////////////////
54
55@define S3D_INTENS_DISCARD_TRESHOLD 0.001
56@define S3D_BRIGHT_EPS 0.01
57
58///////////////////////////////////////////////////////////////////////////////
59
60void main(void)
61{
62@ifdef S3D_GEOMETRIC_SHADOW
63    // Geometric shadow
64    if(gl_TexCoord[1].z < 0.0)
65        discard;
66@endif
67
68@ifdef S3D_LIGHT_SHMAP
69    vec3 clipVal = gl_TexCoord[6].www
70            - abs(2.0 * gl_TexCoord[6].xyz - gl_TexCoord[6].www);
71    if(clipVal.x < 0.0 || clipVal.y < 0.0 || clipVal.z < 0.0)
72        discard;
73@endif
74
75@ifdef S3D_LIGHT_BRIGHT
76@ifdef S3D_LIGHT_BRIGHT_CARE
77    vec4 brightVecMain = texture2DProj(
78            lightBrightSamp, gl_TexCoord[5]);
79    float brightIdent = fract(100.0 * gl_TexCoord[6].z / gl_TexCoord[6].w);
80    float brightDeltaMain = abs(brightVecMain.w - brightIdent);
81    float brightVal = brightVecMain.x;
82    if(brightDeltaMain > S3D_BRIGHT_EPS)
83    {
84        vec2 brightOffs = lightBrightRcpSize.xy * gl_TexCoord[5].w;
85        vec4 brightCoordX = vec4(
86                gl_TexCoord[5].xy + brightOffs,
87                gl_TexCoord[5].zw);
88        vec4 brightCoordY = vec4(
89                gl_TexCoord[5].xy - brightOffs,
90                gl_TexCoord[5].zw);
91        vec4 brightCoordZ = brightCoordX;
92        brightCoordZ.y = brightCoordY.y;
93        vec4 brightCoordW = brightCoordY;
94        brightCoordW.y = brightCoordX.y;
95        vec4 brightVecX = texture2DProj(lightBrightSamp, brightCoordX);
96        vec4 brightVecY = texture2DProj(lightBrightSamp, brightCoordY);
97        vec4 brightVecZ = texture2DProj(lightBrightSamp, brightCoordZ);
98        vec4 brightVecW = texture2DProj(lightBrightSamp, brightCoordW);
99        brightVal = min(brightVecMain.x,
100                        min(min(brightVecX.x, brightVecY.x),
101                            min(brightVecZ.x, brightVecW.x)));
102    }
103@else   
104    // Lookup brightness:
105    float brightVal = texture2DProj(lightBrightSamp, gl_TexCoord[5]).x;
106@endif   
107    // Exit if no light:
108    if(brightVal <= S3D_INTENS_DISCARD_TRESHOLD)
109        discard;
110@endif
111   
112@ifdef S3D_LIGHT_SHMAP
113    // Shadow map:
114    float shmapVal = s3d_shmapFilter(
115            lightShmapSamp, gl_TexCoord[6],
116            lightShmapSize, lightShmapRcpSize);
117    if(shmapVal <= S3D_INTENS_DISCARD_TRESHOLD)
118        discard;
119@endif
120
121    // Texture coordinates:
122    vec2 mainTexCoord = gl_TexCoord[0].xy;
123@ifdef S3D_PARALLAX_MAPPING
124    vec3 camToVertSurfNrm = normalize(gl_TexCoord[4].xyz);
125    float height = texture2D(normalSamp, mainTexCoord).a;
126    vec2 offset = S3D_PARALLAX_SCALE * (S3D_PARALLAX_BIAS - height)
127            * camToVertSurfNrm.xy;
128    mainTexCoord = mainTexCoord + offset;
129@endif
130
131    // Texture lookups:
132    vec4 texCol = texture2D(tex0, mainTexCoord.xy);
133    vec4 rawNormalView = texture2D(normalSamp, mainTexCoord.xy);
134    vec3 normalView = (rawNormalView.xyz - 0.5) * 2.0;
135
136    // Calculate range-factor from scaled surface-to-light vector:
137    float rangeFac =
138        max(1.0 - dot(gl_TexCoord[3].xyz, gl_TexCoord[3].xyz), 0.0);
139
140    // Diffuse direction:
141    vec3 diffuseDirSurfNrm = S3D_NORMALIZE(gl_TexCoord[1].xyz);
142
143    // Calculate diffuse:   
144    vec4 diffuse = vec4(lightDiffuse.xyz * rangeFac, lightDiffuse.w);
145    diffuse += lightAmbient;
146    float diffDot = dot(normalView, diffuseDirSurfNrm);
147@ifdef S3D_MILD
148    float diffFac = diffDot * (1.0 - S3D_MILD) + S3D_MILD;
149@else
150    float diffFac = diffDot;
151@endif
152    diffuse *= clamp(diffFac, 0.0, 1.0);;
153   
154    // Calculate specular:
155    vec4 specular = vec4(lightSpecular.xyz * rangeFac, lightSpecular.w);
156    // ATI workaround: Don't use x component of gl_TexCoord[2]:
157    vec3 specularDirSurfNrm = S3D_NORMALIZE(gl_TexCoord[2].yzw);
158    float specDot = dot(normalView, specularDirSurfNrm);
159    float specFac = clamp(pow(clamp(specDot, 0.0, 1.0),
160            S3D_SPECULAR_EXP), 0.0, 1.0);
161    specular *= specFac;
162
163    vec4 outCol;
164    outCol.rgb = clamp(diffuse.rgb * texCol.rgb
165            + specular.rgb * texCol.a, 0.0, 1.0);
166    // Preserve original opacity from texture.
167    outCol.a = texCol.a;
168
169@ifdef S3D_GEOMETRIC_SHADOW
170    // Geometric shadow
171    float geomShadow = clamp(diffuseDirSurfNrm.z * 4.0, 0.0, 1.0);
172    outCol.rgb = clamp(outCol.rgb * geomShadow, 0.0, 1.0);
173@endif
174
175@ifdef S3D_LIGHT_BRIGHT
176    // Correct brightness:
177    // The smoothing algorithm generates higher brightness gradients
178    // near 0 and 1 due to sticking to 0 and 1.
179    // This is approximately compensated by using a quadratic function
180    // near 0 and 1.
181    brightVal = 2.0 * brightVal * brightVal * (1.5 - brightVal);
182
183    // Apply brightness:
184    outCol.rgb = outCol.rgb * brightVal;
185@endif
186
187@ifdef S3D_LIGHT_PROJ
188    vec4 texColProjTex = texture2DProj(lightProjSamp, gl_TexCoord[5]);
189    outCol *= texColProjTex;
190@endif
191
192@ifdef S3D_LIGHT_SHMAP
193    outCol *= shmapVal;
194@endif
195
196    gl_FragColor = outCol;
197}
198
199///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.