refactor: state into namespace

This commit is contained in:
2025-10-13 17:17:28 +02:00
parent 4e2c62aaf4
commit 74a0bfca41
9 changed files with 19 additions and 27 deletions

View File

@@ -13,7 +13,7 @@ Camera::Camera()
void Camera::prepare() void Camera::prepare()
{ {
// Set projection matrix // Set projection matrix
State::projection_matrix_ = projection_; state::projection_matrix = projection_;
// Calculate view matrix // Calculate view matrix
// For a camera, we need the inverse transformation: // For a camera, we need the inverse transformation:
@@ -29,7 +29,7 @@ void Camera::prepare()
// Inverse translation (translate in opposite direction) // Inverse translation (translate in opposite direction)
view = glm::translate(view, -position_); view = glm::translate(view, -position_);
State::view_matrix_ = view; state::view_matrix = view;
// Set viewport // Set viewport
glViewport(viewport_.x, viewport_.y, viewport_.z, viewport_.w); glViewport(viewport_.x, viewport_.y, viewport_.z, viewport_.w);

View File

@@ -75,11 +75,11 @@ void Engine::initialize()
} }
// Initialize default shader // Initialize default shader
State::default_shader_ = state::default_shader =
std::make_shared<Shader>("data/vertex.glsl", "data/fragment.glsl"); std::make_shared<Shader>("data/vertex.glsl", "data/fragment.glsl");
if (std::strlen(State::default_shader_->error()) > 0) { if (std::strlen(state::default_shader->error()) > 0) {
std::cerr << "Failed to initialize shaders: " std::cerr << "Failed to initialize shaders: "
<< State::default_shader_->error() << "\n"; << state::default_shader->error() << "\n";
} }
} }

View File

@@ -4,6 +4,7 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "../lib/glew/GL/glew.h"
#include "../lib/glfw/glfw3.h" #include "../lib/glfw/glfw3.h"
class World; class World;

View File

@@ -13,15 +13,15 @@ void Mesh::add_buffer(const std::shared_ptr<Buffer>& buffer,
void Mesh::draw() void Mesh::draw()
{ {
// Calculate MVP matrix // Calculate MVP matrix
glm::mat4 mvp = State::projection_matrix_ * State::view_matrix_ glm::mat4 mvp = state::projection_matrix * state::view_matrix
* State::model_matrix_; * state::model_matrix;
// Draw each buffer with its shader // Draw each buffer with its shader
for (size_t i = 0; i < buffers_.size(); ++i) { for (size_t i = 0; i < buffers_.size(); ++i) {
// Use buffer's shader if available, otherwise use default // Use buffer's shader if available, otherwise use default
// shader // shader
std::shared_ptr<Shader> shader = std::shared_ptr<Shader> shader =
shaders_[i] ? shaders_[i] : State::default_shader_; shaders_[i] ? shaders_[i] : state::default_shader;
if (shader) { if (shader) {
shader->use(); shader->use();

View File

@@ -27,8 +27,8 @@ void Model::draw()
// Scale // Scale
model = glm::scale(model, scale_); model = glm::scale(model, scale_);
// Set the model matrix in State // Set the model matrix in state
State::model_matrix_ = model; state::model_matrix = model;
// Draw the mesh // Draw the mesh
mesh_->draw(); mesh_->draw();

View File

@@ -1,6 +0,0 @@
#include "state.h"
std::shared_ptr<Shader> State::default_shader_ = nullptr;
glm::mat4 State::projection_matrix_ = glm::mat4(1.0f);
glm::mat4 State::view_matrix_ = glm::mat4(1.0f);
glm::mat4 State::model_matrix_ = glm::mat4(1.0f);

View File

@@ -6,12 +6,13 @@
class Shader; class Shader;
class State { namespace state {
public:
static std::shared_ptr<Shader> default_shader_; inline std::shared_ptr<Shader> default_shader = nullptr;
static glm::mat4 projection_matrix_; inline glm::mat4 projection_matrix = glm::mat4(1.0f);
static glm::mat4 view_matrix_; inline glm::mat4 view_matrix = glm::mat4(1.0f);
static glm::mat4 model_matrix_; inline glm::mat4 model_matrix = glm::mat4(1.0f);
};
} // namespace state
#endif // STATE_H_ #endif // STATE_H_

View File

@@ -180,7 +180,6 @@
<ClCompile Include="src\mesh.cpp" /> <ClCompile Include="src\mesh.cpp" />
<ClCompile Include="src\model.cpp" /> <ClCompile Include="src\model.cpp" />
<ClCompile Include="src\shader.cpp" /> <ClCompile Include="src\shader.cpp" />
<ClCompile Include="src\state.cpp" />
<ClCompile Include="src\vertex.cpp" /> <ClCompile Include="src\vertex.cpp" />
<ClCompile Include="src\world.cpp" /> <ClCompile Include="src\world.cpp" />
</ItemGroup> </ItemGroup>

View File

@@ -80,9 +80,6 @@
<ClCompile Include="src\buffer.cpp"> <ClCompile Include="src\buffer.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\state.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\mesh.cpp"> <ClCompile Include="src\mesh.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>