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 for (CloudShaderGeometry::CloudSpriteList::iterator iter = _cloudsprites.begin();
130 iter != _cloudsprites.end();
133 if (distSqr(iter->position, p) < cull) {
134 // Too close - cull it
138 _cloudsprites.push_back(CloudSprite(p, tx, ty, w, h, s, cloud_height));
141 bool CloudShaderGeometry_readLocalData(Object& obj, Input& fr)
143 bool iteratorAdvanced = false;
145 CloudShaderGeometry& geom = static_cast<CloudShaderGeometry&>(obj);
147 if ((fr[0].matchWord("geometry"))) {
149 iteratorAdvanced = true;
150 osg::Drawable* drawable = fr.readDrawable();
152 geom._geometry = drawable;
155 if ((fr.matchSequence("instances %i"))) {
156 int entry = fr[0].getNoNestedBrackets();
158 fr[1].getInt(capacity);
159 geom._cloudsprites.reserve(capacity);
161 iteratorAdvanced = true;
163 while (!fr.eof() && fr[0].getNoNestedBrackets() > entry) {
167 if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y())
168 && fr[2].getFloat(v.z()) && fr[3].getInt(tx) && fr[4].getInt(ty) &&
169 fr[5].getFloat(w) && fr[6].getFloat(h)&& fr[7].getFloat(s) && fr[8].getFloat(ch)) {
171 //SGVec3f* v = new SGVec3f(v.x(), v.y(), v.z());
172 geom._cloudsprites.push_back(CloudShaderGeometry::CloudSprite(v, tx, ty, w, h,s,ch));
178 return iteratorAdvanced;
181 bool CloudShaderGeometry_writeLocalData(const Object& obj, Output& fw)
183 const CloudShaderGeometry& geom = static_cast<const CloudShaderGeometry&>(obj);
185 fw.indent() << "geometry" << std::endl;
186 fw.writeObject(*geom._geometry);
187 fw.indent() << "instances " << geom._cloudsprites.size() << std::endl;
188 fw.indent() << "{" << std::endl;
190 for (CloudShaderGeometry::CloudSpriteList::const_iterator itr
191 = geom._cloudsprites.begin();
192 itr != geom._cloudsprites.end();
194 fw.indent() << itr->position.x() << " " << itr->position.y() << " "
195 << itr->position.z() << " " << itr->texture_index_x << " "
196 << itr->texture_index_y << " " << itr->width << " "
197 << itr->height << " " << itr->shade
198 << itr->cloud_height << " "<< std::endl;
201 fw.indent() << "}" << std::endl;
206 osgDB::RegisterDotOsgWrapperProxy cloudShaderGeometryProxy
208 new CloudShaderGeometry,
209 "CloudShaderGeometry",
210 "Object Drawable CloudShaderGeometry",
211 &CloudShaderGeometry_readLocalData,
212 &CloudShaderGeometry_writeLocalData