]> git.tdb.fi Git - libs/gl.git/blob - source/windingtest.cpp
Add a flag to RenderPass to render the back faces of a mesh
[libs/gl.git] / source / windingtest.cpp
1 #include <msp/strings/format.h>
2 #include "windingtest.h"
3
4 namespace Msp {
5 namespace GL {
6
7 void operator>>(const LexicalConverter &conv, FaceWinding &winding)
8 {
9         if(conv.get()=="CLOCKWISE")
10                 winding = CLOCKWISE;
11         else if(conv.get()=="COUNTERCLOCKWISE")
12                 winding = COUNTERCLOCKWISE;
13         else
14                 throw lexical_error(format("conversion of '%s' to FaceWinding", conv.get()));
15 }
16
17 WindingTest::WindingTest():
18         test(false),
19         winding(COUNTERCLOCKWISE)
20 { }
21
22 WindingTest::WindingTest(FaceWinding w):
23         test(true),
24         winding(w)
25 { }
26
27 void WindingTest::bind() const
28 {
29         if(set_current(this))
30         {
31                 if(test)
32                 {
33                         glEnable(GL_CULL_FACE);
34                         glFrontFace(winding);
35                 }
36                 else
37                         glDisable(GL_CULL_FACE);
38         }
39 }
40
41 void WindingTest::unbind()
42 {
43         if(set_current(0))
44                 glDisable(GL_CULL_FACE);
45 }
46
47 const WindingTest &WindingTest::get_reverse() const
48 {
49         if(!test)
50                 return *this;
51         else if(winding==CLOCKWISE)
52                 return counterclockwise();
53         else
54                 return clockwise();
55 }
56
57 const WindingTest &WindingTest::clockwise()
58 {
59         static WindingTest test(CLOCKWISE);
60         return test;
61 }
62
63 const WindingTest &WindingTest::counterclockwise()
64 {
65         static WindingTest test(COUNTERCLOCKWISE);
66         return test;
67 }
68
69 } // namespace GL
70 } // namespace Msp