Ejemplo n.º 1
0
std::string SanitateAssetIdForOgre(const std::string& input)
{
    std::string ret = input;
    ReplaceCharInplace(ret, ':', '_');
    ReplaceCharInplace(ret, '/', '_');
    return ret;
}
Ejemplo n.º 2
0
void EC_OgreMovableTextOverlay::SetText(const std::string& text)
{
    if (!node_)
        return;

    text_ = text;
    boost::trim(text_);
    // Replace few common scandic letters with a and o.
    ReplaceCharInplace(text_, 'Ä', 'A');
    ReplaceCharInplace(text_, 'ä', 'a');
    ReplaceCharInplace(text_, 'Ö', 'O');
    ReplaceCharInplace(text_, 'ö', 'o');
    ReplaceCharInplace(text_, 'Å', 'A');
    ReplaceCharInplace(text_, 'å', 'a');

    try
    {
        text_element_->setCaption(text_);
    }
    catch(...)
    {
        text_ = "";
    }

    textDim_ = GetTextDimensions(text_);
    container_->setDimensions(textDim_.x, textDim_.y);
}
Ejemplo n.º 3
0
    bool EnvironmentModule::HandleNetworkEvent(event_id_t event_id, Foundation::EventDataInterface* data)
    {
        ProtocolUtilities::NetworkEventInboundData *netdata = checked_static_cast<ProtocolUtilities::NetworkEventInboundData *>(data);
        assert(netdata);

        switch(event_id)
        {
        case RexNetMsgLayerData:
        {
            if(terrain_.get())
            {
                static int count = 0;
                bool kill_event = terrain_->HandleOSNE_LayerData(netdata);
                if (environment_editor_)
                    environment_editor_->UpdateTerrain();
                return kill_event;
            }
        }
        case RexNetMsgGenericMessage:
        {
            ProtocolUtilities::NetInMessage &msg = *netdata->message;
            std::string methodname = ProtocolUtilities::ParseGenericMessageMethod(msg);

            if (methodname == "RexPostP")
            {
                OgreRenderer::Renderer *renderer = framework_->GetService<OgreRenderer::Renderer>();
                if (renderer)
                {
                    StringVector vec = ProtocolUtilities::ParseGenericMessageParameters(msg);
                    //Since postprocessing effect was enabled/disabled elsewhere, we have to notify the dialog about the event.
                    //Also, no need to put effect on from the CompositionHandler since the dialog will notify CompositionHandler when 
                    //button is checked
                    if (postprocess_dialog_)
                    {
                        QString effect_name = renderer->GetCompositionHandler()->MapNumberToEffectName(vec.at(0)).c_str();
                        bool enabled = true;
                        if (vec.at(1) == "False")
                            enabled = false;

                        postprocess_dialog_->EnableEffect(effect_name,enabled);
                    }
                }
            }
            else if(methodname == "RexSky" && sky_.get())
            {
                return GetSkyHandler()->HandleRexGM_RexSky(netdata);
            }
            else if (methodname == "RexWaterHeight")
            {
                msg.ResetReading();
                msg.SkipToFirstVariableByName("Parameter");

                // Variable block begins, should have currently (at least) 1 instances.
                size_t instance_count = msg.ReadCurrentBlockInstanceCount();
                if (instance_count < 1)
                    return false;

                if (water_.get() != 0)
                {
                    std::string message = msg.ReadString();
                    // Convert to float.
                    try
                    {
                        float height = boost::lexical_cast<float>(message);
                        water_->SetWaterHeight(height);
                    }
                    catch(boost::bad_lexical_cast&)
                    {
                    }
                }
            }
            else if (methodname == "RexDrawWater")
            {
                msg.ResetReading();
                msg.SkipToFirstVariableByName("Parameter");

                // Variable block begins, should have currently (at least) 1 instances.
                size_t instance_count = msg.ReadCurrentBlockInstanceCount();
                if (instance_count < 1 )
                    return false;

                std::string message = msg.ReadString();
                bool draw = ParseBool(message);
                if (draw)
                    if (water_.get())
                        water_->CreateWaterGeometry();
                    else
                        CreateWater();
                else
                    water_->RemoveWaterGeometry();
            }
            else if (methodname == "RexFog")
            {
                StringVector parameters = ProtocolUtilities::ParseGenericMessageParameters(msg); 
                if ( parameters.size() < 5)
                    return false;

                // may have , instead of . so replace
                ReplaceCharInplace(parameters[0], ',', '.');
                ReplaceCharInplace(parameters[1], ',', '.');
                ReplaceCharInplace(parameters[2], ',', '.');
                ReplaceCharInplace(parameters[3], ',', '.');
                ReplaceCharInplace(parameters[4], ',', '.');
                float fogStart = 0.0, fogEnd = 0.0, fogC_r = 0.0, fogC_g = 0.0, fogC_b = 0.0;

                try
                {
                    fogStart = boost::lexical_cast<float>(parameters[0]);
                    fogEnd = boost::lexical_cast<float>(parameters[1]);
                    fogC_r = boost::lexical_cast<float>(parameters[2]);
                    fogC_g = boost::lexical_cast<float>(parameters[3]);
                    fogC_b = boost::lexical_cast<float>(parameters[4]);
                }
                catch(boost::bad_lexical_cast&)
                {
                    return false;
                }
                if (environment_ != 0)
                {
                    // Adjust fog.
                    QVector<float> color;
                    color<<fogC_r<<fogC_g<<fogC_b;
                    environment_->SetWaterFog(fogStart, fogEnd, color); 
                }
            }
            else if (methodname == "RexAmbientL")
            {
                /**
                 * Deals RexAmbientLight message. 
                 **/
                
                StringVector parameters = ProtocolUtilities::ParseGenericMessageParameters(msg); 
                if ( parameters.size() < 3)
                    return false; 

                // may have , instead of . so replace
                ReplaceCharInplace(parameters[0], ',', '.');
                ReplaceCharInplace(parameters[1], ',', '.');
                ReplaceCharInplace(parameters[2], ',', '.');

                const QChar empty(' ');
                StringVector sun_light_direction = SplitString(parameters[0].c_str(), empty.toAscii() );
                StringVector sun_light_color = SplitString(parameters[1].c_str(), empty.toAscii());
                StringVector ambient_light_color = SplitString(parameters[2].c_str(), empty.toAscii());

                if ( environment_ != 0 )
                {
                      environment_->SetSunDirection(environment_->ConvertToQVector<float>(sun_light_direction));
                      environment_->SetSunColor(environment_->ConvertToQVector<float>(sun_light_color));
                      environment_->SetAmbientLight(environment_->ConvertToQVector<float>(ambient_light_color));
                 }
            }
        }
        case RexNetMsgSimulatorViewerTimeMessage:
        {
            if (environment_!= 0)
                return environment_->HandleSimulatorViewerTimeMessage(netdata);
            break;
        }
        case RexNetMsgRegionHandshake:
        {
            bool kill_event = HandleOSNE_RegionHandshake(netdata);
            if (environment_editor_)
                environment_editor_->UpdateTerrainTextureRanges();
            return kill_event;
        }
        case RexNetMsgRegionInfo:
        {
            if (waiting_for_regioninfomessage_)
            {
                currentWorldStream_->SendTextureCommitMessage();
                waiting_for_regioninfomessage_ = false;
            }
        }
        }

        return false;
    }
Ejemplo n.º 4
0
std::string ReplaceChar(const std::string& str, char replace_this, char replace_with)
{
    std::string ret = str;
    ReplaceCharInplace(ret, replace_this, replace_with);
    return ret;
}
Ejemplo n.º 5
0
bool OgreMaterialResource::SetData(Foundation::AssetPtr source)
{
    // Remove old material if any
    RemoveMaterial();
    references_.clear();
    original_textures_.clear();

    Ogre::MaterialManager& matmgr = Ogre::MaterialManager::getSingleton();

    OgreRenderingModule::LogDebug("Parsing material " + source->GetId());

    if (!source)
    {
        OgreRenderingModule::LogError("Null source asset data pointer");
        return false;
    }
    if (!source->GetSize())
    {
        OgreRenderingModule::LogError("Zero sized material asset");
        return false;
    }

    Ogre::DataStreamPtr data = Ogre::DataStreamPtr(new Ogre::MemoryDataStream(const_cast<u8 *>(source->GetData()), source->GetSize()));

    static int tempname_count = 0;
    tempname_count++;
    std::string tempname = "TempMat" + ToString<int>(tempname_count);

    try
    {
        int num_materials = 0;
        int brace_level = 0;
        bool skip_until_next = false;
        int skip_brace_level = 0;
        // Parsed/modified material script
        std::ostringstream output;


        while (!data->eof())
        {
            Ogre::String line = data->getLine();

            // Skip empty lines & comments
            if ((line.length()) && (line.substr(0, 2) != "//"))
            {
                // Process opening/closing braces
                if (!ResourceHandler::ProcessBraces(line, brace_level))
                {
                    // If not a brace and on level 0, it should be a new material; replace name
                    if ((brace_level == 0) && (line.substr(0, 8) == "material"))
                    {
                        if (num_materials == 0)
                        {
                            line = "material " + tempname;
                            ++num_materials;
                        }
                        else
                        {
                            OgreRenderingModule::LogWarning("More than one material defined in material asset " + source->GetId() + " - only first one supported");
                            break;
                        }
                    }
                    else
                    {
                        // Check for textures
                        if ((line.substr(0, 8) == "texture ") && (line.length() > 8))
                        {
                            std::string tex_name = line.substr(8);
                            // Note: we assume all texture references are asset based. ResourceHandler checks later whether this is true,
                            // before requesting the reference
                            references_.push_back(Foundation::ResourceReference(tex_name, OgreTextureResource::GetTypeStatic()));
                            original_textures_.push_back(tex_name);
                            // Replace any / with \ in the material, then change the texture names back later, so that Ogre does not go nuts
                            ReplaceCharInplace(line, '/', '\\');
                            ReplaceCharInplace(line, ':', '@');
                        }
                    }

                    // Write line to the modified copy
                    if (!skip_until_next)
                        output << line << std::endl;
                }
                else
                {
                    // Write line to the modified copy
                    if (!skip_until_next)
                        output << line << std::endl;
                    if (brace_level <= skip_brace_level)
                        skip_until_next = false;
                }
            }
        }

        std::string output_str = output.str();
        Ogre::DataStreamPtr modified_data = Ogre::DataStreamPtr(new Ogre::MemoryDataStream((u8 *)(&output_str[0]), output_str.size()));

        matmgr.parseScript(modified_data, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
        Ogre::MaterialPtr tempmat;
        tempmat = matmgr.getByName(tempname);
        if (tempmat.isNull())
        {
            OgreRenderingModule::LogWarning(std::string("Failed to create an Ogre material from material asset ") +
                                            source->GetId());

            return false;
        }
        if(!tempmat->getNumTechniques())
        {
            OgreRenderingModule::LogWarning("Failed to create an Ogre material from material asset "  +
                                            source->GetId());
            return false;
        }

        ogre_material_ = tempmat->clone(id_);
        tempmat.setNull();
        matmgr.remove(tempname);
        if (ogre_material_.isNull())
        {
            OgreRenderingModule::LogWarning("Failed to create an Ogre material from material asset "  +
                                            source->GetId());
            return false;
        }

        // Now go through all the texturenames and restore \ back to / and @ to :
        Ogre::Material::TechniqueIterator iter = ogre_material_->getTechniqueIterator();
        while (iter.hasMoreElements())
        {
            Ogre::Technique *tech = iter.getNext();
            Ogre::Technique::PassIterator passIter = tech->getPassIterator();
            while (passIter.hasMoreElements())
            {
                Ogre::Pass *pass = passIter.getNext();
                Ogre::Pass::TextureUnitStateIterator texIter = pass->getTextureUnitStateIterator();
                while (texIter.hasMoreElements())
                {
                    Ogre::TextureUnitState *texUnit = texIter.getNext();
                    std::string texname = texUnit->getTextureName();
                    if (texname.find('\\') != std::string::npos)
                    {
                        ReplaceCharInplace(texname, '\\', '/');
                        ReplaceCharInplace(texname, '@', ':');
                        texUnit->setTextureName(texname);
                    }
                }
            }
        }

        //workaround: if receives shadows, check the amount of shadowmaps. If only 1 specified, add 2 more to support 3 shadowmaps
        if(ogre_material_->getReceiveShadows() && shadowquality_ == Shadows_High && ogre_material_->getNumTechniques() > 0)
        {
            Ogre::Technique *tech = ogre_material_->getTechnique(0);
            if(tech)
            {
                Ogre::Technique::PassIterator passiterator = tech->getPassIterator();
                while(passiterator.hasMoreElements())
                {
                    Ogre::Pass* pass = passiterator.getNext();
                    Ogre::Pass::TextureUnitStateIterator texiterator = pass->getTextureUnitStateIterator();
                    int shadowmaps = 0;
                    while(texiterator.hasMoreElements())
                    {
                        Ogre::TextureUnitState* state = texiterator.getNext();
                        if(state->getContentType() == Ogre::TextureUnitState::CONTENT_SHADOW)
                        {
                            shadowmaps++;
                        }
                    }
                    if(shadowmaps>0 && shadowmaps<3)
                    {
                        Ogre::TextureUnitState* sm2 = pass->createTextureUnitState();
                        sm2->setContentType(Ogre::TextureUnitState::CONTENT_SHADOW);

                        Ogre::TextureUnitState* sm3 = pass->createTextureUnitState();
                        sm3->setContentType(Ogre::TextureUnitState::CONTENT_SHADOW);
                    }
                }
            }

        }

    } catch (Ogre::Exception &e)
    {
        OgreRenderingModule::LogWarning(e.what());
        OgreRenderingModule::LogWarning("Failed to parse Ogre material " + source->GetId() + ".");
        try
        {
            if (!matmgr.getByName(tempname).isNull())
                Ogre::MaterialManager::getSingleton().remove(tempname);
        }
        catch (...) {}

        return false;
    }
    return true;
}