]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/shadowvolume.hxx
Add preliminary spot light animation
[simgear.git] / simgear / scene / model / shadowvolume.hxx
1 // Shadow volume class
2 //
3 // Written by Harald JOHNSEN, started June 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN - hjohnsen@evc.net
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 //
22
23 #ifndef _SHADOWVOLUME_HXX
24 #define _SHADOWVOLUME_HXX
25
26 #include <simgear/compiler.h>
27 #include <simgear/props/props.hxx>
28
29 #include <vector>
30 #include <map>
31
32 using std::vector;
33 using std::map;
34
35 class ssgBranch;
36 class ssgLeaf;
37 class SGPropertyNode;
38
39 /**
40  * A class wich add shadows in a postprocess stage.
41  */
42 class SGShadowVolume {
43
44 public:
45         SGShadowVolume( ssgBranch *root );
46         ~SGShadowVolume();
47
48         typedef enum {
49                 occluderTypeAircraft,
50                 occluderTypeAI,
51                 occluderTypeTileObject
52         } OccluderType;
53
54         void init(SGPropertyNode *sim_rendering_options);
55         void startOfFrame(void);
56         void addOccluder(ssgBranch *occluder, OccluderType occluder_type, ssgBranch *tile=0);
57         void deleteOccluder(ssgBranch *occluder);
58         void deleteOccluderFromTile(ssgBranch *tile);
59         void setupShadows(double lon, double lat,
60                 double gst, double SunRightAscension, double SunDeclination, double sunAngle );
61         void endOfFrame(void);
62         static int ACpostTravCB( ssgEntity *entity, int traversal_mask );
63
64 private:
65
66         class ShadowCaster
67         {
68         public:
69                 typedef struct {
70                         sgVec4 planeEquations;
71                         int neighbourIndices[3];
72                         bool isSilhouetteEdge[3];
73                         bool isFacingLight;
74                 } triData;
75
76                 ssgSharedPtr<ssgBranch> geometry_leaf;
77                 ssgSharedPtr<ssgBranch> scenery_object;
78                 ssgSharedPtr<ssgBranch> lib_object;
79                 ssgSharedPtr<ssgBranch> first_select;
80                 sgVec3 last_lightpos;
81                 sgMat4 last_transform;
82                 int frameNumber;
83
84                 int *indices;
85                 int numTriangles;
86                 triData *triangles;
87
88                 sgVec4 * vertices;
89                 GLushort *silhouetteEdgeIndices;
90                 int lastSilhouetteIndicesCount;
91                 bool isTranslucent;
92
93                 ShadowCaster( int _num_tri, ssgBranch * _geometry_leaf );
94                 ~ShadowCaster();
95                 void addLeaf( int & tri_idx, int & ind_idx, ssgLeaf *_geometry_leaf );
96                 void SetConnectivity();
97                 void CalculateSilhouetteEdges(sgVec3 lightPosition);
98                 void DrawInfiniteShadowVolume(sgVec3 lightPosition, bool drawCaps);
99                 void computeShadows(sgMat4 rotation, sgMat4 rotation_translation, OccluderType occluder_type);
100                 void getNetTransform ( ssgBranch * branch, sgMat4 xform );
101                 bool isSelected (  ssgBranch * branch, float dist);
102
103                 bool sameVertex(int edge1, int edge2);
104         };
105         typedef vector<ShadowCaster *> ShadowCaster_list;
106
107         class SceneryObject {
108         public:
109                 SceneryObject(ssgBranch *_scenery_object, OccluderType _occluder_type);
110                 ~SceneryObject();
111                 void computeShadows(void);
112                 void traverseTree(ssgBranch *branch);
113                 void find_trans(void);
114                 ssgSharedPtr<ssgBranch> scenery_object;
115                 ssgSharedPtr<ssgBranch> lib_object;
116                 ssgSharedPtr<ssgBranch> pending_object;
117                 ssgSharedPtr<ssgBranch> tile;
118                 ShadowCaster_list parts;
119                 OccluderType occluder_type;
120         };
121         typedef map<ssgSharedPtr<ssgBranch>, SceneryObject *> SceneryObject_map;
122
123
124 private:
125         void update_light_view(void);
126         void computeShadows(void);
127         void cull ( ssgBranch *b, sgFrustum *f, sgMat4 m, int test_needed );
128
129         bool    shadows_enabled;
130         bool    shadowsAC_enabled, shadowsAI_enabled, shadowsTO_enabled, shadowsDebug_enabled;
131         bool    shadowsAC_transp_enabled;
132         bool    use_alpha;
133         bool    canDoAlpha, canDoStencil;
134         SGPropertyNode_ptr sim_rendering;
135
136         sgVec3 sunPos;
137         int frameNumber;
138         int lastTraverseTreeFrame;
139         sgMat4 CameraViewM;
140         double  sun_angle;
141         SceneryObject_map sceneryObjects;
142         ssgSharedPtr<ssgBranch> ssg_root;
143         bool shadows_rendered;
144 };
145
146 #endif // _SHADOWVOLUME_HXX