44#include " Utils/FileSystem.h"
55#include " Simulation/TimeManager.h"
66#include " Demos/Visualization/Selection.h"
7- #include " GL/glut.h"
87#include " Simulation/Simulation.h"
98#include " NumericParameter.h"
109#include " Utils/Logger.h"
@@ -174,7 +173,7 @@ void DemoBase::init(int argc, char **argv, const char *demoName)
174173 LOG_DEBUG << " Host name: " << SystemInfo::getHostName ();
175174
176175 // OpenGL
177- MiniGL::init (argc, argv, 1024 , 768 , 0 , 0 , demoName);
176+ MiniGL::init (argc, argv, 1280 , 1024 , demoName);
178177 MiniGL::initLights ();
179178 MiniGL::initTexture ();
180179 MiniGL::getOpenGLVersion (m_context_major_version, m_context_minor_version);
@@ -183,6 +182,13 @@ void DemoBase::init(int argc, char **argv, const char *demoName)
183182
184183 if (MiniGL::checkOpenGLVersion (3 , 3 ))
185184 initShaders ();
185+
186+ MiniGL::addReshapeFunc ([](int width, int height) { TwWindowSize (width, height); });
187+ MiniGL::addKeyboardFunc ([](int key, int scancode, int action, int mods) -> bool { return TwEventKeyGLFW (key, action); });
188+ MiniGL::addCharFunc ([](int key, int action) -> bool { return TwEventCharGLFW (key, action); });
189+ MiniGL::addMousePressFunc ([](int button, int action, int mods) -> bool { return TwEventMouseButtonGLFW (button, action); });
190+ MiniGL::addMouseMoveFunc ([](int x, int y) -> bool { return TwEventMousePosGLFW (x, y); });
191+ MiniGL::addMouseWheelFunc ([](int pos, double xoffset, double yoffset) -> bool { return TwEventMouseWheelGLFW (pos); });
186192}
187193
188194void DemoBase::readScene ()
@@ -224,6 +230,21 @@ void DemoBase::initShaders()
224230 m_shaderTex.addUniform (" shininess" );
225231 m_shaderTex.addUniform (" specular_factor" );
226232 m_shaderTex.end ();
233+
234+ vertFile = m_dataPath + " /shaders/vs_flat.glsl" ;
235+ std::string geomFile = m_dataPath + " /shaders/gs_flat.glsl" ;
236+ fragFile = m_dataPath + " /shaders/fs_flat.glsl" ;
237+ m_shaderFlat.compileShaderFile (GL_VERTEX_SHADER, vertFile);
238+ m_shaderFlat.compileShaderFile (GL_GEOMETRY_SHADER, geomFile);
239+ m_shaderFlat.compileShaderFile (GL_FRAGMENT_SHADER, fragFile);
240+ m_shaderFlat.createAndLinkProgram ();
241+ m_shaderFlat.begin ();
242+ m_shaderFlat.addUniform (" modelview_matrix" );
243+ m_shaderFlat.addUniform (" projection_matrix" );
244+ m_shaderFlat.addUniform (" surface_color" );
245+ m_shaderFlat.addUniform (" shininess" );
246+ m_shaderFlat.addUniform (" specular_factor" );
247+ m_shaderFlat.end ();
227248}
228249
229250
@@ -267,6 +288,26 @@ void DemoBase::shaderEnd()
267288 m_shader.end ();
268289}
269290
291+ void DemoBase::shaderFlatBegin (const float * col)
292+ {
293+ m_shaderFlat.begin ();
294+ glUniform1f (m_shaderFlat.getUniform (" shininess" ), 5 .0f );
295+ glUniform1f (m_shaderFlat.getUniform (" specular_factor" ), 0 .2f );
296+
297+ GLfloat matrix[16 ];
298+ glGetFloatv (GL_MODELVIEW_MATRIX, matrix);
299+ glUniformMatrix4fv (m_shaderFlat.getUniform (" modelview_matrix" ), 1 , GL_FALSE, matrix);
300+ GLfloat pmatrix[16 ];
301+ glGetFloatv (GL_PROJECTION_MATRIX, pmatrix);
302+ glUniformMatrix4fv (m_shaderFlat.getUniform (" projection_matrix" ), 1 , GL_FALSE, pmatrix);
303+ glUniform3fv (m_shaderFlat.getUniform (" surface_color" ), 1 , col);
304+ }
305+
306+ void DemoBase::shaderFlatEnd ()
307+ {
308+ m_shaderFlat.end ();
309+ }
310+
270311void DemoBase::readParameters ()
271312{
272313 m_sceneLoader->readParameterObject (this );
@@ -278,7 +319,7 @@ void DemoBase::readParameters()
278319void DemoBase::render ()
279320{
280321 float gridColor[4 ] = { 0 .2f , 0 .2f , 0 .2f , 1 .0f };
281- MiniGL::drawGrid (gridColor);
322+ MiniGL::drawGrid_xz (gridColor);
282323
283324 MiniGL::coordinateSystem ();
284325
@@ -290,7 +331,7 @@ void DemoBase::render()
290331 SimulationModel::ParticleRigidBodyContactConstraintVector &particleRigidBodyContacts = model->getParticleRigidBodyContactConstraints ();
291332
292333 float selectionColor[4 ] = { 0 .8f , 0 .0f , 0 .0f , 1 };
293- float surfaceColor[4 ] = { 0 .3f , 0 .5f , 0 .8f , 1 };
334+ float surfaceColor[4 ] = { 0 .1f , 0 .4f , 0 .7f , 1 };
294335 float staticColor[4 ] = { 0 .5f , 0 .5f , 0 .5f , 1 };
295336
296337 if (m_renderContacts)
@@ -302,8 +343,6 @@ void DemoBase::render()
302343 }
303344
304345
305- shaderBegin (staticColor);
306-
307346 for (size_t i = 0 ; i < rb.size (); i++)
308347 {
309348 bool selected = false ;
@@ -315,6 +354,12 @@ void DemoBase::render()
315354
316355 const VertexData &vd = rb[i]->getGeometry ().getVertexData ();
317356 const IndexedFaceMesh &mesh = rb[i]->getGeometry ().getMesh ();
357+
358+ if (mesh.getFlatShading ())
359+ shaderFlatBegin (staticColor);
360+ else
361+ shaderBegin (staticColor);
362+
318363 if (!selected)
319364 {
320365 if (rb[i]->getMass () == 0.0 )
@@ -333,9 +378,14 @@ void DemoBase::render()
333378 glUniform3fv (m_shader.getUniform (" surface_color" ), 1 , selectionColor);
334379 Visualization::drawMesh (vd, mesh, 0 , selectionColor);
335380 }
381+
382+ if (mesh.getFlatShading ())
383+ shaderFlatEnd ();
384+ else
385+ shaderEnd ();
336386 }
337387
338- shaderEnd ();
388+
339389
340390 renderTriangleModels ();
341391 renderTetModels ();
@@ -538,7 +588,7 @@ void DemoBase::renderTetModels()
538588{
539589 SimulationModel *model = Simulation::getCurrent ()->getModel ();
540590 const ParticleData &pd = model->getParticles ();
541- float surfaceColor[4 ] = { 0 .1f , 0 .3f , 0 .5f , 1 };
591+ float surfaceColor[4 ] = { 0 .1f , 0 .4f , 0 .7f , 1 };
542592
543593 shaderBegin (surfaceColor);
544594
@@ -800,7 +850,7 @@ void DemoBase::mouseMove(int x, int y, void *clientData)
800850 base->m_oldMousePos = mousePos;
801851}
802852
803- void DemoBase::selection (const Eigen:: Vector2i &start, const Eigen:: Vector2i &end, void *clientData)
853+ void DemoBase::selection (const Vector2i &start, const Vector2i &end, void *clientData)
804854{
805855 SimulationModel *model = Simulation::getCurrent ()->getModel ();
806856 DemoBase *base = (DemoBase*)clientData;
@@ -824,7 +874,7 @@ void DemoBase::selection(const Eigen::Vector2i &start, const Eigen::Vector2i &en
824874 if (rb.size () > 0 )
825875 Selection::selectRect (start, end, &x[0 ], &x[rb.size () - 1 ], base->m_selectedBodies );
826876 if ((base->m_selectedBodies .size () > 0 ) || (base->m_selectedParticles .size () > 0 ))
827- MiniGL::setMouseMoveFunc (GLUT_MIDDLE_BUTTON , mouseMove);
877+ MiniGL::setMouseMoveFunc (2 , mouseMove);
828878 else
829879 MiniGL::setMouseMoveFunc (-1 , NULL );
830880
0 commit comments