Extra2D/include/renderer/mesh.h

120 lines
2.5 KiB
C
Raw Normal View History

#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