summaryrefslogtreecommitdiff
path: root/src/modules/evas/engines/gl_common/shader_3d/normal_map_frag.shd
blob: 766e07fb40a83c4189aca5d13bc1a6adbc80e668 (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
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
varying  vec3        vLightVector;
varying  vec3        vLightHalfVector;
uniform  sampler2D   uTextureNormal0;
varying  vec3        vEyeVector;

#ifdef NEED_TEX_COORD
varying vec2   vTexCoord;
#endif //TEX_COORD

#ifdef FOG_ENABLED
uniform float uFogFactor;
uniform vec4  uFogColor;
#endif //FOG_ENABLED

#ifdef  SHADOWED
varying vec4 vLightPosition;
uniform sampler2D uShadowMap;
float shadow;
#endif //SHADOWED

#ifdef NORMAL_TEXTURE_BLEND
uniform  sampler2D  uTextureNormal1;
uniform  float      uTextureNormalWeight;
#endif //NORMAL_TEXTURE_BLEND

#ifndef VERTEX_TANGENT
varying  vec3        vNormal;
#endif //VERTEX_TANGENT

#ifdef DIFFUSE
uniform   vec4        uMaterialDiffuse;
uniform   vec4        uLightDiffuse;

#ifdef DIFFUSE_TEXTURE
uniform sampler2D  uTextureDiffuse0;
#endif //DIFFUSE_TEXTURE

#ifdef DIFFUSE_TEXTURE_BLEND
uniform sampler2D  uTextureDiffuse1;
uniform float      uTextureDiffuseWeight;
#endif //DIFFUSE_TEXTURE_BLEND

#endif //DIFFUSE

#ifdef SPECULAR
uniform   vec4     uLightSpecular;
uniform   float    uMaterialShininess;
uniform   vec4     uMaterialSpecular;

#ifdef SPECULAR_TEXTURE
uniform sampler2D  uTextureSpecular0;
#endif //SPECULAR_TEXTURE

#ifdef SPECULAR_TEXTURE_BLEND
uniform sampler2D  uTextureSpecular1;
uniform float      uTextureSpecularWeight;
#endif //SPECULAR_TEXTURE_BLEND

#endif //SPECULAR

#ifdef AMBIENT
uniform  vec4       uMaterialAmbient;
uniform  vec4       uLightAmbient;

#ifdef AMBIENT_TEXTURE
uniform sampler2D  uTextureAmbient0;
#endif //AMBIENT_TEXTURE

#ifdef AMBIENT_TEXTURE_BLEND
uniform sampler2D  uTextureAmbient1;
uniform float      uTextureAmbientWeight;
#endif //AMBIENT_TEXTURE_BLEND

#endif //AMBIENT

#ifdef EMISSION
uniform vec4       uMaterialEmission;

#ifdef EMISSION_TEXTURE
uniform sampler2D  uTextureEmission0;
#endif //EMISSION_TEXTURE

#ifdef EMISSION_TEXTURE_BLEND
uniform sampler2D  uTextureEmission1;
uniform float      uTextureEmissionWeight;
#endif //EMISSION_TEXTURE_BLEND

#endif //EMISSION

#ifdef LIGHT_SPOT
uniform   vec3  uLightSpotDir;
uniform   float uLightSpotExp;
uniform   float uLightSpotCutoffCos;
#endif //LIGHT_SPOT

#ifdef LIGHT_ATTENUATION
varying   float    vLightDist;
#endif //LIGHT_ATTENUATION

#ifndef VERTEX_TANGENT

mat3 cotangent_frame(vec3 n, vec3 p, vec2 uv)
{
   vec3 dp1 = dFdx(p);
   vec3 dp2 = dFdy(p);
   vec2 duv1 = dFdx(uv);
   vec2 duv2 = dFdy(uv);
   vec3 dp2perp = cross(dp2, n);
   vec3 dp1perp = cross(n, dp1);
   vec3 t = dp2perp * duv1.x + dp1perp * duv2.x;
   vec3 b = dp2perp * duv1.y + dp1perp * duv2.y;
   float invmax = inversesqrt(max(dot(t, t), dot(b, b)));
   return mat3(t * invmax, b * invmax, n);
}

vec3 perturb_normal(vec3 normal)
{
   mat3 tbn = cotangent_frame(vNormal, -vEyeVector, vTexCoord);
   return normalize(tbn * normal);
}
#endif //VERTEX_TANGENT

void fragmentNormalMap()
{
   float factor;
   vec3  normal;
   vec4  color;

#ifdef NORMAL_TEXTURE_BLEND
   normal = texture2D(uTextureNormal0, vTexCoord).rgb * uTextureNormalWeight;
   normal += texture2D(uTextureNormal1, vTexCoord).rgb *
   (1.0 - uTextureNormalWeight);
#else
   normal = texture2D(uTextureNormal0, vTexCoord).rgb;
#endif //NORMAL_TEXTURE_BLEND

   normal = 2.0 * normal - 1.0;

#ifndef VERTEX_TANGENT
   normal = perturb_normal(normal);
#endif //VERTEX_TANGENT

   vec3  lv = normalize(vLightVector);
   normal = normalize(normal);

   factor = dot(lv, normal);

#ifdef LIGHT_SPOT
   float f = dot(-lv, normalize(uLightSpotDir));

   if (f > uLightSpotCutoffCos)
     factor *= pow(f, uLightSpotExp);
   else
     factor = 0.0;
#endif //LIGHT_SPOT

   if (factor > 0.0)
     {

#ifdef DIFFUSE

#ifdef DIFFUSE_TEXTURE_BLEND
        color = texture2D(uTextureDiffuse0, vTexCoord) * uTextureDiffuseWeight +
        texture2D(uTextureDiffuse1, vTexCoord) * (1.0 - uTextureDiffuseWeight);
#else

#ifdef DIFFUSE_TEXTURE
        color = texture2D(uTextureDiffuse0, vTexCoord);
#else
        color = uMaterialDiffuse;
#endif //DIFFUSE_TEXTURE

#endif //DIFFUSE_TEXTURE_BLEND

        gl_FragColor = uLightDiffuse * color * factor;

#else
        gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
#endif //DIFFUSE

#ifdef SPECULAR

        factor = dot(normalize(vLightHalfVector), normal);
        if (factor > 0.0)
          {
             factor = pow(factor, uMaterialShininess);

#ifdef SPECULAR_TEXTURE_BLEND
             color = texture2D(uTextureSpecular0, vTexCoord) * uTextureSpecularWeight +
             texture2D(uTextureSpecular1, vTexCoord) * (1.0 - uTextureSpecularWeight);
#else
#ifdef SPECULAR_TEXTURE
             color = texture2D(uTextureSpecular0, vTexCoord);
#else
             color = uMaterialSpecular;
#endif //SPECULAR_TEXTURE

#endif //SPECULAR_TEXTURE_BLEND

             gl_FragColor += uLightSpecular * color * factor;
          }

#endif //SPECULAR

#ifdef SHADOWED
        gl_FragColor *= shadow;
#endif //SHADOWED

     }
   else
     gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);

#ifdef AMBIENT
#ifdef AMBIENT_TEXTURE_BLEND
   color = texture2D(uTextureAmbient0, vTexCoord) * uTextureAmbientWeight +
   texture2D(uTextureAmbient1, vTexCoord) * (1.0 - uTextureAmbientWeight);
#else

#ifdef AMBIENT_TEXTURE
   color = texture2D(uTextureAmbient0, vTexCoord);
#else
   color = uMaterialAmbient;
#endif //AMBIENT_TEXTURE

#endif //AMBIENT_TEXTURE_BLEND

     gl_FragColor += uLightAmbient * color;
#endif //AMBIENT

#ifdef LIGHT_ATTENUATION
   gl_FragColor /= dot(uLightAtten, vec3(1.0, vLightDist, vLightDist * vLightDist));
#endif //LIGHT_ATTENUATION

#ifdef EMISSION

#ifdef EMISSION_TEXTURE_BLEND
   color = texture2D(uTextureEmission0, vTexCoord) * uTextureEmissionWeight +
   texture2D(uTextureEmission1, vTexCoord) * (1.0 - uTextureEmissionWeight);
#else

#ifdef EMISSION_TEXTURE
   color = texture2D(uTextureEmission0, vTexCoord);
#else
   color = uMaterialEmission;
#endif //EMISSION_TEXTURE

#endif //EMISSION_TEXTURE_BLEND

   gl_FragColor += color;
#endif //EMISSION

}

#ifdef SHADOWED
float pcf(vec4 lpos, float size)
{
   vec3 smcoord = lpos.xyz / lpos.w * 0.5 + 0.5;
   float i, j, randx, randy, shadow;
   shadow = 0.0;
   for (i = -4.0; i < 4.0; i++)
     for (j = -4.0; j < 4.0; j++)
        shadow += float(smcoord.z <= texture2D(uShadowMap, smcoord.xy +vec2(i / 8.0, j / 8.0)*size).x);
   return shadow / 64.0;
}
#endif //SHADOWED

void main() {

#ifdef SHADOWED
   shadow = pcf(vLightPosition, 1.0 / 200.0);
#endif //SHADOWED

   fragmentNormalMap();

#ifdef FOG_ENABLED
   float z = gl_FragCoord.z / gl_FragCoord.w;
   float fogFactor = exp2( -uFogFactor * uFogFactor * z * z * 1.44);
   fogFactor = clamp(fogFactor, 0.0, 1.0);
   gl_FragColor = mix(uFogColor, gl_FragColor, fogFactor);
#endif //FOG_ENABLED

}