0

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

harryisaac
  • 1,041
  • 1
  • 9
  • 17
  • You should add what your data looks like (what is in the dictionary). Try changing the stride to 24 to begin with in "glVertexAttribPointer" in stead of 12. – Matic Oblak Aug 06 '13 at 06:18
  • That changed the shape of it a little. I have also just added the link to the ThreeJS file I am parsing. – harryisaac Aug 06 '13 at 14:14
  • First, you can remove the calls to `glBufferSubData` immediately after the call to `glBufferData`. Your calls to `glBufferData` will copy the data from your application into the VBO. Also, what's the relationship between **gCubeVertexData**, and **vertex** (and the same for indices)? Further, indices should be integer types. And if the vertex data you pass in are really doubles, your type for `glVertexAttribPointer` shouldn't probably be **GL_FLOAT**. – radical7 Aug 06 '13 at 14:16
  • I just removed the references to gCubeVertexData. That was from testing I was doing to just see if it would load a different array of data. A double shouldn't matter, I just wanted it to use more decimal places to be more accurate in more detailed models. – harryisaac Aug 06 '13 at 14:37
  • You're right; you're assigning that into a float. My bad. – radical7 Aug 06 '13 at 15:13
  • There is still much that can be wrong.. Since you have no normals change the stride back to 12 (rather to 3*sizeof(GLFloat)). You are probably drawing some garbage because your glBufferData function's size is set to vertCount. I don't know what vertCount is but it should be the size of the buffer in bytes so probably vertCount*3*sizeof(GLFloat). – Matic Oblak Aug 07 '13 at 07:02
  • The missing triangles could also be caused by the winding direction of the vertices not being consistent, if you have back face culling enabled. – ClayMontgomery Aug 08 '13 at 22:44

0 Answers0