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 | #ifndef __ShadowCaster_H__ |
---|
29 | #define __ShadowCaster_H__ |
---|
30 | |
---|
31 | #include "OgrePrerequisites.h" |
---|
32 | #include "OgreRenderable.h" |
---|
33 | #include "OgreHeaderPrefix.h" |
---|
34 | |
---|
35 | |
---|
36 | namespace Ogre { |
---|
37 | |
---|
38 | /** \addtogroup Core |
---|
39 | * @{ |
---|
40 | */ |
---|
41 | /** \addtogroup Scene |
---|
42 | * @{ |
---|
43 | */ |
---|
44 | /** Class which represents the renderable aspects of a set of shadow volume faces. |
---|
45 | @remarks |
---|
46 | Note that for casters comprised of more than one set of vertex buffers (e.g. SubMeshes each |
---|
47 | using their own geometry), it will take more than one ShadowRenderable to render the |
---|
48 | shadow volume. Therefore for shadow caster geometry, it is best to stick to one set of |
---|
49 | vertex buffers (not necessarily one buffer, but the positions for the entire geometry |
---|
50 | should come from one buffer if possible) |
---|
51 | */ |
---|
52 | class _OgreExport ShadowRenderable : public Renderable, public ShadowDataAlloc |
---|
53 | { |
---|
54 | protected: |
---|
55 | MaterialPtr mMaterial; |
---|
56 | RenderOperation mRenderOp; |
---|
57 | ShadowRenderable* mLightCap; /// Used only if isLightCapSeparate == true |
---|
58 | public: |
---|
59 | ShadowRenderable() : mMaterial(), mLightCap(0) {} |
---|
60 | virtual ~ShadowRenderable() { delete mLightCap; } |
---|
61 | /** Set the material to be used by the shadow, should be set by the caller |
---|
62 | before adding to a render queue |
---|
63 | */ |
---|
64 | void setMaterial(const MaterialPtr& mat) { mMaterial = mat; } |
---|
65 | /// @copydoc Renderable::getMaterial |
---|
66 | const MaterialPtr& getMaterial(void) const { return mMaterial; } |
---|
67 | /// @copydoc Renderable::getRenderOperation |
---|
68 | void getRenderOperation(RenderOperation& op) { op = mRenderOp; } |
---|
69 | /// Get the internal render operation for set up. |
---|
70 | RenderOperation* getRenderOperationForUpdate(void) {return &mRenderOp;} |
---|
71 | /// @copydoc Renderable::getWorldTransforms |
---|
72 | void getWorldTransforms(Matrix4* xform) const = 0; |
---|
73 | /// @copydoc Renderable::getSquaredViewDepth |
---|
74 | Real getSquaredViewDepth(const Camera*) const{ return 0; /* not used */} |
---|
75 | /// @copydoc Renderable::getLights. |
---|
76 | const LightList& getLights(void) const; |
---|
77 | /** Does this renderable require a separate light cap? |
---|
78 | @remarks |
---|
79 | If possible, the light cap (when required) should be contained in the |
---|
80 | usual geometry of the shadow renderable. However, if for some reason |
---|
81 | the normal depth function (less than) could cause artefacts, then a |
---|
82 | separate light cap with a depth function of 'always fail' can be used |
---|
83 | instead. The primary example of this is when there are floating point |
---|
84 | inaccuracies caused by calculating the shadow geometry separately from |
---|
85 | the real geometry. |
---|
86 | */ |
---|
87 | bool isLightCapSeparate(void) const { return mLightCap != 0; } |
---|
88 | |
---|
89 | /// Get the light cap version of this renderable. |
---|
90 | ShadowRenderable* getLightCapRenderable(void) { return mLightCap; } |
---|
91 | /// Should this ShadowRenderable be treated as visible? |
---|
92 | virtual bool isVisible(void) const { return true; } |
---|
93 | |
---|
94 | /** This function informs the shadow renderable that the global index buffer |
---|
95 | from the SceneManager has been updated. As all shadow use this buffer their pointer |
---|
96 | must be updated as well. |
---|
97 | @param indexBuffer |
---|
98 | Pointer to the new index buffer. |
---|
99 | */ |
---|
100 | virtual void rebindIndexBuffer(const HardwareIndexBufferSharedPtr& indexBuffer) = 0; |
---|
101 | |
---|
102 | }; |
---|
103 | |
---|
104 | /** A set of flags that can be used to influence ShadowRenderable creation. */ |
---|
105 | enum ShadowRenderableFlags |
---|
106 | { |
---|
107 | /// For shadow volume techniques only, generate a light cap on the volume. |
---|
108 | SRF_INCLUDE_LIGHT_CAP = 0x00000001, |
---|
109 | /// For shadow volume techniques only, generate a dark cap on the volume. |
---|
110 | SRF_INCLUDE_DARK_CAP = 0x00000002, |
---|
111 | /// For shadow volume techniques only, indicates volume is extruded to infinity. |
---|
112 | SRF_EXTRUDE_TO_INFINITY = 0x00000004 |
---|
113 | }; |
---|
114 | |
---|
115 | /** This class defines the interface that must be implemented by shadow casters. |
---|
116 | */ |
---|
117 | class _OgreExport ShadowCaster |
---|
118 | { |
---|
119 | public: |
---|
120 | virtual ~ShadowCaster() { } |
---|
121 | /** Returns whether or not this object currently casts a shadow. */ |
---|
122 | virtual bool getCastShadows(void) const = 0; |
---|
123 | |
---|
124 | /** Returns details of the edges which might be used to determine a silhouette. */ |
---|
125 | virtual EdgeData* getEdgeList(void) = 0; |
---|
126 | /** Returns whether the object has a valid edge list. */ |
---|
127 | virtual bool hasEdgeList(void) = 0; |
---|
128 | |
---|
129 | /** Get the world bounding box of the caster. */ |
---|
130 | virtual const AxisAlignedBox& getWorldBoundingBox(bool derive = false) const = 0; |
---|
131 | /** Gets the world space bounding box of the light cap. */ |
---|
132 | virtual const AxisAlignedBox& getLightCapBounds(void) const = 0; |
---|
133 | /** Gets the world space bounding box of the dark cap, as extruded using the light provided. */ |
---|
134 | virtual const AxisAlignedBox& getDarkCapBounds(const Light& light, Real dirLightExtrusionDist) const = 0; |
---|
135 | |
---|
136 | typedef vector<ShadowRenderable*>::type ShadowRenderableList; |
---|
137 | typedef VectorIterator<ShadowRenderableList> ShadowRenderableListIterator; |
---|
138 | |
---|
139 | /** Gets an iterator over the renderables required to render the shadow volume. |
---|
140 | @remarks |
---|
141 | Shadowable geometry should ideally be designed such that there is only one |
---|
142 | ShadowRenderable required to render the the shadow; however this is not a necessary |
---|
143 | limitation and it can be exceeded if required. |
---|
144 | @param shadowTechnique |
---|
145 | The technique being used to generate the shadow. |
---|
146 | @param light |
---|
147 | The light to generate the shadow from. |
---|
148 | @param indexBuffer |
---|
149 | The index buffer to build the renderables into, |
---|
150 | the current contents are assumed to be disposable. |
---|
151 | @param extrudeVertices |
---|
152 | If @c true, this means this class should extrude |
---|
153 | the vertices of the back of the volume in software. If false, it |
---|
154 | will not be done (a vertex program is assumed). |
---|
155 | @param extrusionDistance |
---|
156 | The distance to extrude the shadow volume. |
---|
157 | @param flags |
---|
158 | Technique-specific flags, see ShadowRenderableFlags. |
---|
159 | */ |
---|
160 | virtual ShadowRenderableListIterator getShadowVolumeRenderableIterator( |
---|
161 | ShadowTechnique shadowTechnique, const Light* light, |
---|
162 | HardwareIndexBufferSharedPtr* indexBuffer, size_t* indexBufferUsedSize, |
---|
163 | bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 ) = 0; |
---|
164 | |
---|
165 | /** Utility method for extruding vertices based on a light. |
---|
166 | @remarks |
---|
167 | Unfortunately, because D3D cannot handle homogeneous (4D) position |
---|
168 | coordinates in the fixed-function pipeline (GL can, but we have to |
---|
169 | be cross-API), when we extrude in software we cannot extrude to |
---|
170 | infinity the way we do in the vertex program (by setting w to |
---|
171 | 0.0f). Therefore we extrude by a fixed distance, which may cause |
---|
172 | some problems with larger scenes. Luckily better hardware (ie |
---|
173 | vertex programs) can fix this. |
---|
174 | @param vertexBuffer |
---|
175 | The vertex buffer containing ONLY xyz position |
---|
176 | values, which must be originalVertexCount * 2 * 3 floats long. |
---|
177 | @param originalVertexCount |
---|
178 | The count of the original number of |
---|
179 | vertices, i.e. the number in the mesh, not counting the doubling |
---|
180 | which has already been done (by VertexData::prepareForShadowVolume) |
---|
181 | to provide the extruded area of the buffer. |
---|
182 | @param lightPos |
---|
183 | 4D light position in object space, when w=0.0f this |
---|
184 | represents a directional light. |
---|
185 | @param extrudeDist |
---|
186 | The distance to extrude. |
---|
187 | */ |
---|
188 | static void extrudeVertices(const HardwareVertexBufferSharedPtr& vertexBuffer, |
---|
189 | size_t originalVertexCount, const Vector4& lightPos, Real extrudeDist); |
---|
190 | /** Get the distance to extrude for a point/spot light. */ |
---|
191 | virtual Real getPointExtrusionDistance(const Light* l) const = 0; |
---|
192 | protected: |
---|
193 | /// Helper method for calculating extrusion distance. |
---|
194 | Real getExtrusionDistance(const Vector3& objectPos, const Light* light) const; |
---|
195 | /** Tells the caster to perform the tasks necessary to update the |
---|
196 | edge data's light listing. Can be overridden if the subclass needs |
---|
197 | to do additional things. |
---|
198 | @param edgeData |
---|
199 | The edge information to update. |
---|
200 | @param lightPos |
---|
201 | 4D vector representing the light, a directional light has w=0.0. |
---|
202 | */ |
---|
203 | virtual void updateEdgeListLightFacing(EdgeData* edgeData, |
---|
204 | const Vector4& lightPos); |
---|
205 | |
---|
206 | /** Generates the indexes required to render a shadow volume into the |
---|
207 | index buffer which is passed in, and updates shadow renderables |
---|
208 | to use it. |
---|
209 | @param edgeData |
---|
210 | The edge information to use. |
---|
211 | @param indexBuffer |
---|
212 | The buffer into which to write data into; current |
---|
213 | contents are assumed to be discardable. |
---|
214 | @param indexBufferUsedSize |
---|
215 | If the rest of buffer is enough than it would be locked with |
---|
216 | HBL_NO_OVERWRITE semantic and indexBufferUsedSize would be increased, |
---|
217 | otherwise HBL_DISCARD would be used and indexBufferUsedSize would be reset. |
---|
218 | @param light |
---|
219 | The light, mainly for type info as silhouette calculations |
---|
220 | should already have been done in updateEdgeListLightFacing |
---|
221 | @param shadowRenderables |
---|
222 | A list of shadow renderables which has |
---|
223 | already been constructed but will need populating with details of |
---|
224 | the index ranges to be used. |
---|
225 | @param flags |
---|
226 | Additional controller flags, see ShadowRenderableFlags. |
---|
227 | */ |
---|
228 | virtual void generateShadowVolume(EdgeData* edgeData, |
---|
229 | const HardwareIndexBufferSharedPtr& indexBuffer, size_t& indexBufferUsedSize, |
---|
230 | const Light* light, ShadowRenderableList& shadowRenderables, unsigned long flags); |
---|
231 | /** Utility method for extruding a bounding box. |
---|
232 | @param box |
---|
233 | Original bounding box, will be updated in-place. |
---|
234 | @param lightPos |
---|
235 | 4D light position in object space, when w=0.0f this |
---|
236 | represents a directional light. |
---|
237 | @param extrudeDist |
---|
238 | The distance to extrude. |
---|
239 | */ |
---|
240 | virtual void extrudeBounds(AxisAlignedBox& box, const Vector4& lightPos, |
---|
241 | Real extrudeDist) const; |
---|
242 | |
---|
243 | |
---|
244 | }; |
---|
245 | /** @} */ |
---|
246 | /** @} */ |
---|
247 | } // namespace Ogre |
---|
248 | #include "OgreHeaderSuffix.h" |
---|
249 | |
---|
250 | #endif // __ShadowCaster_H__ |
---|