]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/CloudShaderGeometry.hxx
8a9c31c5172faea4c004a602fcee972fc58df3d7
[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
36 #include <simgear/math/SGMath.hxx>
37 #include <simgear/math/sg_random.h>
38
39
40 namespace simgear
41 {
42
43 class CloudShaderGeometry : public osg::Drawable
44 {
45     public:
46         
47         const static unsigned int USR_ATTR_1 = 10;
48         const static unsigned int USR_ATTR_2 = 11;
49         
50         CloudShaderGeometry()
51         { 
52             setUseDisplayList(false); 
53         }
54
55         CloudShaderGeometry(int vx, int vy, float width, float height) :
56             varieties_x(vx), varieties_y(vy)
57         { 
58             setUseDisplayList(false); 
59             float x = width/2.0f;
60             float z = height/2.0f;
61             _bbox.expandBy(-x, -x, -z);
62             _bbox.expandBy(x, x, z);
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 CloudSprite {
72             CloudSprite(SGVec3f& p, int tx, int ty, float w, float h, float s, float ch) :
73                     position(p), texture_index_x(tx), texture_index_y(ty), width(w), height(h), shade(s), cloud_height(ch)
74                     { }
75         
76                     SGVec3f position;
77                     int texture_index_x;
78                     int texture_index_y;
79                     float width;
80                     float height;
81                     float shade;
82                     float cloud_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, float s, float ch)
91         { insert(CloudSprite(p, tx, ty, w, h, s, ch)); }
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 computeBound() const
100         {
101             return _bbox;
102         }
103         
104         void setGeometry(osg::Drawable* geometry)
105         {
106             _geometry = geometry;
107         }
108         
109     void addSprite(SGVec3f& p, int tx, int ty, float w, float h, float s, float cull, float cloud_height);
110                 
111         osg::ref_ptr<osg::Drawable> _geometry;
112
113         int varieties_x;
114         int varieties_y;
115         
116         // Bounding box extents.
117         osg::BoundingBox _bbox;
118         
119     struct SortData
120     {
121         struct SortItem
122         {
123             SortItem(size_t idx_, float depth_) : idx(idx_), depth(depth_) {}
124             SortItem() : idx(0), depth(0.0f) {}
125             size_t idx;
126             float depth;
127         };
128         SortData() : frameSorted(0), skip_limit(1), spriteIdx(0) {}
129         int frameSorted;
130         int skip_limit;
131         // This will be sorted by Z distance
132         typedef std::vector<SortItem> SortItemList;
133         SortItemList* spriteIdx;
134     };
135 protected:
136     mutable osg::buffered_object<SortData> _sortData;
137     
138     virtual ~CloudShaderGeometry()
139     {
140         for (unsigned int i = 0; i < _sortData.size(); ++i)
141             delete _sortData[i].spriteIdx;
142     }
143 };
144
145 }
146 #endif