Ejemplo n.º 1
0
/// \todo Use Asset API for fetching sky resources.
EC_SkyBox::EC_SkyBox(IModule *module) :
    IComponent(module->GetFramework()),
    materialRef(this, "Material", AssetReference("RexSkyBox")), ///< \todo Add "orge://" when AssetAPI can handle it.
    textureRefs(this, "Texture"),
    orientation(this, "Orientation", Quaternion(f32(M_PI/2.0), Vector3df(1.0,0.0,0.0))),
    distance(this, "Distance",50.0),
    drawFirst(this, "Draw first", true)
{
     connect(this, SIGNAL(AttributeChanged(IAttribute*, AttributeChange::Type)), SLOT(OnAttributeUpdated(IAttribute*)));

     static AttributeMetadata materialRefMetadata;
     AttributeMetadata::ButtonInfoList materialRefButtons;
     materialRefButtons.push_back(AttributeMetadata::ButtonInfo(materialRef.GetName(), "V", "View"));
     materialRefMetadata.buttons = materialRefButtons;
     materialRef.SetMetadata(&materialRefMetadata);

     // Find out default textures.
     renderer_ = module->GetFramework()->GetServiceManager()->GetService<OgreRenderer::Renderer>();

     StringVector names;
     Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().getByName(materialRef.Get().ref.toStdString().c_str());
     if ( materialPtr.get() != 0)
     {
         OgreRenderer::GetTextureNamesFromMaterial(materialPtr, names);
         AssetReferenceList lst;
         if (names.size() == cSkyBoxTextureCount)
         {
            // This code block is not currently working, but if for some reason GetTextureNamesFromMaterialn understands cubic_textures this codeblock is runned
            for(int i = 0; i < cSkyBoxTextureCount; ++i)
                lst.Append(AssetReference(names[i].c_str()));
         }
         else
         {
            // Add default values, hardcoded
            /// HACK use hardcoded-values because ogre textureunit state class cannot find out texture names for cubic_texture type.
            lst.Append(AssetReference(names[0].c_str()));
            lst.Append(AssetReference("rex_sky_back.dds"));
            lst.Append(AssetReference("rex_sky_left.dds"));
            lst.Append(AssetReference("rex_sky_right.dds"));
            lst.Append(AssetReference("rex_sky_top.dds"));
            lst.Append(AssetReference("rex_sky_bot.dds"));
         }

        textureRefs.Set(lst, AttributeChange::LocalOnly);
     }

    // Disable old sky.
    // DisableSky();
     CreateSky();

     lastMaterial_ = materialRef.Get().ref;
     lastOrientation_ = orientation.Get();
     lastDistance_ = distance.Get();
     lastDrawFirst_ = drawFirst.Get();
     lastTextures_ = textureRefs.Get();
}
Ejemplo n.º 2
0
void EC_OgreSky::SetSkyDomeMaterialTexture(const char *texture_name)
{
    type_ = SKYTYPE_DOME;

    Ogre::MaterialPtr skyMaterial = Ogre::MaterialManager::getSingleton().getByName(skyDomeParameters.material);
    if (!skyMaterial.isNull())
    {
        skyMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(texture_name);
        skyMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_WRAP);
        CreateSky();
    }
}
Ejemplo n.º 3
0
void EC_OgreSky::SetSkyBoxParameters(const SkyBoxParameters &params, bool update_sky)
{
    skyBoxParameters.material = params.material;
    skyBoxParameters.angle = params.angle;
    skyBoxParameters.angleAxis = params.angleAxis;
    skyBoxParameters.distance = params.distance;
    skyBoxParameters.drawFirst = params.drawFirst;
    if(update_sky)
    {
        type_ = SKYTYPE_BOX;
        CreateSky();
    }
}
Ejemplo n.º 4
0
void EC_Sky::OnTextureAssetLoaded(AssetPtr tex)
{
    std::vector<std::string> texture_names;
    texture_names.reserve(cSkyBoxTextureCount);
    
    AssetReferenceList textureList = textureRefs.Get();

    const char * const defaultSkyTextures[cSkyBoxTextureCount] =
    {   "rex_sky_front.dds",
        "rex_sky_back.dds",
        "rex_sky_left.dds",
        "rex_sky_right.dds",
        "rex_sky_top.dds",
        "rex_sky_bot.dds"
    };

    for(size_t i = 0; i < textureAssets.size() || i < cSkyBoxTextureCount; ++i)
        if (i < textureAssets.size() && textureAssets[i])
        {
            AssetPtr asset = textureAssets[i]->Asset();
            TextureAsset *textureAsset = dynamic_cast<TextureAsset*>(asset.get());
            if (textureAsset)
                texture_names.push_back(textureAsset->ogreAssetName.toStdString());
            else
                texture_names.push_back(defaultSkyTextures[i]);
        }
        else
            texture_names.push_back(defaultSkyTextures[i]);

    assert(texture_names.size() == cSkyBoxTextureCount);

    ///\todo Use AssetAPI for the material.
    Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().getByName(materialRef.Get().ref.toStdString().c_str());
    if (materialPtr.isNull())
    {
        LogError("EC_Sky::OnTextureAssetLoaded: Cannot find Ogre material \"" + materialRef.Get().ref.toStdString() + "\"!");
        return;
    }
    if (materialPtr->getNumTechniques() == 0 || materialPtr->getTechnique(0) == 0 ||
        materialPtr->getTechnique(0)->getNumPasses() == 0 || materialPtr->getTechnique(0)->getPass(0) == 0 ||
        materialPtr->getTechnique(0)->getPass(0)->getNumTextureUnitStates() == 0 ||
        materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0) == 0)
    {
        LogError("EC_Sky::OnTextureAssetLoaded: Cannot use material \"" + materialRef.Get().ref.toStdString() + "\" as Skybox material: It has 0 techniques, passes or texture unit states!");
        return;
    }

    materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setCubicTextureName(&texture_names[0], false);
    
    CreateSky();    
}
Ejemplo n.º 5
0
void EC_OgreSky::SetSkyPlaneParameters(const SkyPlaneParameters &params, bool update_sky)
{
    skyPlaneParameters.material = params.material;
    skyPlaneParameters.bow = params.bow;
    skyPlaneParameters.scale = params.scale;
    skyPlaneParameters.tiling = params.tiling;
    skyPlaneParameters.distance = params.distance;
    skyPlaneParameters.xSegments = params.xSegments;
    skyPlaneParameters.ySegments = params.ySegments;
    skyPlaneParameters.drawFirst = params.drawFirst;
    if(update_sky)
    {
        type_ = SKYTYPE_PLANE;
        CreateSky();
    }
}
Ejemplo n.º 6
0
    bool EnvironmentModule::HandleEvent(event_category_id_t category_id, event_id_t event_id, Foundation::EventDataInterface* data)
    {
        if(category_id == framework_event_category_)
        {
            HandleFrameworkEvent(event_id, data);
        }
        else if(category_id == resource_event_category_)
        {
            HandleResouceEvent(event_id, data);
        }
        else if(category_id == network_in_event_category_)
        {
            HandleNetworkEvent(event_id, data);
        }
        else if (category_id == network_state_event_category_)
        {
            if (event_id == ProtocolUtilities::Events::EVENT_SERVER_CONNECTED)
            {
                if (GetFramework()->GetDefaultWorldScene().get())
                {
                    CreateEnvironment();
                    CreateTerrain();
                    CreateWater();
                    //CreateEnvironment();
                    CreateSky();
                }
            }

            if (event_id == ProtocolUtilities::Events::EVENT_SERVER_DISCONNECTED)
            {
                if(postprocess_dialog_)
                    postprocess_dialog_->DisableAllEffects();
                ReleaseTerrain();
                ReleaseWater();
                ReleaseEnvironment();
                ReleaseSky();
                firstTime_ = true;
               
            }
        }
        else if(category_id == input_event_category_)
        {
            HandleInputEvent(event_id, data);
        }
        return false;
    }
Ejemplo n.º 7
0
void EC_OgreSky::SetSkyDomeParameters(const SkyDomeParameters &params, bool update_sky)
{
    skyDomeParameters.material = params.material;
    skyDomeParameters.curvature = params.curvature;
    skyDomeParameters.tiling = params.tiling;
    skyDomeParameters.distance = params.distance;
    skyDomeParameters.xSegments = params.xSegments;
    skyDomeParameters.ySegments = params.ySegments;
    skyDomeParameters.ySegmentsKeep = params.ySegmentsKeep;
    skyDomeParameters.drawFirst = params.drawFirst;
    skyDomeParameters.angle = params.angle;
    skyDomeParameters.angleAxis = params.angleAxis;
    if(update_sky)
    {
        type_ = SKYTYPE_DOME;
        CreateSky();
    }
}
Ejemplo n.º 8
0
void EC_Sky::OnAttributeUpdated(IAttribute* attribute)
{
    if (!ViewEnabled())
        return;

    if ((attribute->Name() == materialRef.Name() && materialRef.Get().ref != lastMaterial_ ) ||
        (attribute->Name() ==  distance.Name() && distance.Get() != lastDistance_ ) ||
        (attribute->Name() == drawFirst.Name() && drawFirst.Get() != lastDrawFirst_ ))
    {
        DisableSky();
        CreateSky();

        lastMaterial_ = materialRef.Get().ref;
        lastDistance_ = distance.Get();
        lastDrawFirst_ = drawFirst.Get();
    }
    else if (attribute->Name() == textureRefs.Name())
    {
        AssetReferenceList textures = textureRefs.Get();
        // Make sure that the asset ref list type stays intact.
        textures.type = "Texture";
        textureRefs.Set(textures, AttributeChange::Disconnected);

        // Reallocate the number of texture asset reflisteners.
        while(textureAssets.size() > (size_t)textures.Size())
            textureAssets.pop_back();
        while(textureAssets.size() < (size_t)textures.Size())
            textureAssets.push_back(boost::shared_ptr<AssetRefListener>(new AssetRefListener));

        for(int i = 0; i < textures.Size(); ++i)
        {
            connect(textureAssets[i].get(), SIGNAL(Loaded(AssetPtr)), this, SLOT(OnTextureAssetLoaded(AssetPtr)), Qt::UniqueConnection);
            textureAssets[i]->HandleAssetRefChange(framework->Asset(), textures[i].ref);
        }

        //SetTextures();
    }
}
Ejemplo n.º 9
0
void EC_SkyBox::SetTextures()
{
    if (!ViewEnabled())
        return;

    AssetReferenceList lst = textureRefs.Get();
    std::vector<std::string> texture_names;
    texture_names.reserve(6);

    for(int i = 0; i < lst.Size() && i <= 6; ++i)
        texture_names.push_back(lst[i].ref.toStdString());

    Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().getByName(materialRef.Get().ref.toStdString().c_str());
    if (!materialPtr.isNull() && texture_names.size() == 6)
        materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setCubicTextureName(&texture_names[0], false);
        //skyMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureScale(1, -1);
    else if(!materialPtr.isNull() )
        for(int i = 0; i < texture_names.size(); ++i)
            materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setFrameTextureName(Ogre::String(texture_names[i].c_str()), i);

    DisableSky();
    CreateSky();
}
Ejemplo n.º 10
0
void EC_SkyBox::OnAttributeUpdated(IAttribute* attribute)
{
    if (!ViewEnabled())
        return;

    std::string name = attribute->GetNameString();
    if ((name == materialRef.GetNameString() && materialRef.Get().ref != lastMaterial_ ) ||
        (name ==  distance.GetNameString() && distance.Get() != lastDistance_ ) ||
        (name == drawFirst.GetNameString() && drawFirst.Get() != lastDrawFirst_ ))
    {
        DisableSky();
        CreateSky();

        lastMaterial_ = materialRef.Get().ref;
        lastDistance_ = distance.Get();
        lastDrawFirst_ = drawFirst.Get();
    }
    else if (name == textureRefs.GetNameString() )
    {
        // What texture has changed?
        SetTextures();
    }
}
Ejemplo n.º 11
0
void EC_OgreSky::SetSkyType(SkyType type, bool update_sky)
{
    type_ = type;
    if(update_sky)
        CreateSky();
}
Ejemplo n.º 12
0
void EC_OgreSky::SetSkyBoxMaterialTexture(int index, const char *texture_name, size_t image_count/*const SkyImageData *imageData*/)
{
    type_ = SKYTYPE_BOX;
    //genericSkyParameters.disance = distance;
    // The SkyBox textures are upside down, because there is no easy way to flip cubemap textures vertically.
//    skyBoxImages_.resize(6);
    skyBoxImages_[index] = texture_name;
    
    ++currentSkyBoxImageCount_;
    if (currentSkyBoxImageCount_ == image_count)
    {
        Ogre::MaterialPtr skyMaterial = Ogre::MaterialManager::getSingleton().getByName(skyBoxParameters.material);
        if (!skyMaterial.isNull() && image_count == 6)
        {
            skyMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setCubicTextureName(&skyBoxImages_[0], false);
            //skyMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureScale(1, -1);
            CreateSky();
            currentSkyBoxImageCount_ = 0;
        }
        else if(image_count < 6) // If all materials textures dont need to be changed, use code below.
        {
            for(uint i = 0; i < 6; i++)
            {
                if(skyBoxImages_[i] != "")
                    skyMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setFrameTextureName(Ogre::String(skyBoxImages_[i]), i);
            }
            CreateSky();
            currentSkyBoxImageCount_ = 0;
        }
        ///\todo
        /*
        // Lets create scaled down cubemap texture for water reflections.
        // We apparently need to create a separate cubemap texture for the environment map as
        // there's no way to use the existing ones. It's also good idea to scale the cubemap down since
        // it doesn't need to be very accurate.

        // For ease, we create scaled down versions of the skybox textures and then copy the new textures into
        // cubemap faces and afterwards remove the new scaled down textures.
        std::vector<Ogre::TexturePtr> scaledTextures;
        scaledTextures.reserve(skyBoxImageCount_);
        for (size_t n = 0; n < skyBoxImages_.size(); ++n)
        {
            Ogre::String defaultTextureName = skyBoxImages_[n];
            if (!defaultTextureName.empty())
            {
                Ogre::TexturePtr defaultTexture = Ogre::TextureManager::getSingleton().getByName(defaultTextureName);
                if (!defaultTexture.isNull())
                {
                    Ogre::uint width = std::max(defaultTexture->getWidth() / 2, (Ogre::uint)2);
                    Ogre::uint height = std::max(defaultTexture->getHeight() / 2, (Ogre::uint)2);

                    Ogre::TexturePtr newTexture = Ogre::TextureManager::getSingleton().createManual("scaled" + Ogre::StringConverter::toString(n),
                    Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, width, height, Ogre::MIP_UNLIMITED, defaultTexture->getBuffer()->getFormat());
                    defaultTexture->copyToTexture(newTexture);
                    scaledTextures.push_back(newTexture);
                }
            }
        }

        Ogre::TexturePtr cubeTex = (Ogre::TexturePtr)Ogre::TextureManager::getSingleton().getByName("RexSkyboxCubic");
        if (cubeTex.isNull())
        {
            Ogre::TexturePtr tex = scaledTextures[0];
            cubeTex = Ogre::TextureManager::getSingleton().createManual("RexSkyboxCubic",
                Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_CUBE_MAP,
                tex->getWidth(), tex->getHeight(), tex->getDepth(), tex->getNumMipmaps(), tex->getBuffer()->getFormat());
        }

        const int side[] = {3, 2, 4, 5, 0, 1};
        for (int i = 0; i < skyBoxImageCount_; ++i)
        {
            size_t index = side[i];
            if (index < scaledTextures.size())
            {
                Ogre::TexturePtr face = scaledTextures[index];
                Ogre::HardwarePixelBufferSharedPtr buffer = cubeTex->getBuffer(i);
                buffer->blit(face->getBuffer());
            }
        }

        for(n = 0; n < scaledTextures.size(); ++n)
            Ogre::TextureManager::getSingleton().remove(static_cast<Ogre::ResourcePtr>(scaledTextures[n]));
        
        ///\todo Update water reflection
        Ogre::MaterialPtr waterMaterial = Ogre::MaterialManager::getSingleton().getByName(mWaterMaterial);
        if (!waterMaterial.isNull())
        {
            llinfos << "Updating water material..." << llendl;
            Ogre::TextureUnitState* state = waterMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(1);
            state->setCubicTextureName("RexSkyboxCubic", true);
            state->setTextureCoordSet(1);
        }*/
    }
}
Ejemplo n.º 13
0
/// \todo Use Asset API for fetching sky resources.
EC_Sky::EC_Sky(Scene* scene) :
    IComponent(scene),
    materialRef(this, "Material", AssetReference("RexSkyBox")), ///< \todo Add "ogre://" when AssetAPI can handle it.
    textureRefs(this, "Texture", AssetReferenceList("Texture")),
    orientation(this, "Orientation", Quat::identity),
    distance(this, "Distance",50.0),
    drawFirst(this, "Draw first", true)
{
    connect(this, SIGNAL(AttributeChanged(IAttribute*, AttributeChange::Type)), SLOT(OnAttributeUpdated(IAttribute*)));

    static AttributeMetadata materialRefMetadata;
    AttributeMetadata::ButtonInfoList materialRefButtons;
    materialRefButtons.push_back(AttributeMetadata::ButtonInfo(materialRef.Name(), "V", "View"));
    materialRefMetadata.buttons = materialRefButtons;
    materialRef.SetMetadata(&materialRefMetadata);

    // Find out default textures.
    if (scene)
        world_ = scene->GetWorld<OgreWorld>();

    StringVector names;
    Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().getByName(materialRef.Get().ref.toStdString().c_str());
    if (materialPtr.get() != 0)
    {
        OgreRenderer::GetTextureNamesFromMaterial(materialPtr, names);
        AssetReferenceList lst("Texture");
        if (names.size() == cSkyBoxTextureCount)
        {
            // This code block is not currently working, but if for some reason GetTextureNamesFromMaterial understands cubic_textures this codeblock is runned
            for(int i = 0; i < cSkyBoxTextureCount; ++i)
                lst.Append(AssetReference(names[i].c_str()));
        }
        else
        {
            // Add default values, hardcoded
            /// HACK use hardcoded-values because ogre textureunit state class cannot find out texture names for cubic_texture type.
            lst.Append(AssetReference(names[0].c_str()));
            lst.Append(AssetReference("rex_sky_back.dds"));
            lst.Append(AssetReference("rex_sky_left.dds"));
            lst.Append(AssetReference("rex_sky_right.dds"));
            lst.Append(AssetReference("rex_sky_top.dds"));
            lst.Append(AssetReference("rex_sky_bot.dds"));
    }

    textureRefs.Set(lst, AttributeChange::LocalOnly);
    }

    // Disable old sky.
    // DisableSky();
    CreateSky();

    lastMaterial_ = materialRef.Get().ref;
    lastOrientation_ = orientation.Get();
    lastDistance_ = distance.Get();
    lastDrawFirst_ = drawFirst.Get();

    while(textureAssets.size() < cSkyBoxTextureCount)
    textureAssets.push_back(boost::shared_ptr<AssetRefListener>(new AssetRefListener));

    for(int i = 0; i < cSkyBoxTextureCount; ++i)
    {
        connect(textureAssets[i].get(), SIGNAL(Loaded(AssetPtr)), this, SLOT(OnTextureAssetLoaded(AssetPtr)), Qt::UniqueConnection);
        //materialAssets[i]->HandleAssetRefChange(framework->Asset(), materials[i].ref);
    }
}