[148] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
| 8 | |
---|
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 10 | of this software and associated documentation files (the "Software"), to deal |
---|
| 11 | in the Software without restriction, including without limitation the rights |
---|
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 13 | copies of the Software, and to permit persons to whom the Software is |
---|
| 14 | furnished to do so, subject to the following conditions: |
---|
| 15 | |
---|
| 16 | The above copyright notice and this permission notice shall be included in |
---|
| 17 | all copies or substantial portions of the Software. |
---|
| 18 | |
---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 25 | THE SOFTWARE. |
---|
| 26 | ----------------------------------------------------------------------------- |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #ifndef __RibbonTrail_H__ |
---|
| 30 | #define __RibbonTrail_H__ |
---|
| 31 | |
---|
| 32 | #include "OgrePrerequisites.h" |
---|
| 33 | |
---|
| 34 | #include "OgreBillboardChain.h" |
---|
| 35 | #include "OgreNode.h" |
---|
| 36 | #include "OgreIteratorWrappers.h" |
---|
| 37 | #include "OgreFrameListener.h" |
---|
| 38 | #include "OgreControllerManager.h" |
---|
| 39 | #include "OgreHeaderPrefix.h" |
---|
| 40 | |
---|
| 41 | namespace Ogre { |
---|
| 42 | |
---|
| 43 | /** \addtogroup Core |
---|
| 44 | * @{ |
---|
| 45 | */ |
---|
| 46 | /** \addtogroup Effects |
---|
| 47 | * @{ |
---|
| 48 | */ |
---|
| 49 | /** Subclass of BillboardChain which automatically leaves a trail behind |
---|
| 50 | one or more Node instances. |
---|
| 51 | @remarks |
---|
| 52 | An instance of this class will watch one or more Node instances, and |
---|
| 53 | automatically generate a trail behind them as they move. Because this |
---|
| 54 | class can monitor multiple modes, it generates its own geometry in |
---|
| 55 | world space and thus, even though it has to be attached to a SceneNode |
---|
| 56 | to be visible, changing the position of the scene node it is attached to |
---|
| 57 | makes no difference to the geometry rendered. |
---|
| 58 | @par |
---|
| 59 | The 'head' element grows smoothly in size until it reaches the required size, |
---|
| 60 | then a new element is added. If the segment is full, the tail element |
---|
| 61 | shrinks by the same proportion as the head grows before disappearing. |
---|
| 62 | @par |
---|
| 63 | Elements can be faded out on a time basis, either by altering their colour |
---|
| 64 | or altering their alpha. The width can also alter over time. |
---|
| 65 | @par |
---|
| 66 | 'v' texture coordinates are fixed at 0.0 if used, meaning that you can |
---|
| 67 | use a 1D texture to 'smear' a colour pattern along the ribbon if you wish. |
---|
| 68 | The 'u' coordinates are by default (0.0, 1.0), but you can alter this |
---|
| 69 | using setOtherTexCoordRange if you wish. |
---|
| 70 | */ |
---|
| 71 | class _OgreExport RibbonTrail : public BillboardChain, public Node::Listener |
---|
| 72 | { |
---|
| 73 | public: |
---|
| 74 | /** Constructor (don't use directly, use factory) |
---|
| 75 | @param name The name to give this object |
---|
| 76 | @param maxElements The maximum number of elements per chain |
---|
| 77 | @param numberOfChains The number of separate chain segments contained in this object, |
---|
| 78 | ie the maximum number of nodes that can have trails attached |
---|
| 79 | @param useTextureCoords If true, use texture coordinates from the chain elements |
---|
| 80 | @param useVertexColours If true, use vertex colours from the chain elements (must |
---|
| 81 | be true if you intend to use fading) |
---|
| 82 | */ |
---|
| 83 | RibbonTrail(const String& name, size_t maxElements = 20, size_t numberOfChains = 1, |
---|
| 84 | bool useTextureCoords = true, bool useVertexColours = true); |
---|
| 85 | /// destructor |
---|
| 86 | virtual ~RibbonTrail(); |
---|
| 87 | |
---|
| 88 | typedef vector<Node*>::type NodeList; |
---|
| 89 | typedef ConstVectorIterator<NodeList> NodeIterator; |
---|
| 90 | |
---|
| 91 | /** Add a node to be tracked. |
---|
| 92 | @param n The node that will be tracked. |
---|
| 93 | */ |
---|
| 94 | virtual void addNode(Node* n); |
---|
| 95 | /** Remove tracking on a given node. */ |
---|
| 96 | virtual void removeNode(Node* n); |
---|
| 97 | /** Get an iterator over the nodes which are being tracked. */ |
---|
| 98 | virtual NodeIterator getNodeIterator(void) const; |
---|
| 99 | /** Get the chain index for a given Node being tracked. */ |
---|
| 100 | virtual size_t getChainIndexForNode(const Node* n); |
---|
| 101 | |
---|
| 102 | /** Set the length of the trail. |
---|
| 103 | @remarks |
---|
| 104 | This sets the length of the trail, in world units. It also sets how |
---|
| 105 | far apart each segment will be, ie length / max_elements. |
---|
| 106 | @param len The length of the trail in world units |
---|
| 107 | */ |
---|
| 108 | virtual void setTrailLength(Real len); |
---|
| 109 | /** Get the length of the trail. */ |
---|
| 110 | virtual Real getTrailLength(void) const { return mTrailLength; } |
---|
| 111 | |
---|
| 112 | /** @copydoc BillboardChain::setMaxChainElements */ |
---|
| 113 | void setMaxChainElements(size_t maxElements); |
---|
| 114 | /** @copydoc BillboardChain::setNumberOfChains */ |
---|
| 115 | void setNumberOfChains(size_t numChains); |
---|
| 116 | /** @copydoc BillboardChain::clearChain */ |
---|
| 117 | void clearChain(size_t chainIndex); |
---|
| 118 | |
---|
| 119 | /** Set the starting ribbon colour for a given segment. |
---|
| 120 | @param chainIndex The index of the chain |
---|
| 121 | @param col The initial colour |
---|
| 122 | @note |
---|
| 123 | Only used if this instance is using vertex colours. |
---|
| 124 | */ |
---|
| 125 | virtual void setInitialColour(size_t chainIndex, const ColourValue& col); |
---|
| 126 | /** Set the starting ribbon colour. |
---|
| 127 | @param chainIndex The index of the chain |
---|
| 128 | @param r,b,g,a The initial colour |
---|
| 129 | @note |
---|
| 130 | Only used if this instance is using vertex colours. |
---|
| 131 | */ |
---|
| 132 | virtual void setInitialColour(size_t chainIndex, Real r, Real g, Real b, Real a = 1.0); |
---|
| 133 | /** Get the starting ribbon colour. */ |
---|
| 134 | virtual const ColourValue& getInitialColour(size_t chainIndex) const; |
---|
| 135 | |
---|
| 136 | /** Enables / disables fading the trail using colour. |
---|
| 137 | @param chainIndex The index of the chain |
---|
| 138 | @param valuePerSecond The amount to subtract from colour each second |
---|
| 139 | */ |
---|
| 140 | virtual void setColourChange(size_t chainIndex, const ColourValue& valuePerSecond); |
---|
| 141 | |
---|
| 142 | /** Set the starting ribbon width in world units. |
---|
| 143 | @param chainIndex The index of the chain |
---|
| 144 | @param width The initial width of the ribbon |
---|
| 145 | */ |
---|
| 146 | virtual void setInitialWidth(size_t chainIndex, Real width); |
---|
| 147 | /** Get the starting ribbon width in world units. */ |
---|
| 148 | virtual Real getInitialWidth(size_t chainIndex) const; |
---|
| 149 | |
---|
| 150 | /** Set the change in ribbon width per second. |
---|
| 151 | @param chainIndex The index of the chain |
---|
| 152 | @param widthDeltaPerSecond The amount the width will reduce by per second |
---|
| 153 | */ |
---|
| 154 | virtual void setWidthChange(size_t chainIndex, Real widthDeltaPerSecond); |
---|
| 155 | /** Get the change in ribbon width per second. */ |
---|
| 156 | virtual Real getWidthChange(size_t chainIndex) const; |
---|
| 157 | |
---|
| 158 | /** Enables / disables fading the trail using colour. |
---|
| 159 | @param chainIndex The index of the chain |
---|
| 160 | @param r,g,b,a The amount to subtract from each colour channel per second |
---|
| 161 | */ |
---|
| 162 | virtual void setColourChange(size_t chainIndex, Real r, Real g, Real b, Real a); |
---|
| 163 | |
---|
| 164 | /** Get the per-second fading amount */ |
---|
| 165 | virtual const ColourValue& getColourChange(size_t chainIndex) const; |
---|
| 166 | |
---|
| 167 | /// @see Node::Listener::nodeUpdated |
---|
| 168 | void nodeUpdated(const Node* node); |
---|
| 169 | /// @see Node::Listener::nodeDestroyed |
---|
| 170 | void nodeDestroyed(const Node* node); |
---|
| 171 | |
---|
| 172 | /// Perform any fading / width delta required; internal method |
---|
| 173 | virtual void _timeUpdate(Real time); |
---|
| 174 | |
---|
| 175 | /** Overridden from MovableObject */ |
---|
| 176 | const String& getMovableType(void) const; |
---|
| 177 | |
---|
| 178 | protected: |
---|
| 179 | /// List of nodes being trailed |
---|
| 180 | NodeList mNodeList; |
---|
| 181 | /// Mapping of nodes to chain segments |
---|
| 182 | typedef vector<size_t>::type IndexVector; |
---|
| 183 | /// Ordered like mNodeList, contains chain index |
---|
| 184 | IndexVector mNodeToChainSegment; |
---|
| 185 | // chains not in use |
---|
| 186 | IndexVector mFreeChains; |
---|
| 187 | |
---|
| 188 | // fast lookup node->chain index |
---|
| 189 | // we use positional map too because that can be useful |
---|
| 190 | typedef map<const Node*, size_t>::type NodeToChainSegmentMap; |
---|
| 191 | NodeToChainSegmentMap mNodeToSegMap; |
---|
| 192 | |
---|
| 193 | /// Total length of trail in world units |
---|
| 194 | Real mTrailLength; |
---|
| 195 | /// length of each element |
---|
| 196 | Real mElemLength; |
---|
| 197 | /// Squared length of each element |
---|
| 198 | Real mSquaredElemLength; |
---|
| 199 | typedef vector<ColourValue>::type ColourValueList; |
---|
| 200 | typedef vector<Real>::type RealList; |
---|
| 201 | /// Initial colour of the ribbon |
---|
| 202 | ColourValueList mInitialColour; |
---|
| 203 | /// fade amount per second |
---|
| 204 | ColourValueList mDeltaColour; |
---|
| 205 | /// Initial width of the ribbon |
---|
| 206 | RealList mInitialWidth; |
---|
| 207 | /// Delta width of the ribbon |
---|
| 208 | RealList mDeltaWidth; |
---|
| 209 | /// controller used to hook up frame time to fader |
---|
| 210 | Controller<Real>* mFadeController; |
---|
| 211 | /// controller value for hooking up frame time to fader |
---|
| 212 | ControllerValueRealPtr mTimeControllerValue; |
---|
| 213 | |
---|
| 214 | /// Manage updates to the time controller |
---|
| 215 | virtual void manageController(void); |
---|
| 216 | /// Node has changed position, update |
---|
| 217 | virtual void updateTrail(size_t index, const Node* node); |
---|
| 218 | /// Reset the tracked chain to initial state |
---|
| 219 | virtual void resetTrail(size_t index, const Node* node); |
---|
| 220 | /// Reset all tracked chains to initial state |
---|
| 221 | virtual void resetAllTrails(void); |
---|
| 222 | |
---|
| 223 | }; |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | /** Factory object for creating RibbonTrail instances */ |
---|
| 227 | class _OgreExport RibbonTrailFactory : public MovableObjectFactory |
---|
| 228 | { |
---|
| 229 | protected: |
---|
| 230 | MovableObject* createInstanceImpl( const String& name, const NameValuePairList* params); |
---|
| 231 | public: |
---|
| 232 | RibbonTrailFactory() {} |
---|
| 233 | ~RibbonTrailFactory() {} |
---|
| 234 | |
---|
| 235 | static String FACTORY_TYPE_NAME; |
---|
| 236 | |
---|
| 237 | const String& getType(void) const; |
---|
| 238 | void destroyInstance( MovableObject* obj); |
---|
| 239 | |
---|
| 240 | }; |
---|
| 241 | /** @} */ |
---|
| 242 | /** @} */ |
---|
| 243 | |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | #include "OgreHeaderSuffix.h" |
---|
| 247 | |
---|
| 248 | #endif |
---|