| 403 | | std::auto_ptr<openvrml::field_value> texture_url_fv = vrml_texture_node->field("url"); |
| 404 | | const openvrml::mfstring *mfs = dynamic_cast<const openvrml::mfstring *>(texture_url_fv.get()); |
| 405 | | const std::string &url = mfs->value()[0]; |
| 406 | | |
| 407 | | osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(url); |
| 408 | | |
| 409 | | if (image != 0) |
| | 406 | osg::ref_ptr<osg::Image> image; |
| | 407 | |
| | 408 | if (vrml_texture_node->type().id() == "ImageTexture") |
| | 409 | { |
| | 410 | try |
| | 411 | { |
| | 412 | std::auto_ptr<openvrml::field_value> texture_url_fv = vrml_texture_node->field("url"); |
| | 413 | const openvrml::mfstring *mfs = dynamic_cast<const openvrml::mfstring *>(texture_url_fv.get()); |
| | 414 | const std::string &url = mfs->value()[0]; |
| | 415 | |
| | 416 | image = osgDB::readRefImageFile(url); |
| | 417 | |
| | 418 | if (!image.valid()) |
| | 419 | { |
| | 420 | std::cerr << "texture file " << url << " not found !" << std::endl << std::flush; |
| | 421 | } |
| | 422 | } |
| | 423 | catch (openvrml::unsupported_interface&) |
| | 424 | { |
| | 425 | // no url field in the texture |
| | 426 | } |
| | 427 | } |
| | 428 | |
| | 429 | if (!image.valid()) |
| | 430 | { |
| | 431 | // If we cannot read the image try the openvrml builtin mechanisms to read the image. |
| | 432 | // This includes PixelTexture fields. |
| | 433 | const openvrml::image& vrml_image = vrml_texture_node->image(); |
| | 434 | |
| | 435 | // Convert to an osg image |
| | 436 | image = new osg::Image; |
| | 437 | image->allocateImage(vrml_image.x(), vrml_image.y(), 1, GL_RGBA, GL_UNSIGNED_BYTE); |
| | 438 | for (std::size_t y = 0; y < vrml_image.y(); ++y) |
| | 439 | { |
| | 440 | for (std::size_t x = 0; x < vrml_image.x(); ++x) |
| | 441 | { |
| | 442 | openvrml::int32 p = vrml_image.pixel(x, y); |
| | 443 | unsigned char* data = image->data(x, y); |
| | 444 | data[0] = 0xff & (p >> 24); |
| | 445 | data[1] = 0xff & (p >> 16); |
| | 446 | data[2] = 0xff & (p >> 8); |
| | 447 | data[3] = 0xff & (p >> 0); |
| | 448 | } |
| | 449 | } |
| | 450 | } |
| | 451 | |
| | 452 | if (image.valid()) |
| 414 | | // defaults |
| 415 | | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); |
| | 457 | // get the real texture wrapping parameters |
| | 458 | if (vrml_texture_node->repeat_s()) |
| | 459 | { |
| | 460 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); |
| | 461 | } |
| | 462 | else |
| | 463 | { |
| | 464 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP); |
| | 465 | } |
| | 466 | |
| | 467 | if (vrml_texture_node->repeat_t()) |
| | 468 | { |
| | 469 | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); |
| | 470 | } |
| | 471 | else |
| | 472 | { |
| | 473 | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP); |
| | 474 | } |
| 417 | | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); |
| 418 | | |
| 419 | | // get the real texture wrapping parameters (if any) |
| 420 | | try |
| 421 | | { |
| 422 | | std::auto_ptr<openvrml::field_value> wrap_fv = vrml_texture_node->field("repeatS"); |
| 423 | | const openvrml::sfbool *sfb = dynamic_cast<const openvrml::sfbool *>(wrap_fv.get()); |
| 424 | | |
| 425 | | if (!sfb->value()) |
| 426 | | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP); |
| 427 | | |
| 428 | | } |
| 429 | | catch (...) |
| 430 | | { |
| 431 | | // nothing specified |
| 432 | | } |
| 433 | | |
| 434 | | try |
| 435 | | { |
| 436 | | std::auto_ptr<openvrml::field_value> wrap_fv = vrml_texture_node->field("repeatT"); |
| 437 | | const openvrml::sfbool *sfb = dynamic_cast<const openvrml::sfbool *>(wrap_fv.get()); |
| 438 | | |
| 439 | | if (!sfb->value()) |
| 440 | | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP); |
| 441 | | } |
| 442 | | catch (...) |
| 443 | | { |
| 444 | | // nothing specified |
| 445 | | } |