Hello everyone,
I am not experienced at osg and want to post my question here (please bear me if my question is stupid).
Here is the backround of my question:
I get 400 ~ 800 incoming points of simulated road marks by one programm every 20 ~ 30 ms. My Programm is fed by these points continuously on the main thread. My goal is to visualise them by connecting them into lines:
After reading many releated posts on this forum, I got some ideas and implemented them:
this is basic setup:
osg::ref_ptr<osg::Geode> road_mark_geode; // hold the vertices inside my program
...
osg::ref_ptr<osg::Geometry> geom(new osg::Geometry());
geom->setUseVertexBufferObjects(true);
geom->setUseDisplayList(false);
geom->setUpdateCallback(new DynamicRoadMarkCallback); // I customized a osg::Drawable::UpdateCallback to run vertices->dirty(), to update inside road mark vertices
osg::Vec3Array* vertices(new osg::Vec3Array());
geom->setVertexArray(vertices);
geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP, 0, DEFAULT_VERTEX_SIZE));
road_mark_geode->addDrawable(geom.get());
...
Given that data reception is on main thread, i did this (points_in is incoming data from another program):
Loop on main thread:
receive points and store them -> call function "update_pos_of_vertices " to set the pos (x, y, z) for vertices by new points.
Blow is the interested code snippet:
getdata(points_in, points_save); // save incoming data into "points_save" of my own data type
update_pos_of_vertices (points_save) {
osg::Geometry* geo_drawable = static_cast<osg::Geometry*>(road_mark_geode->getDrawable(0));
geo_drawable->setDataVariance(osg::Object::DYNAMIC);
osg::DrawArrays* drawArrays = static_cast<osg::DrawArrays*>(geo_drawable->getPrimitiveSet(0));
osg::Vec3Array* vertices = static_cast<osg::Vec3Array*>(geo_drawable->getVertexArray());
int num_vertices = points_save.size();
drawArrays->setCount(num_vertices);
vertices->resize(num_vertices);
for (... ) {
vertices->at(i).set(points_save[i]); // set new pos of each vertex
}
}
My program ran and i got visualized lines which are linked by points (see pic in attachment). But my program becomes very slow: average time for one single loop is 50ms. So my question is:
How can I improve the performance?
Did I do anything wrong regarding the my implementation above?
Look forward to any help, tips and remarks!
Thank you
Yuan