From 14a85917477b1351e10adfa85dae7818c7da79c0 Mon Sep 17 00:00:00 2001 From: idontlikegits Date: Tue, 10 Mar 2026 16:25:01 +0000 Subject: [PATCH] FIX: matrix3 identity and zero static --- G3Dcpp/Matrix4.cpp | 36 +++++++++++++++++++----------------- include/G3D/Matrix3.h | 23 ++++++++++++++++++++--- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/G3Dcpp/Matrix4.cpp b/G3Dcpp/Matrix4.cpp index 99ba501..c29f93a 100644 --- a/G3Dcpp/Matrix4.cpp +++ b/G3Dcpp/Matrix4.cpp @@ -17,23 +17,25 @@ namespace G3D { -const Matrix4& Matrix4::identity() { - static Matrix4 m( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1); - return m; -} - -const Matrix4& Matrix4::zero() { - static Matrix4 m( - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0); - return m; -} +// *** Read Matrix.h comment regarding identity and zero *** + +// const Matrix4& Matrix4::identity() { +// static Matrix4 m( +// 1, 0, 0, 0, +// 0, 1, 0, 0, +// 0, 0, 1, 0, +// 0, 0, 0, 1); +// return m; +// } + +// const Matrix4& Matrix4::zero() { +// static Matrix4 m( +// 0, 0, 0, 0, +// 0, 0, 0, 0, +// 0, 0, 0, 0, +// 0, 0, 0, 0); +// return m; +// } // Deprecated. const Matrix4 Matrix4::IDENTITY diff --git a/include/G3D/Matrix3.h b/include/G3D/Matrix3.h index 213f752..30b906f 100644 --- a/include/G3D/Matrix3.h +++ b/include/G3D/Matrix3.h @@ -246,6 +246,11 @@ class Matrix3 { static const float EPSILON; + // The following comment is wrong. Modern linkers are smart enough to deduplicate + // multiple static objects into a single memory address, as long as any function with + // a static local variable is marked inline. + + // *** Original G3D comment *** // Special values. // The unguaranteed order of initialization of static variables across // translation units can be a source of annoying bugs, so now the static @@ -255,7 +260,7 @@ class Matrix3 { // "You might be tempted to write [...] them as inline functions // inside their respective header files, but this is something you // must definitely not do. An inline function can be duplicated - // in every file in which it appears � and this duplication + // in every file in which it appears � and this duplication // includes the static object definition. Because inline functions // automatically default to internal linkage, this would result in // having multiple static objects across the various translation @@ -264,8 +269,20 @@ class Matrix3 { // function, and this means not making the wrapping functions inline", // according to Chapter 10 of "Thinking in C++, 2nd ed. Volume 1" by Bruce Eckel, // http://www.mindview.net/ - static const Matrix3& zero(); - static const Matrix3& identity(); + // *** End of G3D comment *** + + static const Matrix3& zero() { + static Matrix3 m(0, 0, 0, 0, 0, 0, 0, 0, 0); + return m; + } + + static const Matrix3& identity() { + static Matrix3 m(1, 0, 0, 0, 1, 0, 0, 0, 1); + return m; + } + + // static const Matrix3& zero(); + // static const Matrix3& identity(); // Deprecated. /** @deprecated Use Matrix3::zero() */