[solved] Color of VboMesh


#1

It seems VboMesh doesn’t hold Layout color info.
As code below, circles hold color info but Batch changing color each frame.
Am I wrong usage of VboMesh::Layout?
I would be very appreciate for any help!

class Aobj{
public:
    Aobj();
    vec2 pos, dir;
    ColorA col;
    gl::BatchRef batch;
    gl::VboMeshRef mesh;
    void update();
    void draw();
};

Aobj::Aobj(){
    vector<gl::VboMesh::Layout> layout = {gl::VboMesh::Layout().usage(GL_DYNAMIC_DRAW).attrib(geom::POSITION, 2).attrib(geom::COLOR, 4)};
    mesh = gl::VboMesh::create(3, GL_TRIANGLES, layout);
    batch = gl::Batch::create(mesh, gl::getStockShader(gl::ShaderDef().color()));
    pos = vec2(randFloat(app::getWindowWidth()),randFloat(app::getWindowHeight()));
    col = ci::ColorA(randFloat(),randFloat(),randFloat(),1);
    dir = randVec2();
    auto vertColorIter = mesh->mapAttrib4f(geom::COLOR);
    for (int i=0; i<3; i++) *vertColorIter++ = col;
    vertColorIter.unmap();
}

void Aobj::update(){
    auto vertPosIter = mesh->mapAttrib2f( geom::POSITION );
    *vertPosIter++ = vec2( pos+vec2(15,0) );
    *vertPosIter++ = vec2( pos+vec2(0,-20) );
    *vertPosIter++ = vec2( pos+vec2(-15,0) );
    vertPosIter.unmap();
    pos += dir;
};

void Aobj::draw(){
    gl::color(col);
    gl::drawSolidCircle(pos, 10);
    if(batch) batch->draw();
};

class mainApp : public App {
    vector<Aobj> mObj;
};

void mainApp::update(){
    for(int i=0; i<5; ++i) mObj.push_back(Aobj());
    for( auto p=mObj.begin(); p!=mObj.end();){
        p->update();
        if (randInt(50)==0) p = mObj.erase(p);
        else ++p;
    }
}

void mainApp::draw(){
    gl::clear();
    for(auto &p:mObj) p.draw();
}

#2

just forgot to update color mapAttrib… thx