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 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif // HAVE_CONFIG_H
36 
37 #ifdef HAVE_VRML97
38 
39 /*!
40   \class SoVRMLCone SoVRMLCone.h Inventor/VRMLnodes/SoVRMLCone.h
41   \brief The SoVRMLCone class is used to represent a Cone object.
42 
43   \ingroup VRMLnodes
44 
45   \WEB3DCOPYRIGHT
46 
47   \verbatim
48   Cone {
49     field     SFFloat   bottomRadius 1        # (0, inf)
50     field     SFFloat   height       2        # (0, inf)
51     field     SFBool    side         TRUE
52     field     SFBool    bottom       TRUE
53   }
54   \endverbatim
55 
56   The Cone node specifies a cone which is centred in the local
57   coordinate system and whose central axis is aligned with the local
58   Y-axis. The bottomRadius field specifies the radius of the cone's
59   base, and the height field specifies the height of the cone from the
60   centre of the base to the apex.  By default, the cone has a radius
61   of 1.0 at the bottom and a height of 2.0, with its apex at y =
62   height/2 and its bottom at y = -height/2.  Both bottomRadius and
63   height shall be greater than zero. Figure 6.3 illustrates the Cone
64   node.
65 
66   <center>
67   <img src="http://www.web3d.org/documents/specifications/14772/V2.0/Images/cone.gif">
68   Figure 6.3
69   </center>
70 
71   The side field specifies whether sides of the cone are created and
72   the bottom field specifies whether the bottom cap of the cone is
73   created. A value of TRUE specifies that this part of the cone
74   exists, while a value of FALSE specifies that this part does not
75   exist (not rendered or eligible for collision or sensor intersection
76   tests).  When a texture is applied to the sides of the cone, the
77   texture wraps counterclockwise (from above) starting at the back of
78   the cone. The texture has a vertical seam at the back in the X=0
79   plane, from the apex (0, height/2, 0) to the point (0, -height/2, -
80   bottomRadius). For the bottom cap, a circle is cut out of the
81   texture square centred at (0, -height/2, 0) with dimensions (2 �
82   bottomRadius) by (2 � bottomRadius).  The bottom cap texture appears
83   right side up when the top of the cone is rotated towards the
84   -Z-axis. SoVRMLTextureTransform affects the texture coordinates of
85   the Cone.
86 
87   The Cone geometry requires outside faces only. When viewed from the
88   inside the results are undefined.
89 
90 */
91 
92 /*!
93   \var SoSFFloat SoVRMLCone::bottomRadius
94   The cone bottom radius. Default value is 1.0.
95 */
96 
97 /*!
98   \var SoSFFloat SoVRMLCone::height
99   The cone height. Default value is 2.0.
100 */
101 
102 /*!
103   \var SoSFBool SoVRMLCone::side
104   Enable/disable the cone side wall. Default value is TRUE.
105 */
106 
107 /*!
108   \var SoSFBool SoVRMLCone::bottom
109   Enable/disable the cone bottom. Default value is TRUE.
110 */
111 
112 #include <Inventor/VRMLnodes/SoVRMLCone.h>
113 #include "coindefs.h"
114 
115 #include <Inventor/VRMLnodes/SoVRMLMacros.h>
116 #include <Inventor/actions/SoGLRenderAction.h>
117 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
118 #include <Inventor/actions/SoRayPickAction.h>
119 #include <Inventor/bundles/SoMaterialBundle.h>
120 #include <Inventor/details/SoConeDetail.h>
121 #include <Inventor/misc/SoState.h>
122 #include <Inventor/elements/SoGLShapeHintsElement.h>
123 #include <Inventor/elements/SoGLMultiTextureEnabledElement.h>
124 #include <Inventor/elements/SoMultiTextureCoordinateElement.h>
125 
126 #include "nodes/SoSubNodeP.h"
127 #include "rendering/SoGL.h"
128 #include "misc/SoGenerate.h"
129 #include "misc/SoPick.h"
130 
131 SO_NODE_SOURCE(SoVRMLCone);
132 
133 #define CONE_SIDE_NUMTRIS 40.0f
134 
135 // Doc in parent
136 void
initClass(void)137 SoVRMLCone::initClass(void) // static
138 {
139   SO_NODE_INTERNAL_INIT_CLASS(SoVRMLCone, SO_VRML97_NODE_TYPE);
140 }
141 
142 /*!
143   Constructor.
144 */
SoVRMLCone(void)145 SoVRMLCone::SoVRMLCone(void)
146 {
147   SO_VRMLNODE_INTERNAL_CONSTRUCTOR(SoVRMLCone);
148 
149   SO_VRMLNODE_ADD_FIELD(bottomRadius, (1.0f));
150   SO_VRMLNODE_ADD_FIELD(height, (2.0f));
151 
152   SO_VRMLNODE_ADD_FIELD(side, (TRUE));
153   SO_VRMLNODE_ADD_FIELD(bottom, (TRUE));
154 }
155 
156 /*!
157   Destructor.
158 */
~SoVRMLCone()159 SoVRMLCone::~SoVRMLCone()
160 {
161 }
162 
163 // Doc in parent
164 void
GLRender(SoGLRenderAction * action)165 SoVRMLCone::GLRender(SoGLRenderAction * action)
166 {
167   if (!shouldGLRender(action)) return;
168 
169   SoState * state = action->getState();
170 
171   SbBool doTextures = SoGLMultiTextureEnabledElement::get(state);
172 
173   SoMaterialBundle mb(action);
174   mb.sendFirst();
175 
176   SbBool sendNormals = !mb.isColorOnly() ||
177     (SoMultiTextureCoordinateElement::getType(state) == SoMultiTextureCoordinateElement::FUNCTION);
178 
179   unsigned int flags = 0;
180   if (doTextures) flags |= SOGL_NEED_TEXCOORDS;
181   if (sendNormals) flags |= SOGL_NEED_NORMALS;
182 
183   if (this->side.getValue()) flags |= SOGL_RENDER_SIDE;
184   if (this->bottom.getValue()) flags |= SOGL_RENDER_BOTTOM;
185 
186   float complexity = this->getComplexityValue(action);
187 
188   // enable back face culling
189   SoGLShapeHintsElement::forceSend(state, TRUE, TRUE);
190 
191   sogl_render_cone(this->bottomRadius.getValue(),
192                    this->height.getValue(),
193                    (int)(CONE_SIDE_NUMTRIS * complexity),
194                    &mb,
195                    flags, state);
196 }
197 
198 // Doc in parent
199 void
rayPick(SoRayPickAction * action)200 SoVRMLCone::rayPick(SoRayPickAction * action)
201 {
202   if (!this->shouldRayPick(action)) return;
203 
204   unsigned int flags = 0;
205   if (this->side.getValue()) flags |= SOPICK_SIDES;
206   if (this->bottom.getValue()) flags |= SOPICK_BOTTOM;
207 
208   sopick_pick_cone(this->bottomRadius.getValue(),
209                    this->height.getValue(),
210                    flags, this, action);
211 }
212 
213 // Doc in parent
214 void
getPrimitiveCount(SoGetPrimitiveCountAction * action)215 SoVRMLCone::getPrimitiveCount(SoGetPrimitiveCountAction * action)
216 {
217   if (!this->shouldPrimitiveCount(action)) return;
218 
219   float complexity = this->getComplexityValue(action);
220   int numtris = (int)(complexity * CONE_SIDE_NUMTRIS);
221 
222   if (this->bottom.getValue()) {
223     action->addNumTriangles(numtris-2);
224   }
225   if (this->side.getValue()) {
226     action->addNumTriangles(numtris);
227   }
228 }
229 
230 // Doc in parent
231 void
generatePrimitives(SoAction * action)232 SoVRMLCone::generatePrimitives(SoAction * action)
233 {
234   unsigned int flags = 0;
235   if (this->side.getValue()) flags |= SOGEN_GENERATE_SIDE;
236   if (this->bottom.getValue()) flags |= SOGEN_GENERATE_BOTTOM;
237 
238   float complexity = this->getComplexityValue(action);
239 
240   sogen_generate_cone(this->bottomRadius.getValue(),
241                       this->height.getValue(),
242                       (int)(CONE_SIDE_NUMTRIS * complexity),
243                       flags,
244                       this,
245                       action);
246 }
247 
248 // Doc in parent
249 void
computeBBox(SoAction * COIN_UNUSED_ARG (action),SbBox3f & box,SbVec3f & center)250 SoVRMLCone::computeBBox(SoAction * COIN_UNUSED_ARG(action),
251                         SbBox3f & box,
252                         SbVec3f & center)
253 {
254   float r = this->bottomRadius.getValue();
255   float h = this->height.getValue();
256 
257   // Allow negative values.
258   if (h < 0.0f) h = -h;
259   if (r < 0.0f) r = -r;
260 
261   float half_height = h * 0.5f;
262 
263   // The SIDES are present, so just find the middle point and enclose
264   // everything.
265   if (this->side.getValue()) {
266     center.setValue(0.0f, 0.0f, 0.0f);
267     box.setBounds(SbVec3f(-r, -half_height, -r), SbVec3f(r, half_height, r));
268   }
269   // ..no SIDES, but we've still got the bottom (NB: OIV misses this case).
270   else if (this->bottom.getValue()) {
271     center.setValue(0.0f, -half_height, 0.0f);
272     box.setBounds(SbVec3f(-r, -half_height, -r), SbVec3f(r, -half_height, r));
273   }
274   // ..no parts present. My confidence is shot -- I feel very small.
275   else {
276     center.setValue(0.0f, 0.0f, 0.0f);
277     box.setBounds(SbVec3f(0.0f, 0.0f, 0.0f), SbVec3f(0.0f, 0.0f, 0.0f));
278   }
279 }
280 
281 #undef CONE_SIDE_NUMTRIS
282 
283 #endif // HAVE_VRML97
284