247 | | for(unsigned int c=0;c<38;++c) |
248 | | { |
249 | | grid->setHeight(c,r,z_scale*vertex[r+c*39][2]); |
250 | | } |
| 240 | unsigned int numColumns = 38; |
| 241 | unsigned int numRows = 39; |
| 242 | unsigned int r, c; |
| 243 | |
| 244 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
| 245 | osg::Vec3 size(1000.0f,1000.0f,250.0f); |
| 246 | |
| 247 | osg::Geometry* geometry = new osg::Geometry; |
| 248 | |
| 249 | osg::Vec3Array& v = *(new osg::Vec3Array(numColumns*numRows)); |
| 250 | osg::Vec2Array& tc = *(new osg::Vec2Array(numColumns*numRows)); |
| 251 | osg::Vec4ubArray& color = *(new osg::Vec4ubArray(1)); |
| 252 | |
| 253 | color[0].set(255,255,255,255); |
| 254 | |
| 255 | float rowCoordDelta = size.y()/(float)(numRows-1); |
| 256 | float columnCoordDelta = size.x()/(float)(numColumns-1); |
| 257 | |
| 258 | float rowTexDelta = 1.0f/(float)(numRows-1); |
| 259 | float columnTexDelta = 1.0f/(float)(numColumns-1); |
| 260 | |
| 261 | // compute z range of z values of grid data so we can scale it. |
| 262 | float min_z = FLT_MAX; |
| 263 | float max_z = -FLT_MAX; |
| 264 | for(r=0;r<numRows;++r) |
| 265 | { |
| 266 | for(c=0;c<numColumns;++c) |
| 267 | { |
| 268 | min_z = osg::minimum(min_z,vertex[r+c*numRows][2]); |
| 269 | max_z = osg::maximum(max_z,vertex[r+c*numRows][2]); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | float scale_z = size.z()/(max_z-min_z); |
| 274 | |
| 275 | osg::Vec3 pos = origin; |
| 276 | osg::Vec2 tex(0.0f,0.0f); |
| 277 | int vi=0; |
| 278 | for(r=0;r<numRows;++r) |
| 279 | { |
| 280 | pos.x() = origin.x(); |
| 281 | tex.x() = 0.0f; |
| 282 | for(c=0;c<numColumns;++c) |
| 283 | { |
| 284 | v[vi].set(pos.x(),pos.y(),pos.z()+(vertex[r+c*numRows][2]-min_z)*scale_z); |
| 285 | tc[vi] = tex; |
| 286 | pos.x()+=columnCoordDelta; |
| 287 | tex.x()+=columnTexDelta; |
| 288 | ++vi; |
| 289 | } |
| 290 | pos.y() += rowCoordDelta; |
| 291 | tex.y() += rowTexDelta; |
| 292 | } |
| 293 | |
| 294 | geometry->setVertexArray(&v); |
| 295 | geometry->setTexCoordArray(0, &tc); |
| 296 | geometry->setColorArray(&color); |
| 297 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
| 298 | |
| 299 | for(r=0;r<numRows-1;++r) |
| 300 | { |
| 301 | osg::DrawElementsUShort& drawElements = *(new osg::DrawElementsUShort(GL_QUAD_STRIP,2*numColumns)); |
| 302 | geometry->addPrimitiveSet(&drawElements); |
| 303 | int ei=0; |
| 304 | for(c=0;c<numColumns;++c) |
| 305 | { |
| 306 | drawElements[ei++] = (r+1)*numColumns+c; |
| 307 | drawElements[ei++] = (r)*numColumns+c; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | osgUtil::SmoothingVisitor smoother; |
| 312 | smoother.smooth(*geometry); |
| 313 | |
| 314 | terrainGeode->addDrawable(geometry); |