refactor(renderer): 清理代码并优化结构

- 移除renderer_module.cpp中注释掉的代码
- 重新组织director.cpp的代码格式和结构
- 简化shader.cpp的日志输出和代码格式
- 添加glad头文件包含
This commit is contained in:
ChestnutYueyue 2026-03-02 04:57:44 +08:00
parent 92be7d9d18
commit 3b827149ba
3 changed files with 273 additions and 299 deletions

View File

@ -1,3 +1,4 @@
#include "glad/glad.h"
#include <algorithm> #include <algorithm>
#include <event/events.h> #include <event/events.h>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
@ -142,9 +143,6 @@ void RendererModule::onWindowShow() {
setViewport(0, 0, static_cast<int32>(windowWidth), setViewport(0, 0, static_cast<int32>(windowWidth),
static_cast<int32>(windowHeight)); static_cast<int32>(windowHeight));
// 禁用深度测试和背面剔除2D渲染不需要
// glDisable(GL_DEPTH_TEST);
// glDisable(GL_CULL_FACE);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

View File

@ -1,8 +1,8 @@
#include <renderer/shader.h>
#include <utils/logger.h>
#include <glad/glad.h>
#include <fstream> #include <fstream>
#include <glad/glad.h>
#include <renderer/shader.h>
#include <sstream> #include <sstream>
#include <utils/logger.h>
namespace extra2d { namespace extra2d {
@ -15,7 +15,8 @@ Shader::~Shader() {
} }
} }
bool Shader::loadFromFile(const std::string& vsPath, const std::string& fsPath) { bool Shader::loadFromFile(const std::string &vsPath,
const std::string &fsPath) {
// 读取顶点着色器 // 读取顶点着色器
std::ifstream vsFile(vsPath); std::ifstream vsFile(vsPath);
if (!vsFile.is_open()) { if (!vsFile.is_open()) {
@ -39,7 +40,8 @@ bool Shader::loadFromFile(const std::string& vsPath, const std::string& fsPath)
return loadFromSource(vsSource, fsSource); return loadFromSource(vsSource, fsSource);
} }
bool Shader::loadFromSource(const std::string& vsSource, const std::string& fsSource) { bool Shader::loadFromSource(const std::string &vsSource,
const std::string &fsSource) {
// 删除旧程序 // 删除旧程序
if (program_ != 0) { if (program_ != 0) {
glDeleteProgram(program_); glDeleteProgram(program_);
@ -92,12 +94,11 @@ void Shader::bind() const {
} }
} }
void Shader::unbind() const { void Shader::unbind() const { glUseProgram(0); }
glUseProgram(0);
}
void Shader::setUniformBlock(const std::string &name, uint32_t binding) { void Shader::setUniformBlock(const std::string &name, uint32_t binding) {
if (program_ == 0) return; if (program_ == 0)
return;
GLuint index = glGetUniformBlockIndex(program_, name.c_str()); GLuint index = glGetUniformBlockIndex(program_, name.c_str());
if (index != GL_INVALID_INDEX) { if (index != GL_INVALID_INDEX) {
@ -126,7 +127,8 @@ void Shader::setVec2(const std::string& name, float x, float y) {
} }
} }
void Shader::setVec4(const std::string& name, float x, float y, float z, float w) { void Shader::setVec4(const std::string &name, float x, float y, float z,
float w) {
GLint location = getUniformLocation(name); GLint location = getUniformLocation(name);
if (location != -1) { if (location != -1) {
glUniform4f(location, x, y, z, w); glUniform4f(location, x, y, z, w);
@ -137,19 +139,6 @@ void Shader::setMat4(const std::string& name, const float* value) {
GLint location = getUniformLocation(name); GLint location = getUniformLocation(name);
if (location != -1) { if (location != -1) {
glUniformMatrix4fv(location, 1, GL_FALSE, value); glUniformMatrix4fv(location, 1, GL_FALSE, value);
// 调试输出设置成功的uniform
static int logCount = 0;
if (logCount < 3) {
E2D_LOG_INFO("Set uniform '{}' at location {}", name, location);
logCount++;
}
} else {
// 调试输出未找到的uniform
static bool loggedOnce = false;
if (!loggedOnce) {
E2D_LOG_WARN("Uniform '{}' not found in shader", name);
loggedOnce = true;
}
} }
} }
@ -200,7 +189,8 @@ bool Shader::linkProgram(GLuint vertexShader, GLuint fragmentShader) {
} }
GLint Shader::getUniformLocation(const std::string &name) { GLint Shader::getUniformLocation(const std::string &name) {
if (program_ == 0) return -1; if (program_ == 0)
return -1;
// 检查缓存 // 检查缓存
auto it = uniformCache_.find(name); auto it = uniformCache_.find(name);

View File

@ -1,20 +1,15 @@
#include <scene/director.h>
#include <scene/components/camera_component.h>
#include <event/events.h> #include <event/events.h>
#include <scene/components/camera_component.h>
#include <scene/director.h>
#include <utils/logger.h> #include <utils/logger.h>
namespace extra2d { namespace extra2d {
Director::Director() { Director::Director() {}
}
Director::~Director() { Director::~Director() { shutdown(); }
shutdown();
}
bool Director::init() { bool Director::init() { return true; }
return true;
}
void Director::shutdown() { void Director::shutdown() {
// 结束当前场景 // 结束当前场景
@ -32,7 +27,8 @@ void Director::shutdown() {
} }
void Director::runScene(Ptr<Scene> scene) { void Director::runScene(Ptr<Scene> scene) {
if (!scene) return; if (!scene)
return;
// 结束当前场景 // 结束当前场景
if (runningScene_) { if (runningScene_) {
@ -52,7 +48,8 @@ void Director::runScene(Ptr<Scene> scene) {
} }
void Director::replaceScene(Ptr<Scene> scene) { void Director::replaceScene(Ptr<Scene> scene) {
if (!scene) return; if (!scene)
return;
// 结束当前场景 // 结束当前场景
if (runningScene_) { if (runningScene_) {
@ -65,7 +62,8 @@ void Director::replaceScene(Ptr<Scene> scene) {
} }
void Director::pushScene(Ptr<Scene> scene) { void Director::pushScene(Ptr<Scene> scene) {
if (!scene) return; if (!scene)
return;
// 暂停当前场景 // 暂停当前场景
if (runningScene_) { if (runningScene_) {
@ -108,9 +106,7 @@ void Director::end() {
} }
} }
Scene* Director::getRunningScene() const { Scene *Director::getRunningScene() const { return runningScene_.get(); }
return runningScene_.get();
}
CameraComponent *Director::getMainCamera() const { CameraComponent *Director::getMainCamera() const {
if (runningScene_) { if (runningScene_) {
@ -120,31 +116,21 @@ CameraComponent* Director::getMainCamera() const {
} }
void Director::update(float dt) { void Director::update(float dt) {
if (!running_ || !runningScene_) return; if (!running_ || !runningScene_)
return;
runningScene_->update(dt); runningScene_->update(dt);
} }
void Director::render() { void Director::render() {
if (!running_ || !runningScene_) return; if (!running_ || !runningScene_)
return;
// 从场景获取相机并上传矩阵 // 从场景获取相机并上传矩阵
CameraComponent *camera = runningScene_->getMainCamera(); CameraComponent *camera = runningScene_->getMainCamera();
if (camera) { if (camera) {
Mat4 viewProj = camera->getViewProjectionMatrix(); Mat4 viewProj = camera->getViewProjectionMatrix();
// 调试输出:打印视图投影矩阵
static bool logged = false;
if (!logged) {
const float* m = glm::value_ptr(viewProj);
E2D_LOG_INFO("ViewProjection Matrix:");
E2D_LOG_INFO(" {:.4f} {:.4f} {:.4f} {:.4f}", m[0], m[4], m[8], m[12]);
E2D_LOG_INFO(" {:.4f} {:.4f} {:.4f} {:.4f}", m[1], m[5], m[9], m[13]);
E2D_LOG_INFO(" {:.4f} {:.4f} {:.4f} {:.4f}", m[2], m[6], m[10], m[14]);
E2D_LOG_INFO(" {:.4f} {:.4f} {:.4f} {:.4f}", m[3], m[7], m[11], m[15]);
logged = true;
}
events::OnRenderSetCamera::emit(viewProj); events::OnRenderSetCamera::emit(viewProj);
} }