1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 /*!
34   \class SoDepthBufferElement Inventor/elements/SoDepthBufferElement.h
35   \brief The SoDepthBufferElement controls the depth buffer settings.
36 
37   \ingroup elements
38   \COIN_CLASS_EXTENSION
39   \since Coin 3.0
40 */
41 
42 #include <Inventor/elements/SoDepthBufferElement.h>
43 
44 #include <cassert>
45 
46 #include "coindefs.h"
47 #include "SbBasicP.h"
48 
49 SO_ELEMENT_SOURCE(SoDepthBufferElement);
50 
51 /*!
52   This function initializes the SoDepthBufferElement class type for Coin.
53 */
54 void
initClass(void)55 SoDepthBufferElement::initClass(void)
56 {
57   SO_ELEMENT_INIT_CLASS(SoDepthBufferElement, inherited);
58 }
59 
60 /*!
61   The destructor.
62 */
~SoDepthBufferElement(void)63 SoDepthBufferElement::~SoDepthBufferElement(void)
64 {
65 }
66 
67 /*!
68   Internal Coin method.
69 */
70 void
init(SoState * state)71 SoDepthBufferElement::init(SoState * state)
72 {
73   inherited::init(state);
74   this->test = TRUE;
75   this->write = TRUE;
76   this->function = LEQUAL;
77   this->range.setValue(0.0f, 1.0f);
78 }
79 
80 /*!
81   Internal Coin method.
82 */
83 void
push(SoState * state)84 SoDepthBufferElement::push(SoState * state)
85 {
86   const SoDepthBufferElement * prev = coin_assert_cast<const SoDepthBufferElement *>
87     (
88      this->getNextInStack()
89      );
90   this->test = prev->test;
91   this->write = prev->write;
92   this->function = prev->function;
93   this->range = prev->range;
94   prev->capture(state);
95 }
96 
97 /*!
98   Internal Coin method.
99 */
100 void
pop(SoState * COIN_UNUSED_ARG (state),const SoElement * COIN_UNUSED_ARG (prevTopElement))101 SoDepthBufferElement::pop(SoState * COIN_UNUSED_ARG(state),
102                           const SoElement * COIN_UNUSED_ARG(prevTopElement))
103 {
104 }
105 
106 /*!
107   Set this element's values.
108 */
109 void
set(SoState * state,SbBool test,SbBool write,DepthWriteFunction function,SbVec2f range)110 SoDepthBufferElement::set(SoState * state,
111                           SbBool test,
112                           SbBool write,
113                           DepthWriteFunction function,
114                           SbVec2f range)
115 {
116   SoDepthBufferElement * elem =
117     static_cast<SoDepthBufferElement *>(SoElement::getElement(state, classStackIndex));
118 
119   elem->setElt(test, write, function, range);
120 }
121 
122 /*!
123   Fetches this element's values.
124 */
125 void
get(SoState * state,SbBool & test_out,SbBool & write_out,DepthWriteFunction & function_out,SbVec2f & range_out)126 SoDepthBufferElement::get(SoState * state,
127                           SbBool & test_out,
128                           SbBool & write_out,
129                           DepthWriteFunction & function_out,
130                           SbVec2f & range_out)
131 {
132   const SoDepthBufferElement * elem =
133     static_cast<const SoDepthBufferElement *>(SoElement::getConstElement(state, classStackIndex));
134   test_out = elem->test;
135   write_out = elem->write;
136   function_out = elem->function;
137   range_out = elem->range;
138 }
139 
140 /*!
141   Returns the depth test enabled state.
142 */
143 SbBool
getTestEnable(SoState * state)144 SoDepthBufferElement::getTestEnable(SoState * state)
145 {
146   const SoDepthBufferElement * elem =
147     static_cast<const SoDepthBufferElement *>(SoElement::getConstElement(state, classStackIndex));
148   return elem->test;
149 }
150 
151 /*!
152   Returns the depth buffer write enabled state.
153 */
154 SbBool
getWriteEnable(SoState * state)155 SoDepthBufferElement::getWriteEnable(SoState * state)
156 {
157   const SoDepthBufferElement * elem =
158     static_cast<const SoDepthBufferElement *>(SoElement::getConstElement(state, classStackIndex));
159   return elem->write;
160 }
161 
162 /*!
163   Returns the set depth buffer write function.
164 */
165 SoDepthBufferElement::DepthWriteFunction
getFunction(SoState * state)166 SoDepthBufferElement::getFunction(SoState * state)
167 {
168   const SoDepthBufferElement * elem =
169     static_cast<const SoDepthBufferElement *>(SoElement::getConstElement(state, classStackIndex));
170   return elem->function;
171 }
172 
173 /*!
174   Returns the depth buffer value range used.
175 */
176 SbVec2f
getRange(SoState * state)177 SoDepthBufferElement::getRange(SoState * state)
178 {
179   const SoDepthBufferElement * elem =
180     static_cast<const SoDepthBufferElement *>(SoElement::getConstElement(state, classStackIndex));
181   return elem->range;
182 }
183 
184 /*!
185   Internal Coin method.
186 */
187 SbBool
matches(const SoElement * element) const188 SoDepthBufferElement::matches(const SoElement * element) const
189 {
190   const SoDepthBufferElement * elem =
191     static_cast<const SoDepthBufferElement *>(element);
192 
193   return (elem->test == this->test)
194     && (elem->write == this->write)
195     && (elem->function == this->function)
196     && (elem->range == this->range);
197 }
198 
199 /*!
200   Internal Coin method.
201 */
202 SoElement *
copyMatchInfo(void) const203 SoDepthBufferElement::copyMatchInfo(void) const
204 {
205   SoDepthBufferElement * elem =
206     static_cast<SoDepthBufferElement *>(this->getTypeId().createInstance());
207   elem->test = this->test;
208   elem->write = this->write;
209   elem->function = this->function;
210   elem->range = this->range;
211   return elem;
212 }
213 
214 /*!
215   Virtual method to set the state to get derived elements updated.
216 */
217 void
setElt(SbBool test,SbBool write,DepthWriteFunction function,SbVec2f range)218 SoDepthBufferElement::setElt(SbBool test, SbBool write, DepthWriteFunction function, SbVec2f range)
219 {
220   this->test = test;
221   this->write = write;
222   this->function = function;
223   this->range = range;
224 }
225