|
From: Tapio V. <aa...@us...> - 2009-11-10 09:06:20
|
Module: performous
Branch: master
Commit: 7b12d4d0934d84002736a2e7f10cc92d3e584def
Author: Tapio Vierros <tap...@gm...>
Date: Tue Nov 10 10:49:51 2009 +0200
Changed enable/disableLighting to UseLighting RAII wrapper.
---
game/glutil.hh | 25 +++++++++++++++++++++++++
game/guitargraph.cc | 25 +------------------------
2 files changed, 26 insertions(+), 24 deletions(-)
diff --git a/game/glutil.hh b/game/glutil.hh
index a3c7cd1..94cc921 100644
--- a/game/glutil.hh
+++ b/game/glutil.hh
@@ -31,6 +31,31 @@ namespace glutil {
~DisplayList() { glEndList(); }
};
+ /// wrapper struct for RAII
+ struct UseLighting {
+ /// enable lighting and depth test for 3d objects
+ UseLighting(bool doit = true) {
+ if (doit) {
+ glClear(GL_DEPTH_BUFFER_BIT);
+ glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
+
+ GLfloat light_position[] = { -50.0, 15.0, -5.0, 1.0 };
+ glLightfv(GL_LIGHT0, GL_POSITION, light_position);
+
+ glEnable(GL_DEPTH_TEST);
+ glEnable(GL_LIGHTING);
+ glEnable(GL_COLOR_MATERIAL);
+ glEnable(GL_LIGHT0);
+ }
+ }
+ ~UseLighting() {
+ glDisable(GL_LIGHT0);
+ glDisable(GL_COLOR_MATERIAL);
+ glDisable(GL_LIGHTING);
+ glDisable(GL_DEPTH_TEST);
+ }
+ };
+
/// struct to store color information
struct Color {
float r, ///< red component
diff --git a/game/guitargraph.cc b/game/guitargraph.cc
index 663c13f..1032250 100644
--- a/game/guitargraph.cc
+++ b/game/guitargraph.cc
@@ -354,28 +354,6 @@ glutil::Color const& GuitarGraph::color(int fret) const {
return fretColors[fret];
}
-namespace {
- void enableLighting() {
- glClear(GL_DEPTH_BUFFER_BIT);
- glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
-
- GLfloat light_position[] = { -50.0, 15.0, -5.0, 1.0 };
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
-
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_LIGHTING);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_LIGHT0);
- }
-
- void disableLighting() {
- glDisable(GL_LIGHT0);
- glDisable(GL_COLOR_MATERIAL);
- glDisable(GL_LIGHTING);
- glDisable(GL_DEPTH_TEST);
- }
-}
-
void GuitarGraph::draw(double time) {
Dimensions dimensions(1.0); // FIXME: bogus aspect ratio (is this fixable?)
dimensions.screenBottom().middle(m_cx.get()).fixedWidth(m_width.get());
@@ -423,7 +401,7 @@ void GuitarGraph::draw(double time) {
}
}
// Draw the notes
- if (m_use3d) enableLighting();
+ glutil::UseLighting lighting(m_use3d);
for (Chords::const_iterator it = m_chords.begin(); it != m_chords.end(); ++it) {
float tBeg = it->begin - time;
float tEnd = it->end - time;
@@ -481,7 +459,6 @@ void GuitarGraph::draw(double time) {
m_tap.draw();
}
}
- if (m_use3d) disableLighting();
glColor3f(1.0f, 1.0f, 1.0f);
}
|