| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/Geometry> |
|---|
| 20 | #include <osg/Material> |
|---|
| 21 | #include <osg/MatrixTransform> |
|---|
| 22 | #include <osg/Quat> |
|---|
| 23 | #include <osg/Geode> |
|---|
| 24 | |
|---|
| 25 | #include <osgSim/Impostor> |
|---|
| 26 | #include <osgSim/InsertImpostorsVisitor> |
|---|
| 27 | |
|---|
| 28 | #include <osgDB/ReadFile> |
|---|
| 29 | |
|---|
| 30 | #include <osgViewer/Viewer> |
|---|
| 31 | |
|---|
| 32 | #include "TestManipulator.h" |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | #include <iostream> |
|---|
| 36 | #include <list> |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | typedef osg::ref_ptr<osg::Node> NodePtr; |
|---|
| 40 | typedef std::list<NodePtr> NodeContainer; |
|---|
| 41 | typedef NodeContainer::iterator NodeIterator; |
|---|
| 42 | |
|---|
| 43 | NodeContainer nodes; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | osg::Group * Root = 0; |
|---|
| 47 | |
|---|
| 48 | const int HOUSES_SIZE = 25000; |
|---|
| 49 | double XDim = 5000.0f; |
|---|
| 50 | double ZDim = 5000.0f; |
|---|
| 51 | |
|---|
| 52 | int GridX = 20; |
|---|
| 53 | int GridY = 20; |
|---|
| 54 | |
|---|
| 55 | bool UseImpostor = true; |
|---|
| 56 | |
|---|
| 57 | float Threshold = 3000.0f; |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | void CreateHouses() |
|---|
| 61 | { |
|---|
| 62 | int i; |
|---|
| 63 | |
|---|
| 64 | GLubyte indices[48] = { |
|---|
| 65 | 0, 2, 1, |
|---|
| 66 | 3, 2, 0, |
|---|
| 67 | 0, 4, 7, |
|---|
| 68 | 7, 3, 0, |
|---|
| 69 | 0, 1, 5, |
|---|
| 70 | 5, 4, 0, |
|---|
| 71 | 1, 6, 5, |
|---|
| 72 | 2, 6, 1, |
|---|
| 73 | 2, 3, 7, |
|---|
| 74 | 2, 7, 6, |
|---|
| 75 | 4, 8, 7, |
|---|
| 76 | 5, 6, 9, |
|---|
| 77 | 4, 5, 8, |
|---|
| 78 | 8, 5, 9, |
|---|
| 79 | 6, 7, 8, |
|---|
| 80 | 8, 9, 6 |
|---|
| 81 | }; |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | osg::Vec4Array* colors = new osg::Vec4Array(1); |
|---|
| 85 | (*colors)[0] = osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f); |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | osg::Vec3Array * normals = new osg::Vec3Array(16); |
|---|
| 89 | (*normals)[0] = osg::Vec3( 0.0f, -0.0f, -1.0f); |
|---|
| 90 | (*normals)[1] = osg::Vec3( 0.0f, -0.0f, -1.0f); |
|---|
| 91 | (*normals)[2] = osg::Vec3( 0.0f, -1.0f, 0.0f); |
|---|
| 92 | (*normals)[3] = osg::Vec3( 0.0f, -1.0f, 0.0f); |
|---|
| 93 | (*normals)[4] = osg::Vec3( 1.0f, -0.0f, 0.0f); |
|---|
| 94 | (*normals)[5] = osg::Vec3( 1.0f, -0.0f, 0.0f); |
|---|
| 95 | (*normals)[6] = osg::Vec3( 0.0f, 1.0f, 0.0f); |
|---|
| 96 | (*normals)[7] = osg::Vec3( 0.0f, 1.0f, 0.0f); |
|---|
| 97 | (*normals)[8] = osg::Vec3(-1.0f, -0.0f, 0.0f); |
|---|
| 98 | (*normals)[9] = osg::Vec3(-1.0f, -0.0f, 0.0f); |
|---|
| 99 | (*normals)[10] = osg::Vec3( 0.0f, -0.928477f, 0.371391f); |
|---|
| 100 | (*normals)[11] = osg::Vec3( 0.0f, 0.928477f, 0.371391f); |
|---|
| 101 | (*normals)[12] = osg::Vec3( 0.707107f, 0.0f, 0.707107f); |
|---|
| 102 | (*normals)[13] = osg::Vec3( 0.707107f, 0.0f, 0.707107f); |
|---|
| 103 | (*normals)[14] = osg::Vec3(-0.707107f, 0.0f, 0.707107f); |
|---|
| 104 | (*normals)[15] = osg::Vec3(-0.707107f, 0.0f, 0.707107f); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | osg::UByteArray* coordIndices = new osg::UByteArray(48,indices); |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | osg::PrimitiveSet* primitives = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,48); |
|---|
| 111 | |
|---|
| 112 | for (int q = 0; q < HOUSES_SIZE; q++) |
|---|
| 113 | { |
|---|
| 114 | float xPos = ((static_cast<double> (rand()) / |
|---|
| 115 | static_cast<double> (RAND_MAX)) |
|---|
| 116 | * 2.0 * XDim) - XDim; |
|---|
| 117 | |
|---|
| 118 | float yPos = ((static_cast<double> (rand()) / |
|---|
| 119 | static_cast<double> (RAND_MAX)) |
|---|
| 120 | * 2 * ZDim) - ZDim; |
|---|
| 121 | |
|---|
| 122 | float scale = 10.0f; |
|---|
| 123 | |
|---|
| 124 | osg::Vec3 offset(xPos,yPos,0.0f); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | osg::Vec3Array* coords = new osg::Vec3Array(10); |
|---|
| 128 | (*coords)[0] = osg::Vec3( 0.5f, -0.7f, 0.0f); |
|---|
| 129 | (*coords)[1] = osg::Vec3( 0.5f, 0.7f, 0.0f); |
|---|
| 130 | (*coords)[2] = osg::Vec3(-0.5f, 0.7f, 0.0f); |
|---|
| 131 | (*coords)[3] = osg::Vec3(-0.5f, -0.7f, 0.0f); |
|---|
| 132 | (*coords)[4] = osg::Vec3( 0.5f, -0.7f, 1.0f); |
|---|
| 133 | (*coords)[5] = osg::Vec3( 0.5f, 0.7f, 1.0f); |
|---|
| 134 | (*coords)[6] = osg::Vec3(-0.5f, 0.7f, 1.0f); |
|---|
| 135 | (*coords)[7] = osg::Vec3(-0.5f, -0.7f, 1.0f); |
|---|
| 136 | (*coords)[8] = osg::Vec3( 0.0f, -0.5f, 1.5f); |
|---|
| 137 | (*coords)[9] = osg::Vec3( 0.0f, 0.5f, 1.5f); |
|---|
| 138 | |
|---|
| 139 | for (i = 0; i < 10; i++) |
|---|
| 140 | { |
|---|
| 141 | (*coords)[i] = (*coords)[i] * scale + offset; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | osg::Geometry * geometry = new osg::Geometry(); |
|---|
| 147 | |
|---|
| 148 | geometry->addPrimitiveSet(primitives); |
|---|
| 149 | |
|---|
| 150 | geometry->setVertexArray(coords); |
|---|
| 151 | geometry->setVertexIndices(coordIndices); |
|---|
| 152 | |
|---|
| 153 | geometry->setColorArray(colors); |
|---|
| 154 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 155 | |
|---|
| 156 | geometry->setNormalArray(normals); |
|---|
| 157 | geometry->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE); |
|---|
| 158 | |
|---|
| 159 | osg::Geode * geode = new osg::Geode(); |
|---|
| 160 | geode->addDrawable(geometry); |
|---|
| 161 | |
|---|
| 162 | nodes.push_back(geode); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | void LayoutAsGrid() |
|---|
| 167 | { |
|---|
| 168 | |
|---|
| 169 | osg::BoundingBox bbox; |
|---|
| 170 | for (NodeIterator node = nodes.begin(); node != nodes.end(); ++node) |
|---|
| 171 | bbox.expandBy((*node)->getBound()); |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | osg::Group ** groups = new osg::Group*[GridX * GridY]; |
|---|
| 175 | int i; |
|---|
| 176 | for (i = 0; i < GridX * GridY; i++) |
|---|
| 177 | groups[i] = new osg::Group(); |
|---|
| 178 | |
|---|
| 179 | float xGridStart = bbox.xMin(); |
|---|
| 180 | float xGridSize = (bbox.xMax() - bbox.xMin()) / GridX; |
|---|
| 181 | |
|---|
| 182 | float yGridStart = bbox.yMin(); |
|---|
| 183 | float yGridSize = (bbox.yMax() - bbox.yMin()) / GridY; |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | for (NodeIterator nodeIter = nodes.begin(); nodeIter != nodes.end(); ++nodeIter) |
|---|
| 187 | { |
|---|
| 188 | osg::Node * node = nodeIter->get(); |
|---|
| 189 | osg::Vec3 center = node->getBound().center(); |
|---|
| 190 | |
|---|
| 191 | int x = (int)floor((center.x() - xGridStart) / xGridSize); |
|---|
| 192 | int z = (int)floor((center.y() - yGridStart) / yGridSize); |
|---|
| 193 | |
|---|
| 194 | groups[z * GridX + x]->addChild(node); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | for (i = 0; i < GridX * GridY; i++) |
|---|
| 199 | { |
|---|
| 200 | osg::StateSet * stateset = new osg::StateSet(); |
|---|
| 201 | |
|---|
| 202 | osg::Material * material = new osg::Material(); |
|---|
| 203 | osg::Vec4 color = osg::Vec4( |
|---|
| 204 | 0.5f + (static_cast<double> (rand()) / (2.0*static_cast<double> (RAND_MAX))), |
|---|
| 205 | 0.5f + (static_cast<double> (rand()) / (2.0*static_cast<double> (RAND_MAX))), |
|---|
| 206 | 0.5f + (static_cast<double> (rand()) / ( 2.0*static_cast<double>(RAND_MAX))), |
|---|
| 207 | 1.0f); |
|---|
| 208 | |
|---|
| 209 | material->setAmbient(osg::Material::FRONT_AND_BACK, color); |
|---|
| 210 | material->setDiffuse(osg::Material::FRONT_AND_BACK, color); |
|---|
| 211 | stateset->setAttributeAndModes(material, osg::StateAttribute::ON); |
|---|
| 212 | |
|---|
| 213 | groups[i]->setStateSet(stateset); |
|---|
| 214 | |
|---|
| 215 | if (UseImpostor) |
|---|
| 216 | { |
|---|
| 217 | osgSim::Impostor * impostor = new osgSim::Impostor(); |
|---|
| 218 | impostor->setImpostorThreshold(static_cast<float> (Threshold)); |
|---|
| 219 | impostor->addChild(groups[i]); |
|---|
| 220 | impostor->setRange(0, 0.0f, 1e7f); |
|---|
| 221 | impostor->setCenter(groups[i]->getBound().center()); |
|---|
| 222 | Root->addChild(impostor); |
|---|
| 223 | } |
|---|
| 224 | else |
|---|
| 225 | { |
|---|
| 226 | Root->addChild(groups[i]); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | delete[] groups; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | int main( int argc, char **argv ) |
|---|
| 235 | { |
|---|
| 236 | |
|---|
| 237 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | osgViewer::Viewer viewer; |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | viewer.setCameraManipulator(new TestManipulator); |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | osg::Node* model = osgDB::readNodeFiles(arguments); |
|---|
| 248 | if (model) |
|---|
| 249 | { |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | if (dynamic_cast<osg::Group*>(model)==0) |
|---|
| 255 | { |
|---|
| 256 | const osg::BoundingSphere& bs = model->getBound(); |
|---|
| 257 | if (bs.valid()) |
|---|
| 258 | { |
|---|
| 259 | |
|---|
| 260 | osgSim::Impostor* impostor = new osgSim::Impostor; |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | impostor->addChild(model); |
|---|
| 264 | impostor->setRange(0,0.0f,1e7f); |
|---|
| 265 | impostor->setCenter(bs.center()); |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | impostor->setImpostorThresholdToBound(5.0f); |
|---|
| 269 | |
|---|
| 270 | model = impostor; |
|---|
| 271 | |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | osg::Group* rootnode = new osg::Group; |
|---|
| 283 | rootnode->addChild(model); |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | osgSim::InsertImpostorsVisitor ov; |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | model->accept(ov); |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | ov.insertImpostors(); |
|---|
| 297 | } |
|---|
| 298 | else |
|---|
| 299 | { |
|---|
| 300 | |
|---|
| 301 | model = Root = new osg::Group(); |
|---|
| 302 | CreateHouses(); |
|---|
| 303 | LayoutAsGrid(); |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | viewer.setSceneData(model); |
|---|
| 308 | |
|---|
| 309 | return viewer.run(); |
|---|
| 310 | } |
|---|