Visualization of the scene within a real-time loop #5192
-
Hello Sofa Community ! Sorry if the question has already been raised, but after many searches I found no answer for my concerns. I am trying to translate a python code (using an old version of Sofa) into a C++ library (using latest release of Sofa, 24.06.00). This code implements a real-time loop that simulates deformation of an object. The C++ translation of the simulation in itself should not be a big deal, but I face trouble in implementing the visualization loop. My main problems are:
The existing Python code roughly looks like: import Sofa
#other initialization instructions ...
root = Sofa.Core.Node("myroot")
#create the scene ...
Sofa.Simulation.init(root)
init_display(root)
while True
#instructions to update scene...
Sofa.Simulation.animate(root, 0.1)
Sofa.Simulation.updateVisual(root)
render(root)
#get data from simulation to perform other computations... with def init_display(node):
# init openGL and windowing library
Sofa.SofaGL.glewInit()
Sofa.Simulation.initVisual(node)
Sofa.Simulation.initTextures(node)
...
glLoadIdentity()
def render(rootNode):
# reset openGL buffers...
Sofa.SofaGL.draw(rootNode)
#rendering using windowing library What I would like is to do mostly the same in C++. It would be fantastic if I could simply use OpenGL and GLUT for instance. As far as I understood the video tutorial it is not supported by default by recent versions of Sofa: either we use the GUI or we are terminal only without visualization. In a first step I start with the "oneTetrahedron" example: #include <sofa/core/objectmodel/Context.h>
//other includes and using directives...
int main(int argc, char** argv)
{
//program initialization...
sofa::component::init();
sofa::simulation::graph::init();
sofa::gui::init();
sofa::gui::common::GUIManager::Init(argv[0]);
const auto groot = sofa::simulation::getSimulation()->createNewGraph("root");
//building the scene graph...
const VisualStyle::SPtr style = sofa::core::objectmodel::New<VisualStyle>();
groot->addObject(style);
sofa::core::visual::DisplayFlags& flags = *style->displayFlags.beginEdit();
flags.setShowNormals(false);
//... setting other display flags
style->displayFlags.endEdit();
sofa::gui::common::GUIManager::SetScene(groot);
sofa::simulation::node::initRoot(groot.get());
groot->setAnimate(false);
sofa::gui::common::GUIManager::MainLoop(groot);
sofa::simulation::graph::cleanup();
return 0;
} Then I need to replace the I would like something that could look like: void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// make Sofa drawing in the opengl buffer...
glutSwapBuffers();
}
int main(int argc, char** argv) {
sofa::component::init();
sofa::simulation::graph::init();
auto groot = sofa::simulation::getSimulation()->createNewGraph("root");
//building the scene graph and setting display flags as previously...
#initialize glut...
glutDisplayFunc(display);
sofa::simulation::node::initRoot(groot.get());
groot->setAnimate(false);
while (true) {
//performing some actions on the scene...
//controlling animation and getting result
glutMainLoopEvent();//render one frame
usleep(16000); //at ~ 60Hz
}
return 0;
} So what I need, I believe, is a way to make Sofa filling the openGL buffer in the display function. I tried many things but nothing seems to work... Could someone tell me if there is a simple way to achieve this in Sofa 24.06.00 ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I finally found the way (based on SofaGLFW plugin for those interested) ! |
Beta Was this translation helpful? Give feedback.
I finally found the way (based on SofaGLFW plugin for those interested) !