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 SoCoordinate4 SoCoordinate4.h Inventor/nodes/SoCoordinate4.h
35   \brief The SoCoordinate4 class is a node for providing coordinates to shape nodes.
36 
37   \ingroup nodes
38 
39   When encountering nodes of this type during traversal, the
40   coordinates it contains will be put on the statestack for later use
41   by shape nodes of types which needs coordinate sets (like SoFaceSet
42   nodes or SoPointSet nodes).
43 
44   The fourth element of the coordinate vectors is used for
45   normalization. A node of this type where all the normalization
46   values are equal to 1.0 is the equivalent of setting up an
47   SoCoordinate3 node.
48 
49   Note that an SoCoordinate4 node will \e replace the coordinates
50   already present in the state (if any).
51 
52   <b>FILE FORMAT/DEFAULTS:</b>
53   \code
54     Coordinate4 {
55         point 0 0 0 1
56     }
57   \endcode
58 
59   \sa SoCoordinate3
60 */
61 
62 #include <Inventor/nodes/SoCoordinate4.h>
63 
64 #include <Inventor/actions/SoCallbackAction.h>
65 #include <Inventor/actions/SoGLRenderAction.h>
66 #include <Inventor/actions/SoGetBoundingBoxAction.h>
67 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
68 #include <Inventor/actions/SoPickAction.h>
69 #include <Inventor/elements/SoGLCoordinateElement.h>
70 #include <Inventor/elements/SoGLVBOElement.h>
71 
72 #include "nodes/SoSubNodeP.h"
73 #include "rendering/SoVBO.h"
74 
75 /*!
76   \var SoMFVec4f SoCoordinate4::point
77 
78   Coordinate set of 3D points as 4D vectors (each vector contains a 3D
79   coordinate plus normalization value).
80 */
81 
82 class SoCoordinate4P {
83  public:
SoCoordinate4P()84   SoCoordinate4P() : vbo(NULL) { }
~SoCoordinate4P()85   ~SoCoordinate4P() { delete this->vbo; }
86   SoVBO * vbo;
87 };
88 
89 #define PRIVATE(obj) obj->pimpl
90 
91 // *************************************************************************
92 
93 SO_NODE_SOURCE(SoCoordinate4);
94 
95 /*!
96   Constructor.
97 */
SoCoordinate4(void)98 SoCoordinate4::SoCoordinate4(void)
99 {
100   PRIVATE(this) = new SoCoordinate4P;
101   SO_NODE_INTERNAL_CONSTRUCTOR(SoCoordinate4);
102 
103   SO_NODE_ADD_FIELD(point, (SbVec4f(0.0f, 0.0f, 0.0f, 1.0f)));
104 }
105 
106 /*!
107   Destructor.
108 */
~SoCoordinate4()109 SoCoordinate4::~SoCoordinate4()
110 {
111   delete PRIVATE(this);
112 }
113 
114 // Doc from superclass.
115 void
initClass(void)116 SoCoordinate4::initClass(void)
117 {
118   SO_NODE_INTERNAL_INIT_CLASS(SoCoordinate4, SO_FROM_INVENTOR_1);
119 
120   SO_ENABLE(SoGetBoundingBoxAction, SoCoordinateElement);
121   SO_ENABLE(SoGLRenderAction, SoGLCoordinateElement);
122   SO_ENABLE(SoPickAction, SoCoordinateElement);
123   SO_ENABLE(SoCallbackAction, SoCoordinateElement);
124   SO_ENABLE(SoGetPrimitiveCountAction, SoCoordinateElement);
125 }
126 
127 // Doc from superclass.
128 void
getBoundingBox(SoGetBoundingBoxAction * action)129 SoCoordinate4::getBoundingBox(SoGetBoundingBoxAction * action)
130 {
131   SoCoordinate4::doAction(action);
132 }
133 
134 // Doc from superclass.
135 void
doAction(SoAction * action)136 SoCoordinate4::doAction(SoAction * action)
137 {
138   SoCoordinateElement::set4(action->getState(), this,
139                             point.getNum(), point.getValues(0));
140 }
141 
142 // Doc from superclass.
143 void
GLRender(SoGLRenderAction * action)144 SoCoordinate4::GLRender(SoGLRenderAction * action)
145 {
146   SoCoordinate4::doAction(action);
147   SoState * state = action->getState();
148   const int num = this->point.getNum();
149   SbBool setvbo = FALSE;
150   SoBase::staticDataLock();
151   if (SoGLVBOElement::shouldCreateVBO(state, num)) {
152     SbBool dirty = FALSE;
153     setvbo = TRUE;
154     if (PRIVATE(this)->vbo == NULL) {
155       PRIVATE(this)->vbo = new SoVBO(GL_ARRAY_BUFFER, GL_STATIC_DRAW);
156       dirty =  TRUE;
157     }
158     else if (PRIVATE(this)->vbo->getBufferDataId() != this->getNodeId()) {
159       dirty = TRUE;
160     }
161     if (dirty) {
162       PRIVATE(this)->vbo->setBufferData(this->point.getValues(0),
163                                         num*sizeof(SbVec4f),
164                                         this->getNodeId());
165     }
166   }
167   else if (PRIVATE(this)->vbo && PRIVATE(this)->vbo->getBufferDataId()) {
168     // clear buffers to deallocate VBO memory
169     PRIVATE(this)->vbo->setBufferData(NULL, 0, 0);
170   }
171   SoBase::staticDataUnlock();
172   if (setvbo) {
173     SoGLVBOElement::setVertexVBO(state, PRIVATE(this)->vbo);
174   }
175 }
176 
177 // Doc from superclass.
178 void
callback(SoCallbackAction * action)179 SoCoordinate4::callback(SoCallbackAction * action)
180 {
181   SoCoordinate4::doAction(action);
182 }
183 
184 // Doc from superclass.
185 void
pick(SoPickAction * action)186 SoCoordinate4::pick(SoPickAction * action)
187 {
188   SoCoordinate4::doAction(action);
189 }
190 
191 // Doc from superclass.
192 void
getPrimitiveCount(SoGetPrimitiveCountAction * action)193 SoCoordinate4::getPrimitiveCount(SoGetPrimitiveCountAction * action)
194 {
195   SoCoordinate4::doAction(action);
196 }
197 
198 #undef PRIVATE
199