]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/CloudShaderGeometry.hxx
Stuart:
[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
35 #include <simgear/math/SGMath.hxx>
36
37
38 namespace simgear
39 {
40
41 class CloudShaderGeometry : public osg::Drawable
42 {
43     public:
44         
45         const static unsigned int CLOUD_HEIGHT = 10;
46         const static unsigned int TEXTURE_INDEX_X = 11;
47         const static unsigned int TEXTURE_INDEX_Y = 12;
48         const static unsigned int WIDTH = 13;
49         const static unsigned int HEIGHT = 14;
50         const static unsigned int SHADE = 15;
51         
52         CloudShaderGeometry()
53         { 
54             setUseDisplayList(false); 
55             skip_info = new SkipInfo();
56         }
57
58         CloudShaderGeometry(int vx, int vy) :
59             varieties_x(vx), varieties_y(vy)
60         { 
61             setUseDisplayList(false); 
62             skip_info = new SkipInfo();
63         }
64         
65         /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
66         CloudShaderGeometry(const CloudShaderGeometry& CloudShaderGeometry,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
67             osg::Drawable(CloudShaderGeometry,copyop) {}
68
69         META_Object(flightgear, CloudShaderGeometry);
70         
71         struct SkipInfo {
72             SkipInfo() : skip_count(0), skip_limit(1) {}
73             int skip_count;
74             int skip_limit;
75         };
76         
77         SkipInfo* skip_info;
78         
79         struct CloudSprite {
80             CloudSprite(SGVec3f& p, int tx, int ty, float w, float h, float s, float ch) :
81                     position(p), texture_index_x(tx), texture_index_y(ty), width(w), height(h), shade(s), cloud_height(ch)
82                     { }
83         
84                     SGVec3f position;
85                     int texture_index_x;
86                     int texture_index_y;
87                     float width;
88                     float height;
89                     float shade;
90                     float cloud_height;
91         };
92         
93         typedef std::vector<CloudSprite*> CloudSpriteList;
94         
95         void insert(CloudSprite* t)
96         { _cloudsprites.push_back(t); }
97         void insert(SGVec3f& p, int tx, int ty, float w, float h, float s, float ch)
98         { insert(new CloudSprite(p, tx, ty, w, h, s, ch)); }
99         
100         unsigned getNumCloudSprite() const
101         { return _cloudsprites.size(); }
102         CloudSprite* getCloudSprite(unsigned i) const
103         { return _cloudsprites[i]; }
104         CloudSpriteList _cloudsprites;
105         
106         typedef std::vector<osg::Vec4> PositionSizeList;
107         
108         virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
109         virtual osg::BoundingBox computeBound() const;
110     
111         
112         void setGeometry(osg::Drawable* geometry)
113         {
114             _geometry = geometry;
115         }
116         
117         void addSprite(SGVec3f& p, int tx, int ty, float w, float h, float s, float cull, float cloud_height)
118         {
119             // Only add the sprite if it is further than the cull distance to all other sprites
120             for (CloudShaderGeometry::CloudSpriteList::iterator iter = _cloudsprites.begin();
121                  iter != _cloudsprites.end();
122                  ++iter) 
123             {
124                 if (distSqr((*iter)->position, p) < cull)
125                 {
126                     // Too close - cull it
127                     return;
128                 }
129             }
130
131             _cloudsprites.push_back(new CloudSprite(p, tx, ty, w, h, s, cloud_height));
132         }
133         
134         osg::ref_ptr<osg::Drawable> _geometry;
135
136         int varieties_x;
137         int varieties_y;
138         
139     protected:
140     
141         virtual ~CloudShaderGeometry() {
142             delete skip_info;
143             for (int i = 0; i < _cloudsprites.size(); i++)
144             {
145                 delete _cloudsprites[i];
146             }
147         }
148 };
149
150 }
151 #endif