You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从文件填充索引数组为何会导致Silk.Net中缓冲区值损坏?

从JSON加载网格索引时OpenGL缓冲区损坏问题

我正在阅读Madhav Sanjay所著的《Game Programming in C++》,完成书中C++练习后,将代码迁移至使用Silk.Net(.Net平台下的OpenGL包装库)的C#项目中。

在加载网格时遇到异常:手动填充索引数组时,索引能正确传输至OpenGL(已通过RenderDoc验证),代码如下:

var indices = new uint[]
{
    2, 1, 0,
    3, 9, 8,
    4, 11, 10,
    5, 11, 12,
    6, 14, 13,
    7, 14, 15,
    18, 17, 16,
    19, 17, 18,
    22, 21, 20,
    23, 21, 22,
    26, 25, 24,
    27, 25, 26,
};

var vbo = new BufferObject<float>(game.Renderer.GL, vertices, BufferTargetARB.ArrayBuffer);
var ebo = new BufferObject<uint>(game.Renderer.GL, indices, BufferTargetARB.ElementArrayBuffer);
var vao = new VertexArrayObject(game.Renderer.GL, vbo, ebo);

但从JSON文件读取内容填充索引数组时(索引以数组的数组形式存储),缓冲区值出现损坏。JSON文件内容如下:

{
    "version":1,
    "vertexformat":"PosNormTex",
    "shader":"BasicMesh",
    "textures":[
        "Assets/Cube.png"
    ],
    "specularPower":100.0,
    "vertices":[
        [-0.5,-0.5,-0.5,0,0,-1,0,0],
        [0.5,-0.5,-0.5,0,0,-1,1,0],
        [-0.5,0.5,-0.5,0,0,-1,0,-1],
        [0.5,0.5,-0.5,0,0,-1,1,-1],
        [-0.5,0.5,0.5,0,1,0,0,-1],
        [0.5,0.5,0.5,0,1,0,1,-1],
        [-0.5,-0.5,0.5,0,0,1,0,0],
        [0.5,-0.5,0.5,0,0,1,1,0],
        [-0.5,0.5,-0.5,0,0,-1,0,-1],
        [0.5,-0.5,-0.5,0,0,-1,1,0],
        [-0.5,0.5,-0.5,0,1,0,0,-1],
        [0.5,0.5,-0.5,0,1,0,1,-1],
        [-0.5,0.5,0.5,0,1,0,0,-1],
        [-0.5,0.5,0.5,0,0,1,0,-1],
        [0.5,0.5,0.5,0,0,1,1,-1],
        [-0.5,-0.5,0.5,0,0,1,0,0],
        [-0.5,-0.5,0.5,0,-1,0,0,0],
        [0.5,-0.5,0.5,0,-1,0,1,0],
        [-0.5,-0.5,-0.5,0,-1,0,0,0],
        [0.5,-0.5,-0.5,0,-1,0,1,0],
        [0.5,-0.5,-0.5,1,0,0,1,0],
        [0.5,-0.5,0.5,1,0,0,1,0],
        [0.5,0.5,-0.5,1,0,0,1,-1],
        [0.5,0.5,0.5,1,0,0,1,-1],
        [-0.5,-0.5,0.5,-1,0,0,0,0],
        [-0.5,-0.5,-0.5,-1,0,0,0,0],
        [-0.5,0.5,0.5,-1,0,0,0,-1],
        [-0.5,0.5,-0.5,-1,0,0,0,-1]
    ],
    "indices":[
        [2,1,0],
        [3,9,8],
        [4,11,10],
        [5,11,12],
        [6,14,13],
        [7,14,15],
        [18,17,16],
        [19,17,18],
        [22,21,20],
        [23,21,22],
        [26,25,24],
        [27,25,26]
    ]
}

我确认最终索引数组内容和手动填充的完全一致,填充逻辑代码如下:

var indices = new uint[raw.Indices.Length * 3];
for (int i = 0; i < raw.Indices.Length; i++)
{
    var index = raw.Indices[i];
    if (index is null || index.Length != 3)
    {
        throw new MeshException($"Invalid indices for {fileName}.");
    }

    var offset = i * 3;
    for (int j = 0; j < index.Length; j++)
    {
        indices[offset + j] = index[j];
    }
}

完全找不到问题所在。


内容的提问来源于stack exchange,提问作者lehmamic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 17:15:37