2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* 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
|
2003-01-22 00:45:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2003-01-22 00:45:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-09-20 05:19:47 +08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( disable : 4786 )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <float.h>
|
|
|
|
|
2002-06-26 04:27:51 +08:00
|
|
|
#include <osg/Geometry>
|
2002-08-25 03:39:39 +08:00
|
|
|
#include <osg/Texture2D>
|
2001-09-20 05:19:47 +08:00
|
|
|
#include <osg/TexEnv>
|
|
|
|
#include <osg/AlphaFunc>
|
|
|
|
#include <osg/Notify>
|
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
#include <osgSim/ImpostorSprite>
|
|
|
|
|
2005-05-13 19:11:52 +08:00
|
|
|
using namespace osg;
|
2005-05-02 03:48:49 +08:00
|
|
|
using namespace osgSim;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2011-05-09 18:54:50 +08:00
|
|
|
ImpostorSprite::ImpostorSprite():
|
|
|
|
_parent(0),
|
|
|
|
_ism(0),
|
|
|
|
_previous(0),
|
|
|
|
_next(0),
|
|
|
|
_lastFrameUsed(osg::UNINITIALIZED_FRAME_NUMBER),
|
|
|
|
_texture(0),
|
|
|
|
_s(0),
|
|
|
|
_t(0)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
// don't use display list since we will be updating the geometry.
|
2011-05-09 18:54:50 +08:00
|
|
|
setUseDisplayList(false);
|
2003-01-24 17:11:05 +08:00
|
|
|
_color.set(1.0f, 1.0f, 1.0f, 1.0f );
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
2011-05-09 18:54:50 +08:00
|
|
|
ImpostorSprite::ImpostorSprite(const ImpostorSprite&):
|
|
|
|
osg::Drawable(),
|
|
|
|
_parent(0),
|
|
|
|
_ism(0),
|
|
|
|
_previous(0),
|
|
|
|
_next(0),
|
|
|
|
_lastFrameUsed(osg::UNINITIALIZED_FRAME_NUMBER),
|
|
|
|
_texture(0),
|
|
|
|
_s(0),
|
|
|
|
_t(0)
|
|
|
|
{
|
|
|
|
setUseDisplayList(false);
|
|
|
|
_color.set(1.0f, 1.0f, 1.0f, 1.0f );
|
|
|
|
}
|
2005-05-13 19:11:52 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
ImpostorSprite::~ImpostorSprite()
|
|
|
|
{
|
|
|
|
if (_ism)
|
|
|
|
{
|
|
|
|
_ism->remove(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
float ImpostorSprite::calcPixelError(const osg::Matrix& MVPW) const
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
// find the maximum screen space pixel error between the control coords and the quad coners.
|
|
|
|
float max_error_sqrd = 0.0f;
|
2002-04-10 00:09:19 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
for(int i=0;i<4;++i)
|
|
|
|
{
|
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
osg::Vec3 projected_coord = _coords[i]*MVPW;
|
|
|
|
osg::Vec3 projected_control = _controlcoords[i]*MVPW;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
float dx = (projected_coord.x()-projected_control.x());
|
|
|
|
float dy = (projected_coord.y()-projected_control.y());
|
|
|
|
|
|
|
|
float error_sqrd = dx*dx+dy*dy;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
if (error_sqrd > max_error_sqrd) max_error_sqrd = error_sqrd;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return sqrtf(max_error_sqrd);
|
|
|
|
}
|
2009-10-22 00:16:31 +08:00
|
|
|
void ImpostorSprite::drawImplementation(osg::RenderInfo& renderInfo) const
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
2009-10-22 00:16:31 +08:00
|
|
|
osg::GLBeginEndAdapter& gl = (renderInfo.getState()->getGLBeginEndAdapter());
|
|
|
|
|
|
|
|
// when the tex env is set to REPLACE, and the
|
2001-09-20 05:19:47 +08:00
|
|
|
// texture is set up correctly the color has no effect.
|
2009-10-22 00:16:31 +08:00
|
|
|
gl.Color4fv( _color.ptr() );
|
|
|
|
|
|
|
|
gl.Begin( GL_QUADS );
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2009-10-22 00:16:31 +08:00
|
|
|
gl.TexCoord2fv( (GLfloat *)&_texcoords[0] );
|
|
|
|
gl.Vertex3fv( (GLfloat *)&_coords[0] );
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2009-10-22 00:16:31 +08:00
|
|
|
gl.TexCoord2fv( (GLfloat *)&_texcoords[1] );
|
|
|
|
gl.Vertex3fv( (GLfloat *)&_coords[1] );
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2009-10-22 00:16:31 +08:00
|
|
|
gl.TexCoord2fv( (GLfloat *)&_texcoords[2] );
|
|
|
|
gl.Vertex3fv( (GLfloat *)&_coords[2] );
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2009-10-22 00:16:31 +08:00
|
|
|
gl.TexCoord2fv( (GLfloat *)&_texcoords[3] );
|
|
|
|
gl.Vertex3fv( (GLfloat *)&_coords[3] );
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2009-10-22 00:16:31 +08:00
|
|
|
gl.End();
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
osg::BoundingBox ImpostorSprite::computeBound() const
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
2005-05-12 22:03:22 +08:00
|
|
|
osg::BoundingBox bbox;
|
|
|
|
bbox.expandBy(_coords[0]);
|
|
|
|
bbox.expandBy(_coords[1]);
|
|
|
|
bbox.expandBy(_coords[2]);
|
|
|
|
bbox.expandBy(_coords[3]);
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
if (!bbox.valid())
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
2010-05-29 00:09:29 +08:00
|
|
|
OSG_WARN << "******* ImpostorSprite::computeBound() problem"<<std::endl;
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
return bbox;
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
void ImpostorSprite::setTexture(osg::Texture2D* tex,int s,int t)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
_texture = tex;
|
|
|
|
_s = s;
|
|
|
|
_t = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 23:36:14 +08:00
|
|
|
void ImpostorSprite::accept(AttributeFunctor& af)
|
|
|
|
{
|
|
|
|
af.apply(VERTICES,4,_coords);
|
|
|
|
af.apply(TEXTURE_COORDS_0,4,_texcoords);
|
|
|
|
}
|
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
void ImpostorSprite::accept(ConstAttributeFunctor& af) const
|
|
|
|
{
|
|
|
|
af.apply(VERTICES,4,_coords);
|
|
|
|
af.apply(TEXTURE_COORDS_0,4,_texcoords);
|
|
|
|
}
|
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
void ImpostorSprite::accept(osg::PrimitiveFunctor& functor) const
|
2002-07-18 23:36:14 +08:00
|
|
|
{
|
|
|
|
functor.setVertexArray(4,_coords);
|
|
|
|
functor.drawArrays( GL_QUADS, 0, 4);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-07-18 23:36:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helper class for managing the reuse of ImpostorSprite resources.
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2002-07-21 09:29:11 +08:00
|
|
|
ImpostorSpriteManager::ImpostorSpriteManager():
|
|
|
|
_first(NULL),
|
|
|
|
_last(NULL)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
2005-05-02 03:48:49 +08:00
|
|
|
_texenv = new osg::TexEnv;
|
|
|
|
_texenv->setMode(osg::TexEnv::REPLACE);
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
_alphafunc = new osg::AlphaFunc;
|
2005-05-02 03:48:49 +08:00
|
|
|
_alphafunc->setFunction( osg::AlphaFunc::GREATER, 0.000f );
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-04-10 05:53:09 +08:00
|
|
|
_reuseStateSetIndex = 0;
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImpostorSpriteManager::~ImpostorSpriteManager()
|
|
|
|
{
|
|
|
|
while (_first)
|
|
|
|
{
|
|
|
|
ImpostorSprite* next = _first->_next;
|
|
|
|
_first->_ism = NULL;
|
|
|
|
_first->_previous = NULL;
|
|
|
|
_first->_next = NULL;
|
|
|
|
_first = next;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImpostorSpriteManager::push_back(ImpostorSprite* is)
|
|
|
|
{
|
|
|
|
if (is==NULL || is==_last) return;
|
|
|
|
|
|
|
|
// remove entry for exisiting position in linked list
|
|
|
|
// if it is already inserted.
|
|
|
|
if (is->_previous)
|
|
|
|
{
|
|
|
|
(is->_previous)->_next = is->_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is->_next)
|
|
|
|
{
|
|
|
|
(is->_next)->_previous = is->_previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_first==is) _first = is->_next;
|
|
|
|
|
|
|
|
if (empty())
|
|
|
|
{
|
|
|
|
_first = is;
|
|
|
|
_last = is;
|
|
|
|
is->_ism = this;
|
|
|
|
is->_previous = NULL;
|
|
|
|
is->_next = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
// now add the element into the list.
|
|
|
|
ImpostorSprite* previous_last = _last;
|
|
|
|
previous_last->_next = is;
|
|
|
|
_last = is;
|
|
|
|
_last->_ism = this;
|
|
|
|
_last->_previous = previous_last;
|
|
|
|
_last->_next = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImpostorSpriteManager::remove(ImpostorSprite* is)
|
|
|
|
{
|
|
|
|
if (is==NULL) return;
|
|
|
|
|
|
|
|
// remove entry for exisiting position in linked list
|
|
|
|
// if it is already inserted.
|
|
|
|
if (is->_previous)
|
|
|
|
{
|
|
|
|
(is->_previous)->_next = is->_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is->_next)
|
|
|
|
{
|
|
|
|
(is->_next)->_previous = is->_previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_first==is) _first = is->_next;
|
|
|
|
if (_last==is) _last = is->_previous;
|
|
|
|
}
|
|
|
|
|
2010-12-23 04:11:05 +08:00
|
|
|
ImpostorSprite* ImpostorSpriteManager::createOrReuseImpostorSprite(int s,int t,unsigned int frameNumber)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
if (!empty())
|
|
|
|
{
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
// search for a valid impostor to reuse.
|
2001-09-20 05:19:47 +08:00
|
|
|
ImpostorSprite* curr = _first;
|
|
|
|
while (curr)
|
|
|
|
{
|
|
|
|
if (curr->getLastFrameUsed()<=frameNumber &&
|
|
|
|
curr->s()==s &&
|
|
|
|
curr->t()==t)
|
|
|
|
{
|
|
|
|
push_back(curr);
|
|
|
|
return curr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curr = curr->_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// creating new impostor sprite.
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
osg::StateSet* stateset = new osg::StateSet;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
stateset->setMode(GL_CULL_FACE,osg::StateAttribute::OFF);
|
|
|
|
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
|
|
|
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
|
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
stateset->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
stateset->setAttributeAndModes( _alphafunc.get(), osg::StateAttribute::ON );
|
2002-07-10 03:23:53 +08:00
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
osg::Texture2D* texture = new osg::Texture2D;
|
|
|
|
texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
|
|
|
|
texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
|
2002-07-10 03:23:53 +08:00
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
|
2002-07-10 03:23:53 +08:00
|
|
|
stateset->setTextureAttribute(0,_texenv.get());
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
/*
|
2002-12-16 21:40:58 +08:00
|
|
|
TexEnv* texenv = new TexEnv;
|
2001-09-20 05:19:47 +08:00
|
|
|
texenv->setMode(TexEnv::REPLACE);
|
|
|
|
stateset->setAttribute(texenv);
|
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
AlphaFunc* alphafunc = new osg::AlphaFunc;
|
2005-05-02 03:48:49 +08:00
|
|
|
alphafunc->setFunction( osg::AlphaFunc::GREATER, 0.000f );
|
|
|
|
stateset->setAttributeAndModes( alphafunc, osg::StateAttribute::ON );
|
2001-09-22 10:42:08 +08:00
|
|
|
*/
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
// stateset->setMode( GL_ALPHA_TEST, osg::StateAttribute::OFF );
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
ImpostorSprite* is = new ImpostorSprite;
|
2001-09-20 05:19:47 +08:00
|
|
|
is->setStateSet(stateset);
|
|
|
|
is->setTexture(texture,s,t);
|
|
|
|
|
|
|
|
push_back(is);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
return is;
|
|
|
|
|
|
|
|
}
|
2002-07-18 23:36:14 +08:00
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
osg::StateSet* ImpostorSpriteManager::createOrReuseStateSet()
|
2003-04-10 05:53:09 +08:00
|
|
|
{
|
|
|
|
if (_reuseStateSetIndex<_stateSetList.size())
|
|
|
|
{
|
|
|
|
return _stateSetList[_reuseStateSetIndex++].get();
|
|
|
|
}
|
2005-05-02 03:48:49 +08:00
|
|
|
_stateSetList.push_back(new osg::StateSet);
|
2003-04-10 05:53:09 +08:00
|
|
|
_reuseStateSetIndex=_stateSetList.size();
|
|
|
|
return _stateSetList.back().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImpostorSpriteManager::reset()
|
|
|
|
{
|
|
|
|
_reuseStateSetIndex = 0;
|
|
|
|
}
|