iOS上的OpenGL ES如何设置glVertexAttribPointer

时间:2013-08-05 18:58:31

标签: ios opengl-es vertex-attributes

我在设置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

0 个答案:

没有答案