Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

harrys1590

macrumors newbie
Original poster
Jul 14, 2013
8
0
In my app I have a view that renders STL files. STL files are essentially a list of triangle vertices with normals, they have no indices.

Every tutorial that I have found explains how to use multiple VBOs but with Indices and when I try using drawArrays rather than drawElements it doesn't work. I'm new to OpenGL and would really appreciate it if someone could provide code examples for using multiple VBOs for setup and drawing them without indices or just references to understandable documentation from which I could learn how to do it.

Thanks for any help.
 
Setup:

Code:
glGenBuffers(1, &vertices);
glBindBuffer(GL_ARRAY_BUFFER, vertices);
glBufferData(GL_ARRAY_BUFFER, numberOfVertices*sizeof(GLfloat),vertexData, GL_STATIC_DRAW);

glGenBuffers(1, &normals);
glBindBuffer(GL_ARRAY_BUFFER, normals);
glBufferData(GL_ARRAY_BUFFER, numberOfNormals*sizeof(GLfloat),normalData, GL_STATIC_DRAW);


Drawing:

Code:
//Assuming OpenGL ES 2.0 with shaders

glUniformMatrix4fv(shaderProgram.modelViewProjectionMatrix, 1, 0, modelViewProjectionMatrix.m);

//Bind the vertices
glBindBuffer(GL_ARRAY_BUFFER, vertices);
//Tell opengl what attribute this data is for.
glEnableVertexAttribArray(shaderProgram.ATTRIB_VERTEX);
glVertexAttribPointer(shaderProgram.ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, 12, BUFFER_OFFSET(0));

//Bind the normal data buffer
glBindBuffer(GL_ARRAY_BUFFER, normals);
//Specify it to the normal attribute like we did for vertex data
glEnableVertexAttribArray(shaderProgram.ATTRIB_NORMAL);
glVertexAttribPointer(shaderProgram.ATTRIB_NORMAL, 3, GL_FLOAT, GL_FALSE, 12, BUFFER_OFFSET(0));

glDrawArrays(GL_TRIANGLES, 0, triangleCount);
 
Last edited by a moderator:
That's only for one set of vertices though, are you saying that if I repeat those in their respective areas then that will work?

----------

I tried just repeating that and it did not work. I need to render two sets of vertex data without combining them.
 
Okay, here is my code in SetupGL:

Code:
[EAGLContext setCurrentContext:self.context];
    
    [self loadShaders];
    
    self.effect = [[GLKBaseEffect alloc] init];
    self.effect.light0.enabled = GL_TRUE;
    self.effect.light0.diffuseColor = GLKVector4Make(.05f, .55f, 1.0f, 1.0f);
    
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.88f, 0.88f, 0.88f, 1.0f);
    
    //---- First Vertex Array Object --------
    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);
    
    glBindVertexArrayOES(_vertexArray);
    
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, vertCount*sizeof(verticesBuff) * 3 * 2, verticesBuff, GL_STATIC_DRAW);
    
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
    //    glEnableVertexAttribArray(GLKVertexAttribNormal);
    //    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
    
    //----- Second Vertex Array Object ----------
    glGenBuffers(1, &_gridVertexBuffer);
    
    glBindBuffer(GL_ARRAY_BUFFER, _gridVertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData), gCubeVertexData, GL_STATIC_DRAW);
    
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
    //    glEnableVertexAttribArray(GLKVertexAttribNormal);
    //    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
    
    glBindBuffer(GL_ARRAY_BUFFER,0);
    glBindVertexArrayOES(0);
    
    glBindVertexArrayOES(0);

And here is my code in glkView:(GLKView *)view drawInRect:(CGRect)rect:

Code:
glBindVertexArrayOES(_vertexArray);
    
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glUseProgram(_program);
    glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
    glDrawArrays(GL_TRIANGLES, 0, vertCount);
    
    ///////// second VBO and shader program:
    glBindBuffer(GL_ARRAY_BUFFER, _gridVertexBuffer);
    glUseProgram(_program);
    glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
    glDrawArrays(GL_TRIANGLES, 0, 108);

And that is drawing nothing.
 
Try not using the vao. In the draw code bind the buffer and specify the vertex attributes like in the code I posted.

Code:
//Unless you are using a different shader to draw, once you call useprogram anything you draw will use that program.
glUseProgram(_program);
//You only need to pass the modelViewProjectionMatrix to the shader once per draw call unless it's modified for different objects.
glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);

glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));    
glDrawArrays(GL_TRIANGLES, 0, vertCount);
    
 ///////// second VBO and shader program:
glBindBuffer(GL_ARRAY_BUFFER, _gridVertexBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));  
glDrawArrays(GL_TRIANGLES, 0, 108);
 
Last edited by a moderator:
That didn't work, it just cause my program to crash with the no error...

This is code that I got to render one just the verticesBuff but not gridVerticesBuff. SetupGL:
Code:
[EAGLContext setCurrentContext:self.context];

[self loadShaders];

self.effect = [[GLKBaseEffect alloc] init];
self.effect.light0.enabled = GL_TRUE;
self.effect.light0.diffuseColor = GLKVector4Make(.05f, .55f, 1.0f, 1.0f);

glEnable(GL_DEPTH_TEST);
glClearColor(0.88f, 0.88f, 0.88f, 1.0f);

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

glGenBuffers(1, &_gridVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _gridVertexBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
glBufferData(GL_ARRAY_BUFFER, 108 * sizeof(gridVerticesBuff), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(gridVerticesBuff) * 108, gridVerticesBuff);


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

glBindVertexArrayOES(0);

And here's the draw code:
Code:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (loaded) {
    [self.effect prepareToDraw];

    glBindVertexArrayOES(_gridVertexArray);
    glBindBuffer(GL_ARRAY_BUFFER, _gridVertexBuffer);
    glDrawArrays(GL_TRIANGLES, 0, 108);

    glBindVertexArrayOES(_vertexArray);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glDrawArrays(GL_TRIANGLES, 0, vertCount);
 }
 
You only show the creation of the _vertexArray vao. I don't see the _gridVertexArray vao in your setup. Did you create that one?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.