]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/CloudShaderGeometry.hxx
Added some OSG headers for the correct evaluation of the OSG_VERSION_LESS_THAN macro.
[simgear.git] / simgear / scene / sky / CloudShaderGeometry.hxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2008 Stuart Buchanan
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18  * MA 02110-1301, USA.
19  *
20  */
21
22 #ifndef CLOUD_SHADER_GEOMETRY_HXX
23 #define CLOUD_SHADER_GEOMETRY_HXX 1
24
25 #include <vector>
26
27 #include <osg/BoundingBox>
28 #include <osg/CopyOp>
29 #include <osg/Drawable>
30 #include <osg/Geometry>
31 #include <osg/RenderInfo>
32 #include <osg/Vec3>
33 #include <osg/Vec4>
34 #include <osg/buffered_value>
35 #include <osg/Version>
36
37 #include <simgear/math/SGMath.hxx>
38 #include <simgear/math/sg_random.h>
39
40
41 namespace simgear
42 {
43
44 class CloudShaderGeometry : public osg::Drawable
45 {
46     public:
47         
48         const static unsigned int USR_ATTR_1 = 10;
49         const static unsigned int USR_ATTR_2 = 11;
50         const static unsigned int USR_ATTR_3 = 12;        
51                 
52         CloudShaderGeometry()
53         { 
54             setUseDisplayList(false); 
55         }
56
57         CloudShaderGeometry(int vx, int vy, float width, float height, float ts, float ms, float bs, float shade, float ch, float zsc) :
58             varieties_x(vx), varieties_y(vy), top_factor(ts), middle_factor(ms), bottom_factor(bs), shade_factor(shade), cloud_height(ch), zscale(zsc)
59         { 
60             setUseDisplayList(false); 
61             float x = width/2.0f;
62             float z = height/2.0f;
63             _bbox.expandBy(-x, -x, -z);
64             _bbox.expandBy(x, x, z);
65         }
66         
67         /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
68         CloudShaderGeometry(const CloudShaderGeometry& CloudShaderGeometry,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
69             osg::Drawable(CloudShaderGeometry,copyop) {}
70
71         META_Object(flightgear, CloudShaderGeometry);
72         
73         struct CloudSprite {
74             CloudSprite(const SGVec3f& p, int tx, int ty, float w, float h) :
75                     position(p), texture_index_x(tx), texture_index_y(ty), width(w), height(h)
76                     { }
77         
78                     SGVec3f position;
79                     int texture_index_x;
80                     int texture_index_y;
81                     float width;
82                     float height;
83         };
84         
85         typedef std::vector<CloudSprite> CloudSpriteList;
86         CloudSpriteList _cloudsprites;
87         
88         void insert(const CloudSprite& t)
89         { _cloudsprites.push_back(t); }
90         void insert(SGVec3f& p, int tx, int ty, float w, float h)
91         { insert(CloudSprite(p, tx, ty, w, h)); }
92         
93         unsigned getNumCloudSprite() const
94         { return _cloudsprites.size(); }
95         CloudSprite& getCloudSprite(unsigned i)
96         { return _cloudsprites[i]; }
97         
98         virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
99         virtual osg::BoundingBox
100 #if OSG_VERSION_LESS_THAN(3,3,2)
101         computeBound()
102 #else
103         computeBoundingBox()
104 #endif
105         const
106         {
107             return _bbox;
108         }
109         
110         void addSprite(const SGVec3f& p, int tx, int ty, float w, float h, float cull);
111         void generateGeometry();
112         void rebuildGeometry();
113                 
114         osg::ref_ptr<osg::Drawable> _geometry;
115
116         int varieties_x;
117         int varieties_y;
118         float top_factor;
119         float middle_factor;
120         float bottom_factor;
121         float shade_factor;
122         float cloud_height;
123         float zscale;
124                 
125         // Bounding box extents.
126         osg::BoundingBox _bbox;
127         
128     struct SortData
129     {
130         struct SortItem
131         {
132             SortItem(size_t idx_, float depth_) : idx(idx_), depth(depth_) {}
133             SortItem() : idx(0), depth(0.0f) {}
134             size_t idx;
135             float depth;
136         };
137         SortData() : frameSorted(0), skip_limit(1), spriteIdx(0) {}
138         int frameSorted;
139         int skip_limit;
140         // This will be sorted by Z distance
141         typedef std::vector<SortItem> SortItemList;
142         SortItemList* spriteIdx;
143     };
144 protected:
145     mutable osg::buffered_object<SortData> _sortData;
146     
147     virtual ~CloudShaderGeometry()
148     {
149         for (unsigned int i = 0; i < _sortData.size(); ++i)
150             delete _sortData[i].spriteIdx;
151     }
152 };
153
154 }
155 #endif