Skip to content

Commit 096bc0c

Browse files
committed
huge revamp to track down more bugs
Half Lambert Lighting and Wrap Around Lighting are for models make possible to combine halflambert and wraparound no need for avoiding lightmapping
1 parent b729cc9 commit 096bc0c

6 files changed

Lines changed: 310 additions & 123 deletions

File tree

src/engine/renderer/gl_shader.cpp

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,10 @@ static bool IsUnusedPermutation( const char *compileMacros )
762762
{
763763
if ( r_deluxeMapping->integer == 0 ) return true;
764764
}
765+
else if ( strcmp( token, "USE_GRID_DELUXE_MAPPING" ) == 0 )
766+
{
767+
if ( r_deluxeMapping->integer == 0 ) return true;
768+
}
765769
else if ( strcmp( token, "USE_PHYSICAL_MAPPING" ) == 0 )
766770
{
767771
if ( r_physicalMapping->integer == 0 ) return true;
@@ -1369,6 +1373,62 @@ bool GLCompileMacro_USE_DEPTH_FADE::HasConflictingMacros(size_t permutation, con
13691373
return false;
13701374
}
13711375

1376+
bool GLCompileMacro_USE_DELUXE_MAPPING::HasConflictingMacros(size_t permutation, const std::vector<GLCompileMacro*> &macros) const
1377+
{
1378+
for (const GLCompileMacro* macro : macros)
1379+
{
1380+
if ((permutation & macro->GetBit()) != 0 && (macro->GetType() == USE_GRID_DELUXE_MAPPING || macro->GetType() == USE_GRID_LIGHTING))
1381+
{
1382+
//Log::Notice("conflicting macro! canceling '%s' vs. '%s'", GetName(), macro->GetName());
1383+
return true;
1384+
}
1385+
}
1386+
1387+
return false;
1388+
}
1389+
1390+
bool GLCompileMacro_USE_GRID_DELUXE_MAPPING::HasConflictingMacros(size_t permutation, const std::vector<GLCompileMacro*> &macros) const
1391+
{
1392+
for (const GLCompileMacro* macro : macros)
1393+
{
1394+
if ((permutation & macro->GetBit()) != 0 && (macro->GetType() == USE_DELUXE_MAPPING || macro->GetType() == USE_BSP_SURFACE))
1395+
{
1396+
//Log::Notice("conflicting macro! canceling '%s' vs. '%s'", GetName(), macro->GetName());
1397+
return true;
1398+
}
1399+
}
1400+
1401+
return false;
1402+
}
1403+
1404+
bool GLCompileMacro_USE_GRID_LIGHTING::HasConflictingMacros(size_t permutation, const std::vector<GLCompileMacro*> &macros) const
1405+
{
1406+
for (const GLCompileMacro* macro : macros)
1407+
{
1408+
if ((permutation & macro->GetBit()) != 0 && (macro->GetType() == USE_DELUXE_MAPPING))
1409+
{
1410+
//Log::Notice("conflicting macro! canceling '%s' vs. '%s'", GetName(), macro->GetName());
1411+
return true;
1412+
}
1413+
}
1414+
1415+
return false;
1416+
}
1417+
1418+
bool GLCompileMacro_USE_BSP_SURFACE::HasConflictingMacros(size_t permutation, const std::vector<GLCompileMacro*> &macros) const
1419+
{
1420+
for (const GLCompileMacro* macro : macros)
1421+
{
1422+
if ((permutation & macro->GetBit()) != 0 && (macro->GetType() == USE_GRID_DELUXE_MAPPING))
1423+
{
1424+
//Log::Notice("conflicting macro! canceling '%s' vs. '%s'", GetName(), macro->GetName());
1425+
return true;
1426+
}
1427+
}
1428+
1429+
return false;
1430+
}
1431+
13721432
bool GLShader::GetCompileMacrosString( size_t permutation, std::string &compileMacrosOut ) const
13731433
{
13741434
compileMacrosOut.clear();
@@ -1573,8 +1633,9 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
15731633
GLCompileMacro_USE_BSP_SURFACE( this ),
15741634
GLCompileMacro_USE_VERTEX_SKINNING( this ),
15751635
GLCompileMacro_USE_VERTEX_ANIMATION( this ),
1576-
GLCompileMacro_USE_LIGHT_MAPPING( this ),
15771636
GLCompileMacro_USE_DELUXE_MAPPING( this ),
1637+
GLCompileMacro_USE_GRID_LIGHTING( this ),
1638+
GLCompileMacro_USE_GRID_DELUXE_MAPPING( this ),
15781639
GLCompileMacro_USE_HEIGHTMAP_IN_NORMALMAP( this ),
15791640
GLCompileMacro_USE_RELIEF_MAPPING( this ),
15801641
GLCompileMacro_USE_REFLECTIVE_SPECULAR( this ),

src/engine/renderer/gl_shader.h

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3030
#define USE_UNIFORM_FIREWALL 1
3131

3232
// *INDENT-OFF*
33-
static const unsigned int MAX_SHADER_MACROS = 9;
33+
static const unsigned int MAX_SHADER_MACROS = 10;
3434
static const unsigned int GL_SHADER_VERSION = 4;
3535

3636
class ShaderException : public std::runtime_error
@@ -788,8 +788,9 @@ class GLCompileMacro
788788
USE_VERTEX_SPRITE,
789789
USE_TCGEN_ENVIRONMENT,
790790
USE_TCGEN_LIGHTMAP,
791-
USE_LIGHT_MAPPING,
792791
USE_DELUXE_MAPPING,
792+
USE_GRID_LIGHTING,
793+
USE_GRID_DELUXE_MAPPING,
793794
USE_HEIGHTMAP_IN_NORMALMAP,
794795
USE_RELIEF_MAPPING,
795796
USE_REFLECTIVE_SPECULAR,
@@ -857,6 +858,8 @@ class GLCompileMacro_USE_BSP_SURFACE :
857858
return "USE_BSP_SURFACE";
858859
}
859860

861+
bool HasConflictingMacros(size_t permutation, const std::vector< GLCompileMacro * > &macros) const override;
862+
860863
EGLCompileMacro GetType() const override
861864
{
862865
return EGLCompileMacro::USE_BSP_SURFACE;
@@ -1017,26 +1020,28 @@ class GLCompileMacro_USE_TCGEN_LIGHTMAP :
10171020
}
10181021
};
10191022

1020-
class GLCompileMacro_USE_LIGHT_MAPPING :
1023+
class GLCompileMacro_USE_GRID_LIGHTING :
10211024
GLCompileMacro
10221025
{
10231026
public:
1024-
GLCompileMacro_USE_LIGHT_MAPPING( GLShader *shader ) :
1027+
GLCompileMacro_USE_GRID_LIGHTING( GLShader *shader ) :
10251028
GLCompileMacro( shader )
10261029
{
10271030
}
10281031

10291032
const char *GetName() const override
10301033
{
1031-
return "USE_LIGHT_MAPPING";
1034+
return "USE_GRID_LIGHTING";
10321035
}
10331036

1037+
bool HasConflictingMacros(size_t permutation, const std::vector< GLCompileMacro * > &macros) const override;
1038+
10341039
EGLCompileMacro GetType() const override
10351040
{
1036-
return EGLCompileMacro::USE_LIGHT_MAPPING;
1041+
return EGLCompileMacro::USE_GRID_LIGHTING;
10371042
}
10381043

1039-
void SetLightMapping( bool enable )
1044+
void SetGridLighting( bool enable )
10401045
{
10411046
SetMacro( enable );
10421047
}
@@ -1056,6 +1061,8 @@ class GLCompileMacro_USE_DELUXE_MAPPING :
10561061
return "USE_DELUXE_MAPPING";
10571062
}
10581063

1064+
bool HasConflictingMacros(size_t permutation, const std::vector< GLCompileMacro * > &macros) const override;
1065+
10591066
EGLCompileMacro GetType() const override
10601067
{
10611068
return EGLCompileMacro::USE_DELUXE_MAPPING;
@@ -1067,6 +1074,33 @@ class GLCompileMacro_USE_DELUXE_MAPPING :
10671074
}
10681075
};
10691076

1077+
class GLCompileMacro_USE_GRID_DELUXE_MAPPING :
1078+
GLCompileMacro
1079+
{
1080+
public:
1081+
GLCompileMacro_USE_GRID_DELUXE_MAPPING( GLShader *shader ) :
1082+
GLCompileMacro( shader )
1083+
{
1084+
}
1085+
1086+
const char *GetName() const override
1087+
{
1088+
return "USE_GRID_DELUXE_MAPPING";
1089+
}
1090+
1091+
bool HasConflictingMacros(size_t permutation, const std::vector< GLCompileMacro * > &macros) const override;
1092+
1093+
EGLCompileMacro GetType() const override
1094+
{
1095+
return EGLCompileMacro::USE_GRID_DELUXE_MAPPING;
1096+
}
1097+
1098+
void SetGridDeluxeMapping( bool enable )
1099+
{
1100+
SetMacro( enable );
1101+
}
1102+
};
1103+
10701104
class GLCompileMacro_USE_HEIGHTMAP_IN_NORMALMAP :
10711105
GLCompileMacro
10721106
{
@@ -2271,8 +2305,9 @@ class GLShader_lightMapping :
22712305
public GLCompileMacro_USE_BSP_SURFACE,
22722306
public GLCompileMacro_USE_VERTEX_SKINNING,
22732307
public GLCompileMacro_USE_VERTEX_ANIMATION,
2274-
public GLCompileMacro_USE_LIGHT_MAPPING,
22752308
public GLCompileMacro_USE_DELUXE_MAPPING,
2309+
public GLCompileMacro_USE_GRID_LIGHTING,
2310+
public GLCompileMacro_USE_GRID_DELUXE_MAPPING,
22762311
public GLCompileMacro_USE_HEIGHTMAP_IN_NORMALMAP,
22772312
public GLCompileMacro_USE_RELIEF_MAPPING,
22782313
public GLCompileMacro_USE_REFLECTIVE_SPECULAR,

src/engine/renderer/glsl_source/computeLight_fp.glsl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2121
*/
2222
// computeLight_fp.glsl - Light computing helper functions
2323

24+
#if !defined(USE_BSP_SURFACE)
25+
#define USE_MODEL_SURFACE
26+
#endif
27+
28+
#if !defined(USE_GRID_LIGHTING)
29+
#define USE_LIGHT_MAPPING
30+
#endif
31+
2432
#if defined(USE_REFLECTIVE_SPECULAR)
2533
uniform samplerCube u_EnvironmentMap0;
2634
uniform samplerCube u_EnvironmentMap1;
@@ -76,15 +84,18 @@ void computeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
7684
// if no r_halfLambertLighting and no r_wrapAroundLighting.
7785
float NdotL = dot( normal, lightDir );
7886

87+
#if !defined(USE_BSP_SURFACE)
7988
#if defined(r_halfLambertLighting)
8089
// http://developer.valvesoftware.com/wiki/Half_Lambert
8190
NdotL = NdotL * 0.5 + 0.5;
8291
NdotL *= NdotL;
83-
#elif defined(r_wrapAroundLighting)
92+
#endif
93+
#if defined(r_wrapAroundLighting)
8494
NdotL = clamp( NdotL + r_wrapAroundLighting, 0.0, 1.0) / clamp(1.0 + r_wrapAroundLighting, 0.0, 1.0);
8595
#else
8696
NdotL = clamp( NdotL, 0.0, 1.0 );
8797
#endif
98+
#endif // !USE_BSP_SURFACE
8899

89100
#if defined(USE_PHYSICAL_MAPPING)
90101
// Daemon PBR packing defaults to ORM like glTF 2.0 defines

src/engine/renderer/glsl_source/lightMapping_fp.glsl

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,16 @@ IN(smooth) vec3 var_Tangent;
3636
IN(smooth) vec3 var_Binormal;
3737
IN(smooth) vec3 var_Normal;
3838

39-
#if defined(USE_BSP_SURFACE) && defined(USE_LIGHT_MAPPING) && !defined(USE_DELUXE_MAPPING)
40-
/* HACK: restore legacy behavior for bsp surface with lightmap
41-
but no deluxemap, do not use the lightgrid to compute the light
42-
direction, do not do normal mapping neither specular mapping
43-
with static lights.
44-
https://github.com/DaemonEngine/Daemon/issues/324
45-
46-
Comment out this line to enable lightgrid light direction with
47-
lightmap light color when there is no deluxe map.
48-
*/
49-
#define HACK_NO_BSP_GRID_LIGHTDIR
50-
#endif
51-
5239
#if defined(USE_LIGHT_MAPPING)
5340
uniform sampler2D u_LightMap;
54-
#else
41+
#elif defined(USE_GRID_LIGHTING)
5542
uniform sampler3D u_LightMap;
5643
#define u_LightGrid1 u_LightMap
5744
#endif
5845

5946
#if defined(USE_DELUXE_MAPPING)
6047
uniform sampler2D u_DeluxeMap;
61-
#else
48+
#elif defined(USE_GRID_DELUXE_MAPPING)
6249
uniform sampler3D u_DeluxeMap;
6350
#define u_LightGrid2 u_DeluxeMap
6451
#endif
@@ -67,7 +54,7 @@ lightmap light color when there is no deluxe map.
6754
IN(smooth) vec2 var_TexLight;
6855
#endif
6956

70-
#if !defined(USE_LIGHT_MAPPING) || !defined(USE_DELUXE_MAPPING)
57+
#if defined(USE_GRID_LIGHTING) || defined(USE_GRID_DELUXE_MAPPING)
7158
uniform vec3 u_LightGridOrigin;
7259
uniform vec3 u_LightGridScale;
7360
#endif
@@ -112,30 +99,26 @@ void main()
11299
vec4 color;
113100
color.a = diffuse.a;
114101

115-
#if !defined(USE_LIGHT_MAPPING) || !defined(USE_DELUXE_MAPPING)
116-
#if !defined(HACK_NO_BSP_GRID_LIGHTDIR)
117-
// Compute light grid position.
118-
vec3 lightGridPos = (var_Position - u_LightGridOrigin) * u_LightGridScale;
119-
#endif
102+
#if defined(USE_GRID_LIGHTING) || defined(USE_GRID_DELUXE_MAPPING)
103+
// Compute light grid position.
104+
vec3 lightGridPos = (var_Position - u_LightGridOrigin) * u_LightGridScale;
120105
#endif
121106

122107
#if defined(USE_DELUXE_MAPPING)
123108
// Compute light direction in world space from deluxe map.
124109
vec4 deluxe = texture2D(u_DeluxeMap, var_TexLight);
125110
vec3 lightDir = normalize(2.0 * deluxe.xyz - 1.0);
126-
#else
127-
#if !defined(HACK_NO_BSP_GRID_LIGHTDIR)
128-
// Compute light direction in world space from light grid.
129-
vec4 texel = texture3D(u_LightGrid2, lightGridPos);
130-
vec3 lightDir = normalize(texel.xyz - (128.0 / 255.0));
131-
#endif
111+
#elif defined(USE_GRID_DELUXE_MAPPING)
112+
// Compute light direction in world space from light grid.
113+
vec4 texel = texture3D(u_LightGrid2, lightGridPos);
114+
vec3 lightDir = normalize(texel.xyz - (128.0 / 255.0));
132115
#endif
133116

134117
#if defined(USE_LIGHT_MAPPING)
135118
// Compute light color from world space lightmap.
136119
vec3 lightColor = texture2D(u_LightMap, var_TexLight).rgb;
137120

138-
#if !defined(HACK_NO_BSP_GRID_LIGHTDIR)
121+
#if defined(USE_DELUXE_MAPPING) || defined(USE_GRID_DELUXE_MAPPING)
139122
color.rgb = vec3(0.0);
140123
#else
141124
color.rgb = lightColor.rgb * diffuse.rgb;
@@ -146,9 +129,13 @@ void main()
146129
ReadLightGrid(texture3D(u_LightGrid1, lightGridPos), ambientColor, lightColor);
147130

148131
color.rgb = ambientColor * r_AmbientScale * diffuse.rgb;
132+
133+
#if !defined(USE_DELUXE_MAPPING) && !defined(USE_GRID_DELUXE_MAPPING)
134+
color.rgb += lightColor.rgb * diffuse.rgb;
135+
#endif
149136
#endif
150137

151-
#if defined(USE_LIGHT_MAPPING)
138+
#if defined(USE_LIGHT_MAPPING) && defined(USE_DELUXE_MAPPING)
152139
/* Lightmaps generated by q3map2 don't store the raw light value, but
153140
they store light premultiplied with the dot product of the light
154141
direction and surface normal. The line is just an attempt to reverse
@@ -170,13 +157,11 @@ void main()
170157
https://github.com/DaemonEngine/Daemon/issues/299#issuecomment-606186347
171158
*/
172159

173-
#if !defined(HACK_NO_BSP_GRID_LIGHTDIR)
174-
// Divide by cosine term to restore original light color.
175-
lightColor /= clamp(dot(normalize(var_Normal), lightDir), 0.3, 1.0);
176-
#endif
160+
// Divide by cosine term to restore original light color.
161+
lightColor /= clamp(dot(normalize(var_Normal), lightDir), 0.3, 1.0);
177162
#endif
178163

179-
#if !defined(HACK_NO_BSP_GRID_LIGHTDIR)
164+
#if defined(USE_DELUXE_MAPPING) || defined(USE_GRID_DELUXE_MAPPING)
180165
// Blend static light.
181166
computeLight(lightDir, normal, viewDir, lightColor, diffuse, material, color);
182167
#endif
@@ -187,7 +172,7 @@ void main()
187172
#endif
188173

189174
// Add Rim Lighting to highlight the edges on model entities.
190-
#if defined(r_rimLighting) && !defined(USE_BSP_SURFACE) && !defined(USE_LIGHT_MAPPING)
175+
#if defined(r_rimLighting) && defined(USE_MODEL_SURFACE) && defined(USE_GRID_LIGHTING)
191176
float rim = pow(1.0 - clamp(dot(normal, viewDir), 0.0, 1.0), r_RimExponent);
192177
vec3 emission = ambientColor * rim * rim * 0.2;
193178
color.rgb += 0.7 * emission;

src/engine/renderer/glsl_source/lightMapping_vp.glsl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222

2323
/* lightMapping_vp.glsl */
2424

25+
#if !defined(USE_BSP_SURFACE)
26+
#define USE_MODEL_SURFACE
27+
#endif
28+
29+
#if !defined(USE_GRID_LIGHTING)
30+
#define USE_LIGHT_MAPPING
31+
#endif
32+
2533
uniform mat4 u_TextureMatrix;
2634

27-
#if !defined(USE_BSP_SURFACE)
35+
#if defined(USE_MODEL_SURFACE)
2836
uniform mat4 u_ModelMatrix;
2937
#endif
3038

0 commit comments

Comments
 (0)