2003-01-22 00:45:36 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
2001-10-04 23:12:57 +08:00
2001-01-11 00:32:10 +08:00
#ifndef OSG_STATE
#define OSG_STATE 1
#include <osg/Export>
2001-09-20 05:08:56 +08:00
#include <osg/StateSet>
#include <osg/Matrix>
2001-09-22 10:42:08 +08:00
#include <osg/FrameStamp>
2001-12-22 06:48:19 +08:00
#include <osg/DisplaySettings>
2002-06-03 23:39:41 +08:00
#include <osg/Polytope>
2001-09-22 10:42:08 +08:00
2001-09-20 05:08:56 +08:00
#include <vector>
#include <map>
2001-01-11 00:32:10 +08:00
namespace osg {
2002-07-12 02:32:41 +08:00
#ifndef GL_TEXTURE0
#define GL_TEXTURE0 0x84C0
#endif
2002-08-16 04:27:33 +08:00
#ifndef GL_FOG_COORDINATE_ARRAY
#ifdef GL_FOG_COORDINATE_ARRAY_EXT
#define GL_FOG_COORDINATE_ARRAY GL_FOG_COORDINATE_ARRAY_EXT
#else
#define GL_FOG_COORDINATE_ARRAY 0x8457
#endif
#endif
#ifndef GL_SECONDARY_COLOR_ARRAY
2002-10-17 18:04:11 +08:00
#ifdef GL_SECONDARY_COLOR_ARRAY_EXT
2002-08-16 04:27:33 +08:00
#define GL_SECONDARY_COLOR_ARRAY GL_SECONDARY_COLOR_ARRAY_EXT
#else
#define GL_SECONDARY_COLOR_ARRAY 0x845E
#endif
#endif
2002-07-12 05:08:02 +08:00
2001-09-20 05:08:56 +08:00
/** macro for use with osg::StateAttrbiute::apply methods for detected and
* reporting OpenGL error messages.*/
#define OSG_GL_DEBUG(message) \
if (state.getFineGrainedErrorDetection()) \
{ \
GLenum errorNo = glGetError(); \
if (errorNo!=GL_NO_ERROR) \
{ \
osg::notify(WARN)<<"Warning: detected OpenGL error '"<<gluErrorString(errorNo)<<" "<<message<<endl; \
}\
}
/** State class for managing a state stack.
2001-10-01 19:15:55 +08:00
* Lazy state updating is used to minimize state changes.
2001-09-20 05:08:56 +08:00
*/
class SG_EXPORT State : public Referenced
2001-01-11 00:32:10 +08:00
{
public :
2001-09-20 05:08:56 +08:00
State();
/** push stateset onto state stack.*/
void pushStateSet(const StateSet* dstate);
/** pop drawstate off state stack.*/
void popStateSet();
2001-10-23 06:02:47 +08:00
/** copy the modes and attributes which captures the current state.*/
void captureCurrentState(StateSet& stateset) const;
2001-09-20 05:08:56 +08:00
/** reset the state object to an empty stack.*/
void reset();
2003-01-10 17:25:42 +08:00
inline void applyProjectionMatrix(const osg::RefMatrix* matrix)
2002-03-30 01:26:40 +08:00
{
if (_projection!=matrix)
{
glMatrixMode( GL_PROJECTION );
if (matrix)
{
_projection=matrix;
glLoadMatrixf(matrix->ptr());
}
else
{
_projection=_identity;
glLoadIdentity();
}
glMatrixMode( GL_MODELVIEW );
}
}
const osg::Matrix& getProjectionMatrix() const
{
return *_projection;
}
2003-01-10 17:25:42 +08:00
inline void applyModelViewMatrix(const osg::RefMatrix* matrix)
2002-03-30 01:26:40 +08:00
{
if (_modelView!=matrix)
{
if (matrix)
{
_modelView=matrix;
glLoadMatrixf(matrix->ptr());
}
else
{
_modelView=_identity;
glLoadIdentity();
}
}
}
const osg::Matrix& getModelViewMatrix() const
{
return *_modelView;
}
2002-06-03 23:39:41 +08:00
Polytope getViewFrustum() const;
2001-09-20 05:08:56 +08:00
2002-05-21 16:59:26 +08:00
/** Apply stateset.*/
2001-09-20 05:08:56 +08:00
void apply(const StateSet* dstate);
2002-05-21 16:59:26 +08:00
/** Apply the state.*/
2001-09-20 05:08:56 +08:00
void apply();
2002-05-21 16:59:26 +08:00
/** Apply an OpenGL mode if required. */
2002-09-02 20:31:35 +08:00
inline bool applyMode(StateAttribute::GLMode mode,bool enabled)
2002-05-21 16:59:26 +08:00
{
2002-05-22 18:04:28 +08:00
ModeStack& ms = _modeMap[mode];
ms.changed = true;
return applyMode(mode,enabled,ms);
2002-05-21 16:59:26 +08:00
}
2002-09-02 20:31:35 +08:00
inline bool applyTextureMode(unsigned int unit, StateAttribute::GLMode mode,bool enabled)
2002-07-07 22:40:41 +08:00
{
2002-12-10 05:03:02 +08:00
if (setActiveTextureUnit(unit))
{
ModeMap& modeMap = getOrCreateTextureModeMap(unit);
ModeStack& ms = modeMap[mode];
ms.changed = true;
return applyMode(mode,enabled,ms);
}
else
return false;
2002-07-07 22:40:41 +08:00
}
2002-05-21 16:59:26 +08:00
/** Apply an attribute if required. */
2002-09-02 20:31:35 +08:00
inline bool applyAttribute(const StateAttribute* attribute)
2002-05-21 16:59:26 +08:00
{
2002-05-22 18:04:28 +08:00
AttributeStack& as = _attributeMap[attribute->getType()];
as.changed = true;
return applyAttribute(attribute,as);
2002-05-21 16:59:26 +08:00
}
2002-09-02 20:31:35 +08:00
inline bool applyTextureAttribute(unsigned int unit, const StateAttribute* attribute)
2002-07-07 22:40:41 +08:00
{
2002-12-10 05:03:02 +08:00
if (setActiveTextureUnit(unit))
{
AttributeMap& attributeMap = getOrCreateTextureAttributeMap(unit);
AttributeStack& as = attributeMap[attribute->getType()];
as.changed = true;
return applyAttribute(attribute,as);
}
else
return false;
2002-07-07 22:40:41 +08:00
}
2001-09-20 05:08:56 +08:00
2002-05-21 16:59:26 +08:00
/** Mode has been set externally, update state to reflect this setting.*/
2002-09-02 20:31:35 +08:00
void haveAppliedMode(StateAttribute::GLMode mode,StateAttribute::GLModeValue value);
2001-09-20 05:08:56 +08:00
2002-05-21 16:59:26 +08:00
/** Mode has been set externally, therefore dirty the associated mode in osg::State
2002-03-21 20:00:10 +08:00
* so it is applied on next call to osg::State::apply(..)*/
2002-09-02 20:31:35 +08:00
void haveAppliedMode(StateAttribute::GLMode mode);
2002-03-21 20:00:10 +08:00
2002-05-21 16:59:26 +08:00
/** Attribute has been applied externally, update state to reflect this setting.*/
2002-03-21 20:36:05 +08:00
void haveAppliedAttribute(const StateAttribute* attribute);
2002-03-21 20:00:10 +08:00
2002-05-21 16:59:26 +08:00
/** Attribute has been applied externally,
2002-03-21 20:00:10 +08:00
* and therefore this attribute type has been dirtied
* and will need to be re-appplied on next osg::State.apply(..).
* note, if you have an osg::StateAttribute which you have applied externally
* then use the have_applied(attribute) method as this will the osg::State to
* track the current state more accuratly and enable lazy state updating such
* that only changed state will be applied.*/
2002-09-02 20:31:35 +08:00
void haveAppliedAttribute(StateAttribute::Type type);
2001-09-20 05:08:56 +08:00
2002-05-21 16:59:26 +08:00
/** Get whether the current specified mode is enabled (true) or disabled (false).*/
2002-09-02 20:31:35 +08:00
bool getLastAppliedMode(StateAttribute::GLMode mode) const;
2002-05-21 16:59:26 +08:00
/** Get the current specified attribute, return NULL is one has not yet been applied.*/
2002-09-02 20:31:35 +08:00
const StateAttribute* getLastAppliedAttribute(StateAttribute::Type type) const;
2002-07-07 22:40:41 +08:00
/** texture Mode has been set externally, update state to reflect this setting.*/
2002-09-02 20:31:35 +08:00
void haveAppliedTextureMode(unsigned int unit, StateAttribute::GLMode mode,StateAttribute::GLModeValue value);
2002-07-07 22:40:41 +08:00
/** texture Mode has been set externally, therefore dirty the associated mode in osg::State
* so it is applied on next call to osg::State::apply(..)*/
2002-09-02 20:31:35 +08:00
void haveAppliedTextureMode(unsigned int unit, StateAttribute::GLMode mode);
2002-07-07 22:40:41 +08:00
/** texture Attribute has been applied externally, update state to reflect this setting.*/
void haveAppliedTextureAttribute(unsigned int unit, const StateAttribute* attribute);
/** texture Attribute has been applied externally,
* and therefore this attribute type has been dirtied
* and will need to be re-appplied on next osg::State.apply(..).
* note, if you have an osg::StateAttribute which you have applied externally
* then use the have_applied(attribute) method as this will the osg::State to
* track the current state more accuratly and enable lazy state updating such
* that only changed state will be applied.*/
2002-09-02 20:31:35 +08:00
void haveAppliedTextureAttribute(unsigned int unit, StateAttribute::Type type);
2002-07-07 22:40:41 +08:00
/** Get whether the current specified texture mode is enabled (true) or disabled (false).*/
2002-09-02 20:31:35 +08:00
bool getLastAppliedTextureMode(unsigned int unit, StateAttribute::GLMode mode) const;
2002-07-07 22:40:41 +08:00
/** Get the current specified texture attribute, return NULL is one has not yet been applied.*/
2002-09-02 20:31:35 +08:00
const StateAttribute* getLastAppliedTextureAttribute(unsigned int unit, StateAttribute::Type type) const;
2002-07-07 22:40:41 +08:00
2003-01-19 23:28:08 +08:00
/** Dirty the modes previously applied in osg::State.*/
void dirtyAllModes();
/** Dirty the modes attributes previously applied in osg::State.*/
void dirtyAllAttributes();
2002-09-13 21:50:58 +08:00
/** disable the vertex, normal, color, tex coords, secenday color, fog coord and index arrays.*/
void disableAllVertexArrays();
/** dirty the vertex, normal, color, tex coords, secenday color, fog coord and index arrays.*/
void dirtyAllVertexArrays();
2002-07-07 22:40:41 +08:00
2002-11-19 18:56:59 +08:00
/** Wrapper around glInterleavedArrays(..).
* also resets the internal array points and modes within osg::State to keep the other
* vertex array operations consistent. */
2003-03-06 05:02:37 +08:00
void setInterleavedArrays( GLenum format, GLsizei stride, const GLvoid* pointer);
2002-11-19 18:56:59 +08:00
2002-07-07 22:40:41 +08:00
/** wrapper around glEnableClientState(GL_VERTEX_ARRAY);glVertexPointer(..);
* note, only updates values that change.*/
inline void setVertexPointer( GLint size, GLenum type,
GLsizei stride, const GLvoid *ptr )
{
2002-09-13 21:50:58 +08:00
if (!_vertexArray._enabled || _vertexArray._dirty)
2002-07-07 22:40:41 +08:00
{
_vertexArray._enabled = true;
glEnableClientState(GL_VERTEX_ARRAY);
}
2002-09-13 21:50:58 +08:00
if (_vertexArray._pointer!=ptr || _vertexArray._dirty)
2002-07-07 22:40:41 +08:00
{
_vertexArray._pointer=ptr;
glVertexPointer( size, type, stride, ptr );
}
2002-09-13 21:50:58 +08:00
_vertexArray._dirty = false;
2002-07-07 22:40:41 +08:00
}
/** wrapper glDisableClientState(GL_VERTEX_ARRAY).
* note, only updates values that change.*/
inline void disableVertexPointer()
{
2002-09-13 21:50:58 +08:00
if (_vertexArray._enabled || _vertexArray._dirty)
2002-07-07 22:40:41 +08:00
{
_vertexArray._enabled = false;
2002-09-13 21:50:58 +08:00
_vertexArray._dirty = false;
2002-07-07 22:40:41 +08:00
glDisableClientState(GL_VERTEX_ARRAY);
}
}
2002-09-13 21:50:58 +08:00
inline void dirtyVertexPointer()
{
_vertexArray._dirty = true;
}
2002-07-07 22:40:41 +08:00
/** wrapper around glEnableClientState(GL_NORMAL_ARRAY);glNormalPointer(..);
* note, only updates values that change.*/
inline void setNormalPointer( GLenum type, GLsizei stride,
const GLvoid *ptr )
{
2002-09-13 21:50:58 +08:00
if (!_normalArray._enabled || _normalArray._dirty)
2002-07-07 22:40:41 +08:00
{
_normalArray._enabled = true;
glEnableClientState(GL_NORMAL_ARRAY);
}
2002-09-13 21:50:58 +08:00
if (_normalArray._pointer!=ptr || _normalArray._dirty)
2002-07-07 22:40:41 +08:00
{
_normalArray._pointer=ptr;
glNormalPointer( type, stride, ptr );
}
2002-09-13 21:50:58 +08:00
_normalArray._dirty = false;
2002-07-07 22:40:41 +08:00
}
/** wrapper around glDisableClientState(GL_NORMAL_ARRAY);
* note, only updates values that change.*/
inline void disableNormalPointer()
{
2002-09-13 21:50:58 +08:00
if (_normalArray._enabled || _normalArray._dirty)
2002-07-07 22:40:41 +08:00
{
_normalArray._enabled = false;
2002-09-13 21:50:58 +08:00
_normalArray._dirty = false;
2002-07-07 22:40:41 +08:00
glDisableClientState(GL_NORMAL_ARRAY);
}
}
2002-09-13 21:50:58 +08:00
inline void dirtyNormalPointer()
{
_normalArray._dirty = true;
}
2002-07-07 22:40:41 +08:00
/** wrapper around glEnableClientState(GL_COLOR_ARRAY);glColorPointer(..);
* note, only updates values that change.*/
inline void setColorPointer( GLint size, GLenum type,
GLsizei stride, const GLvoid *ptr )
{
2002-09-13 21:50:58 +08:00
if (!_colorArray._enabled || _colorArray._dirty)
2002-07-07 22:40:41 +08:00
{
_colorArray._enabled = true;
glEnableClientState(GL_COLOR_ARRAY);
}
2002-09-13 21:50:58 +08:00
if (_colorArray._pointer!=ptr || _colorArray._dirty)
2002-07-07 22:40:41 +08:00
{
_colorArray._pointer=ptr;
glColorPointer( size, type, stride, ptr );
}
2002-09-13 21:50:58 +08:00
_colorArray._dirty = false;
2002-07-07 22:40:41 +08:00
}
/** wrapper around glDisableClientState(GL_COLOR_ARRAY);
* note, only updates values that change.*/
inline void disableColorPointer()
{
2002-09-13 21:50:58 +08:00
if (_colorArray._enabled || _colorArray._dirty)
2002-07-07 22:40:41 +08:00
{
_colorArray._enabled = false;
2002-09-13 21:50:58 +08:00
_colorArray._dirty = false;
2002-07-07 22:40:41 +08:00
glDisableClientState(GL_COLOR_ARRAY);
}
}
2002-09-13 21:50:58 +08:00
inline void dirtyColorPointer()
{
_colorArray._dirty = true;
}
2002-11-13 19:09:55 +08:00
inline bool isSecondaryColorSupported() const { return _isSecondColorSupportResolved?_isSecondColorSupported:computeSecondaryColorSupported(); }
2002-08-16 04:27:33 +08:00
/** wrapper around glEnableClientState(GL_SECONDARY_COLOR_ARRAY);glSecondayColorPointer(..);
* note, only updates values that change.*/
void setSecondaryColorPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *ptr );
/** wrapper around glDisableClientState(GL_SECONDARY_COLOR_ARRAY);
* note, only updates values that change.*/
inline void disableSecondaryColorPointer()
{
2002-09-13 21:50:58 +08:00
if (_secondaryColorArray._enabled || _secondaryColorArray._dirty)
2002-08-16 04:27:33 +08:00
{
_secondaryColorArray._enabled = false;
2002-09-13 21:50:58 +08:00
_secondaryColorArray._dirty = false;
2002-11-13 19:09:55 +08:00
if (isSecondaryColorSupported()) glDisableClientState(GL_SECONDARY_COLOR_ARRAY);
2002-08-16 04:27:33 +08:00
}
}
2002-09-13 21:50:58 +08:00
inline void dirtySecondaryColorPointer()
{
_secondaryColorArray._dirty = true;
}
2002-07-07 22:40:41 +08:00
/** wrapper around glEnableClientState(GL_INDEX_ARRAY);glIndexPointer(..);
* note, only updates values that change.*/
inline void setIndexPointer( GLenum type, GLsizei stride,
const GLvoid *ptr )
{
2002-09-13 21:50:58 +08:00
if (!_indexArray._enabled || _indexArray._dirty)
2002-07-07 22:40:41 +08:00
{
_indexArray._enabled = true;
glEnableClientState(GL_INDEX_ARRAY);
}
2002-09-13 21:50:58 +08:00
if (_indexArray._pointer!=ptr || _indexArray._dirty)
2002-07-07 22:40:41 +08:00
{
_indexArray._pointer=ptr;
glIndexPointer( type, stride, ptr );
}
2002-09-13 21:50:58 +08:00
_indexArray._dirty = false;
2002-07-07 22:40:41 +08:00
}
/** wrapper around glDisableClientState(GL_INDEX_ARRAY);
* note, only updates values that change.*/
inline void disableIndexPointer()
{
2002-09-13 21:50:58 +08:00
if (_indexArray._enabled || _indexArray._dirty)
2002-07-07 22:40:41 +08:00
{
_indexArray._enabled = false;
2002-09-13 21:50:58 +08:00
_indexArray._dirty = false;
2002-07-07 22:40:41 +08:00
glDisableClientState(GL_INDEX_ARRAY);
}
}
2002-09-13 21:50:58 +08:00
inline void dirtyIndexPointer()
{
_indexArray._dirty = true;
}
2002-11-13 19:09:55 +08:00
inline bool isFogCoordSupported() const { return _isFogCoordSupportResolved?_isFogCoordSupported:computeFogCoordSupported(); }
2002-08-16 04:27:33 +08:00
/** wrapper around glEnableClientState(GL_FOG_COORDINATE_ARRAY);glFogCoordPointer(..);
* note, only updates values that change.*/
void setFogCoordPointer( GLenum type, GLsizei stride, const GLvoid *ptr );
/** wrapper around glDisableClientState(GL_FOG_COORDINATE_ARRAY);
* note, only updates values that change.*/
inline void disableFogCoordPointer()
{
2002-09-13 21:50:58 +08:00
if (_fogArray._enabled || _fogArray._dirty)
2002-08-16 04:27:33 +08:00
{
_fogArray._enabled = false;
2002-09-13 21:50:58 +08:00
_fogArray._dirty = false;
2002-11-13 19:09:55 +08:00
if (isFogCoordSupported()) glDisableClientState(GL_FOG_COORDINATE_ARRAY);
2002-08-16 04:27:33 +08:00
}
}
2002-09-13 21:50:58 +08:00
inline void dirtyFogCoordPointer()
{
_fogArray._dirty = true;
}
2002-08-16 04:27:33 +08:00
2002-07-07 22:40:41 +08:00
/** wrapper around glEnableClientState(GL_TEXTURE_COORD_ARRAY);glTexCoordPointer(..);
* note, only updates values that change.*/
inline void setTexCoordPointer( unsigned int unit,
GLint size, GLenum type,
GLsizei stride, const GLvoid *ptr )
{
if (setClientActiveTextureUnit(unit))
{
if ( unit >= _texCoordArrayList.size()) _texCoordArrayList.resize(unit+1);
EnabledArrayPair& eap = _texCoordArrayList[unit];
2002-09-13 21:50:58 +08:00
if (!eap._enabled || eap._dirty)
2002-07-07 22:40:41 +08:00
{
eap._enabled = true;
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
2002-09-13 21:50:58 +08:00
if (eap._pointer!=ptr || eap._dirty)
2002-07-07 22:40:41 +08:00
{
glTexCoordPointer( size, type, stride, ptr );
eap._pointer = ptr;
}
2002-09-13 21:50:58 +08:00
eap._dirty = false;
2002-07-07 22:40:41 +08:00
}
}
/** wrapper around glDisableClientState(GL_TEXTURE_COORD_ARRAY);
* note, only updates values that change.*/
inline void disableTexCoordPointer( unsigned int unit )
{
if (setClientActiveTextureUnit(unit))
{
2002-07-11 19:33:06 +08:00
if ( unit >= _texCoordArrayList.size()) _texCoordArrayList.resize(unit+1);
2002-07-07 22:40:41 +08:00
EnabledArrayPair& eap = _texCoordArrayList[unit];
2002-09-13 21:50:58 +08:00
if (eap._enabled || eap._dirty)
2002-07-07 22:40:41 +08:00
{
eap._enabled = false;
2002-09-13 21:50:58 +08:00
eap._dirty = false;
2002-07-07 22:40:41 +08:00
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
}
}
2002-09-13 21:50:58 +08:00
inline void dirtyTexCoordPointer( unsigned int unit )
{
if ( unit >= _texCoordArrayList.size()) return; // _texCoordArrayList.resize(unit+1);
EnabledArrayPair& eap = _texCoordArrayList[unit];
eap._dirty = true;
}
2002-07-15 18:03:59 +08:00
inline void disableTexCoordPointersAboveAndIncluding( unsigned int unit )
{
while (unit<_texCoordArrayList.size())
{
EnabledArrayPair& eap = _texCoordArrayList[unit];
2002-09-13 21:50:58 +08:00
if (eap._enabled || eap._dirty)
2002-07-15 18:03:59 +08:00
{
if (setClientActiveTextureUnit(unit))
{
eap._enabled = false;
2002-09-13 21:50:58 +08:00
eap._dirty = false;
2002-07-15 18:03:59 +08:00
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
}
++unit;
}
}
2002-07-07 22:40:41 +08:00
2002-09-13 21:50:58 +08:00
inline void dirtyTexCoordPointersAboveAndIncluding( unsigned int unit )
{
while (unit<_texCoordArrayList.size())
{
EnabledArrayPair& eap = _texCoordArrayList[unit];
eap._dirty = true;
++unit;
}
}
2002-07-07 22:40:41 +08:00
/** set the current tex coord array texture unit, return true if selected, false if selection failed such as when multitexturing is not supported.
* note, only updates values that change.*/
2002-07-12 05:08:02 +08:00
bool setClientActiveTextureUnit( unsigned int unit );
2002-07-07 22:40:41 +08:00
/** set the current texture unit, return true if selected, false if selection failed such as when multitexturing is not supported.
* note, only updates values that change.*/
2002-07-12 05:08:02 +08:00
bool setActiveTextureUnit( unsigned int unit );
2002-07-07 22:40:41 +08:00
2002-05-21 16:59:26 +08:00
2001-09-20 05:08:56 +08:00
/** Set the current OpenGL context uniqueID.
2001-10-01 19:15:55 +08:00
Note, it is the application developers responsibility to
2001-09-20 05:08:56 +08:00
set up unique ID for each OpenGL context. This value is
then used by osg::StateAttribure's and osg::Drawable's to
help manage OpenGL display list and texture binds appropriate
2002-10-10 21:06:24 +08:00
for each context, the contextID simply acts as an index local
arrays that they maintain for the purpose.
Typical settings for contextID are 0,1,2,3... up to the maximum
number of graphics contexts you have setup.
By default contextID is 0.*/
2001-09-20 05:08:56 +08:00
inline void setContextID(unsigned int contextID) { _contextID=contextID; }
/** Get the current OpenGL context unique ID.*/
2002-09-02 20:31:35 +08:00
inline unsigned int getContextID() const { return _contextID; }
2001-09-20 05:08:56 +08:00
2001-09-22 10:42:08 +08:00
/** Set the frame stamp for the current frame.*/
inline void setFrameStamp(FrameStamp* fs) { _frameStamp = fs; }
/** Set the frame stamp for the current frame.*/
inline const FrameStamp* getFrameStamp() const { return _frameStamp.get(); }
2002-03-30 01:26:40 +08:00
2001-12-22 06:48:19 +08:00
/** Set the DisplaySettings. Note, nothing is applied, the visual settings are just used
2001-12-19 08:38:23 +08:00
* used in the State object to pass the current visual settings to Drawables
* during rendering. */
2001-12-22 06:48:19 +08:00
inline void setDisplaySettings(DisplaySettings* vs) { _displaySettings = vs; }
2001-12-19 08:38:23 +08:00
2001-12-22 06:48:19 +08:00
/** Get the DisplaySettings */
inline const DisplaySettings* getDisplaySettings() const { return _displaySettings.get(); }
2001-12-19 08:38:23 +08:00
2002-08-05 20:40:24 +08:00
typedef std::pair<const StateAttribute*,StateAttribute::OverrideValue> AttributePair;
typedef std::vector<AttributePair> AttributeVec;
typedef std::vector<StateAttribute::GLModeValue> ValueVec;
2003-01-22 00:45:36 +08:00
2003-03-11 23:25:49 +08:00
/** Set flag for early termination of the draw traversal.*/
void setAbortRenderingPtr(bool* abortPtr) { _abortRenderingPtr = abortPtr; }
/** Get flag for early termination of the draw traversal,
* if true steps should be taken to complete rendering early.*/
bool getAbortRendering() const { return _abortRenderingPtr!=0?(*_abortRenderingPtr):false; }
2003-01-22 00:45:36 +08:00
void setReportGLErrors(bool flag) { _reportGLErrors = flag; }
bool getReportGLErrors() const { return _reportGLErrors; }
bool checkGLErrors(const char* str) const;
bool checkGLErrors(StateAttribute::GLMode mode) const;
bool checkGLErrors(const StateAttribute* attribute) const;
2002-03-19 05:56:00 +08:00
2003-03-11 23:25:49 +08:00
2003-01-10 17:25:42 +08:00
protected:
virtual ~State();
2002-03-19 05:56:00 +08:00
2001-12-19 08:38:23 +08:00
unsigned int _contextID;
ref_ptr<FrameStamp> _frameStamp;
2002-03-30 01:26:40 +08:00
2003-01-10 17:25:42 +08:00
ref_ptr<const RefMatrix> _identity;
ref_ptr<const RefMatrix> _projection;
ref_ptr<const RefMatrix> _modelView;
2002-03-30 01:26:40 +08:00
2001-12-22 06:48:19 +08:00
ref_ptr<DisplaySettings> _displaySettings;
2001-09-20 05:08:56 +08:00
2003-03-11 23:25:49 +08:00
bool* _abortRenderingPtr;
2003-01-22 00:45:36 +08:00
bool _reportGLErrors;
2001-09-20 05:08:56 +08:00
struct ModeStack
{
ModeStack()
{
changed = false;
last_applied_value = false;
2001-10-15 22:07:54 +08:00
global_default_value = false;
2001-09-20 05:08:56 +08:00
}
bool changed;
bool last_applied_value;
2001-10-15 22:07:54 +08:00
bool global_default_value;
2001-09-20 05:08:56 +08:00
ValueVec valueVec;
};
struct AttributeStack
{
AttributeStack()
{
changed = false;
last_applied_attribute = 0L;
2001-10-15 22:07:54 +08:00
global_default_attribute = 0L;
2001-09-20 05:08:56 +08:00
}
2001-10-15 22:07:54 +08:00
/** apply an attribute if required, passing in attribute and appropriate attribute stack */
2001-09-20 05:08:56 +08:00
bool changed;
const StateAttribute* last_applied_attribute;
2001-10-15 22:29:40 +08:00
ref_ptr<StateAttribute> global_default_attribute;
2001-09-20 05:08:56 +08:00
AttributeVec attributeVec;
};
2003-01-22 00:45:36 +08:00
2001-09-20 05:08:56 +08:00
/** apply an OpenGL mode if required, passing in mode, enable flag and appropriate mode stack */
2002-09-02 20:31:35 +08:00
inline bool applyMode(StateAttribute::GLMode mode,bool enabled,ModeStack& ms)
2001-09-20 05:08:56 +08:00
{
if (ms.last_applied_value != enabled)
{
ms.last_applied_value = enabled;
if (enabled) glEnable(mode);
else glDisable(mode);
2003-01-22 00:45:36 +08:00
if (_reportGLErrors) checkGLErrors(mode);
2001-09-20 05:08:56 +08:00
return true;
}
else
return false;
}
2003-01-22 00:45:36 +08:00
2001-09-20 05:08:56 +08:00
/** apply an attribute if required, passing in attribute and appropriate attribute stack */
2002-09-02 20:31:35 +08:00
inline bool applyAttribute(const StateAttribute* attribute,AttributeStack& as)
2001-09-20 05:08:56 +08:00
{
if (as.last_applied_attribute != attribute)
{
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
if (!as.global_default_attribute.valid()) as.global_default_attribute = dynamic_cast<StateAttribute*>(attribute->cloneType());
2001-10-15 22:07:54 +08:00
2001-09-20 05:08:56 +08:00
as.last_applied_attribute = attribute;
attribute->apply(*this);
2003-01-22 00:45:36 +08:00
if (_reportGLErrors) checkGLErrors(attribute);
2001-09-20 05:08:56 +08:00
return true;
}
else
return false;
}
2002-09-02 20:31:35 +08:00
inline bool applyGlobalDefaultAttribute(AttributeStack& as)
2001-10-15 22:07:54 +08:00
{
2001-10-15 22:29:40 +08:00
if (as.last_applied_attribute != as.global_default_attribute.get())
2001-10-15 22:07:54 +08:00
{
2001-10-15 22:29:40 +08:00
as.last_applied_attribute = as.global_default_attribute.get();
2003-01-22 00:45:36 +08:00
if (as.global_default_attribute.valid())
{
as.global_default_attribute->apply(*this);
if (_reportGLErrors) checkGLErrors(as.global_default_attribute.get());
}
2001-10-15 22:07:54 +08:00
return true;
}
else
return false;
}
2002-07-12 00:12:24 +08:00
2002-07-09 17:35:42 +08:00
2001-09-20 05:08:56 +08:00
typedef std::map<StateAttribute::GLMode,ModeStack> ModeMap;
2002-07-07 22:40:41 +08:00
typedef std::vector<ModeMap> TextureModeMapList;
2001-09-20 05:08:56 +08:00
typedef std::map<StateAttribute::Type,AttributeStack> AttributeMap;
2002-07-07 22:40:41 +08:00
typedef std::vector<AttributeMap> TextureAttributeMapList;
2003-01-30 01:16:26 +08:00
typedef std::vector<const StateSet*> StateSetStack;
2001-09-20 05:08:56 +08:00
typedef std::vector<ref_ptr<const Matrix> > MatrixStack;
2002-07-07 22:40:41 +08:00
ModeMap _modeMap;
AttributeMap _attributeMap;
2002-07-09 17:35:42 +08:00
2002-07-07 22:40:41 +08:00
TextureModeMapList _textureModeMapList;
TextureAttributeMapList _textureAttributeMapList;
2002-07-09 17:35:42 +08:00
2002-07-07 22:40:41 +08:00
StateSetStack _drawStateStack;
struct EnabledArrayPair
{
2002-09-13 21:50:58 +08:00
EnabledArrayPair():_dirty(true),_enabled(false),_pointer(0) {}
EnabledArrayPair(const EnabledArrayPair& eap):_dirty(eap._dirty), _enabled(eap._enabled),_pointer(eap._pointer) {}
EnabledArrayPair& operator = (const EnabledArrayPair& eap) { _dirty=eap._dirty; _enabled=eap._enabled; _pointer=eap._pointer; return *this; }
2002-07-07 22:40:41 +08:00
2002-09-13 21:50:58 +08:00
bool _dirty;
bool _enabled;
const GLvoid* _pointer;
2002-07-07 22:40:41 +08:00
};
typedef std::vector<EnabledArrayPair> EnabledTexCoordArrayList;
EnabledArrayPair _vertexArray;
2002-08-16 04:27:33 +08:00
EnabledArrayPair _normalArray;
2002-07-07 22:40:41 +08:00
EnabledArrayPair _colorArray;
2002-08-16 04:27:33 +08:00
EnabledArrayPair _secondaryColorArray;
2002-07-07 22:40:41 +08:00
EnabledArrayPair _indexArray;
2002-08-16 04:27:33 +08:00
EnabledArrayPair _fogArray;
2002-07-07 22:40:41 +08:00
EnabledTexCoordArrayList _texCoordArrayList;
unsigned int _currentActiveTextureUnit;
unsigned int _currentClientActiveTextureUnit;
2001-09-22 10:42:08 +08:00
2002-07-09 17:35:42 +08:00
inline ModeMap& getOrCreateTextureModeMap(unsigned int unit)
{
if (unit>=_textureModeMapList.size()) _textureModeMapList.resize(unit+1);
return _textureModeMapList[unit];
}
inline AttributeMap& getOrCreateTextureAttributeMap(unsigned int unit)
{
if (unit>=_textureAttributeMapList.size()) _textureAttributeMapList.resize(unit+1);
return _textureAttributeMapList[unit];
}
inline void pushModeList(ModeMap& modeMap,const StateSet::ModeList& modeList);
inline void pushAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList);
inline void popModeList(ModeMap& modeMap,const StateSet::ModeList& modeList);
inline void popAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList);
inline void applyModeList(ModeMap& modeMap,const StateSet::ModeList& modeList);
inline void applyAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList);
inline void applyModeMap(ModeMap& modeMap);
inline void applyAttributeMap(AttributeMap& attributeMap);
2002-09-02 20:31:35 +08:00
void haveAppliedMode(ModeMap& modeMap,StateAttribute::GLMode mode,StateAttribute::GLModeValue value);
void haveAppliedMode(ModeMap& modeMap,StateAttribute::GLMode mode);
2002-07-09 17:35:42 +08:00
void haveAppliedAttribute(AttributeMap& attributeMap,const StateAttribute* attribute);
2002-09-02 20:31:35 +08:00
void haveAppliedAttribute(AttributeMap& attributeMap,StateAttribute::Type type);
bool getLastAppliedMode(const ModeMap& modeMap,StateAttribute::GLMode mode) const;
const StateAttribute* getLastAppliedAttribute(const AttributeMap& attributeMap,StateAttribute::Type type) const;
2002-07-09 17:35:42 +08:00
2002-11-13 19:09:55 +08:00
mutable bool _isSecondColorSupportResolved;
mutable bool _isSecondColorSupported;
bool computeSecondaryColorSupported() const;
mutable bool _isFogCoordSupportResolved;
mutable bool _isFogCoordSupported;
bool computeFogCoordSupported() const;
2001-01-11 00:32:10 +08:00
};
2002-07-09 17:35:42 +08:00
inline void State::pushModeList(ModeMap& modeMap,const StateSet::ModeList& modeList)
{
for(StateSet::ModeList::const_iterator mitr=modeList.begin();
mitr!=modeList.end();
++mitr)
{
// get the mode stack for incomming GLmode {mitr->first}.
ModeStack& ms = modeMap[mitr->first];
if (ms.valueVec.empty())
{
// first pair so simply push incomming pair to back.
ms.valueVec.push_back(mitr->second);
}
2002-08-05 20:40:24 +08:00
else if ((ms.valueVec.back() & StateAttribute::OVERRIDE) && !(mitr->second & StateAttribute::PROTECTED)) // check the existing override flag
2002-07-09 17:35:42 +08:00
{
// push existing back since override keeps the previoius value.
ms.valueVec.push_back(ms.valueVec.back());
}
else
{
// no override on so simply push incomming pair to back.
ms.valueVec.push_back(mitr->second);
}
ms.changed = true;
}
}
inline void State::pushAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList)
{
for(StateSet::AttributeList::const_iterator aitr=attributeList.begin();
aitr!=attributeList.end();
++aitr)
{
// get the attribute stack for incomming type {aitr->first}.
AttributeStack& as = attributeMap[aitr->first];
if (as.attributeVec.empty())
{
// first pair so simply push incomming pair to back.
as.attributeVec.push_back(
AttributePair(aitr->second.first.get(),aitr->second.second));
}
2002-08-05 20:40:24 +08:00
else if ((as.attributeVec.back().second & StateAttribute::OVERRIDE) && !(aitr->second.second & StateAttribute::PROTECTED)) // check the existing override flag
2002-07-09 17:35:42 +08:00
{
// push existing back since override keeps the previoius value.
as.attributeVec.push_back(as.attributeVec.back());
}
else
{
// no override on so simply push incomming pair to back.
as.attributeVec.push_back(
AttributePair(aitr->second.first.get(),aitr->second.second));
}
as.changed = true;
}
}
inline void State::popModeList(ModeMap& modeMap,const StateSet::ModeList& modeList)
{
for(StateSet::ModeList::const_iterator mitr=modeList.begin();
mitr!=modeList.end();
++mitr)
{
// get the mode stack for incomming GLmode {mitr->first}.
ModeStack& ms = modeMap[mitr->first];
if (!ms.valueVec.empty())
{
ms.valueVec.pop_back();
}
ms.changed = true;
}
}
inline void State::popAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList)
{
for(StateSet::AttributeList::const_iterator aitr=attributeList.begin();
aitr!=attributeList.end();
++aitr)
{
// get the attribute stack for incomming type {aitr->first}.
AttributeStack& as = attributeMap[aitr->first];
if (!as.attributeVec.empty())
{
as.attributeVec.pop_back();
}
as.changed = true;
}
}
inline void State::applyModeList(ModeMap& modeMap,const StateSet::ModeList& modeList)
{
StateSet::ModeList::const_iterator ds_mitr = modeList.begin();
ModeMap::iterator this_mitr=modeMap.begin();
while (this_mitr!=modeMap.end() && ds_mitr!=modeList.end())
{
if (this_mitr->first<ds_mitr->first)
{
// note GLMode = this_mitr->first
ModeStack& ms = this_mitr->second;
if (ms.changed)
{
ms.changed = false;
if (!ms.valueVec.empty())
{
bool new_value = ms.valueVec.back() & StateAttribute::ON;
applyMode(this_mitr->first,new_value,ms);
}
else
{
// assume default of disabled.
applyMode(this_mitr->first,ms.global_default_value,ms);
}
}
++this_mitr;
}
else if (ds_mitr->first<this_mitr->first)
{
// ds_mitr->first is a new mode, therefore
// need to insert a new mode entry for ds_mistr->first.
ModeStack& ms = modeMap[ds_mitr->first];
bool new_value = ds_mitr->second & StateAttribute::ON;
applyMode(ds_mitr->first,new_value,ms);
// will need to disable this mode on next apply so set it to changed.
ms.changed = true;
++ds_mitr;
}
else
{
// this_mitr & ds_mitr refer to the same mode, check the overide
// if any otherwise just apply the incomming mode.
ModeStack& ms = this_mitr->second;
2002-08-05 20:40:24 +08:00
if (!ms.valueVec.empty() && (ms.valueVec.back() & StateAttribute::OVERRIDE) && !(ds_mitr->second & StateAttribute::PROTECTED))
2002-07-09 17:35:42 +08:00
{
// override is on, there just treat as a normal apply on modes.
if (ms.changed)
{
ms.changed = false;
bool new_value = ms.valueVec.back() & StateAttribute::ON;
applyMode(this_mitr->first,new_value,ms);
}
}
else
{
// no override on or no previous entry, therefore consider incomming mode.
bool new_value = ds_mitr->second & StateAttribute::ON;
if (applyMode(ds_mitr->first,new_value,ms))
{
ms.changed = true;
}
}
++this_mitr;
++ds_mitr;
}
}
// iterator over the remaining state modes to apply any previous changes.
for(;
this_mitr!=modeMap.end();
++this_mitr)
{
// note GLMode = this_mitr->first
ModeStack& ms = this_mitr->second;
if (ms.changed)
{
ms.changed = false;
if (!ms.valueVec.empty())
{
bool new_value = ms.valueVec.back() & StateAttribute::ON;
applyMode(this_mitr->first,new_value,ms);
}
else
{
// assume default of disabled.
applyMode(this_mitr->first,ms.global_default_value,ms);
}
}
}
// iterator over the remaining incomming modes to apply any new mode.
for(;
ds_mitr!=modeList.end();
++ds_mitr)
{
ModeStack& ms = modeMap[ds_mitr->first];
bool new_value = ds_mitr->second & StateAttribute::ON;
applyMode(ds_mitr->first,new_value,ms);
// will need to disable this mode on next apply so set it to changed.
ms.changed = true;
}
}
inline void State::applyAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList)
{
StateSet::AttributeList::const_iterator ds_aitr=attributeList.begin();
AttributeMap::iterator this_aitr=attributeMap.begin();
while (this_aitr!=attributeMap.end() && ds_aitr!=attributeList.end())
{
if (this_aitr->first<ds_aitr->first)
{
// note attribute type = this_aitr->first
AttributeStack& as = this_aitr->second;
if (as.changed)
{
as.changed = false;
if (!as.attributeVec.empty())
{
const StateAttribute* new_attr = as.attributeVec.back().first;
applyAttribute(new_attr,as);
}
else
{
applyGlobalDefaultAttribute(as);
}
}
++this_aitr;
}
else if (ds_aitr->first<this_aitr->first)
{
// ds_mitr->first is a new attribute, therefore
// need to insert a new attribute entry for ds_aistr->first.
AttributeStack& as = attributeMap[ds_aitr->first];
const StateAttribute* new_attr = ds_aitr->second.first.get();
applyAttribute(new_attr,as);
// will need to disable this mode on next apply so set it to changed.
as.changed = true;
++ds_aitr;
}
else
{
2002-08-05 20:40:24 +08:00
// this_mitr & ds_mitr refer to the same attribute, check the overide
// if any otherwise just apply the incomming attribute
2002-07-09 17:35:42 +08:00
AttributeStack& as = this_aitr->second;
2002-08-05 20:40:24 +08:00
if (!as.attributeVec.empty() && (as.attributeVec.back().second & StateAttribute::OVERRIDE) && !(ds_aitr->second.second & StateAttribute::PROTECTED))
2002-07-09 17:35:42 +08:00
{
2002-08-05 20:40:24 +08:00
// override is on, there just treat as a normal apply on modes.
2002-07-09 17:35:42 +08:00
if (as.changed)
{
as.changed = false;
const StateAttribute* new_attr = as.attributeVec.back().first;
applyAttribute(new_attr,as);
}
}
else
{
// no override on or no previous entry, therefore consider incomming mode.
const StateAttribute* new_attr = ds_aitr->second.first.get();
if (applyAttribute(new_attr,as))
{
as.changed = true;
}
}
++this_aitr;
++ds_aitr;
}
}
// iterator over the remaining state modes to apply any previous changes.
for(;
this_aitr!=attributeMap.end();
++this_aitr)
{
// note attribute type = this_aitr->first
AttributeStack& as = this_aitr->second;
if (as.changed)
{
as.changed = false;
if (!as.attributeVec.empty())
{
const StateAttribute* new_attr = as.attributeVec.back().first;
applyAttribute(new_attr,as);
}
else
{
applyGlobalDefaultAttribute(as);
}
}
}
// iterator over the remaining incomming modes to apply any new mode.
for(;
ds_aitr!=attributeList.end();
++ds_aitr)
{
// ds_mitr->first is a new attribute, therefore
// need to insert a new attribute entry for ds_aistr->first.
AttributeStack& as = attributeMap[ds_aitr->first];
const StateAttribute* new_attr = ds_aitr->second.first.get();
applyAttribute(new_attr,as);
// will need to update this attribute on next apply so set it to changed.
as.changed = true;
}
}
inline void State::applyModeMap(ModeMap& modeMap)
{
for(ModeMap::iterator mitr=modeMap.begin();
mitr!=modeMap.end();
++mitr)
{
// note GLMode = mitr->first
ModeStack& ms = mitr->second;
if (ms.changed)
{
ms.changed = false;
if (!ms.valueVec.empty())
{
bool new_value = ms.valueVec.back() & StateAttribute::ON;
applyMode(mitr->first,new_value,ms);
}
else
{
// assume default of disabled.
applyMode(mitr->first,ms.global_default_value,ms);
}
}
}
}
inline void State::applyAttributeMap(AttributeMap& attributeMap)
{
for(AttributeMap::iterator aitr=attributeMap.begin();
aitr!=attributeMap.end();
++aitr)
{
AttributeStack& as = aitr->second;
if (as.changed)
{
as.changed = false;
if (!as.attributeVec.empty())
{
const StateAttribute* new_attr = as.attributeVec.back().first;
applyAttribute(new_attr,as);
}
else
{
applyGlobalDefaultAttribute(as);
}
}
}
}
2002-02-03 20:33:41 +08:00
}
2001-01-11 00:32:10 +08:00
#endif