17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 14:29:20 +02:00
Files
maszyna/rendering/openglmatrixstack.h
jerrrrycho 6263fe539b Fix/sonar maintainability rebased (#209)
* refactor(sonar): use override instead of redundant virtual (cpp:S3471)

* refactor(sonar): remove empty statements, one declaration per statement (cpp:S1116, cpp:S1659)

* refactor(sonar): drop redundant lambda return types (cpp:S3574)

* refactor(sonar): prefer using over typedef (cpp:S5416)

* refactor(sonar): use =default for trivial special members (cpp:S3490)

* refactor(sonar): use nullptr literal (cpp:S4962)

* refactor(sonar): remove redundant access specifiers (cpp:S3539)

* refactor(sonar): remove redundant casts (cpp:S1905)

* refactor(sonar): use std::scoped_lock instead of std::lock_guard (cpp:S5997)

* refactor(sonar): construct elements in place with emplace_back (cpp:S6003)

* refactor(sonar): make narrowing conversions explicit (cpp:S5276)

* refactor(sonar): mark unused parameters (cpp:S1172)

Unused names are commented out; parameters used only on some platforms
are marked [[maybe_unused]].

* refactor(sonar): declare non-mutating member functions const (cpp:S5817)

Virtual functions are left untouched so overrides keep matching.

* refactor(sonar): merge collapsible nested if statements (cpp:S1066)

Comments that preceded the inner if are kept above the merged statement.

* refactor(sonar): scope single-use variables in if init-statements (cpp:S6004)

* fix(sonar): give polymorphic base classes virtual destructors (cpp:S1235)

* refactor(sonar): use try_emplace for map insertion (cpp:S6030)

* refactor(sonar): use auto where the type is spelled out on the right (cpp:S5827)

* refactor(sonar): remove redundant parentheses (cpp:S1110)

* refactor(sonar): document intentionally empty functions (cpp:S1186)

* refactor(sonar): remove dead stores (cpp:S1854)

Side effects (e.g. LISTLINE++) are kept.

* refactor(sonar): remove unused variables, captures and private fields (cpp:S1481, cpp:S1068)

* refactor(sonar): use structured bindings for pair access (cpp:S6005)

* refactor(sonar): use range-based loops and ranges algorithms (cpp:S5566)

* refactor(sonar): split chained assignments (cpp:S1121)

* refactor(sonar): add default cases to switch statements (cpp:S3562)

* refactor(sonar): use std::size for array lengths (cpp:S7127)

* refactor(sonar): use std::ranges algorithms and std::erase(_if) (cpp:S6197)

* refactor(sonar): access static members through the class (cpp:S2209)

* refactor(sonar): add braces around nested if-else (cpp:S5261)

* refactor(sonar): avoid side effects inside typeid operands (cpp:S5279)

* refactor(sonar): drop top-level const from by-value return types (cpp:S5951)

* refactor(sonar): declare variables separately from their type definitions (cpp:S3646)

* refactor(sonar): declare loop counters in the for statement (cpp:S5955)

* refactor(sonar): rely on class template argument deduction (cpp:S6012)

* refactor(sonar): inherit base constructors instead of forwarding them (cpp:S5952)

* refactor(sonar): rename locals that shadow outer declarations (cpp:S1117)

Only local variables and definition parameters are renamed; shadowing
struct fields and enumerators in headers are left for a separate change.

* refactor(sonar): reduce enum qualification with using enum (cpp:S6177)

Applied only where no unqualified name in the block could be captured by
the introduced enumerators.

* refactor(sonar): mark converting constructors explicit (cpp:S1709)

sound_source and the gl::object conversion operators stay implicit; they
are relied upon by copy-list-initialisation and GL calls.

* refactor(sonar): initialise members in initialiser lists or declarations (cpp:S3230)

* refactor(sonar): make read-only local references and pointers const (cpp:S5350)

* refactor(sonar): take read-only pointer and reference parameters as const (cpp:S995)

TTrain command handlers keep their signature, it is fixed by the handler table.

* refactor(sonar): pass expensive-to-copy parameters by const reference (cpp:S1238)

Signatures declared in files touched by PR 206 (Driver.h, EU07.cpp) and
virtual overrides are left unchanged.

* refactor(sonar): use transparent comparators for string-keyed maps (cpp:S6045)

std::unordered_map instances and map types exposed in headers used by
other modules are left as they are.

* fix(sonar): do not use UI text as a printf format string (cpp:S5281)

Text lines containing '%' would otherwise be interpreted as format
specifiers by ImGui::TextColored.

* refactor(sonar): use std::bind_front for callbacks (cpp:S5995)

* refactor(sonar): drop const_cast on string literals passed to the Python C API (cpp:S859)

* refactor(sonar): take read-only string parameters as std::string_view (cpp:S6009)

The Windows messaging helpers keep std::string, they need c_str().

* refactor(sonar): make read-only global tables const (cpp:S5421)

Mutable global state (flags, timers, singletons) is left as is.

* refactor(sonar): replace single-case switch statements with if (cpp:S1301)

* refactor(sonar): format UUIDs and log timestamps with std::format (cpp:S6494, cpp:S5945)

* refactor(sonar): use std::array for local fixed-size arrays (cpp:S5945)

Arrays that are class members or part of headers are left unchanged.

* fix(sonar): test the bearing damage bit, not bit 0 (cpp:S872)

'DamageFlag & dtrain_bearing > 0' parsed as 'DamageFlag & (dtrain_bearing > 0)',
i.e. DamageFlag & 1, so bearing friction reacted to the wrong damage flag.

* refactor(sonar): assorted small fixes

- empty() instead of size() comparisons (cpp:S1155)
- member initialisers in declaration order (cpp:S3229)
- no redundant c_str() when a std::string is accepted (cpp:S7121)
- contains() on associative containers (cpp:S6171)
- const reference instead of a copying range-for (cpp:S6032)
- starts_with() for prefix checks (cpp:S6178)
- no std::move on temporaries (cpp:S5274)
- has_value() on std::optional (cpp:S7172)
- bool literals for bool members (cpp:S5820)
- inline only on the definition (cpp:S3548)
- explicit base access specifier (cpp:S5965)
- explicit element access instead of dereferencing an array (cpp:S7116)
- rely on the C++20 rewritten operator!= (cpp:S6186)

* refactor(sonar): more small fixes

- std::format instead of iostream manipulators in to_string/to_hex_str (cpp:S6495);
  output verified identical, negative hex values keep the unsigned form
- remove an empty compound statement (cpp:S108)
- drop an always-false unsigned < 0 branch (cpp:S1768)
- explicit lambda capture (cpp:S3608)
- std::make_unique (cpp:S5950)

* chore: strip stray carriage returns left in TTrain constructor comments

* fix: keep quantize() in its own value type

The S5276 change cast the result of the generic quantize<Type_>() to
unsigned int, which truncated float/double callers (shadow angle slider,
editor rotation snapping, AI speed control steps) and broke negative values.

* fix(sonar): use integer loop counters in segment sampling (cpp:S2193)

* refactor(sonar): clean up issues reported on PR 209

- remove locals left unused after the dead-store cleanup (cpp:S1481, cpp:S1854)
- unnamed unused parameter (cpp:S1172)
- single-use declarations moved into if init-statements (cpp:S6004)
- auto for casted initialisers (cpp:S5827)
- const references and pointers where nothing is modified (cpp:S5350)
- rename structured bindings shadowed by inner declarations (cpp:S1117)
2026-10-11 13:22:33 +02:00

256 lines
9.4 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <stack>
#include <vector>
// encapsulation of the fixed pipeline opengl matrix stack
class opengl_matrices {
// types:
class opengl_stack {
public:
// constructors:
opengl_stack() {
// reserve generously up front: the matrix stack is pushed/popped once
// per submodel during scene traversal, and std::deque (the previous
// backing store) allocated a fresh heap block on every push for a
// 64-byte glm::mat4. A reserved vector never reallocates within this
// depth, so push/pop become allocation-free and references returned
// by data() stay valid across pushes, exactly as before.
m_stack.reserve( 256 );
m_stack.emplace_back( 1.f ); }
// methods:
glm::mat4 const &
data() const {
return m_stack.back(); }
void
push_matrix() {
glm::mat4 const top { m_stack.back() };
m_stack.emplace_back( top ); }
void
pop_matrix( bool const Upload = true ) {
if( m_stack.size() > 1 ) {
m_stack.pop_back();
if( Upload ) { upload(); } } }
void
load_identity( bool const Upload = true ) {
m_stack.back() = glm::mat4( 1.f );
if( Upload ) { upload(); } }
void
load_matrix( glm::mat4 const &Matrix, bool const Upload = true ) {
m_stack.back() = Matrix;
if( Upload ) { upload(); } }
void
rotate( float const Angle, glm::vec3 const &Axis, bool const Upload = true ) {
m_stack.back() = glm::rotate( m_stack.back(), Angle, Axis );
if( Upload ) { upload(); } }
void
translate( glm::vec3 const &Translation, bool const Upload = true ) {
m_stack.back() = glm::translate( m_stack.back(), Translation );
if( Upload ) { upload(); } }
void
scale( glm::vec3 const &Scale, bool const Upload = true ) {
m_stack.back() = glm::scale( m_stack.back(), Scale );
if( Upload ) { upload(); } }
void
multiply( glm::mat4 const &Matrix, bool const Upload = true ) {
m_stack.back() *= Matrix;
if( Upload ) { upload(); } }
void
ortho( float const Left, float const Right, float const Bottom, float const Top, float const Znear, float const Zfar, bool const Upload = true ) {
m_stack.back() *= glm::ortho( Left, Right, Bottom, Top, Znear, Zfar );
if( Upload ) { upload(); } }
void
perspective( float const Fovy, float const Aspect, float const Znear, float const Zfar, bool const Upload = true ) {
m_stack.back() *= glm::perspective( Fovy, Aspect, Znear, Zfar );
if( Upload ) { upload(); } }
void
look_at( glm::vec3 const &Eye, glm::vec3 const &Center, glm::vec3 const &Up, bool const Upload = true ) {
m_stack.back() *= glm::lookAt( Eye, Center, Up );
if( Upload ) { upload(); } }
private:
// methods:
void
upload() { ::glLoadMatrixf( glm::value_ptr( m_stack.back() ) ); }
// members:
std::vector<glm::mat4> m_stack;
};
enum stack_mode { gl_modelview = 0, gl_projection = 1, gl_texture = 2 };
using openglstack_array = std::vector<opengl_stack>;
public:
// constructors:
opengl_matrices() {
m_stacks.emplace_back(); // modelview
m_stacks.emplace_back(); // projection
m_stacks.emplace_back(); // texture
}
// methods:
bool &
upload() {
return m_upload; }
void
mode( GLuint const Mode ) {
switch( Mode ) {
case GL_MODELVIEW: { m_mode = stack_mode::gl_modelview; break; }
case GL_PROJECTION: { m_mode = stack_mode::gl_projection; break; }
case GL_TEXTURE: { m_mode = stack_mode::gl_texture; break; }
default: { break; } }
if( m_upload ) {::glMatrixMode( Mode ); } }
glm::mat4 const &
data( GLuint const Mode = -1 ) const {
switch( Mode ) {
case GL_MODELVIEW: { return m_stacks[ stack_mode::gl_modelview ].data(); }
case GL_PROJECTION: { return m_stacks[ stack_mode::gl_projection ].data(); }
case GL_TEXTURE: { return m_stacks[ stack_mode::gl_texture ].data(); }
default: { return m_stacks[ m_mode ].data(); } } }
float const *
data_array( GLuint const Mode = -1 ) const {
return glm::value_ptr( data( Mode ) ); }
void
push_matrix() { m_stacks[ m_mode ].push_matrix(); }
void
pop_matrix() { m_stacks[ m_mode ].pop_matrix( m_upload ); }
void
load_identity() { m_stacks[ m_mode ].load_identity( m_upload ); }
void
load_matrix( glm::mat4 const &Matrix ) { m_stacks[ m_mode ].load_matrix( Matrix, m_upload ); }
template <typename Type_>
void
load_matrix( Type_ const *Matrix ) { load_matrix( glm::make_mat4( Matrix ) ); }
template <typename Type_>
void
rotate( Type_ const Angle, Type_ const X, Type_ const Y, Type_ const Z ) {
m_stacks[ m_mode ].rotate(
static_cast<float>(glm::radians(Angle)),
glm::vec3(
static_cast<float>( X ),
static_cast<float>( Y ),
static_cast<float>( Z ) ),
m_upload ); }
template <typename Type_>
void
translate( Type_ const X, Type_ const Y, Type_ const Z ) {
m_stacks[ m_mode ].translate(
glm::vec3(
static_cast<float>( X ),
static_cast<float>( Y ),
static_cast<float>( Z ) ),
m_upload ); }
template <typename Type_>
void
scale( Type_ const X, Type_ const Y, Type_ const Z ) {
m_stacks[ m_mode ].scale(
glm::vec3(
static_cast<float>( X ),
static_cast<float>( Y ),
static_cast<float>( Z ) ),
m_upload ); }
template <typename Type_>
void
multiply( Type_ const *Matrix ) {
m_stacks[ m_mode ].multiply(
glm::make_mat4( Matrix ),
m_upload ); }
template <typename Type_>
void
ortho( Type_ const Left, Type_ const Right, Type_ const Bottom, Type_ const Top, Type_ const Znear, Type_ const Zfar ) {
m_stacks[ m_mode ].ortho(
static_cast<float>( Left ),
static_cast<float>( Right ),
static_cast<float>( Bottom ),
static_cast<float>( Top ),
static_cast<float>( Znear ),
static_cast<float>( Zfar ),
m_upload ); }
template <typename Type_>
void
perspective( Type_ const Fovy, Type_ const Aspect, Type_ const Znear, Type_ const Zfar ) {
m_stacks[ m_mode ].perspective(
static_cast<float>( glm::radians( Fovy ) ),
static_cast<float>( Aspect ),
static_cast<float>( Znear ),
static_cast<float>( Zfar ),
m_upload ); }
template <typename Type_>
void
look_at( Type_ const Eyex, Type_ const Eyey, Type_ const Eyez, Type_ const Centerx, Type_ const Centery, Type_ const Centerz, Type_ const Upx, Type_ const Upy, Type_ const Upz ) {
m_stacks[ m_mode ].look_at(
glm::vec3(
static_cast<float>( Eyex ),
static_cast<float>( Eyey ),
static_cast<float>( Eyez ) ),
glm::vec3(
static_cast<float>( Centerx ),
static_cast<float>( Centery ),
static_cast<float>( Centerz ) ),
glm::vec3(
static_cast<float>( Upx ),
static_cast<float>( Upy ),
static_cast<float>( Upz ) ),
m_upload ); }
private:
// members:
stack_mode m_mode{ stack_mode::gl_projection };
openglstack_array m_stacks;
bool m_upload { true };
};
extern opengl_matrices OpenGLMatrices;
// NOTE: standard opengl calls re-definitions
#undef glMatrixMode
#undef glPushMatrix
#undef glPopMatrix
#undef glLoadIdentity
#undef glLoadMatrixf
#undef glLoadMatrixd
#undef glRotated
#undef glRotatef
#undef glTranslated
#undef glTranslatef
#undef glScaled
#undef glScalef
#undef glMultMatrixd
#undef glMultMatrixf
#undef glOrtho
#undef gluPerspective
#undef gluLookAt
#define glMatrixMode OpenGLMatrices.mode
#define glPushMatrix OpenGLMatrices.push_matrix
#define glPopMatrix OpenGLMatrices.pop_matrix
#define glLoadIdentity OpenGLMatrices.load_identity
#define glLoadMatrixf OpenGLMatrices.load_matrix
#define glLoadMatrixd OpenGLMatrices.load_matrix
#define glRotated OpenGLMatrices.rotate
#define glRotatef OpenGLMatrices.rotate
#define glTranslated OpenGLMatrices.translate
#define glTranslatef OpenGLMatrices.translate
#define glScaled OpenGLMatrices.scale
#define glScalef OpenGLMatrices.scale
#define glMultMatrixd OpenGLMatrices.multiply
#define glMultMatrixf OpenGLMatrices.multiply
#define glOrtho OpenGLMatrices.ortho
#define gluPerspective OpenGLMatrices.perspective
#define gluLookAt OpenGLMatrices.look_at
//---------------------------------------------------------------------------