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