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 SoShadowCulling SoShadowCulling.h Inventor/annex/FXViz/nodes/SoShadowCulling.h
35 \brief The SoShadowCulling class is a node for setting the shadow style on nodes.
36
37 \ingroup nodes
38
39 <b>FILE FORMAT/DEFAULTS:</b>
40 \code
41 ShadowCulling {
42 mode AS_IS_CULLING
43 }
44 \endcode
45
46 \since Coin 2.5
47 */
48
49 // *************************************************************************
50
51 #include <Inventor/annex/FXViz/nodes/SoShadowCulling.h>
52
53 #include <cstdio>
54
55 #include <Inventor/elements/SoShapeStyleElement.h>
56 #include <Inventor/elements/SoShapeHintsElement.h>
57 #include <Inventor/elements/SoOverrideElement.h>
58 #include <Inventor/elements/SoGLShaderProgramElement.h>
59 #include <Inventor/actions/SoGLRenderAction.h>
60
61 #include "nodes/SoSubNodeP.h"
62 #include "shaders/SoGLShaderProgram.h"
63
64 // *************************************************************************
65
66 /*!
67 \enum SoShadowCulling::Mode
68
69 Enumerates the available culling modes.
70 */
71
72 /*!
73 \var SoSFEnum SoShadowCulling::mode
74
75 Sets the culling mode. Default is AS_IS_CULLING.
76 */
77
78 /*!
79 \var SoShadowCulling::Mode SoShadowCulling::AS_IS_CULLING
80
81 Use the culling specified in the scene graph.
82 */
83
84 /*!
85 \var SoShadowCulling::Style SoShadowCulling::NO_CULLING
86
87 Render both backfacing and frontfacting triangles into the shadow map.
88 */
89
90
91 // *************************************************************************
92
93
94 SO_NODE_SOURCE(SoShadowCulling);
95
96 /*!
97 Constructor.
98 */
SoShadowCulling(void)99 SoShadowCulling::SoShadowCulling(void)
100 {
101 SO_NODE_INTERNAL_CONSTRUCTOR(SoShadowCulling);
102
103 SO_NODE_ADD_FIELD(mode, (AS_IS_CULLING));
104
105 SO_NODE_DEFINE_ENUM_VALUE(Mode, AS_IS_CULLING);
106 SO_NODE_DEFINE_ENUM_VALUE(Mode, NO_CULLING);
107 SO_NODE_SET_SF_ENUM_TYPE(mode, Mode);
108 }
109
110 /*!
111 Destructor.
112 */
~SoShadowCulling()113 SoShadowCulling::~SoShadowCulling()
114 {
115 }
116
117 // Doc from superclass.
118 void
initClass(void)119 SoShadowCulling::initClass(void)
120 {
121 SO_NODE_INTERNAL_INIT_CLASS(SoShadowCulling, SO_FROM_COIN_2_5);
122 SO_ENABLE(SoGLRenderAction, SoGLShadowCullingElement);
123 }
124
125 // Doc from superclass.
126 void
GLRender(SoGLRenderAction * action)127 SoShadowCulling::GLRender(SoGLRenderAction * action)
128 {
129 SoState * state = action->getState();
130
131 if (SoShapeStyleElement::get(state)->getFlags() & SoShapeStyleElement::SHADOWMAP) {
132 int32_t mode = this->mode.getValue();
133 SoGLShadowCullingElement::set(state, this, mode);
134
135 if (mode == NO_CULLING) {
136 SoShapeHintsElement::set(state, NULL,
137 SoShapeHintsElement::UNKNOWN_ORDERING,
138 SoShapeHintsElement::UNKNOWN_SHAPE_TYPE,
139 SoShapeHintsElement::UNKNOWN_FACE_TYPE);
140 SoOverrideElement::setShapeHintsOverride(state, NULL, TRUE);
141 }
142 else {
143 // FIXME: need to restore the previous ShapeHints settings in some way,
144 // or require that this node is used only inside a separator
145 SoOverrideElement::setShapeHintsOverride(state, NULL, FALSE);
146 }
147 }
148 }
149
150
151 #ifdef COIN_TEST_SUITE
152
BOOST_AUTO_TEST_CASE(initialized)153 BOOST_AUTO_TEST_CASE(initialized)
154 {
155 SoShadowCulling * node = new SoShadowCulling;
156 assert(node);
157 node->ref();
158 BOOST_CHECK_MESSAGE(node->getTypeId() != SoType::badType(),
159 "missing class initialization");
160 node->unref();
161 }
162
163 #endif // COIN_TEST_SUITE
164