]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/shadowvolume.hxx
Better encapsulation for personality
[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/structure/ssgSharedPtr.hxx>
28 #include <simgear/props/props.hxx>
29
30 #include <plib/ssg.h>
31 #include <plib/sg.h>
32
33 #include <vector>
34 #include <map>
35
36 SG_USING_STD(vector);
37 SG_USING_STD(map);
38
39 class ssgBranch;
40 class ssgLeaf;
41 class SGPropertyNode;
42
43 /**
44  * A class wich add shadows in a postprocess stage.
45  */
46 class SGShadowVolume {
47
48 public:
49         SGShadowVolume( ssgBranch *root );
50         ~SGShadowVolume();
51
52         typedef enum {
53                 occluderTypeAircraft,
54                 occluderTypeAI,
55                 occluderTypeTileObject
56         } OccluderType;
57
58         void init(SGPropertyNode *sim_rendering_options);
59         void startOfFrame(void);
60         void addOccluder(ssgBranch *occluder, OccluderType occluder_type, ssgBranch *tile=0);
61         void deleteOccluder(ssgBranch *occluder);
62         void deleteOccluderFromTile(ssgBranch *tile);
63         void setupShadows(double lon, double lat,
64                 double gst, double SunRightAscension, double SunDeclination, double sunAngle );
65         void endOfFrame(void);
66         static int ACpostTravCB( ssgEntity *entity, int traversal_mask );
67
68 private:
69
70         class ShadowCaster
71         {
72         public:
73                 typedef struct {
74                         sgVec4 planeEquations;
75                         int neighbourIndices[3];
76                         bool isSilhouetteEdge[3];
77                         bool isFacingLight;
78                 } triData;
79
80                 ssgSharedPtr<ssgBranch> geometry_leaf;
81                 ssgSharedPtr<ssgBranch> scenery_object;
82                 ssgSharedPtr<ssgBranch> lib_object;
83                 ssgSharedPtr<ssgBranch> first_select;
84                 sgVec3 last_lightpos;
85                 sgMat4 last_transform;
86                 int frameNumber;
87
88                 int *indices;
89                 int numTriangles;
90                 triData *triangles;
91
92                 sgVec4 * vertices;
93                 GLushort *silhouetteEdgeIndices;
94                 int lastSilhouetteIndicesCount;
95                 bool isTranslucent;
96
97                 ShadowCaster( int _num_tri, ssgBranch * _geometry_leaf );
98                 ~ShadowCaster();
99                 void addLeaf( int & tri_idx, int & ind_idx, ssgLeaf *_geometry_leaf );
100                 void SetConnectivity();
101                 void CalculateSilhouetteEdges(sgVec3 lightPosition);
102                 void DrawInfiniteShadowVolume(sgVec3 lightPosition, bool drawCaps);
103                 void computeShadows(sgMat4 rotation, sgMat4 rotation_translation, OccluderType occluder_type);
104                 void getNetTransform ( ssgBranch * branch, sgMat4 xform );
105                 bool isSelected (  ssgBranch * branch, float dist);
106
107                 bool sameVertex(int edge1, int edge2);
108         };
109         typedef vector<ShadowCaster *> ShadowCaster_list;
110
111         class SceneryObject {
112         public:
113                 SceneryObject(ssgBranch *_scenery_object, OccluderType _occluder_type);
114                 ~SceneryObject();
115                 void computeShadows(void);
116                 void traverseTree(ssgBranch *branch);
117                 void find_trans(void);
118                 ssgSharedPtr<ssgBranch> scenery_object;
119                 ssgSharedPtr<ssgBranch> lib_object;
120                 ssgSharedPtr<ssgBranch> pending_object;
121                 ssgSharedPtr<ssgBranch> tile;
122                 ShadowCaster_list parts;
123                 OccluderType occluder_type;
124         };
125         typedef map<ssgSharedPtr<ssgBranch>, SceneryObject *> SceneryObject_map;
126
127
128 private:
129         void update_light_view(void);
130         void computeShadows(void);
131         void cull ( ssgBranch *b, sgFrustum *f, sgMat4 m, int test_needed );
132
133         bool    shadows_enabled;
134         bool    shadowsAC_enabled, shadowsAI_enabled, shadowsTO_enabled, shadowsDebug_enabled;
135         bool    shadowsAC_transp_enabled;
136         bool    use_alpha;
137         bool    canDoAlpha, canDoStencil;
138         SGPropertyNode_ptr sim_rendering;
139
140         sgVec3 sunPos;
141         int frameNumber;
142         int lastTraverseTreeFrame;
143         sgMat4 CameraViewM;
144         double  sun_angle;
145         SceneryObject_map sceneryObjects;
146         ssgSharedPtr<ssgBranch> ssg_root;
147         bool shadows_rendered;
148 };
149
150 #endif // _SHADOWVOLUME_HXX