Extra2D/Extra2D/include/extra2d/render/backends/opengl/gl_render_backend.h

303 lines
7.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <extra2d/render/core/render_backend.h>
#include <extra2d/render/rhi/rhi_device.h>
#include <extra2d/render/backends/opengl/gl_rhi_device.h>
#include <glm/mat4x4.hpp>
#include <stack>
namespace extra2d {
class IWindow;
/**
* @brief OpenGL 渲染后端实现
*
* 实现 RenderBackend 接口,使用 OpenGL 作为底层图形 API
*/
class GLRenderBackend : public RenderBackend {
public:
/**
* @brief 构造函数
*/
GLRenderBackend();
/**
* @brief 析构函数
*/
~GLRenderBackend() override;
// ========================================================================
// 生命周期
// ========================================================================
/**
* @brief 初始化渲染后端
* @param window 窗口接口
* @param config 配置参数
* @return 成功返回 true
*/
bool init(IWindow *window, const RenderBackendConfig &config = {}) override;
/**
* @brief 关闭渲染后端
*/
void shutdown() override;
/**
* @brief 检查是否已初始化
*/
bool isValid() const override;
// ========================================================================
// 帧管理
// ========================================================================
/**
* @brief 开始帧渲染
* @param clearColor 清屏颜色
*/
void beginFrame(const Color &clearColor = Colors::Black) override;
/**
* @brief 结束帧渲染
*/
void endFrame() override;
/**
* @brief 呈现帧
*/
void present() override;
// ========================================================================
// 渲染状态
// ========================================================================
/**
* @brief 设置视口
*/
void setViewport(int x, int y, int width, int height) override;
/**
* @brief 设置裁剪区域
*/
void setScissor(int x, int y, int width, int height) override;
/**
* @brief 启用/禁用裁剪测试
*/
void setScissorEnabled(bool enabled) override;
/**
* @brief 设置投影矩阵
*/
void setProjectionMatrix(const glm::mat4 &matrix) override;
/**
* @brief 设置视图矩阵
*/
void setViewMatrix(const glm::mat4 &matrix) override;
/**
* @brief 设置垂直同步
*/
void setVSync(bool enabled) override;
/**
* @brief 获取垂直同步状态
*/
bool isVSyncEnabled() const override;
// ========================================================================
// 精灵渲染
// ========================================================================
/**
* @brief 绘制精灵
*/
void drawSprite(Ptr<rhi::RHITexture> texture, const Rect &destRect,
const Rect &srcRect = Rect(0, 0, 1, 1),
const Color &color = Colors::White,
float rotation = 0.0f, bool flipX = false,
bool flipY = false) override;
/**
* @brief 绘制精灵(使用变换矩阵)
*/
void drawSprite(Ptr<rhi::RHITexture> texture,
const glm::mat4 &transform,
const Rect &srcRect = Rect(0, 0, 1, 1),
const Color &color = Colors::White) override;
/**
* @brief 绘制九宫格精灵
*/
void draw9Slice(Ptr<rhi::RHITexture> texture, const Rect &destRect,
const Rect &srcRect, const Vec2 &borderSize,
const Color &color = Colors::White) override;
// ========================================================================
// 文本渲染
// ========================================================================
/**
* @brief 绘制文本
*/
void drawText(const std::string &text, const Vec2 &position,
float fontSize, const Color &color = Colors::White) override;
/**
* @brief 绘制文本UTF-32
*/
void drawText(const std::u32string &text, const Vec2 &position,
float fontSize, const Color &color = Colors::White) override;
/**
* @brief 测量文本尺寸
*/
Vec2 measureText(const std::string &text, float fontSize) override;
/**
* @brief 测量文本尺寸UTF-32
*/
Vec2 measureText(const std::u32string &text, float fontSize) override;
// ========================================================================
// 形状渲染
// ========================================================================
/**
* @brief 绘制矩形
*/
void drawRect(const Rect &rect, const Color &color,
float lineWidth = 1.0f) override;
/**
* @brief 绘制填充矩形
*/
void fillRect(const Rect &rect, const Color &color) override;
/**
* @brief 绘制圆形
*/
void drawCircle(const Vec2 &center, float radius, const Color &color,
float lineWidth = 1.0f, uint32_t segments = 32) override;
/**
* @brief 绘制填充圆形
*/
void fillCircle(const Vec2 &center, float radius, const Color &color,
uint32_t segments = 32) override;
/**
* @brief 绘制线段
*/
void drawLine(const Vec2 &start, const Vec2 &end, const Color &color,
float lineWidth = 1.0f) override;
/**
* @brief 绘制多边形
*/
void drawPolygon(const Vec2 *points, size_t count, const Color &color,
float lineWidth = 1.0f) override;
/**
* @brief 绘制填充多边形
*/
void fillPolygon(const Vec2 *points, size_t count,
const Color &color) override;
// ========================================================================
// 批处理控制
// ========================================================================
/**
* @brief 开始精灵批处理
*/
void beginSpriteBatch() override;
/**
* @brief 结束精灵批处理
*/
void endSpriteBatch() override;
/**
* @brief 刷新当前批次
*/
void flush() override;
/**
* @brief 设置渲染排序键
*/
void setSortKey(uint64_t key) override;
/**
* @brief 设置渲染层
*/
void setLayer(int layer) override;
/**
* @brief 设置混合模式
*/
void setBlendMode(rhi::BlendState blend) override;
// ========================================================================
// 访问器
// ========================================================================
/**
* @brief 获取 RHI 设备
*/
rhi::RHIDevice *getDevice() const override;
/**
* @brief 获取窗口宽度
*/
int getWidth() const override;
/**
* @brief 获取窗口高度
*/
int getHeight() const override;
/**
* @brief 获取渲染统计
*/
const RenderStats &getStats() const override;
/**
* @brief 重置渲染统计
*/
void resetStats() override;
/**
* @brief 获取图形 API 类型
*/
rhi::GraphicsAPI getAPI() const override;
/**
* @brief 获取设备能力
*/
const rhi::DeviceCaps &getCaps() const override;
private:
bool initialized_ = false;
IWindow *window_ = nullptr;
RenderBackendConfig config_;
RenderStats stats_;
Ptr<rhi::RHIDevice> device_;
glm::mat4 projectionMatrix_ = glm::mat4(1.0f);
glm::mat4 viewMatrix_ = glm::mat4(1.0f);
int width_ = 0;
int height_ = 0;
uint64_t sortKey_ = 0;
int layer_ = 0;
rhi::BlendState blendState_ = rhi::BlendState::alphaBlend();
bool inBatch_ = false;
};
} // namespace extra2d