| 1 |
/* Parallax bump mapping shader effect. |
| 2 |
This is added right after bump_mapping.fs. |
| 3 |
Included by EnableShaderBumpMapping in ../castlerendererinternalshader.pas unit. |
| 4 |
*/ |
| 5 |
|
| 6 |
uniform float castle_parallax_bm_scale; |
| 7 |
|
| 8 |
// declare castle_normal_map, avoiding redeclaring it for GL_ES |
| 9 |
#ifdef GL_ES |
| 10 |
#ifndef castle_normal_map_defined |
| 11 |
#define castle_normal_map_defined |
| 12 |
uniform sampler2D castle_normal_map; |
| 13 |
#endif |
| 14 |
#else |
| 15 |
uniform sampler2D castle_normal_map; |
| 16 |
#endif |
| 17 |
|
| 18 |
varying vec3 castle_vertex_to_eye_in_tangent_space; |
| 19 |
|
| 20 |
#ifdef CASTLE_BUMP_MAPPING_PARALLAX_STEEP |
| 21 |
float castle_bm_height; |
| 22 |
vec2 castle_parallax_tex_coord; |
| 23 |
#endif |
| 24 |
|
| 25 |
void PLUG_texture_coord_shift(inout vec2 tex_coord) |
| 26 |
{ |
| 27 |
// We have to normalize castle_vertex_to_eye_in_tangent_space again, just like normal vectors. |
| 28 |
vec3 v_to_eye = normalize(castle_vertex_to_eye_in_tangent_space); |
| 29 |
|
| 30 |
#ifndef CASTLE_BUMP_MAPPING_PARALLAX_STEEP |
| 31 |
|
| 32 |
// Classic (not steep) parallax bump mapping |
| 33 |
float height = (texture2D(castle_normal_map, tex_coord).a - 1.0/2.0) * castle_parallax_bm_scale; |
| 34 |
tex_coord += height * v_to_eye.xy /* / v_to_eye.z*/; |
| 35 |
|
| 36 |
#else |
| 37 |
|
| 38 |
// Steep parallax bump mapping |
| 39 |
/* At smaller view angles, much more iterations needed, otherwise ugly |
| 40 |
aliasing artifacts quickly appear. */ |
| 41 |
float num_steps = mix(30.0, 10.0, v_to_eye.z); |
| 42 |
#ifdef CASTLE_BUGGY_BUMP_MAPPING_NUM_STEPS |
| 43 |
num_steps = clamp(num_steps, 10.0, 30.0); |
| 44 |
#endif |
| 45 |
float step = 1.0 / num_steps; |
| 46 |
|
| 47 |
/* Should we remove "v_to_eye.z" below, i.e. should we apply |
| 48 |
"offset limiting" ? In works about steep parallax mapping, |
| 49 |
v_to_eye.z is present, and in sample steep parallax mapping |
| 50 |
shader they suggest that it doesn't really matter. |
| 51 |
My tests confirm this, so I leave v_to_eye.z component. */ |
| 52 |
|
| 53 |
vec2 delta = -v_to_eye.xy * castle_parallax_bm_scale / (v_to_eye.z * num_steps); |
| 54 |
float height = 1.0; |
| 55 |
castle_bm_height = texture2D(castle_normal_map, tex_coord).a; |
| 56 |
|
| 57 |
/* TODO: NVidia GeForce FX 5200 fails here with |
| 58 |
|
| 59 |
error C5011: profile does not support "while" statements |
| 60 |
and "while" could not be unrolled. |
| 61 |
|
| 62 |
I could workaround this problem (by using |
| 63 |
for (int i = 0; i < steep_steps_max; i++) |
| 64 |
loop and |
| 65 |
if (! (castle_bm_height < height)) break; |
| 66 |
, this is possible to unroll). But it turns out that this still |
| 67 |
(even with steep_steps_max = 1) works much too slow on this hardware... |
| 68 |
*/ |
| 69 |
|
| 70 |
while (castle_bm_height < height) |
| 71 |
{ |
| 72 |
height -= step; |
| 73 |
tex_coord += delta; |
| 74 |
castle_bm_height = texture2D(castle_normal_map, tex_coord).a; |
| 75 |
} |
| 76 |
|
| 77 |
// Save value for bump_mapping_steep_parallax_shadowing.fs |
| 78 |
castle_parallax_tex_coord = tex_coord; |
| 79 |
|
| 80 |
#endif |
| 81 |
} |