0

我无法设置我的 OpenGL ES 代码。我有理由确定我的代码问题是我设置 glVertexAttribPointer 的地方。这是我如何将顶点和索引插入到它们的数组中:

    NSArray *verts = [JSONDictionary objectForKey:@"vertices"];
    GLfloat *vertices = malloc(sizeof(GLfloat) * [verts count]);
    for (int i = 0; i < [verts count]; i++) {
        double vert = [[verts objectAtIndex:i] doubleValue];
        vertices[i] = vert;
    }
    verticesBuff = vertices;

    NSArray *indexArray = [JSONDictionary objectForKey:@"faces"];
    GLuint *indices = malloc(sizeof(GLuint) * [indexArray count]);
    for (int i = 0; i < [indexArray count]; i++) {
        double index = [[indexArray objectAtIndex:i] doubleValue];
        indices[i] = index;
    }
    indicesBuff = indices;

然后这是我缓冲数据的方式:

    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);

    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, vertCount, verticesBuff, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, vertCount, verticesBuff);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 12, BUFFER_OFFSET(0));
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 12, BUFFER_OFFSET(12));

    glGenBuffers(1, &_indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount, indicesBuff, GL_STATIC_DRAW);
    glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indexCount, indicesBuff);

    glBindVertexArrayOES(0);

这段代码确实以我正在渲染的文件的一般形状呈现东西,但它有许多突出的三角形和缺失的区域。我怀疑问题出在我设置 glEnableVertexAttribArray 的位置,但我一直无法弄清楚如何设置它以使我的数据即使在查看规范之后也能正确加载。

如果有人能告诉我,我会怎么吃饱。

这是我试图加载测试的 ThreeJS 文件:http: //thingiverse-production.s3.amazonaws.com/threejs_json/8c/13/d8/2b/76/ca83d33d20mmTestCube_repaired.js

4

0 回答 0