| 617 | |
| 618 | osg::Texture* LineSegmentIntersector::Intersection::getTextureLookUp(osg::Vec3& tc) const |
| 619 | { |
| 620 | osg::Geometry* geometry = drawable.valid() ? drawable->asGeometry() : 0; |
| 621 | osg::Vec3Array* vertices = geometry ? dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()) : 0; |
| 622 | |
| 623 | if (vertices) |
| 624 | { |
| 625 | if (indexList.size()==3 && ratioList.size()==3) |
| 626 | { |
| 627 | unsigned int i1 = indexList[0]; |
| 628 | unsigned int i2 = indexList[1]; |
| 629 | unsigned int i3 = indexList[2]; |
| 630 | |
| 631 | float r1 = ratioList[0]; |
| 632 | float r2 = ratioList[1]; |
| 633 | float r3 = ratioList[2]; |
| 634 | |
| 635 | osg::Array* texcoords = (geometry->getNumTexCoordArrays()>0) ? geometry->getTexCoordArray(0) : 0; |
| 636 | osg::FloatArray* texcoords_FloatArray = dynamic_cast<osg::FloatArray*>(texcoords); |
| 637 | osg::Vec2Array* texcoords_Vec2Array = dynamic_cast<osg::Vec2Array*>(texcoords); |
| 638 | osg::Vec3Array* texcoords_Vec3Array = dynamic_cast<osg::Vec3Array*>(texcoords); |
| 639 | if (texcoords_FloatArray) |
| 640 | { |
| 641 | // we have tex coord array so now we can compute the final tex coord at the point of intersection. |
| 642 | float tc1 = (*texcoords_FloatArray)[i1]; |
| 643 | float tc2 = (*texcoords_FloatArray)[i2]; |
| 644 | float tc3 = (*texcoords_FloatArray)[i3]; |
| 645 | tc.x() = tc1*r1 + tc2*r2 + tc3*r3; |
| 646 | } |
| 647 | else if (texcoords_Vec2Array) |
| 648 | { |
| 649 | // we have tex coord array so now we can compute the final tex coord at the point of intersection. |
| 650 | const osg::Vec2& tc1 = (*texcoords_Vec2Array)[i1]; |
| 651 | const osg::Vec2& tc2 = (*texcoords_Vec2Array)[i2]; |
| 652 | const osg::Vec2& tc3 = (*texcoords_Vec2Array)[i3]; |
| 653 | tc.x() = tc1.x()*r1 + tc2.x()*r2 + tc3.x()*r3; |
| 654 | tc.y() = tc1.y()*r1 + tc2.y()*r2 + tc3.y()*r3; |
| 655 | } |
| 656 | else if (texcoords_Vec3Array) |
| 657 | { |
| 658 | // we have tex coord array so now we can compute the final tex coord at the point of intersection. |
| 659 | const osg::Vec3& tc1 = (*texcoords_Vec3Array)[i1]; |
| 660 | const osg::Vec3& tc2 = (*texcoords_Vec3Array)[i2]; |
| 661 | const osg::Vec3& tc3 = (*texcoords_Vec3Array)[i3]; |
| 662 | tc.x() = tc1.x()*r1 + tc2.x()*r2 + tc3.x()*r3; |
| 663 | tc.y() = tc1.y()*r1 + tc2.y()*r2 + tc3.y()*r3; |
| 664 | tc.z() = tc1.z()*r1 + tc2.z()*r2 + tc3.z()*r3; |
| 665 | } |
| 666 | else |
| 667 | { |
| 668 | return 0; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | const osg::TexMat* activeTexMat = 0; |
| 673 | const osg::Texture* activeTexture = 0; |
| 674 | |
| 675 | if (drawable->getStateSet()) |
| 676 | { |
| 677 | const osg::TexMat* texMat = dynamic_cast<osg::TexMat*>(drawable->getStateSet()->getTextureAttribute(0,osg::StateAttribute::TEXMAT)); |
| 678 | if (texMat) activeTexMat = texMat; |
| 679 | |
| 680 | const osg::Texture* texture = dynamic_cast<osg::Texture*>(drawable->getStateSet()->getTextureAttribute(0,osg::StateAttribute::TEXTURE)); |
| 681 | if (texture) activeTexture = texture; |
| 682 | } |
| 683 | |
| 684 | for(osg::NodePath::const_reverse_iterator itr = nodePath.rbegin(); |
| 685 | itr != nodePath.rend() && (!activeTexMat || !activeTexture); |
| 686 | ++itr) |
| 687 | { |
| 688 | const osg::Node* node = *itr; |
| 689 | if (node->getStateSet()) |
| 690 | { |
| 691 | if (!activeTexMat) |
| 692 | { |
| 693 | const osg::TexMat* texMat = dynamic_cast<const osg::TexMat*>(node->getStateSet()->getTextureAttribute(0,osg::StateAttribute::TEXMAT)); |
| 694 | if (texMat) activeTexMat = texMat; |
| 695 | } |
| 696 | |
| 697 | if (!activeTexture) |
| 698 | { |
| 699 | const osg::Texture* texture = dynamic_cast<const osg::Texture*>(node->getStateSet()->getTextureAttribute(0,osg::StateAttribute::TEXTURE)); |
| 700 | if (texture) activeTexture = texture; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | if (activeTexMat) |
| 706 | { |
| 707 | osg::Vec4 tc_transformed = osg::Vec4(tc.x(), tc.y(), tc.z() ,0.0f) * activeTexMat->getMatrix(); |
| 708 | tc.x() = tc_transformed.x(); |
| 709 | tc.y() = tc_transformed.y(); |
| 710 | tc.z() = tc_transformed.z(); |
| 711 | |
| 712 | if (activeTexture && activeTexMat->getScaleByTextureRectangleSize()) |
| 713 | { |
| 714 | tc.x() *= static_cast<float>(activeTexture->getTextureWidth()); |
| 715 | tc.y() *= static_cast<float>(activeTexture->getTextureHeight()); |
| 716 | tc.z() *= static_cast<float>(activeTexture->getTextureDepth()); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | return const_cast<osg::Texture*>(activeTexture); |
| 721 | |
| 722 | } |
| 723 | return 0; |
| 724 | } |