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,236 +1,226 @@
|
||||||
#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 {
|
||||||
|
|
||||||
Shader::Shader() = default;
|
Shader::Shader() = default;
|
||||||
|
|
||||||
Shader::~Shader() {
|
Shader::~Shader() {
|
||||||
if (program_ != 0) {
|
if (program_ != 0) {
|
||||||
glDeleteProgram(program_);
|
glDeleteProgram(program_);
|
||||||
program_ = 0;
|
program_ = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
// 读取顶点着色器
|
||||||
if (!vsFile.is_open()) {
|
std::ifstream vsFile(vsPath);
|
||||||
E2D_LOG_ERROR("Failed to open vertex shader: {}", vsPath);
|
if (!vsFile.is_open()) {
|
||||||
return false;
|
E2D_LOG_ERROR("Failed to open vertex shader: {}", vsPath);
|
||||||
}
|
return false;
|
||||||
std::stringstream vsStream;
|
}
|
||||||
vsStream << vsFile.rdbuf();
|
std::stringstream vsStream;
|
||||||
std::string vsSource = vsStream.str();
|
vsStream << vsFile.rdbuf();
|
||||||
|
std::string vsSource = vsStream.str();
|
||||||
// 读取片段着色器
|
|
||||||
std::ifstream fsFile(fsPath);
|
// 读取片段着色器
|
||||||
if (!fsFile.is_open()) {
|
std::ifstream fsFile(fsPath);
|
||||||
E2D_LOG_ERROR("Failed to open fragment shader: {}", fsPath);
|
if (!fsFile.is_open()) {
|
||||||
return false;
|
E2D_LOG_ERROR("Failed to open fragment shader: {}", fsPath);
|
||||||
}
|
return false;
|
||||||
std::stringstream fsStream;
|
}
|
||||||
fsStream << fsFile.rdbuf();
|
std::stringstream fsStream;
|
||||||
std::string fsSource = fsStream.str();
|
fsStream << fsFile.rdbuf();
|
||||||
|
std::string fsSource = fsStream.str();
|
||||||
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) {
|
// 删除旧程序
|
||||||
glDeleteProgram(program_);
|
if (program_ != 0) {
|
||||||
program_ = 0;
|
glDeleteProgram(program_);
|
||||||
}
|
program_ = 0;
|
||||||
|
}
|
||||||
uniformCache_.clear();
|
|
||||||
|
uniformCache_.clear();
|
||||||
// 处理源码(添加版本声明)
|
|
||||||
std::string processedVS = addVersionIfNeeded(vsSource, GL_VERTEX_SHADER);
|
// 处理源码(添加版本声明)
|
||||||
std::string processedFS = addVersionIfNeeded(fsSource, GL_FRAGMENT_SHADER);
|
std::string processedVS = addVersionIfNeeded(vsSource, GL_VERTEX_SHADER);
|
||||||
|
std::string processedFS = addVersionIfNeeded(fsSource, GL_FRAGMENT_SHADER);
|
||||||
E2D_LOG_INFO("Compiling vertex shader...");
|
|
||||||
// 编译顶点着色器
|
E2D_LOG_INFO("Compiling vertex shader...");
|
||||||
GLuint vs = compileShader(GL_VERTEX_SHADER, processedVS);
|
// 编译顶点着色器
|
||||||
if (vs == 0) {
|
GLuint vs = compileShader(GL_VERTEX_SHADER, processedVS);
|
||||||
E2D_LOG_ERROR("Vertex shader compilation failed");
|
if (vs == 0) {
|
||||||
return false;
|
E2D_LOG_ERROR("Vertex shader compilation failed");
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
E2D_LOG_INFO("Compiling fragment shader...");
|
|
||||||
// 编译片段着色器
|
E2D_LOG_INFO("Compiling fragment shader...");
|
||||||
GLuint fs = compileShader(GL_FRAGMENT_SHADER, processedFS);
|
// 编译片段着色器
|
||||||
if (fs == 0) {
|
GLuint fs = compileShader(GL_FRAGMENT_SHADER, processedFS);
|
||||||
E2D_LOG_ERROR("Fragment shader compilation failed");
|
if (fs == 0) {
|
||||||
glDeleteShader(vs);
|
E2D_LOG_ERROR("Fragment shader compilation failed");
|
||||||
return false;
|
glDeleteShader(vs);
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
E2D_LOG_INFO("Linking shader program...");
|
|
||||||
// 链接程序
|
E2D_LOG_INFO("Linking shader program...");
|
||||||
if (!linkProgram(vs, fs)) {
|
// 链接程序
|
||||||
E2D_LOG_ERROR("Shader program linking failed");
|
if (!linkProgram(vs, fs)) {
|
||||||
glDeleteShader(vs);
|
E2D_LOG_ERROR("Shader program linking failed");
|
||||||
glDeleteShader(fs);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 清理着色器对象
|
|
||||||
glDeleteShader(vs);
|
glDeleteShader(vs);
|
||||||
glDeleteShader(fs);
|
glDeleteShader(fs);
|
||||||
|
return false;
|
||||||
E2D_LOG_INFO("Shader program created successfully, program ID: {}", program_);
|
}
|
||||||
return true;
|
|
||||||
|
// 清理着色器对象
|
||||||
|
glDeleteShader(vs);
|
||||||
|
glDeleteShader(fs);
|
||||||
|
|
||||||
|
E2D_LOG_INFO("Shader program created successfully, program ID: {}", program_);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::bind() const {
|
void Shader::bind() const {
|
||||||
if (program_ != 0) {
|
if (program_ != 0) {
|
||||||
glUseProgram(program_);
|
glUseProgram(program_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::unbind() const {
|
void Shader::unbind() const { glUseProgram(0); }
|
||||||
glUseProgram(0);
|
|
||||||
|
void Shader::setUniformBlock(const std::string &name, uint32_t binding) {
|
||||||
|
if (program_ == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
GLuint index = glGetUniformBlockIndex(program_, name.c_str());
|
||||||
|
if (index != GL_INVALID_INDEX) {
|
||||||
|
glUniformBlockBinding(program_, index, binding);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setUniformBlock(const std::string& name, uint32_t binding) {
|
void Shader::setInt(const std::string &name, int value) {
|
||||||
if (program_ == 0) return;
|
GLint location = getUniformLocation(name);
|
||||||
|
if (location != -1) {
|
||||||
GLuint index = glGetUniformBlockIndex(program_, name.c_str());
|
glUniform1i(location, value);
|
||||||
if (index != GL_INVALID_INDEX) {
|
}
|
||||||
glUniformBlockBinding(program_, index, binding);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setInt(const std::string& name, int value) {
|
void Shader::setFloat(const std::string &name, float value) {
|
||||||
GLint location = getUniformLocation(name);
|
GLint location = getUniformLocation(name);
|
||||||
if (location != -1) {
|
if (location != -1) {
|
||||||
glUniform1i(location, value);
|
glUniform1f(location, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setFloat(const std::string& name, float value) {
|
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) {
|
||||||
glUniform1f(location, value);
|
glUniform2f(location, x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setVec2(const std::string& name, float x, float y) {
|
void Shader::setVec4(const std::string &name, float x, float y, float z,
|
||||||
GLint location = getUniformLocation(name);
|
float w) {
|
||||||
if (location != -1) {
|
GLint location = getUniformLocation(name);
|
||||||
glUniform2f(location, x, y);
|
if (location != -1) {
|
||||||
}
|
glUniform4f(location, x, y, z, w);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setVec4(const std::string& name, float x, float y, float z, float w) {
|
void Shader::setMat4(const std::string &name, const float *value) {
|
||||||
GLint location = getUniformLocation(name);
|
GLint location = getUniformLocation(name);
|
||||||
if (location != -1) {
|
if (location != -1) {
|
||||||
glUniform4f(location, x, y, z, w);
|
glUniformMatrix4fv(location, 1, GL_FALSE, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setMat4(const std::string& name, const float* value) {
|
GLuint Shader::compileShader(GLenum type, const std::string &source) {
|
||||||
GLint location = getUniformLocation(name);
|
GLuint shader = glCreateShader(type);
|
||||||
if (location != -1) {
|
|
||||||
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) {
|
const char *src = source.c_str();
|
||||||
GLuint shader = glCreateShader(type);
|
glShaderSource(shader, 1, &src, nullptr);
|
||||||
|
glCompileShader(shader);
|
||||||
const char* src = source.c_str();
|
|
||||||
glShaderSource(shader, 1, &src, nullptr);
|
// 检查编译状态
|
||||||
glCompileShader(shader);
|
GLint success;
|
||||||
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||||
// 检查编译状态
|
if (!success) {
|
||||||
GLint success;
|
char infoLog[512];
|
||||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
|
||||||
if (!success) {
|
|
||||||
char infoLog[512];
|
const char *typeStr = (type == GL_VERTEX_SHADER) ? "vertex" : "fragment";
|
||||||
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
|
E2D_LOG_ERROR("{} shader compilation failed: {}", typeStr, infoLog);
|
||||||
|
|
||||||
const char* typeStr = (type == GL_VERTEX_SHADER) ? "vertex" : "fragment";
|
glDeleteShader(shader);
|
||||||
E2D_LOG_ERROR("{} shader compilation failed: {}", typeStr, infoLog);
|
return 0;
|
||||||
|
}
|
||||||
glDeleteShader(shader);
|
|
||||||
return 0;
|
return shader;
|
||||||
}
|
|
||||||
|
|
||||||
return shader;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Shader::linkProgram(GLuint vertexShader, GLuint fragmentShader) {
|
bool Shader::linkProgram(GLuint vertexShader, GLuint fragmentShader) {
|
||||||
program_ = glCreateProgram();
|
program_ = glCreateProgram();
|
||||||
glAttachShader(program_, vertexShader);
|
glAttachShader(program_, vertexShader);
|
||||||
glAttachShader(program_, fragmentShader);
|
glAttachShader(program_, fragmentShader);
|
||||||
glLinkProgram(program_);
|
glLinkProgram(program_);
|
||||||
|
|
||||||
// 检查链接状态
|
// 检查链接状态
|
||||||
GLint success;
|
GLint success;
|
||||||
glGetProgramiv(program_, GL_LINK_STATUS, &success);
|
glGetProgramiv(program_, GL_LINK_STATUS, &success);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
char infoLog[512];
|
char infoLog[512];
|
||||||
glGetProgramInfoLog(program_, 512, nullptr, infoLog);
|
glGetProgramInfoLog(program_, 512, nullptr, infoLog);
|
||||||
E2D_LOG_ERROR("Shader program linking failed: {}", infoLog);
|
E2D_LOG_ERROR("Shader program linking failed: {}", infoLog);
|
||||||
|
|
||||||
glDeleteProgram(program_);
|
glDeleteProgram(program_);
|
||||||
program_ = 0;
|
program_ = 0;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
// 检查缓存
|
||||||
if (it != uniformCache_.end()) {
|
auto it = uniformCache_.find(name);
|
||||||
return it->second;
|
if (it != uniformCache_.end()) {
|
||||||
}
|
return it->second;
|
||||||
|
}
|
||||||
// 查询 uniform 位置
|
|
||||||
GLint location = glGetUniformLocation(program_, name.c_str());
|
// 查询 uniform 位置
|
||||||
uniformCache_[name] = location;
|
GLint location = glGetUniformLocation(program_, name.c_str());
|
||||||
|
uniformCache_[name] = location;
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加 OpenGL ES 3.2 版本声明
|
// 添加 OpenGL ES 3.2 版本声明
|
||||||
std::string result = "#version 320 es\n";
|
std::string result = "#version 320 es\n";
|
||||||
|
|
||||||
// 片段着色器需要添加精度声明
|
// 片段着色器需要添加精度声明
|
||||||
if (type == GL_FRAGMENT_SHADER) {
|
if (type == GL_FRAGMENT_SHADER) {
|
||||||
result += "precision mediump float;\n";
|
result += "precision mediump float;\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
result += source;
|
result += source;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace extra2d
|
} // namespace extra2d
|
||||||
|
|
|
||||||
|
|
@ -1,154 +1,140 @@
|
||||||
#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() {
|
||||||
// 结束当前场景
|
// 结束当前场景
|
||||||
if (runningScene_) {
|
if (runningScene_) {
|
||||||
runningScene_->onExit();
|
runningScene_->onExit();
|
||||||
runningScene_.reset();
|
runningScene_.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清空场景栈
|
// 清空场景栈
|
||||||
while (!sceneStack_.empty()) {
|
while (!sceneStack_.empty()) {
|
||||||
sceneStack_.pop();
|
sceneStack_.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
running_ = false;
|
running_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Director::runScene(Ptr<Scene> scene) {
|
void Director::runScene(Ptr<Scene> scene) {
|
||||||
if (!scene) return;
|
if (!scene)
|
||||||
|
return;
|
||||||
|
|
||||||
// 结束当前场景
|
// 结束当前场景
|
||||||
if (runningScene_) {
|
if (runningScene_) {
|
||||||
runningScene_->onExit();
|
runningScene_->onExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清空场景栈
|
// 清空场景栈
|
||||||
while (!sceneStack_.empty()) {
|
while (!sceneStack_.empty()) {
|
||||||
sceneStack_.pop();
|
sceneStack_.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 运行新场景
|
// 运行新场景
|
||||||
runningScene_ = scene;
|
runningScene_ = scene;
|
||||||
runningScene_->onEnter();
|
runningScene_->onEnter();
|
||||||
running_ = true;
|
running_ = true;
|
||||||
needEnd_ = false;
|
needEnd_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Director::replaceScene(Ptr<Scene> scene) {
|
void Director::replaceScene(Ptr<Scene> scene) {
|
||||||
if (!scene) return;
|
if (!scene)
|
||||||
|
return;
|
||||||
|
|
||||||
// 结束当前场景
|
// 结束当前场景
|
||||||
if (runningScene_) {
|
if (runningScene_) {
|
||||||
runningScene_->onExit();
|
runningScene_->onExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 替换场景
|
// 替换场景
|
||||||
runningScene_ = scene;
|
runningScene_ = scene;
|
||||||
runningScene_->onEnter();
|
runningScene_->onEnter();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Director::pushScene(Ptr<Scene> scene) {
|
void Director::pushScene(Ptr<Scene> scene) {
|
||||||
if (!scene) return;
|
if (!scene)
|
||||||
|
return;
|
||||||
|
|
||||||
// 暂停当前场景
|
// 暂停当前场景
|
||||||
if (runningScene_) {
|
if (runningScene_) {
|
||||||
// 可以在这里添加暂停逻辑
|
// 可以在这里添加暂停逻辑
|
||||||
sceneStack_.push(runningScene_);
|
sceneStack_.push(runningScene_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 运行新场景
|
// 运行新场景
|
||||||
runningScene_ = scene;
|
runningScene_ = scene;
|
||||||
runningScene_->onEnter();
|
runningScene_->onEnter();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Director::popScene() {
|
void Director::popScene() {
|
||||||
if (sceneStack_.empty()) {
|
if (sceneStack_.empty()) {
|
||||||
// 没有场景可以弹出,结束运行
|
// 没有场景可以弹出,结束运行
|
||||||
end();
|
end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 结束当前场景
|
// 结束当前场景
|
||||||
if (runningScene_) {
|
if (runningScene_) {
|
||||||
runningScene_->onExit();
|
runningScene_->onExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 弹出栈顶场景
|
// 弹出栈顶场景
|
||||||
runningScene_ = sceneStack_.top();
|
runningScene_ = sceneStack_.top();
|
||||||
sceneStack_.pop();
|
sceneStack_.pop();
|
||||||
|
|
||||||
// 恢复场景
|
// 恢复场景
|
||||||
// 可以在这里添加恢复逻辑
|
// 可以在这里添加恢复逻辑
|
||||||
}
|
}
|
||||||
|
|
||||||
void Director::end() {
|
void Director::end() {
|
||||||
needEnd_ = true;
|
needEnd_ = true;
|
||||||
running_ = false;
|
running_ = false;
|
||||||
|
|
||||||
if (runningScene_) {
|
if (runningScene_) {
|
||||||
runningScene_->onExit();
|
runningScene_->onExit();
|
||||||
runningScene_.reset();
|
runningScene_.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
runningScene_->render();
|
events::OnRenderSetCamera::emit(viewProj);
|
||||||
|
}
|
||||||
|
|
||||||
|
runningScene_->render();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace extra2d
|
} // namespace extra2d
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue