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 SoGLVBOElement Inventor/elements/SoGLVBOElement.h
35 \brief The SoGLVBOElement class is used to store VBO state.
36
37 \ingroup elements
38
39 FIXME: write doc.
40
41 \COIN_CLASS_EXTENSION
42
43 \since Coin 2.5
44 */
45
46 #include <Inventor/elements/SoGLVBOElement.h>
47
48 #include <cassert>
49
50 #include <Inventor/elements/SoGLCacheContextElement.h>
51 #include <Inventor/nodes/SoNode.h>
52 #include <Inventor/lists/SbList.h>
53 #include <Inventor/C/glue/gl.h>
54 #include <Inventor/misc/SoGLDriverDatabase.h>
55
56 #include "rendering/SoGL.h"
57 #include "glue/glp.h"
58 #include "rendering/SoVBO.h"
59
60 #define PRIVATE(obj) obj->pimpl
61
62 class SoGLVBOElementP {
63 public:
64
65 SoVBO * vertexvbo;
66 SoVBO * normalvbo;
67 SoVBO * colorvbo;
68 SbList <SoVBO*> texcoordvbo;
69 };
70
71 SO_ELEMENT_CUSTOM_CONSTRUCTOR_SOURCE(SoGLVBOElement);
72
73 /*!
74 This static method initializes static data for the
75 SoMultiTextureCoordinateElement class.
76 */
77
78 void
initClass()79 SoGLVBOElement::initClass()
80 {
81 SO_ELEMENT_INIT_CLASS(SoGLVBOElement, inherited);
82 }
83
84
85 /*!
86 The constructor.
87 */
SoGLVBOElement(void)88 SoGLVBOElement::SoGLVBOElement(void)
89 {
90 PRIVATE(this) = new SoGLVBOElementP;
91
92 this->setTypeId(SoGLVBOElement::classTypeId);
93 this->setStackIndex(SoGLVBOElement::classStackIndex);
94 }
95
96 /*!
97 The destructor.
98 */
99
~SoGLVBOElement()100 SoGLVBOElement::~SoGLVBOElement()
101 {
102 delete PRIVATE(this);
103 }
104
105 /*!
106 Sets the vertex VBO.
107 */
108 void
setVertexVBO(SoState * state,SoVBO * vbo)109 SoGLVBOElement::setVertexVBO(SoState * state, SoVBO * vbo)
110 {
111 SoGLVBOElement * elem = getElement(state);
112 PRIVATE(elem)->vertexvbo = vbo;
113 }
114
115 /*!
116 Sets the normal VBO.
117 */
118 void
setNormalVBO(SoState * state,SoVBO * vbo)119 SoGLVBOElement::setNormalVBO(SoState * state, SoVBO * vbo)
120 {
121 SoGLVBOElement * elem = getElement(state);
122 PRIVATE(elem)->normalvbo = vbo;
123 }
124
125 /*!
126 Sets the color VBO.
127 */
128 void
setColorVBO(SoState * state,SoVBO * vbo)129 SoGLVBOElement::setColorVBO(SoState * state, SoVBO * vbo)
130 {
131 SoGLVBOElement * elem = getElement(state);
132 PRIVATE(elem)->colorvbo = vbo;
133 }
134
135 /*!
136 Sets the texture coordinate VBO.
137 */
138 void
setTexCoordVBO(SoState * state,const int unit,SoVBO * vbo)139 SoGLVBOElement::setTexCoordVBO(SoState * state, const int unit, SoVBO * vbo)
140 {
141 SoGLVBOElement * elem = getElement(state);
142 const int n = PRIVATE(elem)->texcoordvbo.getLength();
143 for (int i = n; i <= unit; i++) {
144 PRIVATE(elem)->texcoordvbo.append(NULL);
145 }
146 PRIVATE(elem)->texcoordvbo[unit] = vbo;
147 }
148
149 // doc in parent
150 void
init(SoState * COIN_UNUSED_ARG (state))151 SoGLVBOElement::init(SoState * COIN_UNUSED_ARG(state))
152 {
153 PRIVATE(this)->vertexvbo = NULL;
154 PRIVATE(this)->normalvbo = NULL;
155 PRIVATE(this)->colorvbo = NULL;
156 PRIVATE(this)->texcoordvbo.truncate(0);
157 }
158
159 // doc in parent
160 void
push(SoState * COIN_UNUSED_ARG (state))161 SoGLVBOElement::push(SoState * COIN_UNUSED_ARG(state))
162 {
163 SoGLVBOElement * prev = (SoGLVBOElement *)
164 this->getNextInStack();
165
166 PRIVATE(this)->vertexvbo = PRIVATE(prev)->vertexvbo;
167 PRIVATE(this)->normalvbo = PRIVATE(prev)->normalvbo;
168 PRIVATE(this)->colorvbo = PRIVATE(prev)->colorvbo;
169 PRIVATE(this)->texcoordvbo.truncate(0);
170
171 for (int i = 0; i < PRIVATE(prev)->texcoordvbo.getLength(); i++) {
172 PRIVATE(this)->texcoordvbo.append(PRIVATE(prev)->texcoordvbo[i]);
173 }
174 }
175
176 // doc in parent
177 void
pop(SoState * COIN_UNUSED_ARG (state),const SoElement * COIN_UNUSED_ARG (prevtopelement))178 SoGLVBOElement::pop(SoState * COIN_UNUSED_ARG(state), const SoElement * COIN_UNUSED_ARG(prevtopelement))
179 {
180 // nothing to do
181 }
182
183 // doc in parent
184 SbBool
matches(const SoElement * COIN_UNUSED_ARG (elt)) const185 SoGLVBOElement::matches(const SoElement * COIN_UNUSED_ARG(elt)) const
186 {
187 assert(0 && "should never get here");
188 return TRUE;
189 }
190
191 // doc in parent
192 SoElement *
copyMatchInfo(void) const193 SoGLVBOElement::copyMatchInfo(void) const
194 {
195 assert(0 && "should never get here");
196 return NULL;
197 }
198
199 /*!
200 Returns a writable element instance.
201 */
202 SoGLVBOElement *
getElement(SoState * state)203 SoGLVBOElement::getElement(SoState * state)
204 {
205 return (SoGLVBOElement*) state->getElement(classStackIndex);
206 }
207
208 /*!
209 Returns a read-only element instance.
210 */
211 const SoGLVBOElement *
getInstance(SoState * state)212 SoGLVBOElement::getInstance(SoState * state)
213 {
214 return (SoGLVBOElement*) state->getConstElement(classStackIndex);
215 }
216
217 SoVBO *
getVertexVBO(void) const218 SoGLVBOElement::getVertexVBO(void) const
219 {
220 return PRIVATE(this)->vertexvbo;
221 }
222
223 SoVBO *
getNormalVBO(void) const224 SoGLVBOElement::getNormalVBO(void) const
225 {
226 return PRIVATE(this)->normalvbo;
227 }
228
229 SoVBO *
getColorVBO(void) const230 SoGLVBOElement::getColorVBO(void) const
231 {
232 return PRIVATE(this)->colorvbo;
233 }
234
235 int
getNumTexCoordVBO(void) const236 SoGLVBOElement::getNumTexCoordVBO(void) const
237 {
238 return PRIVATE(this)->texcoordvbo.getLength();
239 }
240
241 SoVBO *
getTexCoordVBO(const int idx) const242 SoGLVBOElement::getTexCoordVBO(const int idx) const
243 {
244 if (idx < PRIVATE(this)->texcoordvbo.getLength()) {
245 return PRIVATE(this)->texcoordvbo[idx];
246 }
247 return NULL;
248 }
249
250 /*!
251 Returns \a TRUE if VBO is supported for the current context,
252 and if numdata is between the limits set for VBO rendering.
253
254 */
255 SbBool
shouldCreateVBO(SoState * state,const int numdata)256 SoGLVBOElement::shouldCreateVBO(SoState * state, const int numdata)
257 {
258 const cc_glglue * glue = sogl_glue_instance(state);
259 // don't use SoGLCacheContextElement to find the current cache
260 // context since we don't want this call to create a cache dependecy
261 // on SoGLCacheContextElement.
262 return
263 SoGLDriverDatabase::isSupported(glue, SO_GL_FRAMEBUFFER_OBJECT) &&
264 SoVBO::shouldCreateVBO(state, glue->contextid, numdata);
265 }
266
267 #undef PRIVATE
268