]> git.tdb.fi Git - libs/gl.git/blobdiff - source/windingtest.cpp
Add WindingTest and support for it in Mesh and Renderer
[libs/gl.git] / source / windingtest.cpp
diff --git a/source/windingtest.cpp b/source/windingtest.cpp
new file mode 100644 (file)
index 0000000..b54f712
--- /dev/null
@@ -0,0 +1,66 @@
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2011  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include "windingtest.h"
+
+namespace Msp {
+namespace GL {
+
+void operator>>(const LexicalConverter &conv, FaceWinding &winding)
+{
+       if(conv.get()=="CLOCKWISE")
+               winding = CLOCKWISE;
+       else if(conv.get()=="COUNTERCLOCKWISE")
+               winding = COUNTERCLOCKWISE;
+       else
+               throw InvalidParameterValue("Invalid FaceWinding");
+}
+
+WindingTest::WindingTest():
+       test(false),
+       winding(COUNTERCLOCKWISE)
+{ }
+
+WindingTest::WindingTest(FaceWinding w):
+       test(true),
+       winding(w)
+{ }
+
+void WindingTest::bind() const
+{
+       if(set_current(this))
+       {
+               if(test)
+               {
+                       glEnable(GL_CULL_FACE);
+                       glFrontFace(winding);
+               }
+               else
+                       glDisable(GL_CULL_FACE);
+       }
+}
+
+void WindingTest::unbind()
+{
+       if(set_current(0))
+               glDisable(GL_CULL_FACE);
+}
+
+WindingTest &WindingTest::clockwise()
+{
+       static WindingTest test(CLOCKWISE);
+       return test;
+}
+
+WindingTest &WindingTest::counterclockwise()
+{
+       static WindingTest test(COUNTERCLOCKWISE);
+       return test;
+}
+
+} // namespace GL
+} // namespace Msp