From 8f64a27ba65ca5d558c812ef911a00a79d919259 Mon Sep 17 00:00:00 2001
From: pwarszawski
Date: Sat, 14 Nov 2026 14:42:29 +0100
Subject: [PATCH 1/2] Derive flat normals for scenery shapes with zero normals
Legacy scenery triangles (e.g. pkp/szafa-przek1.inc) are often authored
with 0 0 0 normals. normalize() of a zero vector yields NaN in the
shaders, and since gl46 ambient/envmap/sky-occlusion terms all depend on
the normal, such surfaces render completely black. Fill missing normals
with the triangle face normal on text import and when loading binary
terrain caches.
---
scene/scenenode.cpp | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/scene/scenenode.cpp b/scene/scenenode.cpp
index 426f0e077..0caa62930 100644
--- a/scene/scenenode.cpp
+++ b/scene/scenenode.cpp
@@ -87,6 +87,39 @@ shape_node::shapenode_data::serialize( std::ostream &Output ) const {
}
}
+namespace {
+
+// legacy scenery shapes frequently come with all-zero normals (e.g. pkp/szafa-przek1.inc).
+// the shaders normalize the interpolated normal, which turns a zero vector into NaN; with the gl46
+// lighting (ambient sh, envmap, sky occlusion all depend on the normal) that blackens the whole surface.
+// replace missing normals with the flat normal of the triangle, using the same winding as authored data
+void
+fill_missing_normals( std::vector &Vertices ) {
+
+ for( std::size_t idx = 0; idx + 2 < Vertices.size(); idx += 3 ) {
+ auto *triangle { &Vertices[ idx ] };
+ if( ( glm::length2( triangle[ 0 ].normal ) > 1e-12f )
+ && ( glm::length2( triangle[ 1 ].normal ) > 1e-12f )
+ && ( glm::length2( triangle[ 2 ].normal ) > 1e-12f ) ) {
+ continue;
+ }
+ auto const facenormal { glm::cross(
+ triangle[ 1 ].position - triangle[ 0 ].position,
+ triangle[ 2 ].position - triangle[ 0 ].position ) };
+ auto const normal { (
+ glm::length2( facenormal ) > 0.0 ?
+ glm::vec3{ glm::normalize( facenormal ) } :
+ glm::vec3{ 0.f, 1.f, 0.f } ) };
+ for( int vertexidx = 0; vertexidx < 3; ++vertexidx ) {
+ if( glm::length2( triangle[ vertexidx ].normal ) <= 1e-12f ) {
+ triangle[ vertexidx ].normal = normal;
+ }
+ }
+ }
+}
+
+} // namespace
+
// restores content of the struct from provided input stream
void
shape_node::shapenode_data::deserialize( std::istream &Input ) {
@@ -118,6 +151,8 @@ shape_node::shapenode_data::deserialize( std::istream &Input ) {
if(has_userdata)
userdata[i].deserialize( Input );
}
+ // binary caches created by older builds may still hold the zero normals
+ fill_missing_normals( vertices );
}
@@ -312,6 +347,8 @@ shape_node::import( cParser &Input, scene::node_data const &Nodedata ) {
} while( token != "endtri"
&& false == token.empty() ); // data which ends without closing the node would otherwise keep the loop going forever
+ fill_missing_normals( m_data.vertices );
+
return *this;
}
From 22c41c023ba0d308b8a188c2d9b6a76e38dfde12 Mon Sep 17 00:00:00 2001
From: pwarszawski
Date: Sun, 22 Nov 2026 11:46:37 +0100
Subject: [PATCH 2/2] Fill zero normals of terrain loaded from .btf files
A .btf file written by an older build keeps the zero normals of its
text, and it isn't rebuilt while it is newer than the text. Its reader
builds the shapes on its own, so it fills them the same way as the
text and the .sbt readers.
---
scene/scenenode.cpp | 4 ----
scene/scenenode.h | 3 +++
scene/sceneterrain.cpp | 2 ++
3 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/scene/scenenode.cpp b/scene/scenenode.cpp
index 0caa62930..d98c83092 100644
--- a/scene/scenenode.cpp
+++ b/scene/scenenode.cpp
@@ -87,8 +87,6 @@ shape_node::shapenode_data::serialize( std::ostream &Output ) const {
}
}
-namespace {
-
// legacy scenery shapes frequently come with all-zero normals (e.g. pkp/szafa-przek1.inc).
// the shaders normalize the interpolated normal, which turns a zero vector into NaN; with the gl46
// lighting (ambient sh, envmap, sky occlusion all depend on the normal) that blackens the whole surface.
@@ -118,8 +116,6 @@ fill_missing_normals( std::vector &Vertices ) {
}
}
-} // namespace
-
// restores content of the struct from provided input stream
void
shape_node::shapenode_data::deserialize( std::istream &Input ) {
diff --git a/scene/scenenode.h b/scene/scenenode.h
index f10ac0d16..f1c9c43ae 100644
--- a/scene/scenenode.h
+++ b/scene/scenenode.h
@@ -74,6 +74,9 @@ struct node_data {
instance_handle instance { 0 }; // include of an *.inc template which defines the node, if it isn't defined directly by a scenery layer file
};
+// gives the vertices of each triangle with a zero normal the flat normal of the triangle, by its authored winding
+void fill_missing_normals( std::vector &Vertices );
+
// holds unique piece of geometry, covered with single material
class shape_node
{
diff --git a/scene/sceneterrain.cpp b/scene/sceneterrain.cpp
index d0453f2b3..044300812 100644
--- a/scene/sceneterrain.cpp
+++ b/scene/sceneterrain.cpp
@@ -1130,6 +1130,8 @@ void terrain_file::load(basic_section &Section, terrain_block const &Block)
vertex.texture.t = std::clamp(vertex.texture.t, 0.001f, 0.999f);
}
}
+ // a file written by an older build can hold the zero normals of its text
+ fill_missing_normals(shapedata.vertices);
if (true == shapedata.translucent || shapedata.rangesquared_max <= 90000.0 || shapedata.rangesquared_min > 0.0)
{