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