| 30 | | const int HOUSES_SIZE = 25000; // total number of houses |
| 31 | | double XDim = 5000.0f; // area dimension +/- XDim |
| 32 | | double ZDim = 5000.0f; // area dimension +/- YDim |
| 33 | | |
| 34 | | int GridX = 20; // number of grids in x direction |
| 35 | | int GridY = 20; // number of grids in y direction |
| 36 | | |
| 37 | | bool UseImpostor = true; // use impostor (or do not use) |
| 38 | | |
| 39 | | float Threshold = 3000.0f; // distance where impostor are shown |
| | 30 | const int HOUSES_SIZE = 25000; // total number of houses |
| | 31 | double XDim = 5000.0f; // area dimension +/- XDim |
| | 32 | double ZDim = 5000.0f; // area dimension +/- YDim |
| | 33 | |
| | 34 | int GridX = 20; // number of grids in x direction |
| | 35 | int GridY = 20; // number of grids in y direction |
| | 36 | |
| | 37 | bool UseImpostor = true; // use impostor (or do not use) |
| | 38 | |
| | 39 | float Threshold = 3000.0f; // distance where impostor are shown |
| 67 | | (*colors)[0] = osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f); |
| 68 | | |
| 69 | | // normals |
| 70 | | osg::Vec3Array * normals = new osg::Vec3Array(16); |
| 71 | | (*normals)[0] = osg::Vec3( 0.0f, -0.0f, -1.0f); |
| 72 | | (*normals)[1] = osg::Vec3( 0.0f, -0.0f, -1.0f); |
| 73 | | (*normals)[2] = osg::Vec3( 0.0f, -1.0f, 0.0f); |
| 74 | | (*normals)[3] = osg::Vec3( 0.0f, -1.0f, 0.0f); |
| 75 | | (*normals)[4] = osg::Vec3( 1.0f, -0.0f, 0.0f); |
| 76 | | (*normals)[5] = osg::Vec3( 1.0f, -0.0f, 0.0f); |
| 77 | | (*normals)[6] = osg::Vec3( 0.0f, 1.0f, 0.0f); |
| 78 | | (*normals)[7] = osg::Vec3( 0.0f, 1.0f, 0.0f); |
| 79 | | (*normals)[8] = osg::Vec3(-1.0f, -0.0f, 0.0f); |
| 80 | | (*normals)[9] = osg::Vec3(-1.0f, -0.0f, 0.0f); |
| 81 | | (*normals)[10] = osg::Vec3( 0.0f, -0.928477f, 0.371391f); |
| 82 | | (*normals)[11] = osg::Vec3( 0.0f, 0.928477f, 0.371391f); |
| 83 | | (*normals)[12] = osg::Vec3( 0.707107f, 0.0f, 0.707107f); |
| 84 | | (*normals)[13] = osg::Vec3( 0.707107f, 0.0f, 0.707107f); |
| 85 | | (*normals)[14] = osg::Vec3(-0.707107f, 0.0f, 0.707107f); |
| 86 | | (*normals)[15] = osg::Vec3(-0.707107f, 0.0f, 0.707107f); |
| 87 | | |
| 88 | | // coordIndices |
| 89 | | osg::UByteArray* coordIndices = new osg::UByteArray(48,indices); |
| | 67 | (*colors)[0] = osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f); |
| | 68 | |
| | 69 | // normals |
| | 70 | osg::Vec3Array * normals = new osg::Vec3Array(16); |
| | 71 | (*normals)[0] = osg::Vec3( 0.0f, -0.0f, -1.0f); |
| | 72 | (*normals)[1] = osg::Vec3( 0.0f, -0.0f, -1.0f); |
| | 73 | (*normals)[2] = osg::Vec3( 0.0f, -1.0f, 0.0f); |
| | 74 | (*normals)[3] = osg::Vec3( 0.0f, -1.0f, 0.0f); |
| | 75 | (*normals)[4] = osg::Vec3( 1.0f, -0.0f, 0.0f); |
| | 76 | (*normals)[5] = osg::Vec3( 1.0f, -0.0f, 0.0f); |
| | 77 | (*normals)[6] = osg::Vec3( 0.0f, 1.0f, 0.0f); |
| | 78 | (*normals)[7] = osg::Vec3( 0.0f, 1.0f, 0.0f); |
| | 79 | (*normals)[8] = osg::Vec3(-1.0f, -0.0f, 0.0f); |
| | 80 | (*normals)[9] = osg::Vec3(-1.0f, -0.0f, 0.0f); |
| | 81 | (*normals)[10] = osg::Vec3( 0.0f, -0.928477f, 0.371391f); |
| | 82 | (*normals)[11] = osg::Vec3( 0.0f, 0.928477f, 0.371391f); |
| | 83 | (*normals)[12] = osg::Vec3( 0.707107f, 0.0f, 0.707107f); |
| | 84 | (*normals)[13] = osg::Vec3( 0.707107f, 0.0f, 0.707107f); |
| | 85 | (*normals)[14] = osg::Vec3(-0.707107f, 0.0f, 0.707107f); |
| | 86 | (*normals)[15] = osg::Vec3(-0.707107f, 0.0f, 0.707107f); |
| | 87 | |
| | 88 | // coordIndices |
| | 89 | osg::UByteArray* coordIndices = new osg::UByteArray(48,indices); |
| 108 | | // coords |
| 109 | | osg::Vec3Array* coords = new osg::Vec3Array(10); |
| 110 | | (*coords)[0] = osg::Vec3( 0.5f, -0.7f, 0.0f); |
| 111 | | (*coords)[1] = osg::Vec3( 0.5f, 0.7f, 0.0f); |
| 112 | | (*coords)[2] = osg::Vec3(-0.5f, 0.7f, 0.0f); |
| 113 | | (*coords)[3] = osg::Vec3(-0.5f, -0.7f, 0.0f); |
| 114 | | (*coords)[4] = osg::Vec3( 0.5f, -0.7f, 1.0f); |
| 115 | | (*coords)[5] = osg::Vec3( 0.5f, 0.7f, 1.0f); |
| 116 | | (*coords)[6] = osg::Vec3(-0.5f, 0.7f, 1.0f); |
| 117 | | (*coords)[7] = osg::Vec3(-0.5f, -0.7f, 1.0f); |
| 118 | | (*coords)[8] = osg::Vec3( 0.0f, -0.5f, 1.5f); |
| 119 | | (*coords)[9] = osg::Vec3( 0.0f, 0.5f, 1.5f); |
| 120 | | |
| 121 | | for (i = 0; i < 10; i++) |
| 122 | | { |
| 123 | | (*coords)[i] = (*coords)[i] * scale + offset; |
| 124 | | } |
| 125 | | |
| 126 | | |
| 127 | | // create geometry |
| 128 | | osg::Geometry * geometry = new osg::Geometry(); |
| 129 | | |
| 130 | | geometry->addPrimitiveSet(primitives); |
| 131 | | |
| 132 | | geometry->setVertexArray(coords); |
| | 108 | // coords |
| | 109 | osg::Vec3Array* coords = new osg::Vec3Array(10); |
| | 110 | (*coords)[0] = osg::Vec3( 0.5f, -0.7f, 0.0f); |
| | 111 | (*coords)[1] = osg::Vec3( 0.5f, 0.7f, 0.0f); |
| | 112 | (*coords)[2] = osg::Vec3(-0.5f, 0.7f, 0.0f); |
| | 113 | (*coords)[3] = osg::Vec3(-0.5f, -0.7f, 0.0f); |
| | 114 | (*coords)[4] = osg::Vec3( 0.5f, -0.7f, 1.0f); |
| | 115 | (*coords)[5] = osg::Vec3( 0.5f, 0.7f, 1.0f); |
| | 116 | (*coords)[6] = osg::Vec3(-0.5f, 0.7f, 1.0f); |
| | 117 | (*coords)[7] = osg::Vec3(-0.5f, -0.7f, 1.0f); |
| | 118 | (*coords)[8] = osg::Vec3( 0.0f, -0.5f, 1.5f); |
| | 119 | (*coords)[9] = osg::Vec3( 0.0f, 0.5f, 1.5f); |
| | 120 | |
| | 121 | for (i = 0; i < 10; i++) |
| | 122 | { |
| | 123 | (*coords)[i] = (*coords)[i] * scale + offset; |
| | 124 | } |
| | 125 | |
| | 126 | |
| | 127 | // create geometry |
| | 128 | osg::Geometry * geometry = new osg::Geometry(); |
| | 129 | |
| | 130 | geometry->addPrimitiveSet(primitives); |
| | 131 | |
| | 132 | geometry->setVertexArray(coords); |
| 158 | | for (i = 0; i < GridX * GridY; i++) |
| 159 | | groups[i] = new osg::Group(); |
| 160 | | |
| 161 | | float xGridStart = bbox.xMin(); |
| 162 | | float xGridSize = (bbox.xMax() - bbox.xMin()) / GridX; |
| 163 | | |
| 164 | | float yGridStart = bbox.yMin(); |
| 165 | | float yGridSize = (bbox.yMax() - bbox.yMin()) / GridY; |
| 166 | | |
| 167 | | // arrange buildings into right grid |
| 168 | | for (NodeIterator nodeIter = nodes.begin(); nodeIter != nodes.end(); ++nodeIter) |
| 169 | | { |
| 170 | | osg::Node * node = nodeIter->get(); |
| 171 | | osg::Vec3 center = node->getBound().center(); |
| 172 | | |
| 173 | | int x = (int)floor((center.x() - xGridStart) / xGridSize); |
| 174 | | int z = (int)floor((center.y() - yGridStart) / yGridSize); |
| 175 | | |
| 176 | | groups[z * GridX + x]->addChild(node); |
| 177 | | } |
| | 158 | for (i = 0; i < GridX * GridY; i++) |
| | 159 | groups[i] = new osg::Group(); |
| | 160 | |
| | 161 | float xGridStart = bbox.xMin(); |
| | 162 | float xGridSize = (bbox.xMax() - bbox.xMin()) / GridX; |
| | 163 | |
| | 164 | float yGridStart = bbox.yMin(); |
| | 165 | float yGridSize = (bbox.yMax() - bbox.yMin()) / GridY; |
| | 166 | |
| | 167 | // arrange buildings into right grid |
| | 168 | for (NodeIterator nodeIter = nodes.begin(); nodeIter != nodes.end(); ++nodeIter) |
| | 169 | { |
| | 170 | osg::Node * node = nodeIter->get(); |
| | 171 | osg::Vec3 center = node->getBound().center(); |
| | 172 | |
| | 173 | int x = (int)floor((center.x() - xGridStart) / xGridSize); |
| | 174 | int z = (int)floor((center.y() - yGridStart) / yGridSize); |
| | 175 | |
| | 176 | groups[z * GridX + x]->addChild(node); |
| | 177 | } |
| 179 | | // add nodes to building root |
| 180 | | for (i = 0; i < GridX * GridY; i++) |
| 181 | | { |
| 182 | | osg::StateSet * stateset = new osg::StateSet(); |
| 183 | | |
| 184 | | osg::Material * material = new osg::Material(); |
| 185 | | osg::Vec4 color = osg::Vec4( |
| 186 | | 0.5f + (static_cast<double> (rand()) / (2.0*static_cast<double> (RAND_MAX))), |
| 187 | | 0.5f + (static_cast<double> (rand()) / (2.0*static_cast<double> (RAND_MAX))), |
| 188 | | 0.5f + (static_cast<double> (rand()) / ( 2.0*static_cast<double>(RAND_MAX))), |
| 189 | | 1.0f); |
| 190 | | |
| 191 | | material->setAmbient(osg::Material::FRONT_AND_BACK, color); |
| 192 | | material->setDiffuse(osg::Material::FRONT_AND_BACK, color); |
| 193 | | stateset->setAttributeAndModes(material, osg::StateAttribute::ON); |
| 194 | | |
| 195 | | groups[i]->setStateSet(stateset); |
| 196 | | |
| 197 | | if (UseImpostor) |
| 198 | | { |
| 199 | | osgSim::Impostor * impostor = new osgSim::Impostor(); |
| 200 | | impostor->setImpostorThreshold(static_cast<float> (Threshold)); |
| 201 | | impostor->addChild(groups[i]); |
| 202 | | impostor->setRange(0, 0.0f, 1e7f); |
| 203 | | impostor->setCenter(groups[i]->getBound().center()); |
| 204 | | Root->addChild(impostor); |
| 205 | | } |
| 206 | | else |
| 207 | | { |
| 208 | | Root->addChild(groups[i]); |
| 209 | | } |
| 210 | | } |
| 211 | | |
| 212 | | delete[] groups; |
| | 179 | // add nodes to building root |
| | 180 | for (i = 0; i < GridX * GridY; i++) |
| | 181 | { |
| | 182 | osg::StateSet * stateset = new osg::StateSet(); |
| | 183 | |
| | 184 | osg::Material * material = new osg::Material(); |
| | 185 | osg::Vec4 color = osg::Vec4( |
| | 186 | 0.5f + (static_cast<double> (rand()) / (2.0*static_cast<double> (RAND_MAX))), |
| | 187 | 0.5f + (static_cast<double> (rand()) / (2.0*static_cast<double> (RAND_MAX))), |
| | 188 | 0.5f + (static_cast<double> (rand()) / ( 2.0*static_cast<double>(RAND_MAX))), |
| | 189 | 1.0f); |
| | 190 | |
| | 191 | material->setAmbient(osg::Material::FRONT_AND_BACK, color); |
| | 192 | material->setDiffuse(osg::Material::FRONT_AND_BACK, color); |
| | 193 | stateset->setAttributeAndModes(material, osg::StateAttribute::ON); |
| | 194 | |
| | 195 | groups[i]->setStateSet(stateset); |
| | 196 | |
| | 197 | if (UseImpostor) |
| | 198 | { |
| | 199 | osgSim::Impostor * impostor = new osgSim::Impostor(); |
| | 200 | impostor->setImpostorThreshold(static_cast<float> (Threshold)); |
| | 201 | impostor->addChild(groups[i]); |
| | 202 | impostor->setRange(0, 0.0f, 1e7f); |
| | 203 | impostor->setCenter(groups[i]->getBound().center()); |
| | 204 | Root->addChild(impostor); |
| | 205 | } |
| | 206 | else |
| | 207 | { |
| | 208 | Root->addChild(groups[i]); |
| | 209 | } |
| | 210 | } |
| | 211 | |
| | 212 | delete[] groups; |