summaryrefslogtreecommitdiff
path: root/shaders/warsow/46.shader_test
blob: 76c961a6da1f83924f15bc39f1db7c8028310c58 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
[require]
GLSL >= 1.10

[fragment shader]
#define FRAGMENT_SHADER
// Warsow GLSL shader

#if !defined(__GLSL_CG_DATA_TYPES)
#define myhalf float
#define myhalf2 vec2
#define myhalf3 vec3
#define myhalf4 vec4
#else
#define myhalf half
#define myhalf2 half2
#define myhalf3 half3
#define myhalf4 half4
#endif

varying vec4 TexCoord;
varying vec4 ProjVector;
#ifdef APPLY_EYEDOT
varying vec3 EyeVector;
#endif

#ifdef VERTEX_SHADER
// Vertex shader

#ifdef APPLY_EYEDOT
uniform vec3 EyeOrigin;
uniform float FrontPlane;
#endif

void main(void)
{
gl_FrontColor = gl_Color;

mat4 textureMatrix;

textureMatrix = gl_TextureMatrix[0];
TexCoord.st = vec2 (textureMatrix * gl_MultiTexCoord0);

textureMatrix = gl_TextureMatrix[0];
textureMatrix[0] = -textureMatrix[0];
textureMatrix[1] = -textureMatrix[1];
TexCoord.pq = vec2 (textureMatrix * gl_MultiTexCoord0);

#ifdef APPLY_EYEDOT
mat3 strMatrix;
strMatrix[0] = gl_MultiTexCoord1.xyz;
strMatrix[2] = gl_Normal.xyz;
strMatrix[1] = gl_MultiTexCoord1.w * cross (strMatrix[2], strMatrix[0]);

vec3 EyeVectorWorld = (EyeOrigin - gl_Vertex.xyz) * FrontPlane;
EyeVector = EyeVectorWorld * strMatrix;
#endif

gl_Position = ftransform();
ProjVector = gl_Position;
#ifdef APPLY_CLIPPING
#ifdef __GLSL_CG_DATA_TYPES
gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;
#endif
#endif
}

#endif // VERTEX_SHADER


#ifdef FRAGMENT_SHADER
// Fragment shader

#ifdef APPLY_DUDV
uniform sampler2D DuDvMapTexture;
#endif

#ifdef APPLY_EYEDOT
uniform sampler2D NormalmapTexture;
#endif
uniform sampler2D ReflectionTexture;
uniform sampler2D RefractionTexture;
uniform float TextureWidth, TextureHeight;

void main(void)
{
myhalf3 color;

#ifdef APPLY_DUDV
vec3 displacement = vec3(texture2D(DuDvMapTexture, vec2(TexCoord.pq) * vec2(0.25)));
vec2 coord = vec2(TexCoord.st) + vec2(displacement) * vec2 (0.2);

vec3 fdist = vec3 (normalize(vec3(texture2D(DuDvMapTexture, coord)) - vec3 (0.5))) * vec3(0.005);
#else
vec3 fdist = vec3(0.0);
#endif

// get projective texcoords
float scale = float(1.0 / float(ProjVector.w));
float inv2NW = 1.0 / (2.0 * float (TextureWidth));
float inv2NH = 1.0 / (2.0 * float (TextureHeight));
vec2 projCoord = (vec2(ProjVector.xy) * scale + vec2 (1.0)) * vec2 (0.5) + vec2(fdist.xy);
projCoord.s = float (clamp (float(projCoord.s), inv2NW, 1.0 - inv2NW));
projCoord.t = float (clamp (float(projCoord.t), inv2NH, 1.0 - inv2NH));


myhalf3 refr = myhalf3(0.0);
myhalf3 refl = myhalf3(0.0);

#ifdef APPLY_EYEDOT
// calculate dot product between the surface normal and eye vector
// great for simulating varying water translucency based on the view angle
myhalf3 surfaceNormal = normalize(myhalf3(texture2D(NormalmapTexture, coord)) - myhalf3 (0.5));
vec3 eyeNormal = normalize(myhalf3(EyeVector));

float refrdot = float(dot(surfaceNormal, eyeNormal));
//refrdot = float (clamp (refrdot, 0.0, 1.0));
float refldot = 1.0 - refrdot;
// get refraction and reflection

#ifdef APPLY_REFRACTION
refr = (myhalf3(texture2D(RefractionTexture, projCoord))) * refrdot;
#endif
#ifdef APPLY_REFLECTION
refl = (myhalf3(texture2D(ReflectionTexture, projCoord))) * refldot;
#endif

#else

#ifdef APPLY_REFRACTION
refr = (myhalf3(texture2D(RefractionTexture, projCoord)));
#endif
#ifdef APPLY_REFLECTION
refl = (myhalf3(texture2D(ReflectionTexture, projCoord)));
#endif

#endif

// add reflection and refraction
#ifdef APPLY_DISTORTION_ALPHA
color = myhalf3(gl_Color.rgb) + myhalf3(mix (refr, refl, float(gl_Color.a)));
#else
color = myhalf3(gl_Color.rgb) + refr + refl;
#endif

#ifdef APPLY_GRAYSCALE
float grey = dot(color, myhalf3(0.299, 0.587, 0.114));
gl_FragColor = vec4(vec3(grey),1.0);
#else
gl_FragColor = vec4(vec3(color),1.0);
#endif
}

#endif // FRAGMENT_SHADER


[vertex shader]
#define VERTEX_SHADER
// Warsow GLSL shader

#if !defined(__GLSL_CG_DATA_TYPES)
#define myhalf float
#define myhalf2 vec2
#define myhalf3 vec3
#define myhalf4 vec4
#else
#define myhalf half
#define myhalf2 half2
#define myhalf3 half3
#define myhalf4 half4
#endif

varying vec4 TexCoord;
varying vec4 ProjVector;
#ifdef APPLY_EYEDOT
varying vec3 EyeVector;
#endif

#ifdef VERTEX_SHADER
// Vertex shader

#ifdef APPLY_EYEDOT
uniform vec3 EyeOrigin;
uniform float FrontPlane;
#endif

void main(void)
{
gl_FrontColor = gl_Color;

mat4 textureMatrix;

textureMatrix = gl_TextureMatrix[0];
TexCoord.st = vec2 (textureMatrix * gl_MultiTexCoord0);

textureMatrix = gl_TextureMatrix[0];
textureMatrix[0] = -textureMatrix[0];
textureMatrix[1] = -textureMatrix[1];
TexCoord.pq = vec2 (textureMatrix * gl_MultiTexCoord0);

#ifdef APPLY_EYEDOT
mat3 strMatrix;
strMatrix[0] = gl_MultiTexCoord1.xyz;
strMatrix[2] = gl_Normal.xyz;
strMatrix[1] = gl_MultiTexCoord1.w * cross (strMatrix[2], strMatrix[0]);

vec3 EyeVectorWorld = (EyeOrigin - gl_Vertex.xyz) * FrontPlane;
EyeVector = EyeVectorWorld * strMatrix;
#endif

gl_Position = ftransform();
ProjVector = gl_Position;
#ifdef APPLY_CLIPPING
#ifdef __GLSL_CG_DATA_TYPES
gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;
#endif
#endif
}

#endif // VERTEX_SHADER


#ifdef FRAGMENT_SHADER
// Fragment shader

#ifdef APPLY_DUDV
uniform sampler2D DuDvMapTexture;
#endif

#ifdef APPLY_EYEDOT
uniform sampler2D NormalmapTexture;
#endif
uniform sampler2D ReflectionTexture;
uniform sampler2D RefractionTexture;
uniform float TextureWidth, TextureHeight;

void main(void)
{
myhalf3 color;

#ifdef APPLY_DUDV
vec3 displacement = vec3(texture2D(DuDvMapTexture, vec2(TexCoord.pq) * vec2(0.25)));
vec2 coord = vec2(TexCoord.st) + vec2(displacement) * vec2 (0.2);

vec3 fdist = vec3 (normalize(vec3(texture2D(DuDvMapTexture, coord)) - vec3 (0.5))) * vec3(0.005);
#else
vec3 fdist = vec3(0.0);
#endif

// get projective texcoords
float scale = float(1.0 / float(ProjVector.w));
float inv2NW = 1.0 / (2.0 * float (TextureWidth));
float inv2NH = 1.0 / (2.0 * float (TextureHeight));
vec2 projCoord = (vec2(ProjVector.xy) * scale + vec2 (1.0)) * vec2 (0.5) + vec2(fdist.xy);
projCoord.s = float (clamp (float(projCoord.s), inv2NW, 1.0 - inv2NW));
projCoord.t = float (clamp (float(projCoord.t), inv2NH, 1.0 - inv2NH));


myhalf3 refr = myhalf3(0.0);
myhalf3 refl = myhalf3(0.0);

#ifdef APPLY_EYEDOT
// calculate dot product between the surface normal and eye vector
// great for simulating varying water translucency based on the view angle
myhalf3 surfaceNormal = normalize(myhalf3(texture2D(NormalmapTexture, coord)) - myhalf3 (0.5));
vec3 eyeNormal = normalize(myhalf3(EyeVector));

float refrdot = float(dot(surfaceNormal, eyeNormal));
//refrdot = float (clamp (refrdot, 0.0, 1.0));
float refldot = 1.0 - refrdot;
// get refraction and reflection

#ifdef APPLY_REFRACTION
refr = (myhalf3(texture2D(RefractionTexture, projCoord))) * refrdot;
#endif
#ifdef APPLY_REFLECTION
refl = (myhalf3(texture2D(ReflectionTexture, projCoord))) * refldot;
#endif

#else

#ifdef APPLY_REFRACTION
refr = (myhalf3(texture2D(RefractionTexture, projCoord)));
#endif
#ifdef APPLY_REFLECTION
refl = (myhalf3(texture2D(ReflectionTexture, projCoord)));
#endif

#endif

// add reflection and refraction
#ifdef APPLY_DISTORTION_ALPHA
color = myhalf3(gl_Color.rgb) + myhalf3(mix (refr, refl, float(gl_Color.a)));
#else
color = myhalf3(gl_Color.rgb) + refr + refl;
#endif

#ifdef APPLY_GRAYSCALE
float grey = dot(color, myhalf3(0.299, 0.587, 0.114));
gl_FragColor = vec4(vec3(grey),1.0);
#else
gl_FragColor = vec4(vec3(color),1.0);
#endif
}

#endif // FRAGMENT_SHADER