Extra2D/include/renderer/mesh.h

114 lines
2.8 KiB
C++

#pragma once
#include <renderer/rhi/rhi.h>
#include <types/math/color.h>
#include <types/math/rect.h>
#include <types/math/vec2.h>
#include <types/ptr/intrusive_ptr.h>
#include <types/ptr/ref_counted.h>
namespace extra2d {
/**
* @brief 顶点结构
*/
struct Vertex {
Vec2 position; // 位置
Vec2 texCoord; // 纹理坐标
Color color; // 颜色
Vertex() = default;
Vertex(const Vec2& pos, const Vec2& uv, const Color& col)
: position(pos), texCoord(uv), color(col) {}
};
/**
* @brief 网格类
*
* 基于 RHI 的网格包装类,管理顶点缓冲区和索引缓冲区
* 支持静态和动态顶点数据
*/
class Mesh : public RefCounted {
public:
/**
* @brief 默认构造函数
*/
Mesh();
/**
* @brief 析构函数
*/
~Mesh() override;
/**
* @brief 设置顶点数据
* @param vertices 顶点数组
* @param count 顶点数量
*/
void setVertices(const Vertex* vertices, uint32_t count);
/**
* @brief 设置索引数据
* @param indices 索引数组
* @param count 索引数量
*/
void setIndices(const uint16_t* indices, uint32_t count);
/**
* @brief 更新部分顶点数据
* @param vertices 顶点数据
* @param count 顶点数量
* @param offset 偏移量
*/
void updateVertices(const Vertex* vertices, uint32_t count, uint32_t offset);
/**
* @brief 获取顶点数量
* @return 顶点数量
*/
uint32_t getVertexCount() const { return vertexCount_; }
/**
* @brief 获取索引数量
* @return 索引数量
*/
uint32_t getIndexCount() const { return indexCount_; }
/**
* @brief 获取顶点缓冲区句柄
* @return 顶点缓冲区句柄
*/
BufferHandle getVertexBuffer() const { return vertexBuffer_; }
/**
* @brief 获取索引缓冲区句柄
* @return 索引缓冲区句柄
*/
BufferHandle getIndexBuffer() const { return indexBuffer_; }
/**
* @brief 检查是否有索引数据
* @return 是否有索引数据
*/
bool hasIndices() const { return indexCount_ > 0; }
/**
* @brief 创建标准四边形
* @param size 四边形大小
* @param uv 纹理坐标范围
* @return 网格对象
*/
static Ptr<Mesh> createQuad(const Vec2& size,
const Rect& uv = Rect(0, 0, 1, 1));
private:
BufferHandle vertexBuffer_; // 顶点缓冲区句柄
BufferHandle indexBuffer_; // 索引缓冲区句柄
uint32_t vertexCount_ = 0; // 顶点数量
uint32_t indexCount_ = 0; // 索引数量
uint32_t vertexCapacity_ = 0; // 顶点缓冲区容量
uint32_t indexCapacity_ = 0; // 索引缓冲区容量
};
} // namespace extra2d