Skip to content

Commit 0dc1aec

Browse files
author
RealBadAngel
committedMar 21, 2014
Normal maps generation on the fly.
Parallax mapping with slope information. Overriding normal maps.
1 parent f3d83a4 commit 0dc1aec

16 files changed

+732
-431
lines changed
 

Diff for: ‎builtin/mainmenu.lua

+17-11
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,9 @@ function tabbuilder.handle_settings_buttons(fields)
723723
if fields["cb_parallax"] then
724724
engine.setting_set("enable_parallax_occlusion", fields["cb_parallax"])
725725
end
726+
if fields["cb_generate_normalmaps"] then
727+
engine.setting_set("generate_normalmaps", fields["cb_generate_normalmaps"])
728+
end
726729
if fields["cb_waving_water"] then
727730
engine.setting_set("enable_waving_water", fields["cb_waving_water"])
728731
end
@@ -1010,27 +1013,30 @@ function tabbuilder.tab_settings()
10101013
.. dump(engine.setting_getbool("enable_shaders")) .. "]"..
10111014
"button[1,4.5;2.25,0.5;btn_change_keys;".. fgettext("Change keys") .. "]"
10121015

1013-
if engine.setting_getbool("enable_shaders") then
1014-
tab_string = tab_string ..
1016+
if engine.setting_getbool("enable_shaders") then
1017+
tab_string = tab_string ..
10151018
"checkbox[8,0.5;cb_bumpmapping;".. fgettext("Bumpmapping") .. ";"
10161019
.. dump(engine.setting_getbool("enable_bumpmapping")) .. "]"..
10171020
"checkbox[8,1.0;cb_parallax;".. fgettext("Parallax Occlusion") .. ";"
10181021
.. dump(engine.setting_getbool("enable_parallax_occlusion")) .. "]"..
1019-
"checkbox[8,1.5;cb_waving_water;".. fgettext("Waving Water") .. ";"
1022+
"checkbox[8,1.5;cb_generate_normalmaps;".. fgettext("Generate Normalmaps") .. ";"
1023+
.. dump(engine.setting_getbool("generate_normalmaps")) .. "]"..
1024+
"checkbox[8,2.0;cb_waving_water;".. fgettext("Waving Water") .. ";"
10201025
.. dump(engine.setting_getbool("enable_waving_water")) .. "]"..
1021-
"checkbox[8,2.0;cb_waving_leaves;".. fgettext("Waving Leaves") .. ";"
1026+
"checkbox[8,2.5;cb_waving_leaves;".. fgettext("Waving Leaves") .. ";"
10221027
.. dump(engine.setting_getbool("enable_waving_leaves")) .. "]"..
1023-
"checkbox[8,2.5;cb_waving_plants;".. fgettext("Waving Plants") .. ";"
1028+
"checkbox[8,3.0;cb_waving_plants;".. fgettext("Waving Plants") .. ";"
10241029
.. dump(engine.setting_getbool("enable_waving_plants")) .. "]"
1025-
else
1026-
tab_string = tab_string ..
1030+
else
1031+
tab_string = tab_string ..
10271032
"textlist[8.33,0.7;4,1;;#888888" .. fgettext("Bumpmapping") .. ";0;true]" ..
10281033
"textlist[8.33,1.2;4,1;;#888888" .. fgettext("Parallax Occlusion") .. ";0;true]" ..
1029-
"textlist[8.33,1.7;4,1;;#888888" .. fgettext("Waving Water") .. ";0;true]" ..
1030-
"textlist[8.33,2.2;4,1;;#888888" .. fgettext("Waving Leaves") .. ";0;true]" ..
1031-
"textlist[8.33,2.7;4,1;;#888888" .. fgettext("Waving Plants") .. ";0;true]"
1034+
"textlist[8.33,1.7;4,1;;#888888" .. fgettext("Generate Normalmaps") .. ";0;true]" ..
1035+
"textlist[8.33,2.2;4,1;;#888888" .. fgettext("Waving Water") .. ";0;true]" ..
1036+
"textlist[8.33,2.7;4,1;;#888888" .. fgettext("Waving Leaves") .. ";0;true]" ..
1037+
"textlist[8.33,3.2;4,1;;#888888" .. fgettext("Waving Plants") .. ";0;true]"
10321038
end
1033-
return tab_string
1039+
return tab_string
10341040
end
10351041

10361042
--------------------------------------------------------------------------------

Diff for: ‎client/shaders/alpha_shader/opengl_fragment.glsl

+110-71
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,110 @@
1-
uniform sampler2D baseTexture;
2-
uniform sampler2D normalTexture;
3-
uniform sampler2D useNormalmap;
4-
5-
uniform vec4 skyBgColor;
6-
uniform float fogDistance;
7-
uniform vec3 eyePosition;
8-
9-
varying vec3 vPosition;
10-
varying vec3 eyeVec;
11-
12-
#ifdef ENABLE_PARALLAX_OCCLUSION
13-
varying vec3 tsEyeVec;
14-
#endif
15-
16-
const float e = 2.718281828459;
17-
18-
void main (void)
19-
{
20-
vec3 color;
21-
vec2 uv = gl_TexCoord[0].st;
22-
23-
#ifdef USE_NORMALMAPS
24-
float use_normalmap = texture2D(useNormalmap,vec2(1.0,1.0)).r;
25-
#endif
26-
27-
#ifdef ENABLE_PARALLAX_OCCLUSION
28-
float height;
29-
vec2 tsEye = vec2(tsEyeVec.x,-tsEyeVec.y);
30-
31-
if (use_normalmap > 0.0) {
32-
float map_height = texture2D(normalTexture, uv).a;
33-
if (map_height < 1.0){
34-
float height = PARALLAX_OCCLUSION_SCALE * map_height - PARALLAX_OCCLUSION_BIAS;
35-
uv = uv + height * tsEye;
36-
}
37-
}
38-
#endif
39-
40-
#ifdef ENABLE_BUMPMAPPING
41-
if (use_normalmap > 0.0) {
42-
vec3 base = texture2D(baseTexture, uv).rgb;
43-
vec3 vVec = normalize(eyeVec);
44-
vec3 bump = normalize(texture2D(normalTexture, uv).xyz * 2.0 - 1.0);
45-
vec3 R = reflect(-vVec, bump);
46-
vec3 lVec = normalize(vVec);
47-
float diffuse = max(dot(lVec, bump), 0.0);
48-
float specular = pow(clamp(dot(R, lVec), 0.0, 1.0),1.0);
49-
color = mix (base,diffuse*base,1.0) + 0.1 * specular * diffuse;
50-
} else {
51-
color = texture2D(baseTexture, uv).rgb;
52-
}
53-
#else
54-
color = texture2D(baseTexture, uv).rgb;
55-
#endif
56-
57-
float alpha = texture2D(baseTexture, uv).a;
58-
vec4 col = vec4(color.r, color.g, color.b, alpha);
59-
col *= gl_Color;
60-
col = col * col; // SRGB -> Linear
61-
col *= 1.8;
62-
col.r = 1.0 - exp(1.0 - col.r) / e;
63-
col.g = 1.0 - exp(1.0 - col.g) / e;
64-
col.b = 1.0 - exp(1.0 - col.b) / e;
65-
col = sqrt(col); // Linear -> SRGB
66-
if(fogDistance != 0.0){
67-
float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
68-
col = mix(col, skyBgColor, d);
69-
}
70-
gl_FragColor = vec4(col.r, col.g, col.b, alpha);
71-
}
1+
uniform sampler2D baseTexture;
2+
uniform sampler2D normalTexture;
3+
uniform sampler2D useNormalmap;
4+
5+
uniform vec4 skyBgColor;
6+
uniform float fogDistance;
7+
uniform vec3 eyePosition;
8+
9+
varying vec3 vPosition;
10+
varying vec3 worldPosition;
11+
12+
varying vec3 eyeVec;
13+
varying vec3 tsEyeVec;
14+
varying vec3 lightVec;
15+
varying vec3 tsLightVec;
16+
17+
bool normalTexturePresent = false;
18+
19+
const float e = 2.718281828459;
20+
21+
float intensity (vec3 color){
22+
return (color.r + color.g + color.b) / 3.0;
23+
}
24+
25+
float get_rgb_height (vec2 uv){
26+
return intensity(texture2D(baseTexture,uv).rgb);
27+
}
28+
29+
vec4 get_normal_map(vec2 uv){
30+
vec4 bump = texture2D(normalTexture, uv).rgba;
31+
bump.xyz = normalize(bump.xyz * 2.0 -1.0);
32+
bump.y = -bump.y;
33+
return bump;
34+
}
35+
36+
void main (void)
37+
{
38+
vec3 color;
39+
vec4 bump;
40+
vec2 uv = gl_TexCoord[0].st;
41+
bool use_normalmap = false;
42+
43+
#ifdef USE_NORMALMAPS
44+
if (texture2D(useNormalmap,vec2(1.0,1.0)).r > 0.0){
45+
normalTexturePresent = true;
46+
}
47+
#endif
48+
49+
#ifdef ENABLE_PARALLAX_OCCLUSION
50+
if (normalTexturePresent){
51+
vec3 tsEye = normalize(tsEyeVec);
52+
float height = PARALLAX_OCCLUSION_SCALE * texture2D(normalTexture, uv).a - PARALLAX_OCCLUSION_BIAS;
53+
uv = uv + texture2D(normalTexture, uv).z * height * vec2(tsEye.x,-tsEye.y);
54+
}
55+
#endif
56+
57+
#ifdef USE_NORMALMAPS
58+
if (normalTexturePresent){
59+
bump = get_normal_map(uv);
60+
use_normalmap = true;
61+
}
62+
#endif
63+
64+
#ifdef GENERATE_NORMALMAPS
65+
if (use_normalmap == false){
66+
float tl = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y+SAMPLE_STEP));
67+
float t = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y-SAMPLE_STEP));
68+
float tr = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y+SAMPLE_STEP));
69+
float r = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y));
70+
float br = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y-SAMPLE_STEP));
71+
float b = get_rgb_height (vec2(uv.x,uv.y-SAMPLE_STEP));
72+
float bl = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y-SAMPLE_STEP));
73+
float l = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y));
74+
float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
75+
float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
76+
bump = vec4 (normalize(vec3 (dX, -dY, NORMALMAPS_STRENGTH)),1.0);
77+
use_normalmap = true;
78+
}
79+
#endif
80+
81+
vec4 base = texture2D(baseTexture, uv).rgba;
82+
83+
#ifdef ENABLE_BUMPMAPPING
84+
if (use_normalmap){
85+
vec3 L = normalize(lightVec);
86+
vec3 E = normalize(eyeVec);
87+
float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0),0.5);
88+
float diffuse = dot(E,bump.xyz);
89+
color = 0.05*base.rgb + diffuse*base.rgb + 0.2*specular*base.rgb;
90+
} else {
91+
color = base.rgb;
92+
}
93+
#else
94+
color = base.rgb;
95+
#endif
96+
97+
vec4 col = vec4(color.rgb, base.a);
98+
col = col * col; // SRGB -> Linear
99+
col *= 1.8;
100+
col.r = 1.0 - exp(1.0 - col.r) / e;
101+
col.g = 1.0 - exp(1.0 - col.g) / e;
102+
col.b = 1.0 - exp(1.0 - col.b) / e;
103+
col = sqrt(col); // Linear -> SRGB
104+
col *= gl_Color;
105+
if(fogDistance != 0.0){
106+
float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
107+
col = mix(col, skyBgColor, d);
108+
}
109+
gl_FragColor = vec4(col.rgb, base.a);
110+
}

Diff for: ‎client/shaders/alpha_shader/opengl_vertex.glsl

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
uniform mat4 mWorldViewProj;
22
uniform mat4 mInvWorld;
33
uniform mat4 mTransWorld;
4+
uniform mat4 mWorld;
5+
46
uniform float dayNightRatio;
57

68
uniform vec3 eyePosition;
79

810
varying vec3 vPosition;
11+
varying vec3 worldPosition;
12+
913
varying vec3 eyeVec;
14+
varying vec3 lightVec;
1015

11-
#ifdef ENABLE_PARALLAX_OCCLUSION
1216
varying vec3 tsEyeVec;
13-
#endif
17+
varying vec3 tsLightVec;
18+
19+
const float BS = 10.0;
1420

1521
void main(void)
1622
{
23+
gl_TexCoord[0] = gl_MultiTexCoord0;
1724
gl_Position = mWorldViewProj * gl_Vertex;
18-
vPosition = (mWorldViewProj * gl_Vertex).xyz;
19-
eyeVec = (gl_ModelViewMatrix * gl_Vertex).xyz;
25+
vPosition = gl_Position.xyz;
26+
worldPosition = (mWorld * gl_Vertex).xyz;
27+
vec3 sunPosition = vec3 (0.0, eyePosition.y * BS + 900.0, 0.0);
2028

21-
#ifdef ENABLE_PARALLAX_OCCLUSION
22-
vec3 normal,tangent,binormal;
29+
vec3 normal, tangent, binormal;
2330
normal = normalize(gl_NormalMatrix * gl_Normal);
24-
2531
if (gl_Normal.x > 0.5) {
2632
// 1.0, 0.0, 0.0
2733
tangent = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, -1.0));
@@ -47,25 +53,20 @@ void main(void)
4753
tangent = normalize(gl_NormalMatrix * vec3(-1.0, 0.0, 0.0));
4854
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
4955
}
50-
5156
mat3 tbnMatrix = mat3( tangent.x, binormal.x, normal.x,
5257
tangent.y, binormal.y, normal.y,
5358
tangent.z, binormal.z, normal.z);
5459

55-
tsEyeVec = normalize(eyeVec * tbnMatrix);
56-
#endif
60+
lightVec = sunPosition - worldPosition;
61+
tsLightVec = lightVec * tbnMatrix;
62+
eyeVec = (gl_ModelViewMatrix * gl_Vertex).xyz;
63+
tsEyeVec = eyeVec * tbnMatrix;
5764

5865
vec4 color;
59-
//color = vec4(1.0, 1.0, 1.0, 1.0);
60-
6166
float day = gl_Color.r;
6267
float night = gl_Color.g;
6368
float light_source = gl_Color.b;
6469

65-
/*color.r = mix(night, day, dayNightRatio);
66-
color.g = color.r;
67-
color.b = color.r;*/
68-
6970
float rg = mix(night, day, dayNightRatio);
7071
rg += light_source * 2.5; // Make light sources brighter
7172
float b = rg;
@@ -90,13 +91,8 @@ void main(void)
9091
color = color * color; // SRGB -> Linear
9192
if(gl_Normal.y <= 0.5)
9293
color *= 0.6;
93-
//color *= 0.7;
9494
color = sqrt(color); // Linear -> SRGB
95-
9695
color.a = gl_Color.a;
9796

9897
gl_FrontColor = gl_BackColor = color;
99-
100-
gl_TexCoord[0] = gl_MultiTexCoord0;
101-
10298
}

0 commit comments

Comments
 (0)
Please sign in to comment.