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