00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <osgDB/Registry>
00023 #include <osgDB/Input>
00024 #include <osgDB/ParameterOutput>
00025
00026 #include "ShaderGeometry.hxx"
00027
00028 #include <algorithm>
00029
00030 using namespace osg;
00031 using namespace osgDB;
00032
00033 namespace simgear
00034 {
00035 void ShaderGeometry::addObject(const Vec3& position, float scale,
00036 int texture_index)
00037 {
00038 if (!_posScaleArray.valid()) {
00039 _posScaleArray = new Vec4Array();
00040 _vertexAttribArray = new FloatArray();
00041 }
00042 _posScaleArray->push_back(Vec4(position, scale));
00043 _vertexAttribArray->push_back((float)texture_index / varieties);
00044 dirtyBound();
00045 }
00046
00047 void ShaderGeometry::drawImplementation(osg::RenderInfo& renderInfo) const
00048 {
00049 State& state = *renderInfo.getState();
00050 const Extensions* extensions = getExtensions(state.getContextID(), true);
00051 Vec4Array::const_iterator citer = _posScaleArray->begin();
00052 Vec4Array::const_iterator cend = _posScaleArray->end();
00053 FloatArray::const_iterator viter = _vertexAttribArray->begin();
00054 for (; citer != cend; ++citer, ++viter) {
00055 const Vec4& color = *citer;
00056 const float attrib = *viter;
00057 glColor4fv(color.ptr());
00058 extensions->glVertexAttrib1f(1, attrib);
00059 _geometry->draw(renderInfo);
00060 }
00061 }
00062
00063 BoundingBox ShaderGeometry::computeBound() const
00064 {
00065 const BoundingBox& geom_box = _geometry->getBound();
00066 BoundingBox bb;
00067 const Vec4Array* posScales = _posScaleArray.get();
00068 if (!posScales)
00069 return bb;
00070
00071 for (Vec4Array::const_iterator iter = _posScaleArray->begin(),
00072 e = _posScaleArray->end();
00073 iter != e;
00074 ++iter) {
00075 const Vec4& posScale = *iter;
00076 const float scale = posScale.w();
00077 const Vec3 pos(posScale.x(), posScale.y(), posScale.z());
00078 for (unsigned j = 0; j < 7; ++j)
00079 bb.expandBy(geom_box.corner(j) * scale + pos);
00080 }
00081 return bb;
00082 }
00083
00084 bool ShaderGeometry_readLocalData(Object& obj, Input& fr)
00085 {
00086 bool iteratorAdvanced = false;
00087
00088 ShaderGeometry& geom = static_cast<ShaderGeometry&>(obj);
00089
00090 if ((fr[0].matchWord("geometry"))) {
00091 ++fr;
00092 iteratorAdvanced = true;
00093 osg::Geometry* drawable = dynamic_cast<osg::Geometry*>(fr.readDrawable());
00094 if (drawable) {
00095 geom._geometry = drawable;
00096 }
00097 }
00098
00099 if (fr.matchSequence("posScale %i {")) {
00100 int entry = fr[1].getNoNestedBrackets();
00101 int capacity;
00102 fr[1].getInt(capacity);
00103 Vec4Array* posScale = new Vec4Array;
00104 posScale->reserve(capacity);
00105 fr += 3;
00106 while (!fr.eof() && fr[0].getNoNestedBrackets() > entry) {
00107 Vec4 v;
00108 if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y())
00109 && fr[2].getFloat(v.z()) && fr[3].getFloat(v.w())) {
00110 fr += 4;
00111 posScale->push_back(v);
00112 }
00113 else ++fr;
00114 }
00115 ++fr;
00116 geom._posScaleArray = posScale;
00117 }
00118 if (fr.matchSequence("variety %i {")) {
00119 int entry = fr[1].getNoNestedBrackets();
00120 int capacity;
00121 fr[1].getInt(capacity);
00122 FloatArray* variety = new FloatArray;
00123 variety->reserve(capacity);
00124 fr += 3;
00125 while (!fr.eof() && fr[0].getNoNestedBrackets() > entry) {
00126 float val;
00127 if (fr[0].getFloat(val)) {
00128 ++fr;
00129 variety->push_back(val);
00130 }
00131 else ++fr;
00132 }
00133 ++fr;
00134 geom._vertexAttribArray = variety;
00135 }
00136
00137 return iteratorAdvanced;
00138 }
00139
00140 bool ShaderGeometry_writeLocalData(const Object& obj, Output& fw)
00141 {
00142 const ShaderGeometry& geom = static_cast<const ShaderGeometry&>(obj);
00143
00144 fw.indent() << "geometry" << std::endl;
00145 fw.writeObject(*geom._geometry);
00146 if (geom._posScaleArray.valid()) {
00147 fw.indent() << "posScale " << geom._posScaleArray->size() << " {\n";
00148 fw.moveIn();
00149 for (Vec4Array::const_iterator iter = geom._posScaleArray->begin();
00150 iter != geom._posScaleArray->end();
00151 ++iter) {
00152 fw.indent() << iter->x() << " " << iter->y() << " " << iter->z() << " "
00153 << iter->w() << "\n";
00154 }
00155 fw.moveOut();
00156 fw.indent() << "}\n";
00157 }
00158 if (geom._vertexAttribArray.valid()) {
00159 fw.indent() << "variety" << geom._vertexAttribArray->size() << " {\n";
00160 fw.moveIn();
00161 for (FloatArray::const_iterator iter = geom._vertexAttribArray->begin();
00162 iter != geom._vertexAttribArray->end();
00163 ++iter) {
00164 fw.indent() << *iter << "\n";
00165 }
00166 fw.moveOut();
00167 fw.indent() << "}\n";
00168 }
00169 return true;
00170 }
00171
00172 osgDB::RegisterDotOsgWrapperProxy shaderGeometryProxy
00173 (
00174 new ShaderGeometry,
00175 "ShaderGeometry",
00176 "Object Drawable ShaderGeometry",
00177 &ShaderGeometry_readLocalData,
00178 &ShaderGeometry_writeLocalData
00179 );
00180 }
00181