渲染后端加入

This commit is contained in:
2026-02-17 13:28:38 +08:00
commit a5379b3816
49 changed files with 44512 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
#pragma once
#include <fostbite2D/core/color.h>
#include <fostbite2D/core/math_types.h>
#include <fostbite2D/core/types.h>
#include <fostbite2D/render/font.h>
#include <fostbite2D/render/opengl/gl_texture.h>
#include <fostbite2D/render/texture.h>
#include <memory>
#include <stb/stb_rect_pack.h>
#include <stb/stb_truetype.h>
#include <unordered_map>
#include <vector>
namespace frostbite2D {
// ============================================================================
// OpenGL 字体图集实现 - 使用 stb_rect_pack 进行矩形打包
// ============================================================================
class GLFontAtlas : public FontAtlas {
public:
/**
* @brief 构造函数,从字体文件初始化字体图集
* @param filepath 字体文件路径
* @param fontSize 字体大小(像素)
* @param useSDF 是否使用有符号距离场渲染
*/
GLFontAtlas(const std::string &filepath, int fontSize, bool useSDF = false);
/**
* @brief 析构函数
*/
~GLFontAtlas();
// FontAtlas 接口实现
const Glyph *getGlyph(char32_t codepoint) const override;
Texture *getTexture() const override { return texture_.get(); }
int getFontSize() const override { return fontSize_; }
float getAscent() const override { return ascent_; }
float getDescent() const override { return descent_; }
float getLineGap() const override { return lineGap_; }
float getLineHeight() const override { return ascent_ - descent_ + lineGap_; }
Vec2 measureText(const std::string &text) override;
bool isSDF() const override { return useSDF_; }
private:
// 图集配置 - 增大尺寸以支持更多字符
static constexpr int ATLAS_WIDTH = 1024;
static constexpr int ATLAS_HEIGHT = 1024;
static constexpr int PADDING = 2; // 字形之间的间距
int fontSize_;
bool useSDF_;
mutable std::unique_ptr<GLTexture> texture_;
mutable std::unordered_map<char32_t, Glyph> glyphs_;
// stb_rect_pack 上下文
mutable stbrp_context packContext_;
mutable std::vector<stbrp_node> packNodes_;
mutable int currentY_;
std::vector<unsigned char> fontData_;
stbtt_fontinfo fontInfo_;
float scale_;
float ascent_;
float descent_;
float lineGap_;
// 预分配字形位图缓冲区,避免每次动态分配
mutable std::vector<uint8_t> glyphBitmapCache_;
mutable std::vector<uint8_t> glyphRgbaCache_;
/**
* @brief 创建字体图集纹理
*/
void createAtlas();
/**
* @brief 缓存字形到图集
* @param codepoint Unicode码点
*/
void cacheGlyph(char32_t codepoint) const;
};
} // namespace frostbite2D

View File

@@ -0,0 +1,316 @@
#pragma once
#include <fostbite2D/core/color.h>
#include <fostbite2D/core/math_types.h>
#include <fostbite2D/core/types.h>
#include <fostbite2D/render/font.h>
#include <fostbite2D/render/opengl/gl_sprite_batch.h>
#include <fostbite2D/render/shader/shader_interface.h>
#include <fostbite2D/render/texture.h>
#include <array>
#include <glad/glad.h>
#include <vector>
struct SDL_Window;
namespace frostbite2D {
// 混合模式枚举
enum class BlendMode { None, Alpha, Additive, Multiply };
// 渲染统计信息
struct RenderStats {
uint32_t drawCalls = 0;
uint32_t triangleCount = 0;
};
// ============================================================================
// OpenGL 渲染器实现
// ============================================================================
class GLRenderer {
public:
GLRenderer();
~GLRenderer();
/**
* @brief 初始化OpenGL渲染器
* @param window SDL窗口指针
* @return 初始化成功返回true失败返回false
*/
bool init(SDL_Window *window);
/**
* @brief 关闭渲染器释放所有GPU资源
*/
void shutdown();
/**
* @brief 开始新帧,清除颜色缓冲区并重置统计信息
* @param clearColor 清屏颜色
*/
void beginFrame(const Color &clearColor);
/**
* @brief 结束当前帧,刷新所有待处理的渲染批次
*/
void endFrame();
/**
* @brief 设置视口区域
* @param x 视口左下角X坐标
* @param y 视口左下角Y坐标
* @param width 视口宽度
* @param height 视口高度
*/
void setViewport(int x, int y, int width, int height);
/**
* @brief 设置垂直同步
* @param enabled true启用垂直同步false禁用
*/
void setVSync(bool enabled);
/**
* @brief 设置混合模式
* @param mode 混合模式枚举值
*/
void setBlendMode(BlendMode mode);
/**
* @brief 设置视图投影矩阵
* @param matrix 4x4视图投影矩阵
*/
void setViewProjection(const glm::mat4 &matrix);
/**
* @brief 压入变换矩阵到变换栈
* @param transform 变换矩阵
*/
void pushTransform(const glm::mat4 &transform);
/**
* @brief 从变换栈弹出顶部变换矩阵
*/
void popTransform();
/**
* @brief 获取当前累积的变换矩阵
* @return 当前变换矩阵,如果栈为空则返回单位矩阵
*/
glm::mat4 getCurrentTransform() const;
/**
* @brief 创建纹理对象
* @param width 纹理宽度
* @param height 纹理高度
* @param pixels 像素数据指针
* @param channels 颜色通道数
* @return 创建的纹理智能指针
*/
Ptr<Texture> createTexture(int width, int height, const uint8_t *pixels,
int channels);
/**
* @brief 从文件加载纹理
* @param filepath 纹理文件路径
* @return 加载的纹理智能指针
*/
Ptr<Texture> loadTexture(const std::string &filepath);
/**
* @brief 开始精灵批处理
*/
void beginSpriteBatch();
/**
* @brief 绘制精灵(带完整参数)
* @param texture 纹理引用
* @param destRect 目标矩形(屏幕坐标)
* @param srcRect 源矩形(纹理坐标)
* @param tint 着色颜色
* @param rotation 旋转角度(度)
* @param anchor 锚点位置0-1范围
*/
void drawSprite(const Texture &texture, const Rect &destRect,
const Rect &srcRect, const Color &tint, float rotation,
const Vec2 &anchor);
/**
* @brief 绘制精灵(简化版本)
* @param texture 纹理引用
* @param position 绘制位置
* @param tint 着色颜色
*/
void drawSprite(const Texture &texture, const Vec2 &position,
const Color &tint);
/**
* @brief 结束精灵批处理并提交绘制
*/
void endSpriteBatch();
/**
* @brief 绘制线段
* @param start 起点坐标
* @param end 终点坐标
* @param color 线条颜色
* @param width 线条宽度
*/
void drawLine(const Vec2 &start, const Vec2 &end, const Color &color,
float width);
/**
* @brief 绘制矩形边框
* @param rect 矩形区域
* @param color 边框颜色
* @param width 线条宽度
*/
void drawRect(const Rect &rect, const Color &color, float width);
/**
* @brief 填充矩形
* @param rect 矩形区域
* @param color 填充颜色
*/
void fillRect(const Rect &rect, const Color &color);
/**
* @brief 绘制圆形边框
* @param center 圆心坐标
* @param radius 半径
* @param color 边框颜色
* @param segments 分段数
* @param width 线条宽度
*/
void drawCircle(const Vec2 &center, float radius, const Color &color,
int segments, float width);
/**
* @brief 填充圆形
* @param center 圆心坐标
* @param radius 半径
* @param color 填充颜色
* @param segments 分段数
*/
void fillCircle(const Vec2 &center, float radius, const Color &color,
int segments);
/**
* @brief 绘制三角形边框
* @param p1 第一个顶点
* @param p2 第二个顶点
* @param p3 第三个顶点
* @param color 边框颜色
* @param width 线条宽度
*/
void drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
const Color &color, float width);
/**
* @brief 填充三角形
* @param p1 第一个顶点
* @param p2 第二个顶点
* @param p3 第三个顶点
* @param color 填充颜色
*/
void fillTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
const Color &color);
/**
* @brief 绘制多边形边框
* @param points 顶点数组
* @param color 边框颜色
* @param width 线条宽度
*/
void drawPolygon(const std::vector<Vec2> &points, const Color &color,
float width);
/**
* @brief 填充多边形
* @param points 顶点数组
* @param color 填充颜色
*/
void fillPolygon(const std::vector<Vec2> &points, const Color &color);
/**
* @brief 绘制文本
* @param font 字体图集引用
* @param text 文本内容
* @param position 绘制位置
* @param color 文本颜色
*/
void drawText(const FontAtlas &font, const std::string &text,
const Vec2 &position, const Color &color);
/**
* @brief 绘制文本(使用浮点坐标)
* @param font 字体图集引用
* @param text 文本内容
* @param x X坐标
* @param y Y坐标
* @param color 文本颜色
*/
void drawText(const FontAtlas &font, const std::string &text, float x,
float y, const Color &color);
/**
* @brief 获取渲染统计信息
* @return 渲染统计信息
*/
RenderStats getStats() const { return stats_; }
/**
* @brief 重置渲染统计信息
*/
void resetStats();
private:
// 形状批处理常量
static constexpr size_t MAX_CIRCLE_SEGMENTS = 128;
static constexpr size_t MAX_SHAPE_VERTICES = 8192; // 最大形状顶点数
static constexpr size_t MAX_LINE_VERTICES = 16384; // 最大线条顶点数
// 形状顶点结构(包含颜色)
struct ShapeVertex {
float x, y;
float r, g, b, a;
};
SDL_Window *window_;
GLSpriteBatch spriteBatch_;
Ptr<IShader> shapeShader_;
GLuint shapeVao_;
GLuint shapeVbo_;
GLuint lineVao_; // 线条专用 VAO
GLuint lineVbo_; // 线条专用 VBO
glm::mat4 viewProjection_;
std::vector<glm::mat4> transformStack_;
RenderStats stats_;
bool vsync_;
// 形状批处理缓冲区(预分配,避免每帧内存分配)
std::array<ShapeVertex, MAX_SHAPE_VERTICES> shapeVertexCache_;
size_t shapeVertexCount_ = 0;
GLenum currentShapeMode_ = GL_TRIANGLES;
// 线条批处理缓冲区
std::array<ShapeVertex, MAX_LINE_VERTICES> lineVertexCache_;
size_t lineVertexCount_ = 0;
float currentLineWidth_ = 1.0f;
// OpenGL 状态缓存
BlendMode cachedBlendMode_ = BlendMode::None;
bool blendEnabled_ = false;
void initShapeRendering();
void flushShapeBatch();
void flushLineBatch();
void addShapeVertex(float x, float y, const Color &color);
void addLineVertex(float x, float y, const Color &color);
void submitShapeBatch(GLenum mode);
};
} // namespace frostbite2D

View File

@@ -0,0 +1,194 @@
#pragma once
#include <fostbite2D/core/color.h>
#include <fostbite2D/render/shader/shader_interface.h>
#include <glad/glad.h>
#include <unordered_map>
#include <vector>
namespace frostbite2D {
class GLShader : public IShader {
public:
/**
* @brief 构造函数
*/
GLShader();
/**
* @brief 析构函数
*/
~GLShader() override;
/**
* @brief 绑定Shader程序
*/
void bind() const override;
/**
* @brief 解绑Shader程序
*/
void unbind() const override;
/**
* @brief 设置布尔类型uniform变量
* @param name uniform变量名
* @param value 布尔值
*/
void setBool(const std::string &name, bool value) override;
/**
* @brief 设置整数类型uniform变量
* @param name uniform变量名
* @param value 整数值
*/
void setInt(const std::string &name, int value) override;
/**
* @brief 设置浮点类型uniform变量
* @param name uniform变量名
* @param value 浮点值
*/
void setFloat(const std::string &name, float value) override;
/**
* @brief 设置二维向量类型uniform变量
* @param name uniform变量名
* @param value 二维向量值
*/
void setVec2(const std::string &name, const glm::vec2 &value) override;
/**
* @brief 设置三维向量类型uniform变量
* @param name uniform变量名
* @param value 三维向量值
*/
void setVec3(const std::string &name, const glm::vec3 &value) override;
/**
* @brief 设置四维向量类型uniform变量
* @param name uniform变量名
* @param value 四维向量值
*/
void setVec4(const std::string &name, const glm::vec4 &value) override;
/**
* @brief 设置4x4矩阵类型uniform变量
* @param name uniform变量名
* @param value 4x4矩阵值
*/
void setMat4(const std::string &name, const glm::mat4 &value) override;
/**
* @brief 设置颜色类型uniform变量
* @param name uniform变量名
* @param color 颜色值
*/
void setColor(const std::string &name, const Color &color) override;
/**
* @brief 检查Shader是否有效
* @return 有效返回true否则返回false
*/
bool isValid() const override { return programID_ != 0; }
/**
* @brief 获取原生句柄OpenGL程序ID
* @return OpenGL程序ID
*/
uint32_t getNativeHandle() const override { return programID_; }
/**
* @brief 获取Shader名称
* @return Shader名称
*/
const std::string &getName() const override { return name_; }
/**
* @brief 设置Shader名称
* @param name Shader名称
*/
void setName(const std::string &name) override { name_ = name; }
/**
* @brief 从源码编译Shader
* @param vertexSource 顶点着色器源码
* @param fragmentSource 片段着色器源码
* @return 编译成功返回true失败返回false
*/
bool compileFromSource(const char *vertexSource, const char *fragmentSource);
/**
* @brief 从二进制数据创建Shader
* @param binary 二进制数据
* @return 创建成功返回true失败返回false
*/
bool compileFromBinary(const std::vector<uint8_t> &binary);
/**
* @brief 获取Shader二进制数据
* @param outBinary 输出的二进制数据
* @return 成功返回true失败返回false
*/
bool getBinary(std::vector<uint8_t> &outBinary);
/**
* @brief 获取OpenGL程序ID
* @return OpenGL程序ID
*/
GLuint getProgramID() const { return programID_; }
private:
GLuint programID_ = 0;
std::string name_;
std::unordered_map<std::string, GLint> uniformCache_;
/**
* @brief 编译单个着色器
* @param type 着色器类型
* @param source 着色器源码
* @return 着色器ID失败返回0
*/
GLuint compileShader(GLenum type, const char *source);
/**
* @brief 获取uniform位置
* @param name uniform变量名
* @return uniform位置
*/
GLint getUniformLocation(const std::string &name);
};
class GLShaderFactory : public IShaderFactory {
public:
/**
* @brief 从源码创建Shader
* @param name Shader名称
* @param vertSource 顶点着色器源码
* @param fragSource 片段着色器源码
* @return 创建的Shader实例
*/
Ptr<IShader> createFromSource(const std::string &name,
const std::string &vertSource,
const std::string &fragSource) override;
/**
* @brief 从缓存二进制创建Shader
* @param name Shader名称
* @param binary 编译后的二进制数据
* @return 创建的Shader实例
*/
Ptr<IShader> createFromBinary(const std::string &name,
const std::vector<uint8_t> &binary) override;
/**
* @brief 获取Shader的二进制数据
* @param shader Shader实例
* @param outBinary 输出的二进制数据
* @return 成功返回true失败返回false
*/
bool getShaderBinary(const IShader &shader,
std::vector<uint8_t> &outBinary) override;
};
} // namespace frostbite2D

View File

@@ -0,0 +1,98 @@
#pragma once
#include <array>
#include <fostbite2D/core/color.h>
#include <fostbite2D/core/math_types.h>
#include <fostbite2D/core/types.h>
#include <fostbite2D/render/shader/shader_interface.h>
#include <fostbite2D/render/texture.h>
#include <glm/mat4x4.hpp>
#include <vector>
#include <glad/glad.h>
namespace frostbite2D {
// ============================================================================
// OpenGL 精灵批渲染器 - 优化版本
// ============================================================================
class GLSpriteBatch {
public:
static constexpr size_t MAX_SPRITES = 10000;
static constexpr size_t VERTICES_PER_SPRITE = 4;
static constexpr size_t INDICES_PER_SPRITE = 6;
static constexpr size_t MAX_VERTICES = MAX_SPRITES * VERTICES_PER_SPRITE;
static constexpr size_t MAX_INDICES = MAX_SPRITES * INDICES_PER_SPRITE;
struct Vertex {
glm::vec2 position;
glm::vec2 texCoord;
glm::vec4 color;
};
struct SpriteData {
glm::vec2 position;
glm::vec2 size;
glm::vec2 texCoordMin;
glm::vec2 texCoordMax;
glm::vec4 color;
float rotation;
glm::vec2 anchor;
bool isSDF = false;
};
GLSpriteBatch();
~GLSpriteBatch();
bool init();
void shutdown();
void begin(const glm::mat4 &viewProjection);
void draw(const Texture &texture, const SpriteData &data);
void end();
// 批量绘制接口 - 用于自动批处理
void drawBatch(const Texture &texture,
const std::vector<SpriteData> &sprites);
// 立即绘制(不缓存)
void drawImmediate(const Texture &texture, const SpriteData &data);
// 统计
uint32_t getDrawCallCount() const { return drawCallCount_; }
uint32_t getSpriteCount() const { return spriteCount_; }
uint32_t getBatchCount() const { return batchCount_; }
// 检查是否需要刷新
bool needsFlush(const Texture &texture, bool isSDF) const;
private:
GLuint vao_;
GLuint vbo_;
GLuint ibo_;
Ptr<IShader> shader_;
// 使用固定大小数组减少内存分配
std::array<Vertex, MAX_VERTICES> vertexBuffer_;
size_t vertexCount_;
const Texture *currentTexture_;
bool currentIsSDF_;
glm::mat4 viewProjection_;
// 缓存上一帧的 viewProjection避免重复设置
glm::mat4 cachedViewProjection_;
bool viewProjectionDirty_ = true;
uint32_t drawCallCount_;
uint32_t spriteCount_;
uint32_t batchCount_;
void flush();
void setupShader();
// 添加顶点到缓冲区
void addVertices(const SpriteData &data);
};
} // namespace frostbite2D

View File

@@ -0,0 +1,79 @@
#pragma once
#include <fostbite2D/core/color.h>
#include <fostbite2D/core/types.h>
#include <fostbite2D/render/texture.h>
#include <glad/glad.h>
#include <memory>
#include <string>
namespace frostbite2D {
// ============================================================================
// OpenGL 纹理实现
// ============================================================================
class GLTexture : public Texture {
public:
GLTexture(int width, int height, const uint8_t *pixels, int channels);
GLTexture(const std::string &filepath);
~GLTexture();
// Texture 接口实现
int getWidth() const override { return width_; }
int getHeight() const override { return height_; }
Size getSize() const override {
return Size(static_cast<float>(width_), static_cast<float>(height_));
}
int getChannels() const override { return channels_; }
PixelFormat getFormat() const override;
void *getNativeHandle() const override {
return reinterpret_cast<void *>(static_cast<uintptr_t>(textureID_));
}
bool isValid() const override { return textureID_ != 0; }
void setFilter(bool linear) override;
void setWrap(bool repeat) override;
// 从参数创建纹理的工厂方法
static Ptr<Texture> create(int width, int height, PixelFormat format);
// 加载压缩纹理KTX/DDS 格式)
bool loadCompressed(const std::string &filepath);
// OpenGL 特定
GLuint getTextureID() const { return textureID_; }
void bind(unsigned int slot = 0) const;
void unbind() const;
// 获取纹理数据大小(字节),用于 VRAM 跟踪
size_t getDataSize() const { return dataSize_; }
// Alpha 遮罩
bool hasAlphaMask() const {
return alphaMask_ != nullptr && alphaMask_->isValid();
}
const AlphaMask *getAlphaMask() const { return alphaMask_.get(); }
void generateAlphaMask(); // 从当前纹理数据生成遮罩
private:
GLuint textureID_;
int width_;
int height_;
int channels_;
PixelFormat format_;
size_t dataSize_;
// 原始像素数据(用于生成遮罩)
std::vector<uint8_t> pixelData_;
std::unique_ptr<AlphaMask> alphaMask_;
void createTexture(const uint8_t *pixels);
// KTX 文件加载
bool loadKTX(const std::string &filepath);
// DDS 文件加载
bool loadDDS(const std::string &filepath);
};
} // namespace frostbite2D