| 76 | | |
| | 93 | #if 0 |
| | 94 | char fragmentShaderNormalSource[] = |
| | 95 | "uniform sampler2D baseTexture; \n" |
| | 96 | "uniform sampler2D vertexTexture; \n" |
| | 97 | "\n" |
| | 98 | "void main(void) \n" |
| | 99 | "{ \n" |
| | 100 | " const float dx = 0.0010; \n" |
| | 101 | " const float dy = 0.0010; \n" |
| | 102 | " float dz_dx = texture2D( vertexTexture, gl_TexCoord[0].xy + vec2(dx,0.0)).x - texture2D( vertexTexture, gl_TexCoord[0].xy + vec2(-dx,0.0)).x; \n" |
| | 103 | " float dz_dy = texture2D( vertexTexture, gl_TexCoord[0].xy + vec2(0.0,dy)).x - texture2D( vertexTexture, gl_TexCoord[0].xy + vec2(0.0,-dy)).x; \n" |
| | 104 | " vec3 normal = normalize(vec3(-dz_dx, -dz_dy, dx*50));\n" |
| | 105 | " \n" |
| | 106 | " gl_FragColor = vec4(normal.z,normal.z,normal.z,1.0); \n" |
| | 107 | "}\n"; |
| | 108 | #else |
| | 109 | char fragmentShaderNormalSource[] = |
| | 110 | "uniform sampler2D baseTexture; \n" |
| | 111 | "uniform sampler2D vertexTexture; \n" |
| | 112 | "\n" |
| | 113 | "void main(void) \n" |
| | 114 | "{ \n" |
| | 115 | " vec3 normal = normalize(texture2D( baseTexture, gl_TexCoord[0].xy).xyz);\n" |
| | 116 | " \n" |
| | 117 | " gl_FragColor = vec4(normal.z,normal.z,normal.z,1.0); \n" |
| | 118 | "}\n"; |
| | 119 | #endif |
| 175 | | } |
| 176 | | |
| 177 | | osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); |
| 178 | | program->addShader(fragment_shader); |
| 179 | | |
| | 219 | if (!terrainFileName2.empty()) |
| | 220 | { |
| | 221 | osg::Image* image2 = osgDB::readImageFile(terrainFileName2); |
| | 222 | osg::Texture2D* vertexTexture2 = new osg::Texture2D(image2); |
| | 223 | |
| | 224 | |
| | 225 | vertexTexture2->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST); |
| | 226 | vertexTexture2->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST); |
| | 227 | |
| | 228 | vertexTexture2->setInternalFormat(GL_LUMINANCE_FLOAT32_ATI); |
| | 229 | |
| | 230 | vertexTexture2->setResizeNonPowerOfTwoHint(false); |
| | 231 | stateset->setTextureAttributeAndModes(2,vertexTexture2); |
| | 232 | |
| | 233 | osg::Uniform* vertexTextureSampler2 = new osg::Uniform("vertexTexture2",2); |
| | 234 | stateset->addUniform(vertexTextureSampler2); |
| | 235 | |
| | 236 | } |
| | 237 | |
| | 238 | } |
| | 239 | |
| | 240 | if (terrainFileName2.empty()) |
| | 241 | { |
| | 242 | osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderNormalSource); |
| | 243 | program->addShader(fragment_shader); |
| | 244 | |
| | 245 | osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_texture); |
| | 246 | program->addShader(vertex_shader); |
| | 247 | } |
| | 248 | else |
| | 249 | { |
| | 250 | |
| | 251 | osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); |
| | 252 | program->addShader(fragment_shader); |
| | 253 | |
| | 254 | osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource_texture2); |
| | 255 | program->addShader(vertex_shader); |
| | 256 | } |
| | 257 | |
| 212 | | for(iy=0; iy<num_y-1; ++iy) |
| 213 | | { |
| 214 | | unsigned int element_no = 0; |
| 215 | | osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP, num_x*2); |
| 216 | | unsigned int index = iy * num_x; |
| 217 | | for(unsigned int ix = 0; ix<num_x; ++ix) |
| 218 | | { |
| 219 | | (*elements)[element_no++] = index + num_x; |
| 220 | | (*elements)[element_no++] = index++; |
| 221 | | } |
| 222 | | geom->addPrimitiveSet(elements); |
| 223 | | } |
| 224 | | |
| 225 | | // geom->setUseVertexBufferObjects(true); |
| | 290 | unsigned int totalIndices = 0; |
| | 291 | if (mesh=="triangles" || mesh=="tristrip") |
| | 292 | { |
| | 293 | if (cacheSize) |
| | 294 | { |
| | 295 | unsigned int index=0; |
| | 296 | osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLES); |
| | 297 | geom->addPrimitiveSet(elements); |
| | 298 | elements->dirty(); |
| | 299 | |
| | 300 | for(unsigned int is=0; is<num_x; is += cacheSize-1) |
| | 301 | { |
| | 302 | |
| | 303 | for(iy=0; iy<num_y-1; ++iy) |
| | 304 | { |
| | 305 | unsigned int num_in_stripe = osg::minimum(cacheSize, num_x-is); |
| | 306 | |
| | 307 | bool rightToLeft = true; |
| | 308 | |
| | 309 | if (elements->size() > maxSize) |
| | 310 | { |
| | 311 | osg::notify(osg::NOTICE)<<"elements->size() = "<<elements->size()<<std::endl; |
| | 312 | totalIndices += elements->size(); |
| | 313 | |
| | 314 | index=0; |
| | 315 | elements = new osg::DrawElementsUInt(GL_TRIANGLES); |
| | 316 | geom->addPrimitiveSet(elements); |
| | 317 | elements->dirty(); |
| | 318 | |
| | 319 | } |
| | 320 | |
| | 321 | if (rightToLeft) |
| | 322 | { |
| | 323 | |
| | 324 | index = iy * num_x + is; |
| | 325 | |
| | 326 | for(unsigned int ix = 0; ix<num_in_stripe-1; ++ix) |
| | 327 | { |
| | 328 | (*elements).push_back(index + num_x); |
| | 329 | (*elements).push_back(index); |
| | 330 | (*elements).push_back(index+1); |
| | 331 | |
| | 332 | (*elements).push_back(index + num_x); |
| | 333 | (*elements).push_back(index+1); |
| | 334 | (*elements).push_back(index + num_x + 1); |
| | 335 | |
| | 336 | ++index; |
| | 337 | } |
| | 338 | } |
| | 339 | else |
| | 340 | { |
| | 341 | index = iy * num_x + is + (num_x-2); |
| | 342 | |
| | 343 | for(unsigned int ix = 0; ix<num_in_stripe-1; ++ix) |
| | 344 | { |
| | 345 | (*elements).push_back(index + num_x); |
| | 346 | (*elements).push_back(index); |
| | 347 | (*elements).push_back(index+1); |
| | 348 | |
| | 349 | (*elements).push_back(index + num_x); |
| | 350 | (*elements).push_back(index+1); |
| | 351 | (*elements).push_back(index + num_x + 1); |
| | 352 | |
| | 353 | --index; |
| | 354 | } |
| | 355 | } |
| | 356 | |
| | 357 | rightToLeft = !rightToLeft; |
| | 358 | |
| | 359 | } |
| | 360 | |
| | 361 | } |
| | 362 | totalIndices += elements->size(); |
| | 363 | osg::notify(osg::NOTICE)<<"elements->size() = "<<elements->size()<<std::endl; |
| | 364 | } |
| | 365 | else |
| | 366 | { |
| | 367 | osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLES, (num_x-1)*(num_y-1)*6); |
| | 368 | elements->dirty(); |
| | 369 | unsigned int element_no = 0; |
| | 370 | for(iy=0; iy<num_y-1; ++iy) |
| | 371 | { |
| | 372 | unsigned int index = iy * num_x; |
| | 373 | for(unsigned int ix = 0; ix<num_x-1; ++ix) |
| | 374 | { |
| | 375 | (*elements)[element_no++] = index + num_x; |
| | 376 | (*elements)[element_no++] = index; |
| | 377 | (*elements)[element_no++] = index+1; |
| | 378 | |
| | 379 | (*elements)[element_no++] = index + num_x; |
| | 380 | (*elements)[element_no++] = index+1; |
| | 381 | (*elements)[element_no++] = index + num_x + 1; |
| | 382 | |
| | 383 | ++index; |
| | 384 | } |
| | 385 | } |
| | 386 | geom->addPrimitiveSet(elements); |
| | 387 | osg::notify(osg::NOTICE)<<"elements->size() = "<<elements->size()<<std::endl; |
| | 388 | } |
| | 389 | } |
| | 390 | else |
| | 391 | { |
| | 392 | |
| | 393 | if (cacheSize) |
| | 394 | { |
| | 395 | bool needToJoin = false; |
| | 396 | unsigned int index=0; |
| | 397 | osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP); |
| | 398 | geom->addPrimitiveSet(elements); |
| | 399 | elements->dirty(); |
| | 400 | |
| | 401 | for(unsigned int is=0; is<num_x; is += cacheSize-1) |
| | 402 | { |
| | 403 | |
| | 404 | for(iy=0; iy<num_y-1; ++iy) |
| | 405 | { |
| | 406 | unsigned int num_in_stripe = osg::minimum(cacheSize, num_x-is); |
| | 407 | |
| | 408 | bool rightToLeft = true; |
| | 409 | |
| | 410 | if (elements->size() > maxSize) |
| | 411 | { |
| | 412 | osg::notify(osg::NOTICE)<<"elements->size() = "<<elements->size()<<std::endl; |
| | 413 | |
| | 414 | totalIndices += elements->size(); |
| | 415 | |
| | 416 | needToJoin = false; |
| | 417 | index=0; |
| | 418 | elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP); |
| | 419 | geom->addPrimitiveSet(elements); |
| | 420 | elements->dirty(); |
| | 421 | |
| | 422 | } |
| | 423 | |
| | 424 | if (needToJoin) (*elements).push_back(elements->back()); |
| | 425 | |
| | 426 | if (rightToLeft) |
| | 427 | { |
| | 428 | |
| | 429 | index = iy * num_x + is; |
| | 430 | |
| | 431 | if (needToJoin) (*elements).push_back(index + num_x); |
| | 432 | |
| | 433 | for(unsigned int ix = 0; ix<num_in_stripe; ++ix) |
| | 434 | { |
| | 435 | (*elements).push_back(index + num_x); |
| | 436 | (*elements).push_back(index++); |
| | 437 | } |
| | 438 | } |
| | 439 | else |
| | 440 | { |
| | 441 | index = iy * num_x + is + (num_x-1); |
| | 442 | |
| | 443 | if (needToJoin) (*elements).push_back(index); |
| | 444 | |
| | 445 | for(unsigned int ix = 0; ix<num_in_stripe; ++ix) |
| | 446 | { |
| | 447 | (*elements).push_back(index); |
| | 448 | (*elements).push_back(index-- + num_x); |
| | 449 | } |
| | 450 | } |
| | 451 | |
| | 452 | rightToLeft = !rightToLeft; |
| | 453 | |
| | 454 | needToJoin = true; |
| | 455 | } |
| | 456 | |
| | 457 | } |
| | 458 | totalIndices += elements->size(); |
| | 459 | } |
| | 460 | else |
| | 461 | { |
| | 462 | osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP); |
| | 463 | elements->dirty(); |
| | 464 | geom->addPrimitiveSet(elements); |
| | 465 | bool needToJoin = false; |
| | 466 | unsigned int index=0; |
| | 467 | for(iy=0; iy<num_y-1; ++iy) |
| | 468 | { |
| | 469 | |
| | 470 | if (needToJoin) (*elements).push_back(index-1); |
| | 471 | |
| | 472 | index = iy * num_x; |
| | 473 | |
| | 474 | if (needToJoin) (*elements).push_back(index + num_x); |
| | 475 | |
| | 476 | for(unsigned int ix = 0; ix<num_x; ++ix) |
| | 477 | { |
| | 478 | (*elements).push_back(index + num_x); |
| | 479 | (*elements).push_back(index++); |
| | 480 | } |
| | 481 | needToJoin = true; |
| | 482 | |
| | 483 | } |
| | 484 | } |
| | 485 | } |
| | 486 | |
| | 487 | osg::notify(osg::NOTICE)<<"totalIndices = "<<totalIndices<<std::endl; |
| | 488 | |
| | 489 | if (mesh=="tristrip") |
| | 490 | { osgUtil::TriStripVisitor stripper; |
| | 491 | stripper.stripify(*geom); |
| | 492 | } |
| | 493 | |
| | 494 | #if 0 |
| | 495 | geom->setUseVertexBufferObjects(true); |
| | 496 | #else |
| | 497 | geom->setUseVertexBufferObjects(false); |
| | 498 | #endif |