77#include " Demos/Simulation/SimulationModel.h"
88#include " Demos/Simulation/TimeStepController.h"
99#include < iostream>
10+ #include " Demos/Visualization/Visualization.h"
11+ #include " Demos/Utils/Utilities.h"
1012
1113// Enable memory leak detection
1214#ifdef _DEBUG
@@ -23,6 +25,7 @@ void createMesh();
2325void render ();
2426void cleanup ();
2527void reset ();
28+ void initShader ();
2629void selection (const Eigen::Vector2i &start, const Eigen::Vector2i &end);
2730void TW_CALL setTimeStep (const void *value, void *clientData);
2831void TW_CALL getTimeStep (void *value, void *clientData);
@@ -62,19 +65,26 @@ const float height = 10.0f;
6265bool doPause = true ;
6366std::vector<unsigned int > selectedParticles;
6467Eigen::Vector3f oldMousePos;
68+ Shader *shader;
69+ string exePath;
70+ string dataPath;
6571
6672// main
6773int main ( int argc, char **argv )
6874{
6975 REPORT_MEMORY_LEAKS
7076
77+ exePath = Utilities::getFilePath (argv[0 ]);
78+ dataPath = exePath + " /" + std::string (PBD_DATA_PATH);
79+
7180 // OpenGL
7281 MiniGL::init (argc, argv, 1024 , 768 , 0 , 0 , " Cloth demo" );
7382 MiniGL::initLights ();
7483 MiniGL::initTexture ();
7584 MiniGL::setClientIdleFunc (50 , timeStep);
7685 MiniGL::setKeyFunc (0 , ' r' , reset);
7786 MiniGL::setSelectionFunc (selection);
87+ initShader ();
7888
7989 buildModel ();
8090
@@ -109,9 +119,28 @@ int main( int argc, char **argv )
109119 return 0 ;
110120}
111121
122+ void initShader ()
123+ {
124+ std::string vertFile = dataPath + " /shaders/vs_smoothTex.glsl" ;
125+ std::string fragFile = dataPath + " /shaders/fs_smoothTex.glsl" ;
126+ shader = MiniGL::createShader (vertFile, " " , fragFile);
127+
128+ if (shader == NULL )
129+ return ;
130+
131+ shader->begin ();
132+ shader->addUniform (" modelview_matrix" );
133+ shader->addUniform (" projection_matrix" );
134+ shader->addUniform (" surface_color" );
135+ shader->addUniform (" shininess" );
136+ shader->addUniform (" specular_factor" );
137+ shader->end ();
138+ }
139+
112140void cleanup ()
113141{
114142 delete TimeManager::getCurrent ();
143+ delete shader;
115144}
116145
117146void reset ()
@@ -180,39 +209,31 @@ void renderTriangleModels()
180209 // Draw simulation model
181210
182211 const ParticleData &pd = model.getParticles ();
212+ float surfaceColor[4 ] = { 0 .2f , 0 .5f , 1 .0f , 1 };
213+
214+ if (shader)
215+ {
216+ shader->begin ();
217+ glUniform3fv (shader->getUniform (" surface_color" ), 1 , surfaceColor);
218+ glUniform1f (shader->getUniform (" shininess" ), 5 .0f );
219+ glUniform1f (shader->getUniform (" specular_factor" ), 0 .2f );
220+
221+ GLfloat matrix[16 ];
222+ glGetFloatv (GL_MODELVIEW_MATRIX, matrix);
223+ glUniformMatrix4fv (shader->getUniform (" modelview_matrix" ), 1 , GL_FALSE, matrix);
224+ GLfloat pmatrix[16 ];
225+ glGetFloatv (GL_PROJECTION_MATRIX, pmatrix);
226+ glUniformMatrix4fv (shader->getUniform (" projection_matrix" ), 1 , GL_FALSE, pmatrix);
227+ }
183228
184229 for (unsigned int i = 0 ; i < model.getTriangleModels ().size (); i++)
185230 {
186231 // mesh
187232 const IndexedFaceMesh &mesh = model.getTriangleModels ()[i]->getParticleMesh ();
188- const unsigned int *faces = mesh.getFaces ().data ();
189- const unsigned int nFaces = mesh.numFaces ();
190- const Eigen::Vector3f *vertexNormals = mesh.getVertexNormals ().data ();
191- const Eigen::Vector2f *uvs = mesh.getUVs ().data ();
192-
193- float surfaceColor[4 ] = { 0 .2f , 0 .5f , 1 .0f , 1 };
194- float speccolor[4 ] = { 1.0 , 1.0 , 1.0 , 1.0 };
195- glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, surfaceColor);
196- glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, surfaceColor);
197- glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, speccolor);
198- glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 100.0 );
199- glColor3fv (surfaceColor);
200-
201- MiniGL::bindTexture ();
202-
203- glEnableClientState (GL_VERTEX_ARRAY);
204- glEnableClientState (GL_NORMAL_ARRAY);
205- glEnableClientState (GL_TEXTURE_COORD_ARRAY);
206- glVertexPointer (3 , GL_FLOAT, 0 , &pd.getPosition (model.getTriangleModels ()[i]->getIndexOffset ())[0 ]);
207- glTexCoordPointer (2 , GL_FLOAT, 0 , &uvs[0 ][0 ]);
208- glNormalPointer (GL_FLOAT, 0 , &vertexNormals[0 ][0 ]);
209- glDrawElements (GL_TRIANGLES, (GLsizei)3 * mesh.numFaces (), GL_UNSIGNED_INT, mesh.getFaces ().data ());
210- glDisableClientState (GL_VERTEX_ARRAY);
211- glDisableClientState (GL_NORMAL_ARRAY);
212- glDisableClientState (GL_TEXTURE_COORD_ARRAY);
213-
214- MiniGL::unbindTexture ();
233+ Visualization::drawTexturedMesh (pd, mesh, surfaceColor);
215234 }
235+ if (shader)
236+ shader->end ();
216237}
217238
218239
0 commit comments