Files
XCEngine/docs/api/rhi/opengl/opengl-vertex-array.md

89 lines
1.8 KiB
Markdown

# OpenGLVertexArray
OpenGL 顶点数组对象 (VAO) 封装。
## 头文件
```cpp
#include <XCEngine/RHI/OpenGL/OpenGLVertexArray.h>
```
## VertexAttribute
```cpp
struct VertexAttribute {
unsigned int index; // 属性索引 (layout location)
int count; // 组件数量 (1-4)
unsigned int type; // GL 类型 (GL_FLOAT, etc.)
bool normalized; // 是否归一化
size_t stride; // 顶点步长
size_t offset; // 属性偏移
};
```
## 公共成员函数
#### `OpenGLVertexArray()`
#### `~OpenGLVertexArray()`
#### `bool Initialize()`
#### `void AddVertexBuffer(unsigned int buffer, const VertexAttribute& attribute)`
#### `void SetIndexBuffer(unsigned int buffer, unsigned int type)`
#### `void Shutdown()`
#### `void Bind() const`
#### `void Unbind() const`
#### `unsigned int GetID() const`
#### `unsigned int GetIndexBuffer() const`
#### `unsigned int GetIndexCount() const`
## 内部成员
| 成员 | 类型 | 描述 |
|------|------|------|
| `m_vao` | `unsigned int` | GL VAO ID |
| `m_indexBuffer` | `unsigned int` | 索引缓冲区 |
| `m_indexCount` | `unsigned int` | 索引数量 |
| `m_vertexBufferCount` | `int` | 顶点缓冲区数量 |
## 使用示例
```cpp
OpenGLVertexArray vao;
vao.Initialize();
// Position attribute
VertexAttribute pos;
pos.index = 0;
pos.count = 3;
pos.type = GL_FLOAT;
pos.normalized = false;
pos.stride = sizeof(Vertex);
pos.offset = 0;
vao.AddVertexBuffer(vb.GetID(), pos);
// UV attribute
VertexAttribute uv;
uv.index = 1;
uv.count = 2;
uv.type = GL_FLOAT;
uv.normalized = false;
uv.stride = sizeof(Vertex);
uv.offset = sizeof(float) * 3;
vao.AddVertexBuffer(vb.GetID(), uv);
// Index buffer
vao.SetIndexBuffer(ib.GetID(), GL_UNSIGNED_SHORT);
vao.Bind();
glDrawElements(GL_TRIANGLES, vao.GetIndexCount(), GL_UNSIGNED_SHORT, 0);
```