| 1 |
/* Bump mapping shader effect. |
| 2 |
Included by EnableShaderBumpMapping in ../castlerendererinternalshader.pas unit. |
| 3 |
*/ |
| 4 |
|
| 5 |
varying mat3 castle_tangent_to_eye_space; |
| 6 |
|
| 7 |
// declare castle_normal_map, avoiding redeclaring it for GL_ES |
| 8 |
#ifdef GL_ES |
| 9 |
#ifndef castle_normal_map_defined |
| 10 |
#define castle_normal_map_defined |
| 11 |
uniform sampler2D castle_normal_map; |
| 12 |
uniform float castle_normalScale; |
| 13 |
#endif |
| 14 |
#else |
| 15 |
uniform sampler2D castle_normal_map; |
| 16 |
uniform float castle_normalScale; |
| 17 |
#endif |
| 18 |
|
| 19 |
// avoid redeclaring for GL_ES |
| 20 |
#ifndef GL_ES |
| 21 |
varying vec4 castle_TexCoord<NormalMapTextureCoordinatesId>; |
| 22 |
#endif |
| 23 |
|
| 24 |
void PLUG_fragment_eye_space(const vec4 vertex, inout vec3 normal_eye_fragment) |
| 25 |
{ |
| 26 |
// Read normal from the texture (the core idea of bump mapping). |
| 27 |
// Unpack normals, they are in texture in [0..1] range and we want in [-1..1]. |
| 28 |
vec3 normal_tangent = texture2D(castle_normal_map, |
| 29 |
castle_TexCoord<NormalMapTextureCoordinatesId>.st).xyz * 2.0 - vec3(1.0); |
| 30 |
|
| 31 |
normal_tangent *= vec3(castle_normalScale, castle_normalScale, 1.0); |
| 32 |
|
| 33 |
/* We have to take two-sided lighting into account here, in tangent space. |
| 34 |
Simply negating whole normal in eye space (like we do without bump mapping) |
| 35 |
would not work good, |
| 36 |
check e.g. insides of demo_models/bump_mapping/room_for_parallax_final.wrl. */ |
| 37 |
if (gl_FrontFacing) |
| 38 |
/* Avoid AMD bug http://forums.amd.com/devforum/messageview.cfm?catid=392&threadid=148827&enterthread=y |
| 39 |
It causes both (gl_FrontFacing) and (!gl_FrontFacing) to be true... |
| 40 |
To minimize the number of problems, never use "if (!gl_FrontFacing)", |
| 41 |
only "if (gl_FrontFacing)". |
| 42 |
See template_phong.fs for more comments. |
| 43 |
*/ ; else |
| 44 |
normal_tangent.z = -normal_tangent.z; |
| 45 |
|
| 46 |
normal_eye_fragment = normalize(castle_tangent_to_eye_space * normal_tangent); |
| 47 |
} |