hx3d  1
2D/3D Simple Game Framework
stencil.cpp
1 /*
2  Stencil.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/stencil.hpp"
22 
23 namespace hx3d {
24 namespace graphics {
25 
27  glEnable(GL_STENCIL_TEST);
28 }
29 
31  glDisable(GL_STENCIL_TEST);
32 }
33 
34 void Stencil::setFunction(Function func, int ref, int mask) {
35  glStencilFunc(convertFunction(func), ref, mask);
36 }
37 
38 void Stencil::setOperation(Operation sfail, Operation dpfail, Operation dppass) {
39  glStencilOp(convertOperation(sfail), convertOperation(dpfail), convertOperation(dppass));
40 }
41 
43  glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
44  glDepthMask(GL_FALSE);
45 
46  glStencilMask(0xFF);
47 
48  glClear(GL_STENCIL_BUFFER_BIT);
49 }
50 
51 void Stencil::end() {
52  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
53  glDepthMask(GL_TRUE);
54 
55  glStencilMask(0x00);
56 
57  glStencilFunc(GL_EQUAL, 2, 0xFF);
58 }
59 
60 GLenum Stencil::convertFunction(Function func) {
61  return
62  func == Function::Never ? GL_NEVER :
63  func == Function::Always ? GL_ALWAYS :
64  func == Function::Equal ? GL_EQUAL :
65  func == Function::NotEqual ? GL_NOTEQUAL :
66  func == Function::Less ? GL_LESS :
67  func == Function::Greater ? GL_GREATER :
68  func == Function::LessOrEqual ? GL_LEQUAL :
69  GL_GEQUAL;
70 }
71 
72 GLenum Stencil::convertOperation(Operation op) {
73  return
74  op == Operation::Keep ? GL_KEEP :
75  op == Operation::Zero ? GL_ZERO :
76  op == Operation::Replace ? GL_REPLACE :
77  op == Operation::Increment ? GL_INCR :
78  op == Operation::IncrementWrap ? GL_INCR_WRAP :
79  op == Operation::Decrement ? GL_DECR :
80  op == Operation::DecrementWrap ? GL_DECR_WRAP :
81  GL_INVERT;
82 }
83 
84 } /* graphics */
85 } /* hx3d */
Function
Stencil function.
Definition: stencil.hpp:39
Hide when it's not equal.
void setFunction(Function func, int ref, int mask)
Set the stencil function.
Definition: stencil.cpp:34
static void disable()
Disable stencil mode.
Definition: stencil.cpp:30
static void enable()
Enable stencil mode.
Definition: stencil.cpp:26
hx3d framework namespace
Definition: audio.hpp:26
void setOperation(Operation sfail, Operation dpfail, Operation dppass)
Set the stencil operation.
Definition: stencil.cpp:38
Operation
Stencil operation.
Definition: stencil.hpp:61
Hide when it's less or equal.
Hide when it's greater.
void end()
End the stencil.
Definition: stencil.cpp:51
void begin()
Begin to use the stencil.
Definition: stencil.cpp:42