openscenegraph
ImpostorSprite
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSG_ImpostorSprite
15#define OSG_ImpostorSprite 1
16
17#include <osg/Vec2>
18#include <osg/Geometry>
19#include <osg/AlphaFunc>
20#include <osg/TexEnv>
21#include <osg/Texture2D>
22#include <osg/Camera>
23
24#include <osgSim/Export>
25
26namespace osgSim {
27
28class Impostor;
29class ImpostorSpriteManager;
30
31/** An ImposterSprite is a textured quad which is rendered in place of
32 * 3D geometry. The ImposterSprite is generated by rendering the original
33 * 3D geometry to a texture as an image cache. The ImpostorSprite is
34 * automatically generated by the osgUtil::CullVisitor so it not
35 * necessary to deal with it directly.
36*/
37class OSGSIM_EXPORT ImpostorSprite : public osg::Geometry
38{
39 public:
40
41 ImpostorSprite();
42
43 /** Clone an object of the same type as an ImpostorSprite. */
44 virtual osg::Object* cloneType() const { return new ImpostorSprite(); }
45
46 /** Clone on ImpostorSprite just returns a clone of type,
47 * since it is not appropriate to share data of an ImpostorSprite.
48 */
49 virtual osg::Object* clone(const osg::CopyOp&) const { return new ImpostorSprite(); }
50 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const ImpostorSprite*>(obj)!=NULL; }
51 virtual const char* libraryName() const { return "osgSim"; }
52 virtual const char* className() const { return "ImpostorSprite"; }
53
54 /** Set the parent, which must be an Impostor.
55 * Unlike conventional Drawables, ImpostorSprites can only ever have
56 * one parent.
57 */
58 void setParent(Impostor* parent) { _parent = parent; }
59
60 /** Get the parent, which is an Impostor. */
61 Impostor* getParent() { return _parent; }
62
63 /** Get the const parent, which is an Impostor. */
64 const Impostor* getParent() const { return _parent; }
65
66 /** Set the eye point for when the ImpostorSprite was snapped. */
67 inline void setStoredLocalEyePoint(const osg::Vec3& v) { _storedLocalEyePoint=v; }
68
69 /** Get the eye point for when the ImpostorSprite was snapped. */
70 inline const osg::Vec3& getStoredLocalEyePoint() const { return _storedLocalEyePoint; }
71
72 /** Set the frame number for when the ImpostorSprite was last used in rendering. */
73 inline void setLastFrameUsed(unsigned int frameNumber) { _lastFrameUsed = frameNumber; }
74
75 /** Get the frame number for when the ImpostorSprite was last used in rendering. */
76 inline unsigned int getLastFrameUsed() const { return _lastFrameUsed; }
77
78
79 void dirty();
80
81
82 /** Get the coordinates of the corners of the quad.
83 * Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
84 */
85 inline osg::Vec3* getCoords() { return &(_coords->front()); }
86
87 /** Get the const coordinates of the corners of the quad. */
88 inline const osg::Vec3* getCoords() const { return &(_coords->front()); }
89
90
91
92 /** Get the texture coordinates of the corners of the quad.
93 * Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
94 */
95 inline osg::Vec2* getTexCoords() { return &(_texcoords->front()); }
96
97 /** Get the const texture coordinates of the corners of the quad. */
98 inline const osg::Vec2* getTexCoords() const { return &(_texcoords->front()); }
99
100 /** Get the control coordinates of the corners of the quad.
101 * The control coordinates are the corners of the quad projected
102 * out onto the front face of bounding box which enclosed the impostor
103 * geometry when it was pre-rendered into the impostor sprite's texture.
104 * At the point of creation/or update of the impostor sprite the control
105 * coords will lie on top of the corners of the quad in screen space - with a pixel error
106 * of zero. Once the camera moves relative to the impostor sprite the
107 * control coords will no longer lie on top of the corners of the quad in
108 * screen space - a pixel error will have accumulated. This pixel error
109 * can then be used to determine whether the impostor needs to be updated.
110 * Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
111 */
112 inline osg::Vec3* getControlCoords() { return _controlcoords; }
113
114 /** Get the const control coordinates of the corners of the quad. */
115 inline const osg::Vec3* getControlCoords() const { return _controlcoords; }
116
117
118 /** Calculate the pixel error value for passing in the ModelViewProjectionWindow transform,
119 * which transform local coords into screen space.
120 */
121 float calcPixelError(const osg::Matrix& MVPW) const;
122
123 void setTexture(osg::Texture2D* tex,int s,int t);
124 osg::Texture2D* getTexture() { return _texture; }
125 const osg::Texture2D* getTexture() const { return _texture; }
126
127 int s() const { return _s; }
128 int t() const { return _t; }
129
130 /** Set the camera node to use for pre rendering the impostor sprite's texture.*/
131 void setCamera(osg::Camera* camera) { _camera = camera; }
132
133 /** Get the camera node to use for pre rendering the impostor sprite's texture.*/
134 osg::Camera* getCamera() { return _camera.get(); }
135
136 /** Get the const camera node to use for pre rendering the impostor sprite's texture.*/
137 const osg::Camera* getCamera() const { return _camera.get(); }
138
139 protected:
140
141 ImpostorSprite(const ImpostorSprite&);
142 ImpostorSprite& operator = (const ImpostorSprite&) { return *this;}
143
144 virtual ~ImpostorSprite();
145
146 void init();
147
148 Impostor* _parent;
149
150 friend class osgSim::ImpostorSpriteManager;
151
152 // camera node for doing the pre rendering.
153 osg::ref_ptr<osg::Camera> _camera;
154
155 // support for a double linked list managed by the
156 // ImposotorSpriteManager.
157 ImpostorSpriteManager* _ism;
158 ImpostorSprite* _previous;
159 ImpostorSprite* _next;
160
161 unsigned int _lastFrameUsed;
162
163 osg::Vec3 _storedLocalEyePoint;
164
165 osg::ref_ptr<osg::Vec3Array> _coords;
166 osg::ref_ptr<osg::Vec2Array> _texcoords;
167 osg::Vec3 _controlcoords[4];
168
169 osg::Texture2D* _texture;
170 int _s;
171 int _t;
172
173
174};
175
176/** Helper class for managing the reuse of ImpostorSprite resources. */
177class OSGSIM_EXPORT ImpostorSpriteManager : public osg::Referenced
178{
179 public:
180
181 ImpostorSpriteManager();
182
183 bool empty() const { return _first==0; }
184
185 ImpostorSprite* first() { return _first; }
186
187 ImpostorSprite* last() { return _last; }
188
189 void push_back(ImpostorSprite* is);
190
191 void remove(ImpostorSprite* is);
192
193 ImpostorSprite* createOrReuseImpostorSprite(int s,int t,unsigned int frameNumber);
194
195 osg::StateSet* createOrReuseStateSet();
196
197 void reset();
198
199 protected:
200
201
202 ~ImpostorSpriteManager();
203
204 osg::ref_ptr<osg::TexEnv> _texenv;
205 osg::ref_ptr<osg::AlphaFunc> _alphafunc;
206
207 ImpostorSprite* _first;
208 ImpostorSprite* _last;
209
210 typedef std::vector< osg::ref_ptr<osg::StateSet> > StateSetList;
211 StateSetList _stateSetList;
212 unsigned int _reuseStateSetIndex;
213
214
215};
216
217}
218
219#endif