Skip to content

Commit c9ba92c

Browse files
committedJul 7, 2014
Faces shading fixes
1 parent a2e1b0f commit c9ba92c

File tree

3 files changed

+27
-99
lines changed

3 files changed

+27
-99
lines changed
 

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

-14
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,28 @@ void main(void)
6363

6464
vec3 normal, tangent, binormal;
6565
normal = normalize(gl_NormalMatrix * gl_Normal);
66-
float tileContrast = 1.0;
6766
if (gl_Normal.x > 0.5) {
6867
// 1.0, 0.0, 0.0
69-
tileContrast = 0.8;
7068
tangent = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, -1.0));
7169
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
7270
} else if (gl_Normal.x < -0.5) {
7371
// -1.0, 0.0, 0.0
74-
tileContrast = 0.8;
7572
tangent = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
7673
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
7774
} else if (gl_Normal.y > 0.5) {
7875
// 0.0, 1.0, 0.0
79-
tileContrast = 1.2;
8076
tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
8177
binormal = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
8278
} else if (gl_Normal.y < -0.5) {
8379
// 0.0, -1.0, 0.0
84-
tileContrast = 0.3;
8580
tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
8681
binormal = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
8782
} else if (gl_Normal.z > 0.5) {
8883
// 0.0, 0.0, 1.0
89-
tileContrast = 0.5;
9084
tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
9185
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
9286
} else if (gl_Normal.z < -0.5) {
9387
// 0.0, 0.0, -1.0
94-
tileContrast = 0.5;
9588
tangent = normalize(gl_NormalMatrix * vec3(-1.0, 0.0, 0.0));
9689
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
9790
}
@@ -129,13 +122,6 @@ void main(void)
129122
color.g = rg;
130123
color.b = b;
131124

132-
#if !(MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE)
133-
// Make sides and bottom darker than the top
134-
color = color * color; // SRGB -> Linear
135-
color *= tileContrast;
136-
color = sqrt(color); // Linear -> SRGB
137-
#endif
138-
139125
color.a = gl_Color.a;
140126
gl_FrontColor = gl_BackColor = clamp(color,0.0,1.0);
141127
}

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

-14
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,28 @@ void main(void)
6363

6464
vec3 normal, tangent, binormal;
6565
normal = normalize(gl_NormalMatrix * gl_Normal);
66-
float tileContrast = 1.0;
6766
if (gl_Normal.x > 0.5) {
6867
// 1.0, 0.0, 0.0
69-
tileContrast = 0.8;
7068
tangent = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, -1.0));
7169
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
7270
} else if (gl_Normal.x < -0.5) {
7371
// -1.0, 0.0, 0.0
74-
tileContrast = 0.8;
7572
tangent = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
7673
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
7774
} else if (gl_Normal.y > 0.5) {
7875
// 0.0, 1.0, 0.0
79-
tileContrast = 1.2;
8076
tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
8177
binormal = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
8278
} else if (gl_Normal.y < -0.5) {
8379
// 0.0, -1.0, 0.0
84-
tileContrast = 0.3;
8580
tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
8681
binormal = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
8782
} else if (gl_Normal.z > 0.5) {
8883
// 0.0, 0.0, 1.0
89-
tileContrast = 0.5;
9084
tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
9185
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
9286
} else if (gl_Normal.z < -0.5) {
9387
// 0.0, 0.0, -1.0
94-
tileContrast = 0.5;
9588
tangent = normalize(gl_NormalMatrix * vec3(-1.0, 0.0, 0.0));
9689
binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
9790
}
@@ -129,13 +122,6 @@ void main(void)
129122
color.g = rg;
130123
color.b = b;
131124

132-
#if !(MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE)
133-
// Make sides and bottom darker than the top
134-
color = color * color; // SRGB -> Linear
135-
color *= tileContrast;
136-
color = sqrt(color); // Linear -> SRGB
137-
#endif
138-
139125
color.a = gl_Color.a;
140126
gl_FrontColor = gl_BackColor = clamp(color,0.0,1.0);
141127
}

Diff for: ‎src/mapblock_mesh.cpp

+27-71
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3232
#include "settings.h"
3333
#include "util/directiontables.h"
3434

35-
float srgb_linear_multiply(float f, float m, float max)
35+
void applyContrast(video::SColor& color, float Factor)
3636
{
37-
f = f * f; // SRGB -> Linear
38-
f *= m;
39-
f = sqrt(f); // Linear -> SRGB
40-
if(f > max)
41-
f = max;
42-
return f;
37+
float r = color.getRed();
38+
float g = color.getGreen();
39+
float b = color.getBlue();
40+
color.setRed(irr::core::clamp((int)sqrt(r * r * Factor), 0, 255));
41+
color.setGreen(irr::core::clamp((int)sqrt(g * g * Factor), 0, 255));
42+
color.setBlue(irr::core::clamp((int)sqrt(b * b * Factor), 0, 255));
4343
}
4444

4545
/*
@@ -203,20 +203,6 @@ static u8 getFaceLight(enum LightBank bank, MapNode n, MapNode n2,
203203
//return decode_light(light_source);
204204
light = light_source;
205205

206-
// Make some nice difference to different sides
207-
208-
// This makes light come from a corner
209-
/*if(face_dir.X == 1 || face_dir.Z == 1 || face_dir.Y == -1)
210-
light = diminish_light(diminish_light(light));
211-
else if(face_dir.X == -1 || face_dir.Z == -1)
212-
light = diminish_light(light);*/
213-
214-
// All neighboring faces have different shade (like in minecraft)
215-
if(face_dir.X == 1 || face_dir.X == -1 || face_dir.Y == -1)
216-
light = diminish_light(diminish_light(light));
217-
else if(face_dir.Z == 1 || face_dir.Z == -1)
218-
light = diminish_light(light);
219-
220206
return decode_light(light);
221207
}
222208

@@ -352,21 +338,15 @@ static void finalColorBlend(video::SColor& result,
352338
1, 4, 6, 6, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0,
353339
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
354340
};
355-
if(b < 0)
356-
b = 0;
357-
if(b > 255)
358-
b = 255;
359341
b += emphase_blue_when_dark[b / 8];
342+
b = irr::core::clamp (b, 0, 255);
360343

361344
// Artificial light is yellow-ish
362345
static u8 emphase_yellow_when_artificial[16] = {
363346
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 15, 15, 15
364347
};
365348
rg += emphase_yellow_when_artificial[night/16];
366-
if(rg < 0)
367-
rg = 0;
368-
if(rg > 255)
369-
rg = 255;
349+
rg = irr::core::clamp (rg, 0, 255);
370350

371351
result.setRed(rg);
372352
result.setGreen(rg);
@@ -1168,25 +1148,32 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
11681148
os.str(),
11691149
&p.tile.texture_id);
11701150
}
1171-
// - Classic lighting (shaders handle this by themselves)
1172-
if(!enable_shaders)
1151+
1152+
for(u32 j = 0; j < p.vertices.size(); j++)
11731153
{
1174-
for(u32 j = 0; j < p.vertices.size(); j++)
1154+
video::SColor &vc = p.vertices[j].Color;
1155+
if(p.vertices[j].Normal.Y > 0.5) {
1156+
applyContrast (vc, 1.2);
1157+
} else if (p.vertices[j].Normal.Y < -0.5) {
1158+
applyContrast (vc, 0.3);
1159+
} else if (p.vertices[j].Normal.X > 0.5) {
1160+
applyContrast (vc, 0.5);
1161+
} else if (p.vertices[j].Normal.X < -0.5) {
1162+
applyContrast (vc, 0.5);
1163+
} else if (p.vertices[j].Normal.Z > 0.5) {
1164+
applyContrast (vc, 0.8);
1165+
} else if (p.vertices[j].Normal.Z < -0.5) {
1166+
applyContrast (vc, 0.8);
1167+
}
1168+
if(!enable_shaders)
11751169
{
1176-
video::SColor &vc = p.vertices[j].Color;
1170+
// - Classic lighting (shaders handle this by themselves)
11771171
// Set initial real color and store for later updates
11781172
u8 day = vc.getRed();
11791173
u8 night = vc.getGreen();
11801174
finalColorBlend(vc, day, night, 1000);
11811175
if(day != night)
11821176
m_daynight_diffs[i][j] = std::make_pair(day, night);
1183-
// Brighten topside (no shaders)
1184-
if(p.vertices[j].Normal.Y > 0.5)
1185-
{
1186-
vc.setRed (srgb_linear_multiply(vc.getRed(), 1.3, 255.0));
1187-
vc.setGreen(srgb_linear_multiply(vc.getGreen(), 1.3, 255.0));
1188-
vc.setBlue (srgb_linear_multiply(vc.getBlue(), 1.3, 255.0));
1189-
}
11901177
}
11911178
}
11921179

@@ -1293,7 +1280,6 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat
12931280
bool enable_shaders = g_settings->getBool("enable_shaders");
12941281
bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
12951282
bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
1296-
bool smooth_lighting = g_settings->getBool("smooth_lighting");
12971283

12981284
if(!m_has_animation)
12991285
{
@@ -1407,36 +1393,6 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat
14071393
u8 night = j->second.second;
14081394
finalColorBlend(vertices[vertexIndex].Color,
14091395
day, night, daynight_ratio);
1410-
// If no smooth lighting, shading is already correct
1411-
if(!smooth_lighting)
1412-
continue;
1413-
// Make sides and bottom darker than the top
1414-
video::SColor &vc = vertices[vertexIndex].Color;
1415-
if(vertices[vertexIndex].Normal.Y > 0.5) {
1416-
vc.setRed (srgb_linear_multiply(vc.getRed(), 1.2, 255.0));
1417-
vc.setGreen(srgb_linear_multiply(vc.getGreen(), 1.2, 255.0));
1418-
vc.setBlue (srgb_linear_multiply(vc.getBlue(), 1.2, 255.0));
1419-
} else if (vertices[vertexIndex].Normal.Y < -0.5) {
1420-
vc.setRed (srgb_linear_multiply(vc.getRed(), 0.3, 255.0));
1421-
vc.setGreen(srgb_linear_multiply(vc.getGreen(), 0.3, 255.0));
1422-
vc.setBlue (srgb_linear_multiply(vc.getBlue(), 0.3, 255.0));
1423-
} else if (vertices[vertexIndex].Normal.X > 0.5) {
1424-
vc.setRed (srgb_linear_multiply(vc.getRed(), 0.8, 255.0));
1425-
vc.setGreen(srgb_linear_multiply(vc.getGreen(), 0.8, 255.0));
1426-
vc.setBlue (srgb_linear_multiply(vc.getBlue(), 0.8, 255.0));
1427-
} else if (vertices[vertexIndex].Normal.X < -0.5) {
1428-
vc.setRed (srgb_linear_multiply(vc.getRed(), 0.8, 255.0));
1429-
vc.setGreen(srgb_linear_multiply(vc.getGreen(), 0.8, 255.0));
1430-
vc.setBlue (srgb_linear_multiply(vc.getBlue(), 0.8, 255.0));
1431-
} else if (vertices[vertexIndex].Normal.Z > 0.5) {
1432-
vc.setRed (srgb_linear_multiply(vc.getRed(), 0.5, 255.0));
1433-
vc.setGreen(srgb_linear_multiply(vc.getGreen(), 0.5, 255.0));
1434-
vc.setBlue (srgb_linear_multiply(vc.getBlue(), 0.5, 255.0));
1435-
} else if (vertices[vertexIndex].Normal.Z < -0.5) {
1436-
vc.setRed (srgb_linear_multiply(vc.getRed(), 0.5, 255.0));
1437-
vc.setGreen(srgb_linear_multiply(vc.getGreen(), 0.5, 255.0));
1438-
vc.setBlue (srgb_linear_multiply(vc.getBlue(), 0.5, 255.0));
1439-
}
14401396
}
14411397
}
14421398
m_last_daynight_ratio = daynight_ratio;

0 commit comments

Comments
 (0)
Please sign in to comment.