2001-10-04 23:12:57 +08:00
|
|
|
//C++ header - Open Scene Graph - Copyright (C) 1998-2001 Robert Osfield
|
|
|
|
//Distributed under the terms of the GNU Library General Public License (LGPL)
|
|
|
|
//as published by the Free Software Foundation.
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
#ifndef OSG_ImpostorSprite
|
|
|
|
#define OSG_ImpostorSprite 1
|
|
|
|
|
|
|
|
#include <osg/Vec2>
|
|
|
|
#include <osg/BoundingSphere>
|
|
|
|
#include <osg/Drawable>
|
|
|
|
#include <osg/Camera>
|
|
|
|
#include <osg/ImpostorSprite>
|
2001-09-22 10:42:08 +08:00
|
|
|
#include <osg/AlphaFunc>
|
|
|
|
#include <osg/TexEnv>
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
|
|
|
class Texture;
|
|
|
|
class Impostor;
|
|
|
|
class ImpostorSpriteManager;
|
|
|
|
|
|
|
|
/** An ImposterSprite is a textured quad which is rendered in place a
|
|
|
|
* 3D geometry. The ImposterSprite is generated by rendering the original
|
|
|
|
* 3D geometry to a texture as an image cache. The ImpostorSprite is
|
2001-10-01 19:15:55 +08:00
|
|
|
* automatically generated by the osgUtil::CullVisitor so it not
|
2001-09-20 05:19:47 +08:00
|
|
|
* necessary to deal with it directly.
|
|
|
|
*/
|
|
|
|
class SG_EXPORT ImpostorSprite : public Drawable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ImpostorSprite();
|
|
|
|
virtual Object* clone() const { return new ImpostorSprite(); }
|
|
|
|
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const ImpostorSprite*>(obj)!=NULL; }
|
|
|
|
virtual const char* className() const { return "ImpostorSprite"; }
|
|
|
|
|
|
|
|
/** Set the parent, which must be an Impostor.
|
|
|
|
* Unlike conventional Drawables, ImpostorSprite's can only ever have
|
|
|
|
* one parent.
|
|
|
|
*/
|
|
|
|
void setParent(Impostor* parent) { _parent = parent; }
|
|
|
|
|
|
|
|
/** Get the parent, which is an Impostor. */
|
|
|
|
Impostor* getParent() { return _parent; }
|
|
|
|
|
|
|
|
/** Get the const parent, which is an Impostor. */
|
|
|
|
const Impostor* getParent() const { return _parent; }
|
|
|
|
|
|
|
|
/** Set the eye point for when the ImpsotorSprite was snapped.*/
|
|
|
|
inline void setStoredLocalEyePoint(const Vec3& v) { _storedLocalEyePoint=v; }
|
|
|
|
|
|
|
|
/** Get the eye point for when the ImpsotorSprite was snapped.*/
|
|
|
|
inline const Vec3& getStoredLocalEyePoint() const { return _storedLocalEyePoint; }
|
|
|
|
|
|
|
|
/** Set the frame number for when the ImpostorSprite was last used in rendering.*/
|
|
|
|
inline void setLastFrameUsed(const int frameNumber) { _lastFrameUsed = frameNumber; }
|
|
|
|
|
|
|
|
/** Get the frame number for when the ImpostorSprite was last used in rendering.*/
|
|
|
|
inline int getLastFrameUsed() const { return _lastFrameUsed; }
|
|
|
|
|
|
|
|
|
|
|
|
/** Get the coordinates of the corners of the quad.
|
|
|
|
* Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
|
|
|
|
*/
|
|
|
|
inline Vec3* getCoords() { return _coords; }
|
|
|
|
|
|
|
|
/** Get the const coordinates of the corners of the quad.
|
|
|
|
*/
|
|
|
|
inline const Vec3* getCoords() const { return _coords; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Get the texture coordinates of the corners of the quad.
|
|
|
|
* Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
|
|
|
|
*/
|
|
|
|
inline Vec2* getTexCoords() { return _texcoords; }
|
|
|
|
|
|
|
|
/** Get the const texture coordinates of the corners of the quad.*/
|
|
|
|
inline const Vec2* getTexCoords() const { return _texcoords; }
|
|
|
|
|
|
|
|
/** Get the control coordinates of the corners of the quad.
|
2001-10-01 19:15:55 +08:00
|
|
|
* The control coordinates are the corners of the quad projected
|
2001-09-20 05:19:47 +08:00
|
|
|
* out onto the front face of bounding box which enclosed the impostor
|
|
|
|
* geometry when it was pre-rendered into the impostor sprite's texture.
|
|
|
|
* At the point of creation/or update of the impostor sprite the control
|
2001-10-01 19:15:55 +08:00
|
|
|
* coords will lie on top of the corners of the quad in screen space - with a pixel error
|
2001-09-20 05:19:47 +08:00
|
|
|
* or zero. Once the camera moves relative to the impostor sprite the
|
2001-10-01 19:15:55 +08:00
|
|
|
* control coords will no longer lie on top of the corners of the quad in
|
2001-09-20 05:19:47 +08:00
|
|
|
* screen space - a pixel error will have accumulated. This pixel error
|
2001-10-01 19:15:55 +08:00
|
|
|
* can then be used to determine whether the impostor needs to be updated.
|
2001-09-20 05:19:47 +08:00
|
|
|
* Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
|
|
|
|
*/
|
|
|
|
inline Vec3* getControlCoords() { return _controlcoords; }
|
|
|
|
|
|
|
|
/** Get the const control coordinates of the corners of the quad.*/
|
|
|
|
inline const Vec3* getControlCoords() const { return _controlcoords; }
|
|
|
|
|
|
|
|
|
|
|
|
/** calculate the pixel error value for current camera position and object position.*/
|
2001-09-22 10:42:08 +08:00
|
|
|
const float calcPixelError(const Camera& camera,const Viewport& viewport,const osg::Matrix* matrix) const;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
void setTexture(Texture* tex,int s,int t);
|
|
|
|
|
|
|
|
Texture* getTexture() { return _texture; }
|
|
|
|
const Texture* getTexture() const { return _texture; }
|
|
|
|
const int s() const { return _s; }
|
|
|
|
const int t() const { return _t; }
|
|
|
|
|
|
|
|
|
|
|
|
/** draw ImpostorSprite directly. */
|
|
|
|
virtual void drawImmediateMode(State& state);
|
|
|
|
|
2001-10-23 06:02:47 +08:00
|
|
|
bool getStats(Statistics &stat);
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
ImpostorSprite(const ImpostorSprite&):Drawable() {}
|
|
|
|
ImpostorSprite& operator = (const ImpostorSprite&) { return *this;}
|
|
|
|
|
|
|
|
virtual ~ImpostorSprite();
|
|
|
|
|
|
|
|
virtual const bool computeBound() const;
|
|
|
|
|
|
|
|
Impostor* _parent;
|
|
|
|
|
2002-01-19 06:34:07 +08:00
|
|
|
friend class osg::ImpostorSpriteManager;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
// support for a double linked list managed by the
|
|
|
|
// ImposotorSpriteManager.
|
|
|
|
ImpostorSpriteManager* _ism;
|
|
|
|
ImpostorSprite* _previous;
|
|
|
|
ImpostorSprite* _next;
|
|
|
|
|
|
|
|
int _lastFrameUsed;
|
|
|
|
|
|
|
|
Vec3 _storedLocalEyePoint;
|
|
|
|
|
|
|
|
Vec3 _coords[4];
|
|
|
|
Vec2 _texcoords[4];
|
|
|
|
Vec3 _controlcoords[4];
|
|
|
|
|
|
|
|
Texture* _texture;
|
|
|
|
int _s;
|
|
|
|
int _t;
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper class for managing the reuse of ImpostorSprite resources.*/
|
|
|
|
class SG_EXPORT ImpostorSpriteManager : public Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ImpostorSpriteManager();
|
|
|
|
|
|
|
|
const bool empty() const { return _first==0; }
|
|
|
|
|
|
|
|
ImpostorSprite* first() { return _first; }
|
|
|
|
|
|
|
|
ImpostorSprite* last() { return _last; }
|
|
|
|
|
|
|
|
void push_back(ImpostorSprite* is);
|
|
|
|
|
|
|
|
void remove(ImpostorSprite* is);
|
|
|
|
|
|
|
|
ImpostorSprite* createOrReuseImpostorSprite(int s,int t,int frameNumber);
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
|
|
~ImpostorSpriteManager();
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
ref_ptr<TexEnv> _texenv;
|
|
|
|
ref_ptr<AlphaFunc> _alphafunc;
|
|
|
|
|
|
|
|
ImpostorSprite* _first;
|
|
|
|
ImpostorSprite* _last;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|