class CubeModel : IBufferSource { private const float halfLength = 0.5f; private static readonly vec3[] positions = new vec3[] { new vec3(+halfLength, +halfLength, +halfLength), // 0 new vec3(+halfLength, +halfLength, -halfLength), // 1 new vec3(+halfLength, -halfLength, +halfLength), // 2 new vec3(+halfLength, -halfLength, -halfLength), // 3 new vec3(-halfLength, +halfLength, +halfLength), // 4 new vec3(-halfLength, +halfLength, -halfLength), // 5 new vec3(-halfLength, -halfLength, +halfLength), // 6 new vec3(-halfLength, -halfLength, -halfLength), // 7 }; private static readonly uint[] indexes = new uint[] { 0, 2, 1, 1, 2, 3, // +X faces. 0, 1, 5, 0, 5, 4, // +Y faces. 0, 4, 2, 2, 4, 6, // +Z faces. 7, 6, 4, 7, 4, 5, // -X faces. 7, 5, 3, 3, 5, 1, // -Z faces. 7, 3, 2, 7, 2, 6, // -Y faces. }; public const string strPosition = "position"; private VertexBuffer positionBuffer; // array in GPU side. private IDrawCommand drawCommand; #region IBufferSource 成员 public IEnumerable<VertexBuffer> GetVertexAttribute(string bufferName) { if (strPosition == bufferName) // requiring position buffer. { if (this.positionBuffer == null) { // transform managed array to vertex buffer. this.positionBuffer = positions.GenVertexBuffer( VBOConfig.Vec3, // mapping to 'in vec3 someVar;' in vertex shader. BufferUsage.StaticDraw); // GL_STATIC_DRAW. } yield return this.positionBuffer; } else { throw new ArgumentException("bufferName"); } } public IEnumerable<IDrawCommand> GetDrawCommand() { if (this.drawCommand == null) { // indexes in GPU side. IndexBuffer indexBuffer = indexes.GenIndexBuffer(BufferUsage.StaticDraw); this.drawCommand = new DrawElementsCmd(indexBuffer, DrawMode.Triangles); // GL_TRIANGLES. } yield return this.drawCommand; } #endregion }逐行分段解释上述程序
时间: 2023-06-14 07:07:07 浏览: 48
这段程序定义了一个名为 CubeModel 的类,实现了 IBufferSource 接口。
```c#
class CubeModel : IBufferSource {
```
类中定义了一个常量 halfLength,值为 0.5。
```c#
private const float halfLength = 0.5f;
```
类中定义了一个静态只读的 vec3 数组 positions,包含了八个顶点的位置。
```c#
private static readonly vec3[] positions = new vec3[] {
new vec3(+halfLength, +halfLength, +halfLength), // 0
new vec3(+halfLength, +halfLength, -halfLength), // 1
new vec3(+halfLength, -halfLength, +halfLength), // 2
new vec3(+halfLength, -halfLength, -halfLength), // 3
new vec3(-halfLength, +halfLength, +halfLength), // 4
new vec3(-halfLength, +halfLength, -halfLength), // 5
new vec3(-halfLength, -halfLength, +halfLength), // 6
new vec3(-halfLength, -halfLength, -halfLength), // 7
};
```
类中定义了一个静态只读的 uint 数组 indexes,包含了组成立方体的三角形面的顶点索引。
```c#
private static readonly uint[] indexes = new uint[] {
0, 2, 1, 1, 2, 3, // +X faces.
0, 1, 5, 0, 5, 4, // +Y faces.
0, 4, 2, 2, 4, 6, // +Z faces.
7, 6, 4, 7, 4, 5, // -X faces.
7, 5, 3, 3, 5, 1, // -Z faces.
7, 3, 2, 7, 2, 6, // -Y faces.
};
```
类中定义了一个常量 strPosition,值为 "position"。
```c#
public const string strPosition = "position";
```
类中定义了一个私有的 VertexBuffer 类型的 positionBuffer,用于在 GPU 中存储顶点位置数据。
```c#
private VertexBuffer positionBuffer; // array in GPU side.
```
类中定义了一个私有的 IDrawCommand 类型的 drawCommand,用于在 GPU 中绘制立方体。
```c#
private IDrawCommand drawCommand;
```
接下来是实现 IBufferSource 接口的方法。
```c#
#region IBufferSource 成员
public IEnumerable<VertexBuffer> GetVertexAttribute(string bufferName) {
if (strPosition == bufferName) // requiring position buffer.
{
if (this.positionBuffer == null) {
// transform managed array to vertex buffer.
this.positionBuffer = positions.GenVertexBuffer(
VBOConfig.Vec3, // mapping to 'in vec3 someVar;' in vertex shader.
BufferUsage.StaticDraw); // GL_STATIC_DRAW.
}
yield return this.positionBuffer;
}
else {
throw new ArgumentException("bufferName");
}
}
```
GetVertexAttribute 方法返回一个可枚举的 VertexBuffer 集合,根据输入的 bufferName 参数选择返回的 VertexBuffer。如果 bufferName 的值等于 strPosition 常量的值,则返回存储顶点位置数据的 positionBuffer;否则抛出 ArgumentException 异常。
```c#
public IEnumerable<IDrawCommand> GetDrawCommand() {
if (this.drawCommand == null) {
// indexes in GPU side.
IndexBuffer indexBuffer = indexes.GenIndexBuffer(BufferUsage.StaticDraw);
this.drawCommand = new DrawElementsCmd(indexBuffer, DrawMode.Triangles); // GL_TRIANGLES.
}
yield return this.drawCommand;
}
#endregion
```
GetDrawCommand 方法返回一个可枚举的 IDrawCommand 集合,返回用于绘制立方体的 IDrawCommand。如果 drawCommand 为空,则创建一个 IDrawCommand 对象,存储绘制立方体所需的索引数据,并将其赋值给 drawCommand 变量。然后返回该 drawCommand 变量。