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