]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/CloudShaderGeometry.cxx
Stuart:
[simgear.git] / simgear / scene / sky / CloudShaderGeometry.cxx
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 #include <osgDB/Registry>
23 #include <osgDB/Input>
24 #include <osgDB/ParameterOutput>
25
26 #include "CloudShaderGeometry.hxx"
27
28 #include <simgear/props/props.hxx>
29
30 using namespace osg;
31 using namespace osgDB;
32
33 namespace simgear
34 {
35 void CloudShaderGeometry::drawImplementation(RenderInfo& renderInfo) const
36 {
37     if (!_cloudsprites.size()) return;
38     
39     osg::State& state = *renderInfo.getState();
40     
41     // If the cloud is already sorted, then it is likely to still be sorted.
42     // Therefore we can avoid re-sorting it for a period. If it is still
43     // sorted after that period, then we can wait for a longer period before
44     // checking again. In this way, only clouds that are changing regularly
45     // are sorted.
46     
47     skip_info->skip_count = (skip_info->skip_count + 1) % skip_info->skip_limit;
48     
49     if (skip_info->skip_count == 0)
50     {
51         osg::Matrix vm = state.getModelViewMatrix();
52         bool sorted = true;
53         
54         // Transform the viewing direction, represented by the eye space vector (0,0,-1, 0), into model-space
55         // (here we simply take the opposite direction and reverse the ordering when sorting)
56         osg::Vec3f view_dir(vm(0, 2), vm(1, 2), vm(2, 2));      // Caveat: OpenSceneGraph matrices are transposed!
57     
58         float p = view_dir*_cloudsprites[0]->position.osg();
59         // Do a single iteration of a bubble sort, sorting
60         // back to front.
61         for(int i = 0; i < _cloudsprites.size() - 1; i++)
62         {
63             float q = view_dir*_cloudsprites[i+1]->position.osg();
64             if (p > q) {  
65                 CloudSprite c = *_cloudsprites[i];
66                 *_cloudsprites[i] = *_cloudsprites[i+1];
67                 *_cloudsprites[i+1] = c;
68                 
69                 sorted = false;
70             }
71             else
72                 p = q;
73         }
74         
75         if (sorted)
76         {
77             // This cloud is sorted, so no need to re-sort.
78             // Maximum of every 128 frames (2 - 4 seconds)
79             skip_info->skip_limit = skip_info->skip_limit * 2;
80             if (skip_info->skip_limit > 128) 
81             {
82                 skip_info->skip_limit = 128;
83             }
84         }
85         else
86         {
87             // This cloud is unsorted, so we need to sort next frame
88             skip_info->skip_limit = 1;
89         }
90     }
91
92     const Extensions* extensions = getExtensions(state.getContextID(),true);
93
94     for(CloudSpriteList::const_iterator t = _cloudsprites.begin(); t != _cloudsprites.end(); ++t)
95     {
96         extensions->glVertexAttrib1f(TEXTURE_INDEX_X, (GLfloat) (*t)->texture_index_x/varieties_x);
97         extensions->glVertexAttrib1f(TEXTURE_INDEX_Y, (GLfloat) (*t)->texture_index_y/varieties_y);
98         extensions->glVertexAttrib1f(WIDTH, (GLfloat) (*t)->width);
99         extensions->glVertexAttrib1f(HEIGHT, (GLfloat) (*t)->height);
100         extensions->glVertexAttrib1f(SHADE, (GLfloat) (*t)->shade);
101         extensions->glVertexAttrib1f(CLOUD_HEIGHT, (GLfloat) (*t)->cloud_height);
102         glColor4f((*t)->position.x(), (*t)->position.y(), (*t)->position.z(), 1.0);
103         _geometry->draw(renderInfo);
104     }
105 }
106     
107 BoundingBox CloudShaderGeometry::computeBound() const
108 {
109     BoundingBox geom_box = _geometry->getBound();
110     BoundingBox bb;
111     for(CloudSpriteList::const_iterator itr = _cloudsprites.begin();
112         itr != _cloudsprites.end();
113         ++itr) {
114          bb.expandBy(geom_box.corner(0)*(*itr)->width +
115                  osg::Vec3( (*itr)->position.x(), (*itr)->position.y(), (*itr)->position.z() ));
116          bb.expandBy(geom_box.corner(7)*(*itr)->height +
117                  osg::Vec3( (*itr)->position.x(), (*itr)->position.y(), (*itr)->position.z() ));
118     }
119     return bb;
120 }
121
122 bool CloudShaderGeometry_readLocalData(Object& obj, Input& fr)
123 {
124     bool iteratorAdvanced = false;
125
126     CloudShaderGeometry& geom = static_cast<CloudShaderGeometry&>(obj);
127
128     if ((fr[0].matchWord("geometry"))) {
129         ++fr;
130         iteratorAdvanced = true;
131         osg::Drawable* drawable = fr.readDrawable();
132         if (drawable) {
133             geom._geometry = drawable;
134         }
135     }
136     if ((fr.matchSequence("instances %i"))) {
137         int entry = fr[0].getNoNestedBrackets();
138         int capacity;
139         fr[1].getInt(capacity);
140         geom._cloudsprites.reserve(capacity);
141         fr += 3;
142         iteratorAdvanced = true;
143         // skip {
144         while (!fr.eof() && fr[0].getNoNestedBrackets() > entry) {
145             SGVec3f v;
146             int tx, ty;
147             float w, h, s, ch;
148             if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y())
149                 && fr[2].getFloat(v.z()) && fr[3].getInt(tx) && fr[4].getInt(ty) &&  
150                 fr[5].getFloat(w) && fr[6].getFloat(h)&& fr[7].getFloat(s) && fr[8].getFloat(ch)) {
151                     fr += 5;
152                     //SGVec3f* v = new SGVec3f(v.x(), v.y(), v.z());
153                     geom._cloudsprites.push_back(new CloudShaderGeometry::CloudSprite(v, tx, ty, w, h,s,ch));
154             } else {
155                 ++fr;
156             }
157         }
158     }
159     return iteratorAdvanced;
160 }
161
162 bool CloudShaderGeometry_writeLocalData(const Object& obj, Output& fw)
163 {
164     const CloudShaderGeometry& geom = static_cast<const CloudShaderGeometry&>(obj);
165
166     fw.indent() << "geometry" << std::endl;
167     fw.writeObject(*geom._geometry);
168     fw.indent() << "instances " << geom._cloudsprites.size() << std::endl;
169     fw.indent() << "{" << std::endl;
170     fw.moveIn();
171     for (CloudShaderGeometry::CloudSpriteList::const_iterator itr
172              = geom._cloudsprites.begin();
173          itr != geom._cloudsprites.end();
174          ++itr) {
175              fw.indent() << (*itr)->position.x() << " " << (*itr)->position.y() << " " 
176                      << (*itr)->position.z() << " " << (*itr)->texture_index_x << " "
177                      << (*itr)->texture_index_y << " "  << (*itr)->width << " " 
178                      << (*itr)->height << " " << (*itr)->shade 
179                      << (*itr)->cloud_height << " "<< std::endl;
180     }
181     fw.moveOut();
182     fw.indent() << "}" << std::endl;
183     return true;
184 }
185
186
187 osgDB::RegisterDotOsgWrapperProxy cloudShaderGeometryProxy
188 (
189     new CloudShaderGeometry,
190     "CloudShaderGeometry",
191     "Object Drawable CloudShaderGeometry",
192     &CloudShaderGeometry_readLocalData,
193     &CloudShaderGeometry_writeLocalData
194     );
195 }