//-----------------------------------------------------------------------
    void ColourImageAffector::_initParticle(Particle* pParticle)
	{
        if (!mColourImageLoaded)
        {
            _loadImage();
        }

        pParticle->colour = mColourImage.getColourAt(0, 0, 0);
    
	}
    //-----------------------------------------------------------------------
    void ColourImageAffector::_affectParticles(ParticleSystem* pSystem, Real timeElapsed)
    {
        Particle*			p;
		ParticleIterator	pi				= pSystem->_getIterator();

        if (!mColourImageLoaded)
        {
            _loadImage();
        }

		int				   width			= (int)mColourImage.getWidth()  - 1;
        
		while (!pi.end())
		{
			p = pi.getNext();
			const Real		life_time		= p->totalTimeToLive;
			Real			particle_time	= 1.0f - (p->timeToLive / life_time); 

			if (particle_time > 1.0f)
				particle_time = 1.0f;
			if (particle_time < 0.0f)
				particle_time = 0.0f;

			const Real		float_index		= particle_time * width;
			const int		index			= (int)float_index;

            if(index < 0)
            {
				p->colour = mColourImage.getColourAt(0, 0, 0);
            }
            else if(index >= width) 
            {
                p->colour = mColourImage.getColourAt(width, 0, 0);
            }
            else
            {
                // Linear interpolation
				const Real		fract		= float_index - (Real)index;
				const Real		to_colour	= fract;
				const Real		from_colour	= 1.0f - to_colour;
             
                ColourValue from=mColourImage.getColourAt(index, 0, 0),
							to=mColourImage.getColourAt(index+1, 0, 0);

				p->colour.r = from.r*from_colour + to.r*to_colour;
                p->colour.g = from.g*from_colour + to.g*to_colour;
                p->colour.b = from.b*from_colour + to.b*to_colour;
                p->colour.a = from.a*from_colour + to.a*to_colour;
			}
		}
    }
Exemplo n.º 3
0
	bool TerrainQuery::getTerrainData(osg::Vec3d& location, osg::Vec4 &texture_color, std::string &coverage_name, CoverageColor &coverage_color, osg::Vec3d &inter)
	{
		osg::Vec3d start_location(location.x(),location.y(), -10000);
		osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector =	new osgUtil::LineSegmentIntersector(start_location,start_location + osg::Vec3(0.0f,0.0f,20000));
		m_IntersectionVisitor.setIntersector(intersector.get());
		m_Terrain->accept(m_IntersectionVisitor);
		if (intersector->containsIntersections())
		{
			osgUtil::LineSegmentIntersector::Intersections& intersections = intersector->getIntersections();
			for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
				itr != intersections.end();
				++itr)
			{
				const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
				osg::Vec3 tc;
				osg::Texture* texture = _getTexture(intersection,tc);

				if(texture && texture->getImage(0))
				{
					std::string tex_filename = osgDB::getSimpleFileName(texture->getImage(0)->getFileName());
				    //check if dds, if so we will try to load alternative image file because we have no utils to decompress dds
					if(osgDB::getFileExtension(tex_filename) == "dds")
					{
						tex_filename = osgDB::getNameLessExtension(tex_filename) + m_ColorTextureSuffix;

						//std::cout << tex_filename <<"\n";
						osg::Image* image = _loadImage(tex_filename);
						if(m_FlipColorCoordinates)
							tc.set(tc.x(),1.0 - tc.y(),tc.z());
						texture_color = image->getColor(tc);
					}
					else
						texture_color = texture->getImage(0)->getColor(tc);

					if (m_CoverageTexture != "" || m_CoverageTextureSuffix != "")
					{
						//get material texture
						std::string mat_image_filename;
						if (m_CoverageTexture != "")
							mat_image_filename = m_CoverageTexture;
						else
							mat_image_filename = osgDB::getNameLessExtension(osgDB::getSimpleFileName(tex_filename)) + m_CoverageTextureSuffix;

						osg::Image* image = _loadImage(mat_image_filename);

						if (m_FlipCoverageCoordinates)
							tc.set(tc.x(), 1.0 - tc.y(), tc.z());

						//tc2 = osg::clampTo(tc2, osg::Vec3(0,0,0),osg::Vec3(1,1,1));
						tc.set(osg::clampTo((double)tc.x(), (double) 0.0, (double) 1.0),
							osg::clampTo((double)tc.y(), (double) 0.0, (double)1.0), (double)tc.z());
						coverage_color = image->getColor(tc);
						coverage_name = m_CoverageData.getCoverageMaterialName(coverage_color);
					}
					else
					{
						coverage_name = "WOODS";
					}
				}
				inter = intersection.getWorldIntersectPoint();
				return true;
			}
		}
		return false;
	}