]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/CloudShaderGeometry.hxx
Fixes for headless mode.
[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         const static unsigned int USR_ATTR_3 = 12;
50                 
51         CloudShaderGeometry()
52         { 
53             setUseDisplayList(false); 
54         }
55
56         CloudShaderGeometry(int vx, int vy, float width, float height, float ts, float ms, float bs, float shade, float ch, float zsc) :
57             varieties_x(vx), varieties_y(vy), top_factor(ts), middle_factor(ms), bottom_factor(bs), shade_factor(shade), cloud_height(ch), zscale(zsc)
58         { 
59             setUseDisplayList(false); 
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 CloudSprite {
73             CloudSprite(const SGVec3f& p, int tx, int ty, float w, float h) :
74                     position(p), texture_index_x(tx), texture_index_y(ty), width(w), height(h)
75                     { }
76         
77                     SGVec3f position;
78                     int texture_index_x;
79                     int texture_index_y;
80                     float width;
81                     float height;
82         };
83         
84         typedef std::vector<CloudSprite> CloudSpriteList;
85         CloudSpriteList _cloudsprites;
86         
87         void insert(const CloudSprite& t)
88         { _cloudsprites.push_back(t); }
89         void insert(SGVec3f& p, int tx, int ty, float w, float h)
90         { insert(CloudSprite(p, tx, ty, w, h)); }
91         
92         unsigned getNumCloudSprite() const
93         { return _cloudsprites.size(); }
94         CloudSprite& getCloudSprite(unsigned i)
95         { return _cloudsprites[i]; }
96         
97         virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
98         virtual osg::BoundingBox computeBound() const
99         {
100             return _bbox;
101         }
102         
103         void setGeometry(osg::Drawable* geometry)
104         {
105             _geometry = geometry;
106         }
107         
108         void addSprite(const SGVec3f& p, int tx, int ty, float w, float h, float cull);
109                 
110         osg::ref_ptr<osg::Drawable> _geometry;
111
112         int varieties_x;
113         int varieties_y;
114         float top_factor;
115         float middle_factor;
116         float bottom_factor;
117         float shade_factor;
118         float cloud_height;
119         float zscale;
120                 
121         // Bounding box extents.
122         osg::BoundingBox _bbox;
123         
124     struct SortData
125     {
126         struct SortItem
127         {
128             SortItem(size_t idx_, float depth_) : idx(idx_), depth(depth_) {}
129             SortItem() : idx(0), depth(0.0f) {}
130             size_t idx;
131             float depth;
132         };
133         SortData() : frameSorted(0), skip_limit(1), spriteIdx(0) {}
134         int frameSorted;
135         int skip_limit;
136         // This will be sorted by Z distance
137         typedef std::vector<SortItem> SortItemList;
138         SortItemList* spriteIdx;
139     };
140 protected:
141     mutable osg::buffered_object<SortData> _sortData;
142     
143     virtual ~CloudShaderGeometry()
144     {
145         for (unsigned int i = 0; i < _sortData.size(); ++i)
146             delete _sortData[i].spriteIdx;
147     }
148 };
149
150 }
151 #endif