OpenGL ES on iOS What to set glVertexAttribPointer

575 views Asked by At

I am having trouble setting up my OpenGL ES code. I am reasonably sure that my codes problem is where I set glVertexAttribPointer. Here is how I am inserting vertices and indices into their arrays:

    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;

And then here is how I am buffering my data:

    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);

This code does render things in the general shape of the file that I am rending but it has many triangles jutting out and areas missing. I suspect that the problem is where I set glEnableVertexAttribArray but I have been unable figure out how to set it to in order for my data to load properly even after looking at the spec.

If anyone could tell me how I would be great full.

This is the ThreeJS file that I am trying to load to test: http://thingiverse-production.s3.amazonaws.com/threejs_json/8c/13/d8/2b/76/ca83d33d20mmTestCube_repaired.js

0

There are 0 answers