Constructing VBO

Hi,

it’s been a while since I used Cinder and it looks like many things have changed.

I’m trying to construct a vbo manually, but I must be doing something wrong as I can’t see anything on screen. Regardless the actual geometry(random points atm) I’m creating a simple vbo with static points, indices and colors.

const int numVerts = 9;
std::vector<vec3>       positions;
std::vector<ColorA>     colors;
std::vector<uint32_t>   indices;

for( size_t k=0; k < numVerts; k++ )
{
    indices.push_back( k );
    positions.push_back( vec3( randFloat(-10.0f, 10.0f), randFloat(-10.0f, 10.0f), randFloat(-10.0f, 10.0f) ) );
    colors.push_back( ColorA(1.0,0.0,0.0,1.0) );
}

vector<gl::VboMesh::Layout> layout = {
    gl::VboMesh::Layout().usage( GL_STATIC_DRAW ).attrib( geom::Attrib::POSITION, 3 ),
    gl::VboMesh::Layout().usage( GL_STATIC_DRAW ).attrib( geom::Attrib::COLOR, 4 )
};

mVboMesh = gl::VboMesh::create( numVerts, GL_TRIANGLES, { layout }, numVerts );

mVboMesh->bufferAttrib<vec3>( geom::POSITION, positions );
mVboMesh->bufferAttrib<ColorA>( geom::COLOR, colors );
mVboMesh->bufferIndices( numVerts, indices.data() );

mBatch = gl::Batch::create( mVboMesh, gl::getStockShader( gl::ShaderDef().color() ) );

this is my draw()

gl::ScopedGlslProg scpGlslProg(gl::getStockShader(gl::ShaderDef().color()));
gl::draw( mVboMesh );
OR
mBatch->draw();

Two options, firstly, since in this case your indices match your vertices, you can just remove the “numIndices” argument you provided to VboMesh::create() and remove the call to ->bufferIndices()

Going forward though, you will need to allocate a Vbo as an index buffer and pass that to the VboMesh constructor, it’s not enough to simply tell the function how many indices there are. First of all, remove the call to ->bufferIndices(), and create your IBO and VboMesh thus:

gl::VboRef ibo = gl::Vbo::create ( GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW );
mVboMesh = gl::VboMesh::create( numVerts, GL_TRIANGLES, { layout }, indices.size(), GL_UNSIGNED_INT, ibo );

A.

1 Like

I’ve managed to draw points and triangles using the method above and also by streaming the data on each frame(GL_ARRAY_BUFFER / GL_STREAM_DRAW ), however the GL_QUADS doesn’t seem to work.

GL_QUADS were removed in OpenGL 3.1.

makes sense.
so the last thing I need to do is to buffer my indices, to draw quads I usually use 6 indices and 4 vertices. I’m doing something like this but it doesn’t seem to work.

// create indices and verts
indices.push_back( cellsCount * 4 );
indices.push_back( cellsCount * 4 + 1 );
indices.push_back( cellsCount * 4 + 2 );

indices.push_back( cellsCount * 4 );
indices.push_back( cellsCount * 4 + 2 );
indices.push_back( cellsCount * 4 + 3 );

mVerts.push_back( { vec3(randFloat(),randFloat(),randFloat()), ColorA( 1.0f, 1.0f, 1.0f, 1.0f ) } );
mVerts.push_back( { vec3(randFloat(),randFloat(),randFloat()), ColorA( 1.0f, 1.0f, 1.0f, 0.7f ) } );
mVerts.push_back( { vec3(randFloat(),randFloat(),randFloat()), ColorA( 1.0f, 1.0f, 1.0f, 0.2f ) } );
mVerts.push_back( { vec3(randFloat(),randFloat(),randFloat()), ColorA( 1.0f, 1.0f, 1.0f, 0.1f ) } );

// buffer indices
mVbo = gl::Vbo::create( GL_ARRAY_BUFFER, mVerts, GL_STREAM_DRAW );

geom::BufferLayout layout;
layout.append( geom::Attrib::POSITION, 3, sizeof( Vert ), offsetof( Vert, pos ) );
layout.append( geom::Attrib::COLOR, 4, sizeof( Vert ), offsetof( Vert, color ) );

auto mesh = gl::VboMesh::create( mVerts.size(), GL_TRIANGLES, { { layout, mVbo } }, indices.size() );
mesh->bufferIndices( indices.size(), indices.data() );

When you say it doesn’t work, do you mean you aren’t seeing anything, or you aren’t seeing what you want? I pasted your code into a sample app and it produced this:

Is that what you’re expecting to see?

1 Like

That looks pretty cool.