示例#1
0
void TerrainManager::EditTerrain(EditTerrainType type, float lastframe, const Ogre::Ray &ray)
{
	if( !m_pkTerrainGroup )
		return;
	Ogre::TerrainGroup::RayResult rayResult = m_pkTerrainGroup->rayIntersects(ray);
	if( !rayResult.hit )
		return;
	Ogre::TerrainGroup::TerrainList list;
	float fBrushWorldSize = m_fWorldTerrainSize * m_fBrushSize;
	Ogre::Sphere sphere(rayResult.position, fBrushWorldSize);
	m_pkTerrainGroup->sphereIntersects(sphere, &list);

	if( type == ET_HEIGHT )
	{
		for(Ogre::TerrainGroup::TerrainList::iterator it = list.begin(); it != list.end(); ++it)
			EditHeightTerrain(*it, rayResult.position, lastframe);
	}
	else if( type == ET_LAYER )
	{
		for(Ogre::TerrainGroup::TerrainList::iterator it = list.begin(); it != list.end(); ++it)
			EditLayerTerrain(*it, rayResult.position, lastframe);
	}
}
//-----------------------------------------------------------------------------------------
bool CTerrainGroupEditor::update(float timePassed)
{
    if(mEditActive && mDecalFrustum->getVisible())
    {
        mEditDirection = CViewportEditor::mViewKeyboard[CViewportEditor::mSpecial.SPK_REVERSE_UPDATE];

        Ogre::Vector3 cursorpos = mDecalNode->getPosition();
        Ogre::Terrain *terrain;
        Ogre::Vector3 editpos;

        Ogre::TerrainGroup::TerrainList terrainList;
        Ogre::Real halfBrushSizeWorldSpace = (Ogre::Real)(mWorldSize->get() * mBrushSize) / (Ogre::Real)(mMapSize->get()) / 2.0f;
        Ogre::Sphere sphere(cursorpos, halfBrushSizeWorldSpace);
        mHandle->sphereIntersects(sphere, &terrainList);

        mModificationRect.merge(Ogre::Rect(cursorpos.x - halfBrushSizeWorldSpace, cursorpos.z - halfBrushSizeWorldSpace, cursorpos.x + halfBrushSizeWorldSpace, cursorpos.z + halfBrushSizeWorldSpace));

        bool groupUpdateNeeded = false;

        float avg_total = 0.0f;
        float sample_count_total = 0;

        if(mEditMode == EM_SMOOTH)
        {
            for (Ogre::TerrainGroup::TerrainList::iterator ti = terrainList.begin(); ti != terrainList.end(); ++ti)
            {
                terrain = *ti;
                terrain->getTerrainPosition(cursorpos, &editpos);

                CTerrainPageEditor *terED = 0;

                for(NameObjectPairList::iterator it = mChildren.begin(); it != mChildren.end();it++)
                {
                    if(it->second->getHandle() == (void*)terrain)
                    {
                        terED = static_cast<CTerrainPageEditor*>(it->second);
                        break;
                    }
                }

                float avg = 0.0f;
                int sample_count = 0;
                _calculatesmoothingfactor(terED, editpos, avg, sample_count);

                avg_total += avg;
                sample_count_total += sample_count;
            }
        }

        for (Ogre::TerrainGroup::TerrainList::iterator ti = terrainList.begin(); ti != terrainList.end(); ++ti)
        {
            terrain = *ti;
            terrain->getTerrainPosition(cursorpos, &editpos);

            CTerrainPageEditor *terED = 0;

            for(NameObjectPairList::iterator it = mChildren.begin(); it != mChildren.end();it++)
            {
                if(it->second->getHandle() == (void*)terrain)
                {
                    terED = static_cast<CTerrainPageEditor*>(it->second);
                    break;
                }
            }

            if(mEditMode == EM_DEFORM)
            {
                _deform(terED, editpos, timePassed);
                groupUpdateNeeded |= true;
            }
            else if(mEditMode == EM_SMOOTH)
            {
                if(sample_count_total)
                {
                    _smooth(terED, editpos, avg_total / (float)sample_count_total, timePassed);
                    groupUpdateNeeded |= true;
                }
            }
            else if(mEditMode == EM_SPLAT)
                _splat(terED, editpos, timePassed);
            else if(mEditMode == EM_PAINT)
                _paint(terED, editpos, timePassed);
            else if(mEditMode == EM_SPLATGRASS)
                _splatGrass(terED, editpos, timePassed);
        }

        if(groupUpdateNeeded)
            mHandle->update();

        mOgitorsRoot->SetSceneModified(true);
    }

    if(mPGHandle)
        mPGHandle->update();

    return false;
}