Skip to content

Commit 8a85e5e

Browse files
committedJul 2, 2015
Shaders fixes and cleanup relief mapping code.
1 parent 75d2cfe commit 8a85e5e

File tree

3 files changed

+33
-45
lines changed

3 files changed

+33
-45
lines changed
 

‎client/shaders/nodes_shader/opengl_fragment.glsl

+24-39
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,21 @@ vec4 get_normal_map(vec2 uv)
3939

4040
float find_intersection(vec2 dp, vec2 ds)
4141
{
42-
const int linear_steps = 10;
43-
const int binary_steps = 5;
44-
const float depth_step = 1.0 / linear_steps;
45-
float size = depth_step;
42+
const float depth_step = 1.0 / 24.0;
4643
float depth = 1.0;
47-
float best_depth = 1.0;
48-
for (int i = 0 ; i < linear_steps - 1 ; ++i) {
49-
vec4 t = texture2D(normalTexture, dp + ds * depth);
50-
if (best_depth > 0.05)
51-
if (depth >= t.a)
52-
best_depth = depth;
53-
depth -= size;
54-
}
55-
depth = best_depth - size;
56-
for (int i = 0 ; i < binary_steps ; ++i) {
57-
size *= 0.5;
58-
vec4 t = texture2D(normalTexture, dp + ds * depth);
59-
if (depth >= t.a) {
60-
best_depth = depth;
61-
depth -= 2 * size;
62-
}
63-
depth += size;
44+
for (int i = 0 ; i < 24 ; i++) {
45+
float h = texture2D(normalTexture, dp + ds * depth).a;
46+
if (h >= depth)
47+
break;
48+
depth -= depth_step;
6449
}
65-
return best_depth;
50+
return depth;
6651
}
6752

6853
float find_intersectionRGB(vec2 dp, vec2 ds) {
69-
const float iterations = 24.0;
70-
const float depth_step = 1.0 / iterations;
54+
const float depth_step = 1.0 / 24.0;
7155
float depth = 1.0;
72-
for (int i = 0 ; i < iterations ; i++) {
56+
for (int i = 0 ; i < 24 ; i++) {
7357
float h = get_rgb_height(dp + ds * depth);
7458
if (h >= depth)
7559
break;
@@ -85,40 +69,41 @@ void main (void)
8569
vec2 uv = gl_TexCoord[0].st;
8670
bool use_normalmap = false;
8771

88-
#ifdef USE_NORMALMAPS
72+
#if USE_NORMALMAPS == 1
8973
if (texture2D(useNormalmap,vec2(1.0, 1.0)).r > 0.0) {
9074
normalTexturePresent = true;
9175
}
9276
#endif
9377

9478
#ifdef ENABLE_PARALLAX_OCCLUSION
95-
vec3 eyeRay = normalize(tsEyeVec);
79+
vec2 eyeRay = vec2 (tsEyeVec.x, -tsEyeVec.y);
80+
const float scale = PARALLAX_OCCLUSION_SCALE / PARALLAX_OCCLUSION_ITERATIONS;
81+
const float bias = PARALLAX_OCCLUSION_BIAS / PARALLAX_OCCLUSION_ITERATIONS;
82+
9683
#if PARALLAX_OCCLUSION_MODE == 0
9784
// Parallax occlusion with slope information
9885
if (normalTexturePresent && area_enable_parallax > 0.0) {
99-
const float scale = PARALLAX_OCCLUSION_SCALE / PARALLAX_OCCLUSION_ITERATIONS;
100-
const float bias = PARALLAX_OCCLUSION_BIAS / PARALLAX_OCCLUSION_ITERATIONS;
101-
for(int i = 0; i < PARALLAX_OCCLUSION_ITERATIONS; i++) {
86+
for (int i = 0; i < PARALLAX_OCCLUSION_ITERATIONS; i++) {
10287
vec4 normal = texture2D(normalTexture, uv.xy);
10388
float h = normal.a * scale - bias;
104-
uv += h * normal.z * eyeRay.xy;
89+
uv += h * normal.z * eyeRay;
10590
}
10691
#endif
10792
#if PARALLAX_OCCLUSION_MODE == 1
10893
// Relief mapping
10994
if (normalTexturePresent && area_enable_parallax > 0.0) {
110-
vec2 ds = eyeRay.xy * PARALLAX_OCCLUSION_SCALE;
95+
vec2 ds = eyeRay * PARALLAX_OCCLUSION_SCALE;
11196
float dist = find_intersection(uv, ds);
11297
uv += dist * ds;
11398
#endif
114-
} else if (area_enable_parallax > 0.0) {
115-
vec2 ds = eyeRay.xy * PARALLAX_OCCLUSION_SCALE;
99+
} else if (GENERATE_NORMALMAPS == 1 && area_enable_parallax > 0.0) {
100+
vec2 ds = eyeRay * PARALLAX_OCCLUSION_SCALE;
116101
float dist = find_intersectionRGB(uv, ds);
117102
uv += dist * ds;
118103
}
119104
#endif
120105

121-
#ifdef USE_NORMALMAPS
106+
#if USE_NORMALMAPS == 1
122107
if (normalTexturePresent) {
123108
bump = get_normal_map(uv);
124109
use_normalmap = true;
@@ -136,7 +121,7 @@ void main (void)
136121
float l = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y));
137122
float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
138123
float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
139-
bump = vec4(normalize(vec3 (-dX, -dY, NORMALMAPS_STRENGTH)), 1.0);
124+
bump = vec4(normalize(vec3 (dX, dY, NORMALMAPS_STRENGTH)), 1.0);
140125
use_normalmap = true;
141126
}
142127

@@ -145,9 +130,9 @@ void main (void)
145130
#ifdef ENABLE_BUMPMAPPING
146131
if (use_normalmap) {
147132
vec3 L = normalize(lightVec);
148-
vec3 E = normalize(-eyeVec);
149-
float specular = pow(clamp(dot(reflect(L, bump.xyz), -E), 0.0, 1.0), 1.0);
150-
float diffuse = dot(E,bump.xyz);
133+
vec3 E = normalize(eyeVec);
134+
float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
135+
float diffuse = dot(-E,bump.xyz);
151136
color = (diffuse + 0.1 * specular) * base.rgb;
152137
} else {
153138
color = base.rgb;

‎client/shaders/nodes_shader/opengl_vertex.glsl

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void main(void)
4141

4242
//Allow parallax/relief mapping only for certain kind of nodes
4343
//Variable is also used to control area of the effect
44-
#if ((DRAW_TYPE == NDT_NORMAL || DRAW_TYPE == NDT_LIQUID || DRAW_TYPE == NDT_FLOWINGLIQUID) && GENERATE_NORMALMAPS)
44+
#if (DRAW_TYPE == NDT_NORMAL || DRAW_TYPE == NDT_LIQUID || DRAW_TYPE == NDT_FLOWINGLIQUID)
4545
area_enable_parallax = 1.0;
4646
#else
4747
area_enable_parallax = 0.0;
@@ -94,7 +94,7 @@ void main(void)
9494

9595
// Don't generate heightmaps when too far from the eye
9696
float dist = distance (vec3(0.0, 0.0 ,0.0), vPosition);
97-
if (dist > 100.0) {
97+
if (dist > 120.0) {
9898
area_enable_parallax = 0.0;
9999
}
100100

@@ -103,21 +103,21 @@ void main(void)
103103
vec3 normal, tangent, binormal;
104104
normal = normalize(gl_NormalMatrix * gl_Normal);
105105
tangent = normalize(gl_NormalMatrix * gl_MultiTexCoord1.xyz);
106-
binormal = normalize(gl_NormalMatrix * -gl_MultiTexCoord2.xyz);
106+
binormal = normalize(gl_NormalMatrix * gl_MultiTexCoord2.xyz);
107107

108108
vec3 v;
109109

110110
lightVec = sunPosition - worldPosition;
111111
v.x = dot(lightVec, tangent);
112112
v.y = dot(lightVec, binormal);
113113
v.z = dot(lightVec, normal);
114-
tsLightVec = v;
114+
tsLightVec = normalize (v);
115115

116116
eyeVec = -(gl_ModelViewMatrix * gl_Vertex).xyz;
117117
v.x = dot(eyeVec, tangent);
118118
v.y = dot(eyeVec, binormal);
119119
v.z = dot(eyeVec, normal);
120-
tsEyeVec = v;
120+
tsEyeVec = normalize (v);
121121

122122
vec4 color;
123123
float day = gl_Color.r;

‎src/shader.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,11 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype,
731731
shaders_header += "\n";
732732
}
733733

734+
shaders_header += "#define USE_NORMALMAPS ";
734735
if (g_settings->getBool("enable_bumpmapping") || g_settings->getBool("enable_parallax_occlusion"))
735-
shaders_header += "#define USE_NORMALMAPS\n";
736+
shaders_header += "1\n";
737+
else
738+
shaders_header += "0\n";
736739

737740
if (g_settings->getBool("enable_waving_water")){
738741
shaders_header += "#define ENABLE_WAVING_WATER 1\n";

0 commit comments

Comments
 (0)
Please sign in to comment.