added: vertex attrib divisor
[opensg.git] / Source / System / NodeCores / Drawables / Geometry / PropertiesBase / OSGGeoProperty.cpp
blob41b4d90c97cc54e5ddb87afe83088032ebca8ead
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
45 #include <boost/bind.hpp>
47 #include "OSGConfig.h"
49 #include "OSGGLEXT.h"
50 #include "OSGWindow.h"
52 #include "OSGGeoProperty.h"
53 #include "OSGDrawEnv.h"
55 #include "OSGGLFuncProtos.h"
57 #include "OSGConceptPropertyChecks.h"
59 OSG_USING_NAMESPACE
61 // Documentation for this class is emited in the
62 // OSGGeoPropertyBase.cpp file.
63 // To modify it, please change the .fcd file (OSGGeoProperty.fcd) and
64 // regenerate the base file.
66 /*! \fn UInt32 GeoProperty::getFormat (void)
67 Returns the OpenGL type constant identifying the data type of the
68 properties values.
71 /*! \fn UInt32 GeoProperty::getFormatSize(void)
72 Returns the size (as a multiple of <code>sizeof(char)</code>) of the data
73 type of the properties values.
76 /*! \fn UInt32 GeoProperty::getStride (void)
77 Returns the stride (the distance between successive values) of the
78 properties values.
81 /*! \fn UInt32 GeoProperty::getDimension (void)
82 Returns the number of dimensions a single value of the property has.
85 /*! \fn UInt32 GeoProperty::size (void)
86 Returns the number of values the property holds.
88 \return Size of this property.
91 /*! \fn UInt8* GeoProperty::getData (void)
92 Returns a pointer to the values stored in this property. This mainly exists
93 to pass arrays to OpenGL.
96 /***************************************************************************\
97 * Class variables *
98 \***************************************************************************/
100 StateChunkClass GeoProperty::_class("GeoProperty", 16, 150);
102 UInt32 GeoProperty::_extVertexBufferObject = Window::invalidExtensionID;
103 UInt32 GeoProperty::_extMultitexture = Window::invalidExtensionID;
104 UInt32 GeoProperty::_arbVertexProgram = Window::invalidExtensionID;
105 UInt32 GeoProperty::_extSecondaryColor = Window::invalidExtensionID;
106 UInt32 GeoProperty::_arbInstancedArrays = Window::invalidExtensionID;
108 UInt32 GeoProperty::_funcBindBuffer =
109 Window::invalidFunctionID;
110 UInt32 GeoProperty::_funcMapBuffer =
111 Window::invalidFunctionID;
112 UInt32 GeoProperty::_funcUnmapBuffer =
113 Window::invalidFunctionID;
114 UInt32 GeoProperty::_funcBufferData =
115 Window::invalidFunctionID;
116 UInt32 GeoProperty::_funcBufferSubData =
117 Window::invalidFunctionID;
118 UInt32 GeoProperty::_funcGenBuffers =
119 Window::invalidFunctionID;
120 UInt32 GeoProperty::_funcDeleteBuffers =
121 Window::invalidFunctionID;
122 UInt32 GeoProperty::_funcVertexAttribPointerARB =
123 Window::invalidFunctionID;
124 UInt32 GeoProperty::_funcEnableVertexAttribArrayARB =
125 Window::invalidFunctionID;
126 UInt32 GeoProperty::_funcDisableVertexAttribArrayARB =
127 Window::invalidFunctionID;
129 UInt32 GeoProperty::_funcSecondaryColorPointer =
130 Window::invalidFunctionID;
131 UInt32 GeoProperty::_funcClientActiveTextureARB =
132 Window::invalidFunctionID;
133 UInt32 GeoProperty::_funcVertexAttribDivisorARB =
134 Window::invalidFunctionID;
136 /***************************************************************************\
137 * Class methods *
138 \***************************************************************************/
140 void GeoProperty::initMethod(InitPhase ePhase)
142 Inherited::initMethod(ePhase);
144 if(ePhase == TypeObject::SystemPost)
146 _extVertexBufferObject=
147 Window::registerExtension("GL_ARB_vertex_buffer_object");
149 _funcBindBuffer = Window::registerFunction(
150 OSG_DLSYM_UNDERSCORE"glBindBufferARB",
151 _extVertexBufferObject);
153 _funcMapBuffer = Window::registerFunction(
154 OSG_DLSYM_UNDERSCORE"glMapBufferARB",
155 _extVertexBufferObject);
157 _funcUnmapBuffer = Window::registerFunction(
158 OSG_DLSYM_UNDERSCORE"glUnmapBufferARB",
159 _extVertexBufferObject);
161 _funcBufferData = Window::registerFunction(
162 OSG_DLSYM_UNDERSCORE"glBufferDataARB",
163 _extVertexBufferObject);
165 _funcBufferSubData = Window::registerFunction(
166 OSG_DLSYM_UNDERSCORE"glBufferSubDataARB",
167 _extVertexBufferObject);
169 _funcDeleteBuffers = Window::registerFunction(
170 OSG_DLSYM_UNDERSCORE"glDeleteBuffersARB",
171 _extVertexBufferObject);
173 _funcGenBuffers = Window::registerFunction(
174 OSG_DLSYM_UNDERSCORE"glGenBuffersARB",
175 _extVertexBufferObject);
177 _arbVertexProgram =
178 Window::registerExtension("GL_ARB_vertex_program");
180 _funcVertexAttribPointerARB = Window::registerFunction(
181 OSG_DLSYM_UNDERSCORE"glVertexAttribPointerARB",
182 _arbVertexProgram);
184 _funcEnableVertexAttribArrayARB = Window::registerFunction(
185 OSG_DLSYM_UNDERSCORE"glEnableVertexAttribArrayARB",
186 _arbVertexProgram);
188 _funcDisableVertexAttribArrayARB = Window::registerFunction(
189 OSG_DLSYM_UNDERSCORE"glDisableVertexAttribArrayARB",
190 _arbVertexProgram);
192 _extMultitexture =
193 Window::registerExtension("GL_ARB_multitexture");
195 _funcClientActiveTextureARB = Window::registerFunction(
196 OSG_DLSYM_UNDERSCORE"glClientActiveTextureARB",
197 _extMultitexture);
199 _extSecondaryColor =
200 Window::registerExtension("GL_EXT_secondary_color");
202 _funcSecondaryColorPointer = Window::registerFunction(
203 OSG_DLSYM_UNDERSCORE"glSecondaryColorPointerEXT",
204 _extSecondaryColor);
206 _arbInstancedArrays =
207 Window::registerExtension("GL_ARB_instanced_arrays");
209 _funcVertexAttribDivisorARB = Window::registerFunction(
210 OSG_DLSYM_UNDERSCORE"glVertexAttribDivisorARB",
211 _arbInstancedArrays);
216 /***************************************************************************\
217 * Instance methods *
218 \***************************************************************************/
220 /*-------------------------------------------------------------------------*\
221 - private -
222 \*-------------------------------------------------------------------------*/
224 /*----------------------- constructors & destructors ----------------------*/
226 GeoProperty::GeoProperty(void) :
227 Inherited()
231 GeoProperty::GeoProperty(const GeoProperty &source) :
232 Inherited(source)
236 GeoProperty::~GeoProperty(void)
241 /*------------------------- Chunk Class Access ---------------------------*/
243 const StateChunkClass *GeoProperty::getClass(void) const
245 return &_class;
248 /*----------------------------- class specific ----------------------------*/
250 void GeoProperty::changed(ConstFieldMaskArg whichField,
251 UInt32 origin,
252 BitVector details)
254 Inherited::changed(whichField, origin, details);
256 if(0x0000 != (whichField & ~UsageFieldMask))
258 if(getGLId() > 0)
260 Window::refreshGLObject(getGLId());
265 void GeoProperty::dump( UInt32 ,
266 const BitVector ) const
268 SLOG << "GeoProperty:"
269 << "Format: " << getFormat()
270 << " FormatSize: " << getFormatSize()
271 << " Stride: " << getStride()
272 << " Dim: " << getDimension()
273 << " Size: " << size() << std::endl;
276 /*------------------------------ State ------------------------------------*/
279 /*! GL object handler
280 create the VBO and destroy it
283 UInt32 GeoProperty::handleGL(DrawEnv *pEnv,
284 UInt32 id,
285 Window::GLObjectStatusE mode,
286 UInt64 uiOptions)
288 GLuint glid;
290 Window *pWin = pEnv->getWindow();
292 osgSinkUnusedWarning(pWin);
294 if(mode == Window::initialize || mode == Window::reinitialize ||
295 mode == Window::needrefresh )
297 if(mode == Window::initialize)
299 OSGGETGLFUNCBYID_GL3_ES( glGenBuffers,
300 osgGlGenBuffers,
301 _funcGenBuffers,
302 pWin);
304 osgGlGenBuffers(1, &glid);
306 pWin->setGLObjectId(id, glid);
308 else
310 glid = pWin->getGLObjectId(id);
313 OSGGETGLFUNCBYID_GL3_ES( glBindBuffer,
314 osgGlBindBuffer,
315 _funcBindBuffer,
316 pWin);
318 OSGGETGLFUNCBYID_GL3_ES( glBufferData,
319 osgGlBufferData,
320 _funcBufferData,
321 pWin);
323 OSGGETGLFUNCBYID_GL3_ES( glBufferSubData,
324 osgGlBufferSubData,
325 _funcBufferSubData,
326 pWin);
328 osgGlBindBuffer(getBufferType(), glid);
330 UInt32 uiBufferSize = getFormatSize() * getDimension() * size32();
332 bool bSizeChanged = (uiBufferSize != pWin->getGLObjectInfo(id));
335 if(mode == Window::initialize ||
336 mode == Window::reinitialize ||
337 bSizeChanged == true )
339 osgGlBufferData(getBufferType(),
340 uiBufferSize,
341 NULL, //getData (),
342 getVboUsage ());
344 pWin->setGLObjectInfo(id, uiBufferSize);
347 if(getData() != NULL)
349 osgGlBufferSubData(getBufferType(),
351 uiBufferSize,
352 getData());
355 osgGlBindBuffer(getBufferType(), 0);
357 else
359 SWARNING << "GeoProperty(" << this << "::handleGL: Illegal mode: "
360 << mode << " for id " << id << std::endl;
363 return 0;
366 void GeoProperty::handleDestroyGL(DrawEnv *pEnv,
367 UInt32 id,
368 Window::GLObjectStatusE mode)
370 GLuint glid;
372 Window *pWin = pEnv->getWindow();
374 osgSinkUnusedWarning(pWin);
376 if(mode == Window::destroy)
378 OSGGETGLFUNCBYID_GL3_ES( glDeleteBuffers,
379 osgGlDeleteBuffers,
380 _funcDeleteBuffers,
381 pWin);
383 glid = pWin->getGLObjectId(id);
385 osgGlDeleteBuffers(1, &glid);
387 pWin->setGLObjectId(id, 0);
389 else if(mode == Window::finaldestroy)
391 //SWARNING << "Last texture user destroyed" << std::endl;
393 else
395 SWARNING << "GeoProperty::handleDestroyGL: Illegal mode: "
396 << mode << " for id " << id << std::endl;
400 /*-------------------------- Comparison -----------------------------------*/
402 bool GeoProperty::isTransparent(void) const
404 return false;
407 Real32 GeoProperty::switchCost(StateChunk *)
409 return 0;
412 bool GeoProperty::operator < (const StateChunk &other) const
414 return this < &other;
417 bool GeoProperty::operator == (const StateChunk &other) const
419 GeoProperty const *tother = dynamic_cast<GeoProperty const*>(&other);
421 if(!tother)
422 return false;
424 if(tother == this)
425 return true;
427 return false;
430 bool GeoProperty::operator != (const StateChunk &other) const
432 return ! (*this == other);