H3D API  2.4.1
ShadowCaster.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 #ifndef __SHADOWCASTER_H__
30 #define __SHADOWCASTER_H__
31 
33 
34 #include <H3D/X3DChildNode.h>
35 #include <H3D/X3DLightNode.h>
36 #include <H3D/MFNode.h>
37 #include <H3D/Shape.h>
38 #include <H3D/SFString.h>
39 #include <H3D/X3DShaderNode.h>
40 #include <H3D/DEFNodes.h>
41 
42 namespace H3D {
43 
44  class FrameBufferTextureGenerator;
98  class H3DAPI_API ShadowCaster : public X3DChildNode,
99  public H3DDisplayListObject {
100  public:
101 
105 
107  ShadowCaster( Inst< SFNode > _metadata = 0,
108  Inst< MFShadowObjectNode > _object = 0,
109  Inst< MFLightNode > _light = 0,
110  Inst< SFFloat > _shadowDarkness = 0,
111  Inst< SFFloat > _shadowDepthOffset = 0,
112  Inst< DisplayList > _displayList = 0,
113  Inst< SFString > _algorithm = 0,
114  Inst< SFShaderNode > _shadowShader = 0,
115  Inst< SFBool > _multiThreading = 0);
116 
118  virtual void render();
119 
120  virtual void traverseSG( TraverseInfo &ti );
121 
122  void addHeadLight();
123  // This method updates & computes shadows
124  // That are to be rendered in the scene
125  // Note: This always has to be called after traverse
126  static void updateShadowInformation(TraverseInfo& ti, void* data);
127 
132  H3DUniquePtr< MFShadowObjectNode > object;
133 
139  H3DUniquePtr< MFLightNode > light;
140 
148  H3DUniquePtr< SFFloat > shadowDarkness;
149 
160  H3DUniquePtr< SFFloat > shadowDepthOffset;
161 
177  H3DUniquePtr< SFString > algorithm;
178 
188  H3DUniquePtr< SFShaderNode > shadowShader;
189 
195  H3DUniquePtr< SFBool > multiThreading;
196 
199 
200  protected:
201  // Internal fbo that renders the main stencil buffer to a RGBA texture.
202  AutoRef< Node > generator;
203 
204  // DEFNodes of the nodes in shape.
205  X3D::DEFNodes dn;
206 
207  // holds the Shape used to render the texture from generator onto
208  // a FullscreenRectangle with shadowShader applied.
209  AutoRef<Shape> shape;
210 
211  // the shader that was used last loop.
212  X3DShaderNode *last_shader;
213 
214  // Callback function to FrameBufferTextureGenerator to render the stencil buffer
215  // to an fbo.
216  static void renderShadows( FrameBufferTextureGenerator *fbo, int i, void *args );
217 
218  // Map for all geometry mesh. One per light.
219  struct BufferData {
220  BufferData() {
221  }
222  ~BufferData() {
223  coord3.clear();
224  coord4.clear();
225  index.clear();
226  }
227 
228  // GPU
229  std::vector< Vec3d > coord3;
230  std::vector< H3DUInt32 > index;
231  // CPU
232  std::vector< Vec4d > coord4;
233 
234  };
235 
236  std::map< X3DLightNode*, BufferData > single_buffer_map;
237  Matrix4f accumulated_fwd_mat;
238 
239  static void * collectInformationCallback( void* data );
240  void computeThreadedShadowVolumeInformation();
241  void fillThreadSafeInformation( Matrix4f acc_fwd, bool is_render_caps );
242 
243  H3DUtil::MutexLock synchronous_lock;
244 
245  Matrix4f accumulated_fwd_ts;
246  bool render_caps_ts;
247 
248  std::vector<X3DLightNode*> lights_ts;
249  std::vector<H3DShadowObjectNode*> shadow_objects_ts;
250 
251  //data is filled into these containers and then transfered
252  std::map< X3DLightNode*, std::vector<Vec4d> > data_container_cpu_ts;
253  bool should_transfer_data_ts;
254  };
255 }
256 
257 #endif
Contains the DEFNodes class.
Header file for H3DShadowObjectNode.
Contains the MFNode class.
Contains the SFString field class.
Header file for Shape, X3D scene-graph node.
Header file for X3DChildNode, X3D scene-graph node.
Header file for X3DLightNode, X3D scene-graph node.
Header file for X3DShaderNode, X3D scene-graph node.
The FrameBufferTextureGenerator node provides a mean to render a sub-scene into a texture instead of ...
Definition: FrameBufferTextureGenerator.h:209
This abstract interface class is the base class for all node types that wants to create an OpenGL dis...
Definition: H3DDisplayListObject.h:54
The ShadowCaster node uses shadow volumes using stencil buffer to cast shadows from objects in a scen...
Definition: ShadowCaster.h:99
static H3DNodeDatabase database
The H3DNodeDatablase object for this node.
Definition: ShadowCaster.h:198
H3DUniquePtr< MFShadowObjectNode > object
The objects that should cast shadows.
Definition: ShadowCaster.h:132
H3DUniquePtr< SFFloat > shadowDarkness
The shadowDarkness field controls the darkness of the shadow.
Definition: ShadowCaster.h:148
H3DUniquePtr< SFShaderNode > shadowShader
The shadowShader field can contain a shader to perform effects on the shadow volume texture(stencil b...
Definition: ShadowCaster.h:188
H3DUniquePtr< SFBool > multiThreading
Field that decides to use a separate thread for collecting shadow volume information NOTE: applies on...
Definition: ShadowCaster.h:195
H3DUniquePtr< SFString > algorithm
The algorithm field defines what shadow volume algorithm to use.
Definition: ShadowCaster.h:177
H3DUniquePtr< SFFloat > shadowDepthOffset
The shadowDepthOffset field controls the z offset of drawn shadow volumes.
Definition: ShadowCaster.h:160
H3DUniquePtr< MFLightNode > light
The lights that shines on the objects.
Definition: ShadowCaster.h:139
TraverseInfo is a structure that is passed along when traversing the scene graph.
Definition: TraverseInfo.h:57
Template to make sure that the Nodes that are added to a MFNode are of a specified Node type.
Definition: MFNode.h:221
Template to make sure that the Node that is set in a SFNode is of a specified Node type.
Definition: SFNode.h:97
This abstract node type indicates that the concrete nodes which are instantiated based on it may be u...
Definition: X3DChildNode.h:42
This abstract node type is the base type for all node types which specify a programmable shader.
Definition: X3DShaderNode.h:61
Provides a mapping between defined DEF names in X3D and the nodes they refer to.
Definition: DEFNodes.h:92
H3D API namespace.
Definition: Anchor.h:38
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194