Files
XCEngine/docs/api/rhi/opengl/vertex-array/vertex-attribute.md
2026-03-20 02:35:45 +08:00

72 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# VertexAttribute
**命名空间**: `XCEngine::RHI`
**描述**: 顶点属性描述结构体,用于定义顶点缓冲区中单个属性的布局。
## 成员
| 成员 | 类型 | 描述 |
|------|------|------|
| `index` | `unsigned int` | 属性索引,对应着色器中 `layout(location = n)` |
| `count` | `int` | 分量数量1-4如位置用 3纹理坐标用 2 |
| `type` | `unsigned int` | OpenGL 数据类型,如 `GL_FLOAT``GL_INT` |
| `normalized` | `bool` | 是否归一化Fixed-point类型 |
| `stride` | `size_t` | 连续顶点属性之间的字节偏移 |
| `offset` | `size_t` | 此属性在顶点结构体中的字节偏移 |
## 类型取值参考
| 类型常量 | 描述 |
|----------|------|
| `GL_BYTE` | 有符号字节 |
| `GL_UNSIGNED_BYTE` | 无符号字节 |
| `GL_SHORT` | 有符号短整型 |
| `GL_UNSIGNED_SHORT` | 无符号短整型 |
| `GL_INT` | 有符号整型 |
| `GL_UNSIGNED_INT` | 无符号整型 |
| `GL_FLOAT` | 单精度浮点 |
| `GL_DOUBLE` | 双精度浮点 |
## 使用示例
```cpp
// 定义顶点结构体
struct Vertex {
float position[3];
float normal[3];
float texCoord[2];
};
// 位置属性 (index=0)
VertexAttribute posAttr;
posAttr.index = 0;
posAttr.count = 3;
posAttr.type = GL_FLOAT;
posAttr.normalized = GL_FALSE;
posAttr.stride = sizeof(Vertex);
posAttr.offset = 0;
// 法线属性 (index=1)
VertexAttribute normalAttr;
normalAttr.index = 1;
normalAttr.count = 3;
normalAttr.type = GL_FLOAT;
normalAttr.normalized = GL_FALSE;
normalAttr.stride = sizeof(Vertex);
normalAttr.offset = offsetof(Vertex, normal);
// 纹理坐标属性 (index=2)
VertexAttribute texAttr;
texAttr.index = 2;
texAttr.count = 2;
texAttr.type = GL_FLOAT;
texAttr.normalized = GL_FALSE;
texAttr.stride = sizeof(Vertex);
texAttr.offset = offsetof(Vertex, texCoord);
```
## 相关文档
- [OpenGLVertexArray](vertex-array.md) - 返回类总览
- [AddVertexBuffer](add-vertex-buffer.md) - 添加顶点缓冲方法