refactor(renderer): 清理代码并优化结构
- 移除renderer_module.cpp中注释掉的代码 - 重新组织director.cpp的代码格式和结构 - 简化shader.cpp的日志输出和代码格式 - 添加glad头文件包含
This commit is contained in:
parent
92be7d9d18
commit
3b827149ba
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
@ -105,58 +106,46 @@ void Shader::setUniformBlock(const std::string& name, uint32_t binding) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setInt(const std::string& name, int value) {
|
void Shader::setInt(const std::string &name, int value) {
|
||||||
GLint location = getUniformLocation(name);
|
GLint location = getUniformLocation(name);
|
||||||
if (location != -1) {
|
if (location != -1) {
|
||||||
glUniform1i(location, value);
|
glUniform1i(location, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setFloat(const std::string& name, float value) {
|
void Shader::setFloat(const std::string &name, float value) {
|
||||||
GLint location = getUniformLocation(name);
|
GLint location = getUniformLocation(name);
|
||||||
if (location != -1) {
|
if (location != -1) {
|
||||||
glUniform1f(location, value);
|
glUniform1f(location, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setVec2(const std::string& name, float x, float y) {
|
void Shader::setVec2(const std::string &name, float x, float y) {
|
||||||
GLint location = getUniformLocation(name);
|
GLint location = getUniformLocation(name);
|
||||||
if (location != -1) {
|
if (location != -1) {
|
||||||
glUniform2f(location, x, y);
|
glUniform2f(location, x, 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setMat4(const std::string& name, const float* value) {
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GLuint Shader::compileShader(GLenum type, const std::string& source) {
|
GLuint Shader::compileShader(GLenum type, const std::string &source) {
|
||||||
GLuint shader = glCreateShader(type);
|
GLuint shader = glCreateShader(type);
|
||||||
|
|
||||||
const char* src = source.c_str();
|
const char *src = source.c_str();
|
||||||
glShaderSource(shader, 1, &src, nullptr);
|
glShaderSource(shader, 1, &src, nullptr);
|
||||||
glCompileShader(shader);
|
glCompileShader(shader);
|
||||||
|
|
||||||
|
|
@ -167,7 +156,7 @@ GLuint Shader::compileShader(GLenum type, const std::string& source) {
|
||||||
char infoLog[512];
|
char infoLog[512];
|
||||||
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
|
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
|
||||||
|
|
||||||
const char* typeStr = (type == GL_VERTEX_SHADER) ? "vertex" : "fragment";
|
const char *typeStr = (type == GL_VERTEX_SHADER) ? "vertex" : "fragment";
|
||||||
E2D_LOG_ERROR("{} shader compilation failed: {}", typeStr, infoLog);
|
E2D_LOG_ERROR("{} shader compilation failed: {}", typeStr, infoLog);
|
||||||
|
|
||||||
glDeleteShader(shader);
|
glDeleteShader(shader);
|
||||||
|
|
@ -199,8 +188,9 @@ bool Shader::linkProgram(GLuint vertexShader, GLuint fragmentShader) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
@ -215,7 +205,7 @@ GLint Shader::getUniformLocation(const std::string& name) {
|
||||||
return location;
|
return location;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Shader::addVersionIfNeeded(const std::string& source, GLenum type) {
|
std::string Shader::addVersionIfNeeded(const std::string &source, GLenum type) {
|
||||||
// 如果已经包含版本声明,直接返回
|
// 如果已经包含版本声明,直接返回
|
||||||
if (source.find("#version") != std::string::npos) {
|
if (source.find("#version") != std::string::npos) {
|
||||||
return source;
|
return source;
|
||||||
|
|
|
||||||
|
|
@ -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,11 +106,9 @@ 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_) {
|
||||||
return runningScene_->getMainCamera();
|
return runningScene_->getMainCamera();
|
||||||
}
|
}
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue