/src/ogre/OgreMain/include/OgreRenderable.h
Line | Count | Source (jump to first uncovered line) |
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-2014 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 | | #ifndef __Renderable_H__ |
29 | | #define __Renderable_H__ |
30 | | |
31 | | #include "OgrePrerequisites.h" |
32 | | #include "OgreCommon.h" |
33 | | |
34 | | #include "OgreGpuProgram.h" |
35 | | #include "OgreGpuProgramParams.h" |
36 | | #include "OgreMatrix4.h" |
37 | | #include "OgreMaterial.h" |
38 | | #include "OgrePlane.h" |
39 | | #include "OgreVector.h" |
40 | | #include "OgreException.h" |
41 | | #include "OgreUserObjectBindings.h" |
42 | | #include "OgreHeaderPrefix.h" |
43 | | |
44 | | namespace Ogre { |
45 | | |
46 | | /** \addtogroup Core |
47 | | * @{ |
48 | | */ |
49 | | /** \addtogroup Scene |
50 | | * @{ |
51 | | */ |
52 | | /** Abstract class defining the interface all renderable objects must implement. |
53 | | |
54 | | This interface abstracts renderable discrete objects which will be queued in the render pipeline, |
55 | | grouped by material. Classes implementing this interface must be based on a single material, a single |
56 | | world matrix (or a collection of world matrices which are blended by weights), and must be |
57 | | renderable via a single render operation. |
58 | | @par |
59 | | Note that deciding whether to put these objects in the rendering pipeline is done from the more specific |
60 | | classes e.g. entities. Only once it is decided that the specific class is to be rendered is the abstract version |
61 | | created (could be more than one per visible object) and pushed onto the rendering queue. |
62 | | */ |
63 | | class _OgreExport Renderable |
64 | | { |
65 | | public: |
66 | | enum |
67 | | { |
68 | | DEFAULT_PRIORITY = 100 |
69 | | }; |
70 | | |
71 | | Renderable() |
72 | 0 | : mMaterialLodIndex(0), mPolygonModeOverrideable(true), mUseIdentityProjection(false), |
73 | 0 | mUseIdentityView(false) |
74 | 0 | { |
75 | 0 | } |
76 | | /** Virtual destructor needed as class has virtual methods. */ |
77 | 0 | virtual ~Renderable() {} |
78 | | /** Retrieves a weak reference to the material this renderable object uses. |
79 | | |
80 | | Note that the Renderable also has the option to override the getTechnique method |
81 | | to specify a particular Technique to use instead of the best one available. |
82 | | */ |
83 | | virtual const MaterialPtr& getMaterial(void) const = 0; |
84 | | /** Retrieves a pointer to the Material Technique this renderable object uses. |
85 | | |
86 | | This is to allow Renderables to use a chosen Technique if they wish, otherwise |
87 | | they will use the best Technique available for the Material they are using. |
88 | | */ |
89 | 0 | virtual Technique* getTechnique(void) const { return getMaterial()->getBestTechnique(mMaterialLodIndex, this); } |
90 | | /** Gets the render operation required to send this object to the frame buffer. |
91 | | */ |
92 | | virtual void getRenderOperation(RenderOperation& op) = 0; |
93 | | |
94 | 0 | uint16 _getMaterialLodIndex() const { return mMaterialLodIndex; } |
95 | | |
96 | | /** Called just prior to the Renderable being rendered. |
97 | | |
98 | | OGRE is a queued renderer, so the actual render commands are executed |
99 | | at a later time than the point at which an object is discovered to be |
100 | | visible. This allows ordering & grouping of renders without the discovery |
101 | | process having to be aware of it. It also means OGRE uses declarative |
102 | | render information rather than immediate mode rendering - this is very useful |
103 | | in that certain effects and processes can automatically be applied to |
104 | | a wide range of scenes, but the downside is that special cases are |
105 | | more difficult to handle, because there is not the declared state to |
106 | | cope with it. |
107 | | @par |
108 | | This method allows a Renderable to do something special at the actual |
109 | | point of rendering if it wishes to. When this method is called, all the |
110 | | material render state as declared by this Renderable has already been set, |
111 | | all that is left to do is to bind the buffers and perform the render. |
112 | | The Renderable may modify render state itself if it wants to (and restore it in the |
113 | | postRender call) before the automated render happens, or by returning |
114 | | 'false' from this method can actually suppress the automatic render |
115 | | and perform one of its own. |
116 | | @return |
117 | | true if the automatic render should proceed, false to skip it on |
118 | | the assumption that the Renderable has done it manually. |
119 | | */ |
120 | | virtual bool preRender(SceneManager* sm, RenderSystem* rsys) |
121 | 0 | { (void)sm; (void)rsys; return true; } |
122 | | |
123 | | /** Called immediately after the Renderable has been rendered. |
124 | | */ |
125 | | virtual void postRender(SceneManager* sm, RenderSystem* rsys) |
126 | 0 | { (void)sm; (void)rsys; } |
127 | | |
128 | | /** Gets the world transform matrix / matrices for this renderable object. |
129 | | |
130 | | If the object has any derived transforms, these are expected to be up to date as long as |
131 | | all the SceneNode structures have been updated before this is called. |
132 | | |
133 | | @note |
134 | | Internal Ogre never supports non-affine matrix for world transform matrix/matrices, |
135 | | the behavior is undefined if returns non-affine matrix here. |
136 | | |
137 | | This method will populate transform with 1 matrix if it does not use GPU vertex blending. If it |
138 | | does use GPU vertex blending it will fill the passed in pointer with an array of matrices, |
139 | | the length being the value returned from @ref getNumWorldTransforms. |
140 | | |
141 | | @note If @ref MeshManager::getBonesUseObjectSpace() is true, the first matrix must contain the world |
142 | | transform of the object, and the rest of the matrices must contain the bone transforms in object space. |
143 | | */ |
144 | | virtual void getWorldTransforms(Matrix4* xform) const = 0; |
145 | | |
146 | | /** Returns the number of world transform matrices this renderable requires. |
147 | | |
148 | | When a renderable uses GPU vertex blending, it uses multiple world matrices instead of a single |
149 | | one. Each vertex sent to the pipeline can reference one or more matrices in this list |
150 | | with given weights. |
151 | | If a renderable does not use vertex blending this method returns 1, which is the default for |
152 | | simplicity. |
153 | | @note If @ref MeshManager::getBonesUseObjectSpace() is true, this method must return |
154 | | numBones + 1 |
155 | | */ |
156 | 0 | virtual uint16 getNumWorldTransforms(void) const { return 1; } |
157 | | |
158 | | /** Sets whether or not to use an 'identity' projection. |
159 | | |
160 | | Usually Renderable objects will use a projection matrix as determined |
161 | | by the active camera. However, if they want they can cancel this out |
162 | | and use an identity projection, which effectively projects in 2D using |
163 | | a {-1, 1} view space. Useful for overlay rendering. Normal renderables |
164 | | need not change this. The default is false. |
165 | | @see Renderable::getUseIdentityProjection |
166 | | */ |
167 | | void setUseIdentityProjection(bool useIdentityProjection) |
168 | 0 | { |
169 | 0 | mUseIdentityProjection = useIdentityProjection; |
170 | 0 | } |
171 | | |
172 | | /** Returns whether or not to use an 'identity' projection. |
173 | | |
174 | | Usually Renderable objects will use a projection matrix as determined |
175 | | by the active camera. However, if they want they can cancel this out |
176 | | and use an identity projection, which effectively projects in 2D using |
177 | | a {-1, 1} view space. Useful for overlay rendering. Normal renderables |
178 | | need not change this. |
179 | | @see Renderable::setUseIdentityProjection |
180 | | */ |
181 | 0 | bool getUseIdentityProjection(void) const { return mUseIdentityProjection; } |
182 | | |
183 | | /** Sets whether or not to use an 'identity' view. |
184 | | |
185 | | Usually Renderable objects will use a view matrix as determined |
186 | | by the active camera. However, if they want they can cancel this out |
187 | | and use an identity matrix, which means all geometry is assumed |
188 | | to be relative to camera space already. Useful for overlay rendering. |
189 | | Normal renderables need not change this. The default is false. |
190 | | @see Renderable::getUseIdentityView |
191 | | */ |
192 | | void setUseIdentityView(bool useIdentityView) |
193 | 0 | { |
194 | 0 | mUseIdentityView = useIdentityView; |
195 | 0 | } |
196 | | |
197 | | /** Returns whether or not to use an 'identity' view. |
198 | | |
199 | | Usually Renderable objects will use a view matrix as determined |
200 | | by the active camera. However, if they want they can cancel this out |
201 | | and use an identity matrix, which means all geometry is assumed |
202 | | to be relative to camera space already. Useful for overlay rendering. |
203 | | Normal renderables need not change this. |
204 | | @see Renderable::setUseIdentityView |
205 | | */ |
206 | 0 | bool getUseIdentityView(void) const { return mUseIdentityView; } |
207 | | |
208 | | /** Returns the squared distance between the camera and this renderable. |
209 | | |
210 | | Used to sort transparent objects. Squared distance is used |
211 | | to avoid having to perform a square root on the result. |
212 | | */ |
213 | | virtual Real getSquaredViewDepth(const Camera* cam) const = 0; |
214 | | |
215 | | /** Gets a list of lights, ordered relative to how close they are to this renderable. |
216 | | |
217 | | Directional lights, which have no position, will always be first on this list. |
218 | | */ |
219 | | virtual const LightList& getLights(void) const = 0; |
220 | | |
221 | | /** Method which reports whether this renderable would normally cast a |
222 | | shadow. |
223 | | |
224 | | Subclasses should override this if they could have been used to |
225 | | generate a shadow. |
226 | | */ |
227 | 0 | virtual bool getCastsShadows(void) const { return false; } |
228 | | |
229 | | /** Sets a custom parameter for this Renderable, which may be used to |
230 | | drive calculations for this specific Renderable, like GPU program parameters. |
231 | | |
232 | | Calling this method simply associates a numeric index with a 4-dimensional |
233 | | value for this specific Renderable. This is most useful if the material |
234 | | which this Renderable uses a vertex or fragment program, and has an |
235 | | ACT_CUSTOM parameter entry. This parameter entry can refer to the |
236 | | index you specify as part of this call, thereby mapping a custom |
237 | | parameter for this renderable to a program parameter. |
238 | | @param index The index with which to associate the value. Note that this |
239 | | does not have to start at 0, and can include gaps. It also has no direct |
240 | | correlation with a GPU program parameter index - the mapping between the |
241 | | two is performed by the ACT_CUSTOM entry, if that is used. |
242 | | @param value The value to associate. |
243 | | */ |
244 | | void setCustomParameter(size_t index, const Vector4f& value); |
245 | | |
246 | | /** Removes a custom value which is associated with this Renderable at the given index. |
247 | | @param index Index of the parameter to remove. |
248 | | @see setCustomParameter for full details. |
249 | | */ |
250 | | void removeCustomParameter(size_t index); |
251 | | |
252 | | /** Checks whether a custom value is associated with this Renderable at the given index. |
253 | | @param index Index of the parameter to check for existence. |
254 | | @see setCustomParameter for full details. |
255 | | */ |
256 | | bool hasCustomParameter(size_t index) const; |
257 | | |
258 | | /** Gets the custom value associated with this Renderable at the given index. |
259 | | @param index Index of the parameter to retrieve. |
260 | | @see setCustomParameter for full details. |
261 | | */ |
262 | | const Vector4f& getCustomParameter(size_t index) const; |
263 | | |
264 | | /** Update a custom GpuProgramParameters constant which is derived from |
265 | | information only this Renderable knows. |
266 | | |
267 | | This method allows a Renderable to map in a custom GPU program parameter |
268 | | based on it's own data. This is represented by a GPU auto parameter |
269 | | of ACT_CUSTOM, and to allow there to be more than one of these per |
270 | | Renderable, the 'data' field on the auto parameter will identify |
271 | | which parameter is being updated. The implementation of this method |
272 | | must identify the parameter being updated, and call a 'setConstant' |
273 | | method on the passed in GpuProgramParameters object, using the details |
274 | | provided in the incoming auto constant setting to identify the index |
275 | | at which to set the parameter. |
276 | | @par |
277 | | You do not need to override this method if you're using the standard |
278 | | sets of data associated with the Renderable as provided by setCustomParameter |
279 | | and getCustomParameter. By default, the implementation will map from the |
280 | | value indexed by the 'constantEntry.data' parameter to a value previously |
281 | | set by setCustomParameter. But custom Renderables are free to override |
282 | | this if they want, in any case. |
283 | | @param constantEntry The auto constant entry referring to the parameter |
284 | | being updated |
285 | | @param params The parameters object which this method should call to |
286 | | set the updated parameters. |
287 | | */ |
288 | | virtual void _updateCustomGpuParameter(const GpuProgramParameters::AutoConstantEntry& constantEntry, |
289 | | GpuProgramParameters* params) const; |
290 | | |
291 | | /** Sets whether this renderable's chosen detail level can be |
292 | | overridden (downgraded) by the camera setting. |
293 | | @param override true means that a lower camera detail will override this |
294 | | renderables detail level, false means it won't. |
295 | | */ |
296 | | void setPolygonModeOverrideable(bool override) |
297 | 0 | { |
298 | 0 | mPolygonModeOverrideable = override; |
299 | 0 | } |
300 | | |
301 | | /** Gets whether this renderable's chosen detail level can be |
302 | | overridden (downgraded) by the camera setting. |
303 | | */ |
304 | | bool getPolygonModeOverrideable(void) const |
305 | 0 | { |
306 | 0 | return mPolygonModeOverrideable; |
307 | 0 | } |
308 | | |
309 | | /** @deprecated use UserObjectBindings::setUserAny via getUserObjectBindings() instead. |
310 | | */ |
311 | 0 | OGRE_DEPRECATED void setUserAny(const Any& anything) { getUserObjectBindings().setUserAny(anything); } |
312 | | |
313 | | /** @deprecated use UserObjectBindings::getUserAny via getUserObjectBindings() instead. |
314 | | */ |
315 | 0 | OGRE_DEPRECATED const Any& getUserAny(void) const { return getUserObjectBindings().getUserAny(); } |
316 | | |
317 | | /// @copydoc UserObjectBindings |
318 | 0 | UserObjectBindings& getUserObjectBindings() { return mUserObjectBindings; } |
319 | | |
320 | | /// @overload |
321 | 0 | const UserObjectBindings& getUserObjectBindings() const { return mUserObjectBindings; } |
322 | | |
323 | | |
324 | | /** Visitor object that can be used to iterate over a collection of Renderable |
325 | | instances abstractly. |
326 | | |
327 | | Different scene objects use Renderable differently; some will have a |
328 | | single Renderable, others will have many. This visitor interface allows |
329 | | classes using Renderable to expose a clean way for external code to |
330 | | get access to the contained Renderable instance(s) that it will |
331 | | eventually add to the render queue. |
332 | | @par |
333 | | To actually have this method called, you have to call a method on the |
334 | | class containing the Renderable instances. One example is |
335 | | MovableObject::visitRenderables. |
336 | | */ |
337 | | class Visitor |
338 | | { |
339 | | public: |
340 | | /** Virtual destructor needed as class has virtual methods. */ |
341 | 0 | virtual ~Visitor() { } |
342 | | /** Generic visitor method. |
343 | | @param rend The Renderable instance being visited |
344 | | @param lodIndex The LOD index to which this Renderable belongs. Some |
345 | | objects support LOD and this will tell you whether the Renderable |
346 | | you're looking at is from the top LOD (0) or otherwise |
347 | | @param isDebug Whether this is a debug renderable or not. |
348 | | @param pAny Optional pointer to some additional data that the class |
349 | | calling the visitor may populate if it chooses to. |
350 | | */ |
351 | | virtual void visit(Renderable* rend, ushort lodIndex, bool isDebug, |
352 | | Any* pAny = 0) = 0; |
353 | | }; |
354 | | |
355 | | protected: |
356 | | typedef std::map<size_t, Vector4f> CustomParameterMap; |
357 | | CustomParameterMap mCustomParameters; |
358 | | UserObjectBindings mUserObjectBindings; /// User objects binding. |
359 | | uint16 mMaterialLodIndex; |
360 | | bool mPolygonModeOverrideable; |
361 | | bool mUseIdentityProjection; |
362 | | bool mUseIdentityView; |
363 | | }; |
364 | | |
365 | | /** @} */ |
366 | | /** @} */ |
367 | | |
368 | | } // namespace Ogre |
369 | | |
370 | | #include "OgreHeaderSuffix.h" |
371 | | #endif //__Renderable_H__ |