120 lines
2.5 KiB
C++
120 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#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>
|
|
|
|
// 前向声明 OpenGL 类型
|
|
typedef unsigned int GLuint;
|
|
|
|
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 网格类
|
|
*
|
|
* 管理 OpenGL 顶点数组对象和缓冲区
|
|
* 支持静态和动态顶点数据
|
|
*/
|
|
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 绑定网格
|
|
*/
|
|
void bind() const;
|
|
|
|
/**
|
|
* @brief 解绑网格
|
|
*/
|
|
void unbind() const;
|
|
|
|
/**
|
|
* @brief 绘制网格
|
|
*/
|
|
void draw() const;
|
|
|
|
/**
|
|
* @brief 实例化绘制
|
|
* @param instanceCount 实例数量
|
|
*/
|
|
void drawInstanced(uint32_t instanceCount) const;
|
|
|
|
/**
|
|
* @brief 获取顶点数量
|
|
* @return 顶点数量
|
|
*/
|
|
uint32_t getVertexCount() const { return vertexCount_; }
|
|
|
|
/**
|
|
* @brief 获取索引数量
|
|
* @return 索引数量
|
|
*/
|
|
uint32_t getIndexCount() const { return indexCount_; }
|
|
|
|
/**
|
|
* @brief 创建标准四边形
|
|
* @param size 四边形大小
|
|
* @param uv 纹理坐标范围
|
|
* @return 网格对象
|
|
*/
|
|
static Ptr<Mesh> createQuad(const Vec2 &size,
|
|
const Rect &uv = Rect(0, 0, 1, 1));
|
|
|
|
private:
|
|
GLuint vao_ = 0; // 顶点数组对象
|
|
GLuint vbo_ = 0; // 顶点缓冲区
|
|
GLuint ibo_ = 0; // 索引缓冲区
|
|
uint32_t vertexCount_ = 0; // 顶点数量
|
|
uint32_t indexCount_ = 0; // 索引数量
|
|
uint32_t vertexCapacity_ = 0; // 顶点缓冲区容量
|
|
uint32_t indexCapacity_ = 0; // 索引缓冲区容量
|
|
};
|
|
|
|
} // namespace extra2d
|