1818#include " ../framework/Scene.h"
1919#include < glm/glm.hpp>
2020#include < glm/gtc/type_ptr.hpp>
21+ #include < glm/gtc/quaternion.hpp>
2122#include < glm/gtc/matrix_transform.hpp>
2223
2324#define ARRAY_COUNT ( array ) (sizeof ( array ) / (sizeof ( array[0 ] ) * (sizeof ( array ) != sizeof (void *) || sizeof ( array[0 ] ) <= sizeof (void *))))
@@ -172,7 +173,7 @@ void LoadTextures()
172173// View setup.
173174glutil::ViewData g_initialView =
174175{
175- glm::vec3 (- 60 . 257084f , 10 . 947238f , 62 . 636356f ),
176+ glm::vec3 (0 . 0f , 0 . 0f , 0 . 0f ),
176177 glm::fquat (-0 .972817f , -0 .099283f , -0 .211198f , -0 .020028f ),
177178 30 .0f ,
178179 0 .0f
@@ -196,21 +197,55 @@ namespace
196197}
197198
198199Framework::Scene *g_pScene = NULL ;
200+ std::vector<Framework::NodeRef> g_nodes;
201+ Framework::Timer g_timer (Framework::Timer::TT_LOOP, 10 .0f );
199202
200- // Called after the window and OpenGL are initialized. Called exactly once, before the main loop.
201- void init ()
203+ class ColorUniformBinder : public Framework ::StateBinder
202204{
203- try
205+ public:
206+ ColorUniformBinder ()
207+ : m_clrUnif(-1 )
208+ , m_clr(0 .0f , 0 .0f , 0 .0f , 1 .0f ) {}
209+
210+ void AssociateWithProgram (GLuint prog, const std::string &unifName)
204211 {
205- g_pScene = new Framework::Scene (" test.scene" );
206- // InitializePrograms();
212+ m_clrUnif = glGetUniformLocation (prog, unifName.c_str ());
207213 }
208- catch (std::exception &except)
214+
215+ void SetColor (const glm::vec4 &clr) { m_clr = clr; }
216+
217+ virtual void BindState () const
209218 {
210- printf (" %s\n " , except.what ());
211- throw ;
219+ glUniform4fv (m_clrUnif, 1 , glm::value_ptr (m_clr));
212220 }
213221
222+ virtual void UnbindState () const {}
223+
224+ private:
225+ GLint m_clrUnif;
226+ glm::vec4 m_clr;
227+ };
228+
229+ ColorUniformBinder g_clrUnif;
230+
231+ void LoadAndSetupScene ()
232+ {
233+ g_nodes.clear ();
234+ g_pScene = new Framework::Scene (" sceneTest.xml" );
235+ g_nodes.push_back (g_pScene->FindNode (" blue" ));
236+ g_nodes.push_back (g_pScene->FindNode (" user" ));
237+
238+ GLuint colorProg = g_pScene->FindProgram (" p_colored" );
239+
240+ // No more things that can throw.
241+ g_clrUnif.AssociateWithProgram (colorProg, " objectColor" );
242+ g_nodes[1 ].SetStateBinder (&g_clrUnif);
243+ g_clrUnif.SetColor (glm::vec4 (0 .1f , 1 .0f , 0 .1f , 1 .0f ));
244+ }
245+
246+ // Called after the window and OpenGL are initialized. Called exactly once, before the main loop.
247+ void init ()
248+ {
214249 glutMouseFunc (MouseButton);
215250 glutMotionFunc (MouseMotion);
216251 glutMouseWheelFunc (MouseWheel);
@@ -227,6 +262,7 @@ void init()
227262 glDepthFunc (GL_LEQUAL);
228263 glDepthRange (depthZNear, depthZFar);
229264 glEnable (GL_DEPTH_CLAMP);
265+ glEnable (GL_FRAMEBUFFER_SRGB);
230266
231267 // Setup our Uniform Buffers
232268 glGenBuffers (1 , &g_projectionUniformBuffer);
@@ -236,6 +272,17 @@ void init()
236272 glBindBufferRange (GL_UNIFORM_BUFFER, g_projectionBlockIndex, g_projectionUniformBuffer,
237273 0 , sizeof (ProjectionBlock));
238274
275+ try
276+ {
277+ LoadAndSetupScene ();
278+ // InitializePrograms();
279+ }
280+ catch (std::exception &except)
281+ {
282+ printf (" %s\n " , except.what ());
283+ throw ;
284+ }
285+
239286/*
240287 glGenBuffers(1, &g_lightUniformBuffer);
241288 glBindBuffer(GL_UNIFORM_BUFFER, g_lightUniformBuffer);
@@ -265,9 +312,16 @@ void display()
265312 if (!g_pScene)
266313 return ;
267314
315+ g_timer.Update ();
316+
268317 glClearColor (0 .8f , 0 .8f , 0 .8f , 1 .0f );
269318 glClearDepth (1 .0f );
270319 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
320+
321+ g_nodes[0 ].NodeSetOrient (glm::rotate (glm::fquat (),
322+ 360 .0f * g_timer.GetAlpha (), glm::vec3 (0 .0f , 1 .0f , 0 .0f )));
323+
324+ g_pScene->Render (g_viewPole.CalcMatrix ());
271325/*
272326 if(!g_pLightEnv)
273327 return;
@@ -368,8 +422,9 @@ void display()
368422 }
369423 }
370424
371- glutPostRedisplay();
372- */
425+ */
426+
427+ glutPostRedisplay ();
373428 glutSwapBuffers ();
374429}
375430
@@ -406,6 +461,34 @@ void keyboard(unsigned char key, int x, int y)
406461 glutLeaveMainLoop ();
407462 return ;
408463 case 32 :
464+ g_nodes[0 ].NodeSetTrans (glm::vec3 (0 .0f , 0 .0f , 0 .0f ));
465+ break ;
466+ case ' i' :
467+ g_nodes[0 ].NodeOffset (glm::vec3 (0 .0f , 1 .0f , 0 .0f ));
468+ break ;
469+ case ' j' :
470+ g_nodes[0 ].NodeOffset (glm::vec3 (0 .0f , -1 .0f , 0 .0f ));
471+ break ;
472+ case ' \r ' :
473+ {
474+ std::auto_ptr<Framework::Scene> pOldScene (g_pScene);
475+ g_pScene = NULL ;
476+ std::vector<Framework::NodeRef> tmpNodes;
477+ tmpNodes.swap (g_nodes);
478+ try
479+ {
480+ LoadAndSetupScene ();
481+ }
482+ catch (std::exception &except)
483+ {
484+ printf (" Failed to reload, due to: %s\n " , except.what ());
485+ if (g_pScene)
486+ delete g_pScene;
487+ g_pScene = pOldScene.release ();
488+ g_nodes.swap (tmpNodes);
489+ return ;
490+ }
491+ }
409492 break ;
410493 }
411494
0 commit comments