Hi, there,
I’m having some trouble animating the vertex color when using a VBO mesh. In a nutshell, I’m animating a texture drawn onto a plane. I’m animating the model matrix to get the appropriate movement, and I’d like to animate the color of the vertices in conjunction with this. Using the VboMesh sample as a model, I’m setting up the VBO like this (and for the moment, the customShader
is currently the stock shader created using ci::gl::ShaderDef().texture().color()
:
// create VBO and batch
ci::geom::Plane plane = ci::geom::Plane().size( ci::vec2( 1 ) ).normal( ci::vec3( 0, 0, 1 ) );
std::vector<ci::gl::VboMesh::Layout> bufferLayout = {
ci::gl::VboMesh::Layout().usage( GL_STATIC_DRAW ).attrib( ci::geom::Attrib::POSITION, 3 ),
ci::gl::VboMesh::Layout().usage( GL_STATIC_DRAW ).attrib( ci::geom::Attrib::TEX_COORD_0, 2 ),
ci::gl::VboMesh::Layout().usage( GL_DYNAMIC_DRAW ).attrib( ci::geom::Attrib::COLOR, 4 )
};
mPlaneVBOMesh = ci::gl::VboMesh::create( plane, bufferLayout );
mTiltedTextureVBOBatch = ci::gl::Batch::create( mPlaneVBOMesh, customShader );
I’m trying to change the color attributes in the update loop. To start, I’m just trying to change them based on time, to see if it’s working correctly, which it isn’t:
//
float testFloat = sin( ci::app::getElapsedSeconds() );
auto mappedColorAttrib = mPlaneVBOMesh->mapAttrib4f( ci::geom::Attrib::COLOR, true );
for( int i = 0; i < mPlaneVBOMesh->getNumVertices(); i++ ) {
if( i == 0 ) {
mappedColorAttrib->r = testFloat;
}
else if( i == 1 ) {
mappedColorAttrib->g = testFloat;
}
else if( i == 2 ) {
mappedColorAttrib->b = testFloat;
}
++mappedColorAttrib;
}
mappedColorAttrib.unmap();
mTiltedTextureVBOBatch->draw();
I’m getting really inconsistent results: sometimes when I build it, it won’t draw at all; other times the plane will show all white or a different color. This makes me think I’m setting up the attributes incorrectly when I’m creating the VboMesh.
I’ve tried changing the position attribute dimensions from 3 to 4, but that doesn’t solve the issue. The VboMesh sample uses 3 here, but the shader itself seems to expect a vec4.
Any advice on getting this working would be much appreciated. In addition, I’d also love to get a better way to update each vertex individually, rather than looping over them and testing which iteration I’m on. This feels very clunky, and I’m sure there’s a cleaner way.
Thanks for any help!
Best,
Jennifer