3 * Copyright (C) 2008 Stuart Buchanan
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.
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.
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,
24 #include <osgDB/Registry>
25 #include <osgDB/Input>
26 #include <osgDB/ParameterOutput>
28 #include "CloudShaderGeometry.hxx"
30 #include <simgear/props/props.hxx>
33 using namespace osgDB;
34 using namespace simgear;
40 bool operator() (const CloudShaderGeometry::SortData::SortItem& lhs,
41 const CloudShaderGeometry::SortData::SortItem& rhs) const
43 return lhs.depth > rhs.depth;
49 void CloudShaderGeometry::drawImplementation(RenderInfo& renderInfo) const
51 if (!_cloudsprites.size()) return;
53 osg::State& state = *renderInfo.getState();
54 unsigned int contextID = state.getContextID();
55 SortData& sortData = _sortData[contextID];
56 int frameNumber = state.getFrameStamp()->getFrameNumber();
58 if (!sortData.spriteIdx)
59 sortData.spriteIdx = new SortData::SortItemList;
60 if (sortData.spriteIdx->size() < _cloudsprites.size()) {
61 for (unsigned i = sortData.spriteIdx->size(); i < (unsigned)_cloudsprites.size(); ++i)
62 sortData.spriteIdx->push_back(SortData::SortItem(i, 0.0f));
63 sortData.frameSorted = frameNumber - (sortData.skip_limit + 1);
65 // If the cloud is already sorted, then it is likely to still be sorted.
66 // Therefore we can avoid re-sorting it for a period. If it is still
67 // sorted after that period, then we can wait for a longer period before
68 // checking again. In this way, only clouds that are changing regularly
70 if (frameNumber - sortData.skip_limit >= sortData.frameSorted) {
71 Matrix mvp = state.getModelViewMatrix() * state.getProjectionMatrix();
72 for (SortData::SortItemList::iterator itr = sortData.spriteIdx->begin(),
73 end = sortData.spriteIdx->end();
77 = Vec4f(toOsg(_cloudsprites[itr->idx].position), 1.0f) * mvp;
78 itr->depth = projPos.z() / projPos.w();
81 if (std::adjacent_find(sortData.spriteIdx->rbegin(),
82 sortData.spriteIdx->rend(), SpriteComp())
83 == sortData.spriteIdx->rend()) {
84 // This cloud is sorted, so no need to re-sort.
85 sortData.skip_limit = sortData.skip_limit * 2;
86 if (sortData.skip_limit > 30) {
87 // Jitter the skip frames to avoid synchronized sorts
88 // which will cause periodic frame-rate drops
89 sortData.skip_limit += sg_random() * 10;
91 if (sortData.skip_limit > 128) {
92 // Maximum of every 128 frames (2 - 4 seconds)
93 sortData.skip_limit = 128 + sg_random() * 10;
97 std::sort(sortData.spriteIdx->begin(), sortData.spriteIdx->end(),
99 sortData.skip_limit = 1;
101 sortData.frameSorted = frameNumber;
104 const Extensions* extensions = getExtensions(state.getContextID(),true);
106 for(SortData::SortItemList::const_iterator itr = sortData.spriteIdx->begin(),
107 end = sortData.spriteIdx->end();
110 const CloudSprite& t = _cloudsprites[itr->idx];
111 GLfloat ua1[3] = { (GLfloat) t.texture_index_x/varieties_x,
112 (GLfloat) t.texture_index_y/varieties_y,
114 GLfloat ua2[3] = { (GLfloat) t.height,
116 (GLfloat) t.cloud_height };
117 extensions->glVertexAttrib3fv(USR_ATTR_1, ua1 );
118 extensions->glVertexAttrib3fv(USR_ATTR_2, ua2 );
119 glColor4f(t.position.x(), t.position.y(), t.position.z(), 1.0);
120 _geometry->draw(renderInfo);
124 void CloudShaderGeometry::addSprite(const SGVec3f& p, int tx, int ty,
126 float s, float cull, float cloud_height)
128 // Only add the sprite if it is further than the cull distance to all other sprites
129 // except for the center sprite.
130 for (CloudShaderGeometry::CloudSpriteList::iterator iter = _cloudsprites.begin();
131 iter != _cloudsprites.end();
134 if ((iter != _cloudsprites.begin()) &&
135 (distSqr(iter->position, p) < cull)) {
136 // Too close - cull it
141 _cloudsprites.push_back(CloudSprite(p, tx, ty, w, h, s, cloud_height));
144 bool CloudShaderGeometry_readLocalData(Object& obj, Input& fr)
146 bool iteratorAdvanced = false;
148 CloudShaderGeometry& geom = static_cast<CloudShaderGeometry&>(obj);
150 if ((fr[0].matchWord("geometry"))) {
152 iteratorAdvanced = true;
153 osg::Drawable* drawable = fr.readDrawable();
155 geom._geometry = drawable;
158 if ((fr.matchSequence("instances %i"))) {
159 int entry = fr[0].getNoNestedBrackets();
161 fr[1].getInt(capacity);
162 geom._cloudsprites.reserve(capacity);
164 iteratorAdvanced = true;
166 while (!fr.eof() && fr[0].getNoNestedBrackets() > entry) {
170 if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y())
171 && fr[2].getFloat(v.z()) && fr[3].getInt(tx) && fr[4].getInt(ty) &&
172 fr[5].getFloat(w) && fr[6].getFloat(h)&& fr[7].getFloat(s) && fr[8].getFloat(ch)) {
174 //SGVec3f* v = new SGVec3f(v.x(), v.y(), v.z());
175 geom._cloudsprites.push_back(CloudShaderGeometry::CloudSprite(v, tx, ty, w, h,s,ch));
181 return iteratorAdvanced;
184 bool CloudShaderGeometry_writeLocalData(const Object& obj, Output& fw)
186 const CloudShaderGeometry& geom = static_cast<const CloudShaderGeometry&>(obj);
188 fw.indent() << "geometry" << std::endl;
189 fw.writeObject(*geom._geometry);
190 fw.indent() << "instances " << geom._cloudsprites.size() << std::endl;
191 fw.indent() << "{" << std::endl;
193 for (CloudShaderGeometry::CloudSpriteList::const_iterator itr
194 = geom._cloudsprites.begin();
195 itr != geom._cloudsprites.end();
197 fw.indent() << itr->position.x() << " " << itr->position.y() << " "
198 << itr->position.z() << " " << itr->texture_index_x << " "
199 << itr->texture_index_y << " " << itr->width << " "
200 << itr->height << " " << itr->shade
201 << itr->cloud_height << " "<< std::endl;
204 fw.indent() << "}" << std::endl;
209 osgDB::RegisterDotOsgWrapperProxy cloudShaderGeometryProxy
211 new CloudShaderGeometry,
212 "CloudShaderGeometry",
213 "Object Drawable CloudShaderGeometry",
214 &CloudShaderGeometry_readLocalData,
215 &CloudShaderGeometry_writeLocalData