Exemple #1
0
void EC_WaterPlane::CreateWaterPlane()
{
    if (!ViewEnabled())
        return;
    
    if (entity_)
        RemoveWaterPlane();
    
    OgreWorldPtr world = world_.lock();
    // Create water plane
    if (world)
    {
        Ogre::SceneManager *sceneMgr = world->GetSceneManager();
        assert(sceneMgr);

        if (node_ != 0)
        {
            int x = xSize.Get();
            int y = ySize.Get();
            float uTile =  scaleUfactor.Get() * x; /// Default x-size 5000 --> uTile 1.0
            float vTile =  scaleVfactor.Get() * y;
            
            Ogre::MeshPtr mesh = Ogre::MeshManager::getSingleton().createPlane(Name().toStdString().c_str(),
                Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::Plane(Ogre::Vector3::UNIT_Y, 0),
                x, y, xSegments.Get(), ySegments.Get(), true, 1, uTile, vTile, Ogre::Vector3::UNIT_X);
            
            entity_ = sceneMgr->createEntity(world->GetUniqueObjectName("EC_WaterPlane_entity"), Name().toStdString().c_str());
            entity_->setMaterialName(materialName.Get().toStdString().c_str());
            entity_->setCastShadows(false);
            // Tries to attach entity, if there is not EC_Placeable availible, it will not attach object
            AttachEntity();
        }
    }
}
Exemple #2
0
EC_Mesh::EC_Mesh(Scene* scene) :
    IComponent(scene),
    nodeTransformation(this, "Transform", Transform(float3(0,0,0),float3(0,0,0),float3(1,1,1))),
    meshRef(this, "Mesh ref", AssetReference("", "OgreMesh")),
    skeletonRef(this, "Skeleton ref", AssetReference("", "OgreSkeleton")),
    meshMaterial(this, "Mesh materials", AssetReferenceList("OgreMaterial")),
    drawDistance(this, "Draw distance", 0.0f),
    castShadows(this, "Cast shadows", false),
    entity_(0),
    attached_(false)
{
    if (scene)
        world_ = scene->GetWorld<OgreWorld>();

    static AttributeMetadata drawDistanceData("", "0", "10000");
    drawDistance.SetMetadata(&drawDistanceData);

    static AttributeMetadata materialMetadata;
    materialMetadata.elementType = "assetreference";
    meshMaterial.SetMetadata(&materialMetadata);

    meshAsset = AssetRefListenerPtr(new AssetRefListener());
    skeletonAsset = AssetRefListenerPtr(new AssetRefListener());
    
    OgreWorldPtr world = world_.lock();
    if (world)
    {
        Ogre::SceneManager* sceneMgr = world->OgreSceneManager();
        adjustment_node_ = sceneMgr->createSceneNode(world->GetUniqueObjectName("EC_Mesh_adjustment_node"));

        connect(this, SIGNAL(ParentEntitySet()), SLOT(UpdateSignals()));
        connect(meshAsset.get(), SIGNAL(Loaded(AssetPtr)), this, SLOT(OnMeshAssetLoaded(AssetPtr)), Qt::UniqueConnection);
        connect(skeletonAsset.get(), SIGNAL(Loaded(AssetPtr)), this, SLOT(OnSkeletonAssetLoaded(AssetPtr)), Qt::UniqueConnection);
    }
}
Exemple #3
0
void EC_Mesh::RemoveMesh()
{
    OgreWorldPtr world = world_.lock();

    if (entity_)
    {
        emit MeshAboutToBeDestroyed();
        
        RemoveAllAttachments();
        DetachEntity();
        
        Ogre::SceneManager* sceneMgr = world->OgreSceneManager();
        sceneMgr->destroyEntity(entity_);
        
        entity_ = 0;
    }
    
    if (!cloned_mesh_name_.empty())
    {
        try
        {
            Ogre::MeshManager::getSingleton().remove(cloned_mesh_name_);
        }
        catch(Ogre::Exception& e)
        {
            LogWarning("EC_Mesh::RemoveMesh: Could not remove cloned mesh:" + std::string(e.what()));
        }
        
        cloned_mesh_name_ = std::string();
    }
}
Exemple #4
0
EC_Placeable::~EC_Placeable()
{
    if (world_.expired())
    {
        if (sceneNode_)
            LogError("EC_Placeable: World has expired, skipping uninitialization!");
        return;
    }
    
    emit AboutToBeDestroyed();
    
    OgreWorldPtr world = world_.lock();
    Ogre::SceneManager* sceneMgr = world->GetSceneManager();
    
    if (sceneNode_)
    {
        DetachNode();
        
        sceneMgr->destroySceneNode(sceneNode_);
        sceneNode_ = 0;
    }
    // Destroy the attachment node if it was created
    if (boneAttachmentNode_)
    {
        sceneMgr->getRootSceneNode()->removeChild(boneAttachmentNode_);
        sceneMgr->destroySceneNode(boneAttachmentNode_);
        boneAttachmentNode_ = 0;
    }
}
Exemple #5
0
Ogre::Mesh* EC_Mesh::PrepareMesh(const std::string& mesh_name, bool clone)
{
    if (!ViewEnabled())
        return 0;
    OgreWorldPtr world = world_.lock();
    
    Ogre::MeshManager& mesh_mgr = Ogre::MeshManager::getSingleton();
    Ogre::MeshPtr mesh = mesh_mgr.getByName(AssetAPI::SanitateAssetRef(mesh_name));
    
    // For local meshes, mesh will not get automatically loaded until used in an entity. Load now if necessary
    if (mesh.isNull())
    {
        try
        {
            mesh_mgr.load(mesh_name, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
            mesh = mesh_mgr.getByName(mesh_name);
        }
        catch(Ogre::Exception& e)
        {
            LogError("EC_Mesh::PrepareMesh: Could not load mesh " + mesh_name + ": " + std::string(e.what()));
            return 0;
        }
    }
    
    // If mesh is still null, must abort
    if (mesh.isNull())
    {
        LogError("EC_Mesh::PrepareMesh: Mesh " + mesh_name + " does not exist");
        return 0;
    }
    
    if (clone)
    {
        try
        {
            mesh = mesh->clone(world->GetUniqueObjectName("EC_Mesh_clone"));
            mesh->setAutoBuildEdgeLists(false);
            cloned_mesh_name_ = mesh->getName();
        }
        catch(Ogre::Exception& e)
        {
            LogError("EC_Mesh::PrepareMesh: Could not clone mesh " + mesh_name + ":" + std::string(e.what()));
            return 0;
        }
    }
    
    if (mesh->hasSkeleton())
    {
        Ogre::SkeletonPtr skeleton = Ogre::SkeletonManager::getSingleton().getByName(mesh->getSkeletonName());
        if (skeleton.isNull() || skeleton->getNumBones() == 0)
        {
            LogDebug("EC_Mesh::PrepareMesh: Mesh " + mesh_name + " has a skeleton with 0 bones. Disabling the skeleton.");
            mesh->setSkeletonName("");
        }
    }
    
    return mesh.get();
}
void OcclusionDebugRenderer::addPoint(const Umbra::Vector3& pt, const Umbra::Vector4& color)
{
    OgreWorldPtr ogreWorld = renderer_->GetActiveOgreWorld();
    if(!ogreWorld.get())
        return;

    // Nothing to do here
    float3 point = Vector3ToFloat3(pt);
    ogreWorld->DebugDrawSphere(point, 0.1f, 6, Color::Red);
}
void OcclusionDebugRenderer::addLine(const Umbra::Vector3& start, const Umbra::Vector3& end, const Umbra::Vector4& color)
{
    OgreWorldPtr ogreWorld = renderer_->GetActiveOgreWorld();
    if(!ogreWorld.get())
        return;

    float3 start_ = Vector3ToFloat3(start);
    float3 end_ = Vector3ToFloat3(end);

    ogreWorld->DebugDrawLine(start_, end_, Color::Yellow);
}
Exemple #8
0
EC_SelectionBox::~EC_SelectionBox()
{
    if (selectionBox_)
    {
        OgreWorldPtr world = world_.lock();
        if (world)
        {
            Ogre::SceneManager* sceneMgr = world->GetSceneManager();
            sceneMgr->destroyManualObject(selectionBox_);
            selectionBox_ = 0;
        }
    }
}
void OcclusionDebugRenderer::addAABB(const Umbra::Vector3& mn, const Umbra::Vector3& mx, const Umbra::Vector4& color)
{
    OgreWorldPtr ogreWorld = renderer_->GetActiveOgreWorld();
    if(!ogreWorld.get())
        return;

    float3 mn_ = Vector3ToFloat3(mn);
    float3 mx_ = Vector3ToFloat3(mx);
    Color color_ = Vector4ToColor(color);

    AABB aabb(mn_, mx_);
    ogreWorld->DebugDrawAABB(aabb, color_);
}
Exemple #10
0
void EC_Hydrax::Create()
{
    PROFILE(EC_Hydrax_Create);
    SAFE_DELETE(impl);

    if (!framework || framework->IsHeadless())
        return;

    try
    {
        if (!ParentScene())
        {
            LogError("EC_Hydrax: no parent scene. Cannot be created.");
            return;
        }

        OgreWorldPtr w = ParentScene()->Subsystem<OgreWorld>();
        assert(w);

        connect(w->Renderer(), SIGNAL(MainCameraChanged(Entity *)), SLOT(OnActiveCameraChanged(Entity *)), Qt::UniqueConnection);
        Entity *mainCamera = w->Renderer()->MainCamera();
        if (!mainCamera)
        {
            // Can't create Hydrax just yet, no main camera set (Hydrax needs a valid camera to initialize).
            // This error is benign, and Hydrax will now postpone its initialization to until a camera is set.
            // (see OnActiveCameraChanged()).
            LogDebug("Cannot create EC_Hydrax: No main camera set!");
            return;
        }

        Ogre::Camera *cam = mainCamera->GetComponent<EC_Camera>()->GetCamera();
        impl = new EC_HydraxImpl();
        impl->hydrax = new Hydrax::Hydrax(w->OgreSceneManager(), cam, w->Renderer()->MainViewport());

        // Using projected grid module by default
        Hydrax::Module::ProjectedGrid *module = new Hydrax::Module::ProjectedGrid(impl->hydrax, new Hydrax::Noise::Perlin(),
            Ogre::Plane(Ogre::Vector3::UNIT_Y, Ogre::Vector3::ZERO), Hydrax::MaterialManager::NM_VERTEX);
        impl->hydrax->setModule(module);
        impl->module = module;

        // Load all parameters from config file, but position attribute is always authoritative for the position.
        RequestConfigAsset();

        connect(framework->Frame(), SIGNAL(PostFrameUpdate(float)), SLOT(Update(float)), Qt::UniqueConnection);
    }
    catch(const Ogre::Exception &e)
    {
        // Currently if we try to create more than one Hydrax component we end up here due to Ogre internal name collision.
        LogError("Could not create EC_Hydrax: " + std::string(e.what()));
    }
}
Exemple #11
0
EC_SelectionBox::EC_SelectionBox(Scene* scene) :
    IComponent(scene),
    selectionBox_(0)
{
    if (scene)
        world_ = scene->GetWorld<OgreWorld>();
    OgreWorldPtr world = world_.lock();
    Ogre::SceneManager* sceneMgr = world->GetSceneManager();
    selectionBox_ = sceneMgr->createManualObject(world->GetUniqueObjectName("EC_Selected"));
    selectionBox_->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY);
    selectionBox_->setUseIdentityProjection(true);
    selectionBox_->setUseIdentityView(true);
    selectionBox_->setQueryFlags(0);
    sceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(selectionBox_);
}
Exemple #12
0
EC_WaterPlane::~EC_WaterPlane()
{
   if (world_.expired())
    return;
    
   OgreWorldPtr world = world_.lock();
   RemoveWaterPlane();

    if (node_ != 0)
    {
        Ogre::SceneManager* sceneMgr = world->GetSceneManager();
        sceneMgr->destroySceneNode(node_);
        node_ = 0;
    }
}
Exemple #13
0
void EC_HoveringText::ShowMessage(const QString &text)
{
    if (!ViewEnabled())
        return;
    if (world_.expired())
        return;
    
    OgreWorldPtr world = world_.lock();
    Ogre::SceneManager *scene = world->OgreSceneManager();
    assert(scene);
    if (!scene)
        return;

    Entity* entity = ParentEntity();
    assert(entity);
    if (!entity)
        return;

    EC_Placeable *node = entity->GetComponent<EC_Placeable>().get();
    if (!node)
        return;

    Ogre::SceneNode *sceneNode = node->GetSceneNode();
    assert(sceneNode);
    if (!sceneNode)
        return;

    // Create billboard if it doesn't exist.
    if (!billboardSet_)
    {
        billboardSet_ = scene->createBillboardSet(world->GetUniqueObjectName("EC_HoveringText"), 1);
        assert(billboardSet_);
        billboardSet_->Ogre::MovableObject::setUserAny(Ogre::Any(static_cast<IComponent *>(this)));
        billboardSet_->Ogre::Renderable::setUserAny(Ogre::Any(static_cast<IComponent *>(this)));
        sceneNode->attachObject(billboardSet_);
    }

    if (billboardSet_ && !billboard_)
    {
        billboard_ = billboardSet_->createBillboard(Ogre::Vector3(0, 0, 0.7f));

        SetBillboardSize(width.Get(), height.Get());
        SetPosition(position.Get());
    }

    Redraw();
}
Exemple #14
0
EC_Placeable::EC_Placeable(Scene* scene) :
    IComponent(scene),
    sceneNode_(0),
    boneAttachmentNode_(0),
    parentBone_(0),
    parentPlaceable_(0),
    parentMesh_(0),
    attached_(false),
    transform(this, "Transform"),
    drawDebug(this, "Show bounding box", false),
    visible(this, "Visible", true),
    selectionLayer(this, "Selection layer", 1),
    parentRef(this, "Parent entity ref", EntityReference()),
    parentBone(this, "Parent bone name", "")
{
    if (scene)
        world_ = scene->GetWorld<OgreWorld>();
    
    // Enable network interpolation for the transform
    static AttributeMetadata transAttrData;
    static AttributeMetadata nonDesignableAttrData;
    static bool metadataInitialized = false;
    if(!metadataInitialized)
    {
        transAttrData.interpolation = AttributeMetadata::Interpolate;
        nonDesignableAttrData.designable = false;
        metadataInitialized = true;
    }
    transform.SetMetadata(&transAttrData);

    OgreWorldPtr world = world_.lock();
    if (world)
    {
        Ogre::SceneManager* sceneMgr = world->GetSceneManager();
        sceneNode_ = sceneMgr->createSceneNode(world->GetUniqueObjectName("EC_Placeable_SceneNode"));
// Would like to do this for improved debugging in the Profiler window, but because we don't have the parent entity yet, we don't know the id or the name of this entity.
//        sceneNode_ = sceneMgr->createSceneNode(world->GetUniqueObjectName(("EC_Placeable_SceneNode_" + QString::number(ParentEntity()->Id()) + "_" + ParentEntity()->Name()).toStdString()));
    
        // Hook the transform attribute change
        connect(this, SIGNAL(AttributeChanged(IAttribute*, AttributeChange::Type)),
            SLOT(HandleAttributeChanged(IAttribute*, AttributeChange::Type)));

        connect(this, SIGNAL(ParentEntitySet()), SLOT(RegisterActions()));
    
        AttachNode();
    }
}
void OcclusionDebugRenderer::addQuad(const Umbra::Vector3& x0y0, const Umbra::Vector3& x0y1, const Umbra::Vector3& x1y1, const Umbra::Vector3& x1y0, const Umbra::Vector4& color)
{
    OgreWorldPtr ogreWorld = renderer_->GetActiveOgreWorld();
    if(!ogreWorld.get())
        return;

    // Nothing to do here
    float3 x0y0_, x0y1_, x1y0_, x1y1_;
    x0y0_ = Vector3ToFloat3(x0y0);
    x0y1_ = Vector3ToFloat3(x0y1);
    x1y0_ = Vector3ToFloat3(x1y0);
    x1y1_ = Vector3ToFloat3(x1y1);

    ogreWorld->DebugDrawLine(x0y0_, x1y0_, Color::Yellow);
    ogreWorld->DebugDrawLine(x1y0_, x1y1_, Color::Yellow);
    ogreWorld->DebugDrawLine(x1y1_, x0y1_, Color::Yellow);
    ogreWorld->DebugDrawLine(x0y1_, x0y0_, Color::Yellow);
}
Exemple #16
0
void PhysicsWorld::DrawDebugGeometry()
{
    if (!IsDebugGeometryEnabled())
        return;

    PROFILE(PhysicsModule_DrawDebugGeometry);
    
    // Draw debug only for the active (visible) scene
    OgreWorldPtr ogreWorld = scene_.lock()->GetWorld<OgreWorld>();
    cachedOgreWorld_ = ogreWorld.get();
    if (!ogreWorld)
        return;
    if (!ogreWorld->IsActive())
        return;
    
    // Get all lines of the physics world
    world_->debugDrawWorld();
}
Exemple #17
0
EC_Mesh::~EC_Mesh()
{
    if (world_.expired())
    {
        // Log error only if there was an Ogre object to be destroyed
        if (entity_)
            LogError("EC_Mesh: World has expired, skipping uninitialization!");
        return;
    }
    OgreWorldPtr world = world_.lock();

    RemoveMesh();

    if (adjustment_node_)
    {
        Ogre::SceneManager* sceneMgr = world->OgreSceneManager();
        sceneMgr->destroySceneNode(adjustment_node_);
        adjustment_node_ = 0;
    }
}
Exemple #18
0
void EC_SkyX::Create()
{
    if (framework->IsHeadless())
        return;

    if (!ParentScene())
    {
        LogError("EC_SkyX: Aborting creation, parent scene is null!");
        return;
    }

    // Return if main camera is not set
    OgreWorldPtr w = ParentScene()->GetWorld<OgreWorld>();
    if (!w || !w->Renderer())
        return;
    if (!w->Renderer()->MainCamera())
    {
        connect(w->Renderer(), SIGNAL(MainCameraChanged(Entity*)), this, SLOT(Create()), Qt::UniqueConnection);
        return;
    }
Exemple #19
0
void EC_Mesh::RemoveAttachmentMesh(uint index)
{
    OgreWorldPtr world = world_.lock();
    
    if (!entity_)
        return;
        
    if (index >= attachment_entities_.size())
        return;
    
    Ogre::SceneManager* sceneMgr = world->OgreSceneManager();
    
    if (attachment_entities_[index] && attachment_nodes_[index])
    {
        // See if attached to a tagpoint or an ordinary node
        Ogre::TagPoint* tag = dynamic_cast<Ogre::TagPoint*>(attachment_nodes_[index]);
        if (tag)
        {
            entity_->detachObjectFromBone(attachment_entities_[index]);
        }
        else
        {
            Ogre::SceneNode* scenenode = dynamic_cast<Ogre::SceneNode*>(attachment_nodes_[index]);
            if (scenenode)
            {
                scenenode->detachObject(attachment_entities_[index]);
                sceneMgr->destroySceneNode(scenenode);
            }
        }
        
        attachment_nodes_[index] = 0;
    }
    if (attachment_entities_[index])
    {
        if (attachment_entities_[index]->sharesSkeletonInstance())
            attachment_entities_[index]->stopSharingSkeletonInstance();
        sceneMgr->destroyEntity(attachment_entities_[index]);
        attachment_entities_[index] = 0;
    }
}
Exemple #20
0
void EC_SelectionBox::SetBoundingBox(QRect &view)
{
    if (world_.expired())
        return;
    OgreWorldPtr world = world_.lock();
    Renderer* renderer = world->GetRenderer();
    Ogre::RenderWindow *renderWindow = renderer->GetCurrentRenderWindow();
    float w= (float)renderWindow->getWidth();
    float h= (float)renderWindow->getHeight();
    float left = (float)(view.left()) / w, right = (float)(view.right()) / w;
    float top = (float)(view.top()) / h, bottom = (float)(view.bottom()) / h;
    
    if(left > right) { float tmp; tmp = left; left = right; right = tmp; }
    if(top > bottom) { float tmp; tmp = top; top = bottom; bottom = tmp; }
    // don't do selection box if too small
    if((right - left) * (bottom-top) < 0.0001) return;
    
    // correct coordinates for overlay
    left = left * 2 - 1;
    right = right * 2 - 1;
    top = 1 - top * 2;
    bottom = 1 - bottom * 2;
 
    selectionBox_->clear();
    selectionBox_->begin("BlueTransparent",Ogre::RenderOperation::OT_TRIANGLE_STRIP);
    selectionBox_->position(left, bottom, -1);
    selectionBox_->position(right, bottom, -1);
    selectionBox_->position(left, top, -1);
    selectionBox_->position(right, top, -1);
    selectionBox_->end();
    selectionBox_->begin("",Ogre::RenderOperation::OT_LINE_STRIP);
    selectionBox_->position(left, top, -1);
    selectionBox_->position(right, top, -1);
    selectionBox_->position(right, bottom, -1);
    selectionBox_->position(left, bottom, -1);
    selectionBox_->position(left, top, -1);
    selectionBox_->end();
    
    selectionBox_->setBoundingBox(Ogre::AxisAlignedBox::BOX_INFINITE);
}
bool RayVisibilityFilter::Filter(const IMParameters& params)
{
    if(enabled_)
    {
        float cutoffrange = range_ * range_;

        if(params.headless)
            return true;

        if(params.distance < cutoffrange)  //If the entity is close enough, only then do a raycast
        {
            std::map<entity_id_t, bool>::iterator it;

            it = params.connection->syncState->visibleEntities.find(params.changed_entity->Id());

            /*Check when was the last time we raycasted and dont do it if its not the time*/
            int lastRaycasted = im_->FindLastRaycastedEntity(params.connection, params.changed_entity->Id());
            int currentTime = im_->ElapsedTime();

            if(it != params.connection->syncState->visibleEntities.end() && (lastRaycasted + raycastinterval_) > currentTime)   //If the entity is located from the map
            {
                if(it->second == true) //bool which contains a value determining if the entity was visible to the user last time it was raycasted
                {
#ifdef IM_DEBUG
                    if(params.connection->ConnectionId() == 1)
                        ::LogInfo("Not the time to raycast, returning true. Last " + QString::number(lastRaycasted + raycastinterval_) + " Current " + QString::number(currentTime));
#endif
                    return true;
                }
                else
                {
#ifdef IM_DEBUG
                    if(params.connection->ConnectionId() == 1)
                        ::LogInfo("Not the time to raycast, returning false. Last " + QString::number(lastRaycasted + raycastinterval_) + " Current " + QString::number(currentTime));
#endif
                    return false;
                }
            }

            else
            {
                Ray ray(params.client_position, (params.entity_position - params.client_position).Normalized());
                RaycastResult *result = 0;
                OgreWorldPtr w = params.scene->GetWorld<OgreWorld>();

                result = w->Raycast(ray, 0xFFFFFFFF);
                im_->UpdateLastRaycastedEntity(params.connection, params.changed_entity->Id());

                if(result && result->entity && result->entity->Id() == params.changed_entity->Id())  //If the ray hit someone and its our target entity
                {
                    im_->UpdateEntityVisibility(params.connection, params.changed_entity->Id(), true);
#ifdef IM_DEBUG
                    ::LogInfo("Entity " + QString::number(params.changed_entity->Id()) + " is visible to connection " + QString::number(params.connection->ConnectionId()));
#endif
                    return true;
                }
                else
                {
                    im_->UpdateEntityVisibility(params.connection, params.changed_entity->Id(), false);
                    im_->UpdateRelevance(params.connection, params.changed_entity->Id(), 0);
                    return false;
                }

            }

        }
        else
            return false;
    }
    else
        return true;
}
Exemple #22
0
bool EC_Mesh::SetAttachmentMesh(uint index, const std::string& mesh_name, const std::string& attach_point, bool share_skeleton)
{
    if (!ViewEnabled())
        return false;
    OgreWorldPtr world = world_.lock();

    if (!entity_)
    {
        LogError("EC_Mesh::SetAttachmentMesh: No mesh entity created yet, can not create attachments!");
        return false;
    }
    
    Ogre::SceneManager* sceneMgr = world->OgreSceneManager();
    
    size_t oldsize = attachment_entities_.size();
    size_t newsize = index + 1;
    
    if (oldsize < newsize)
    {
        attachment_entities_.resize(newsize);
        attachment_nodes_.resize(newsize);
        for(uint i = oldsize; i < newsize; ++i)
        {
            attachment_entities_[i] = 0;
            attachment_nodes_[i] = 0;
        }
    }
    
    RemoveAttachmentMesh(index);
    
    Ogre::Mesh* mesh = PrepareMesh(mesh_name, false);
    if (!mesh)
        return false;

    if (share_skeleton)
    {
        // If sharing a skeleton, force the attachment mesh to use the same skeleton
        // This is theoretically quite a scary operation, for there is possibility for things to go wrong
        Ogre::SkeletonPtr entity_skel = entity_->getMesh()->getSkeleton();
        if (entity_skel.isNull())
        {
            LogError("EC_Mesh::SetAttachmentMesh: Cannot share skeleton for attachment, not found");
            return false;
        }
        try
        {
            mesh->_notifySkeleton(entity_skel);
        }
        catch(const Ogre::Exception &/*e*/)
        {
            LogError("EC_Mesh::SetAttachmentMesh: Could not set shared skeleton for attachment");
            return false;
        }
    }

    try
    {
        QString entityName = QString("EC_Mesh_attach") + QString::number(index);
        attachment_entities_[index] = sceneMgr->createEntity(world->GetUniqueObjectName(entityName.toStdString()), mesh->getName());
        if (!attachment_entities_[index])
        {
            LogError("EC_Mesh::SetAttachmentMesh: Could not set attachment mesh " + mesh_name);
            return false;
        }

        attachment_entities_[index]->setRenderingDistance(drawDistance.Get());
        attachment_entities_[index]->setCastShadows(castShadows.Get());
        attachment_entities_[index]->setUserAny(entity_->getUserAny());
        // Set UserAny also on subentities
        for(uint i = 0; i < attachment_entities_[index]->getNumSubEntities(); ++i)
            attachment_entities_[index]->getSubEntity(i)->setUserAny(entity_->getUserAny());

        Ogre::Bone* attach_bone = 0;
        if (!attach_point.empty())
        {
            Ogre::Skeleton* skel = entity_->getSkeleton();
            if (skel && skel->hasBone(attach_point))
                attach_bone = skel->getBone(attach_point);
        }
        if (attach_bone)
        {
            Ogre::TagPoint* tag = entity_->attachObjectToBone(attach_point, attachment_entities_[index]);
            attachment_nodes_[index] = tag;
        }
        else
        {
            QString nodeName = QString("EC_Mesh_attachment_") + QString::number(index);
            Ogre::SceneNode* node = sceneMgr->createSceneNode(world->GetUniqueObjectName(nodeName.toStdString()));
            node->attachObject(attachment_entities_[index]);
            adjustment_node_->addChild(node);
            attachment_nodes_[index] = node;
        }
        
        if (share_skeleton && entity_->hasSkeleton() && attachment_entities_[index]->hasSkeleton())
        {
            attachment_entities_[index]->shareSkeletonInstanceWith(entity_);
        }
    }
    catch(Ogre::Exception& e)
    {
        LogError("EC_Mesh::SetAttachmentMesh: Could not set attachment mesh " + mesh_name + ": " + std::string(e.what()));
        return false;
    }
    return true;
}
Exemple #23
0
bool EC_Mesh::SetMeshWithSkeleton(const std::string& mesh_name, const std::string& skeleton_name, bool clone)
{
    if (!ViewEnabled())
        return false;
    OgreWorldPtr world = world_.lock();

    Ogre::SkeletonPtr skel = Ogre::SkeletonManager::getSingleton().getByName(AssetAPI::SanitateAssetRef(skeleton_name));
    if (skel.isNull())
    {
        LogError("EC_Mesh::SetMeshWithSkeleton: Could not set skeleton " + skeleton_name + " to mesh " + mesh_name + ": not found");
        return false;
    }
    
    RemoveMesh();

    Ogre::SceneManager* sceneMgr = world->OgreSceneManager();
    
    Ogre::Mesh* mesh = PrepareMesh(mesh_name, clone);
    if (!mesh)
        return false;
    
    try
    {
        mesh->_notifySkeleton(skel);
//        LogDebug("Set skeleton " + skeleton_name + " to mesh " + mesh_name);
    }
    catch(Ogre::Exception& e)
    {
        LogError("EC_Mesh::SetMeshWithSkeleton: Could not set skeleton " + skeleton_name + " to mesh " + mesh_name + ": " + std::string(e.what()));
        return false;
    }
    
    try
    {
        entity_ = sceneMgr->createEntity(world->GetUniqueObjectName("EC_Mesh_entwithskel"), mesh->getName());
        if (!entity_)
        {
            LogError("EC_Mesh::SetMeshWithSkeleton: Could not set mesh " + mesh_name);
            return false;
        }
        
        entity_->setRenderingDistance(drawDistance.Get());
        entity_->setCastShadows(castShadows.Get());
        entity_->setUserAny(Ogre::Any(static_cast<IComponent *>(this)));
        // Set UserAny also on subentities
        for(uint i = 0; i < entity_->getNumSubEntities(); ++i)
            entity_->getSubEntity(i)->setUserAny(entity_->getUserAny());
        
        if (entity_->hasSkeleton())
        {
            Ogre::SkeletonInstance* skel = entity_->getSkeleton();
            // Enable cumulative mode on skeletal animations
            if (skel)
                skel->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE);
        }
    }
    catch(Ogre::Exception& e)
    {
        LogError("EC_Mesh::SetMeshWithSkeleton: Could not set mesh " + mesh_name + ": " + std::string(e.what()));
        return false;
    }
    
    AttachEntity();
    
    emit MeshChanged();
    
    return true;
}
Exemple #24
0
EC_WaterPlane::EC_WaterPlane(Scene* scene) :
    IComponent(scene),
    xSize(this, "x-size", 5000),
    ySize(this, "y-size", 5000),
    depth(this, "Depth", 20),
    position(this, "Position", float3::zero),
    rotation(this, "Rotation", Quat::identity),
    scaleUfactor(this, "U factor", 0.0002f),
    scaleVfactor(this, "V factor", 0.0002f),
    xSegments(this, "Segments in x", 10),
    ySegments(this, "Segments in y", 10),
    materialName(this, "Material", QString("Ocean")),
    materialRef(this, "Material ref"),
   //textureNameAttr(this, "Texture", QString("DefaultOceanSkyCube.dds")),
    fogColor(this, "Fog color", Color(0.2f,0.4f,0.35f,1.0f)),
    fogStartDistance(this, "Fog start dist.", 100.f),
    fogEndDistance(this, "Fog end dist.", 2000.f),
    fogMode(this, "Fog mode", 3),
    entity_(0),
    node_(0),
    attached_(false),
    attachedToRoot_(false)
{
    static AttributeMetadata metadata;
    static bool metadataInitialized = false;
    if(!metadataInitialized)
    {
        metadata.enums[Ogre::FOG_NONE] = "NoFog";
        metadata.enums[Ogre::FOG_EXP] = "Exponential";
        metadata.enums[Ogre::FOG_EXP2] = "ExponentiallySquare";
        metadata.enums[Ogre::FOG_LINEAR] = "Linear";
        metadataInitialized = true;
    }

    fogMode.SetMetadata(&metadata);

    if (scene)
        world_ = scene->GetWorld<OgreWorld>();
    OgreWorldPtr world = world_.lock();
    if (world)
    {
        Ogre::SceneManager *sceneMgr = world->GetSceneManager();
        node_ = sceneMgr->createSceneNode(world->GetUniqueObjectName("EC_WaterPlane_Root"));
    }

    QObject::connect(this, SIGNAL(AttributeChanged(IAttribute*, AttributeChange::Type)),
        SLOT(OnAttributeUpdated(IAttribute*, AttributeChange::Type)));

    lastXsize_ = xSize.Get();
    lastYsize_ = ySize.Get();

    connect(this, SIGNAL(ParentEntitySet()), this, SLOT(SetParent()));

    // If there exist placeable copy its position for default position and rotation.
   
    /*
    EC_Placeable* placeable = dynamic_cast<EC_Placeable*>(FindPlaceable().get());
    if (placeable != 0)
    {
        float3 vec = placeable->GetPosition();
        position.Set(vec,AttributeChange::Default);
   
        Quaternion rot =placeable->GetOrientation();
        rotation.Set(rot, AttributeChange::Default);
        ComponentChanged(AttributeChange::Default);
    }
    */
}
Exemple #25
0
bool EC_Mesh::SetMesh(QString meshResourceName, bool clone)
{
    if (!ViewEnabled())
        return false;
    
    OgreWorldPtr world = world_.lock();

    std::string mesh_name = meshResourceName.trimmed().toStdString();

    RemoveMesh();

    // If placeable is not set yet, set it manually by searching it from the parent entity
    if (!placeable_)
    {
        Entity* entity = ParentEntity();
        if (entity)
        {
            ComponentPtr placeable = entity->GetComponent(EC_Placeable::TypeNameStatic());
            if (placeable)
                placeable_ = placeable;
        }
    }
    
    Ogre::SceneManager* sceneMgr = world->OgreSceneManager();
    
    Ogre::Mesh* mesh = PrepareMesh(mesh_name, clone);
    if (!mesh)
        return false;
    
    try
    {
        entity_ = sceneMgr->createEntity(world->GetUniqueObjectName("EC_Mesh_entity"), mesh->getName());
        if (!entity_)
        {
            LogError("EC_Mesh::SetMesh: Could not set mesh " + mesh_name);
            return false;
        }
        
        entity_->setRenderingDistance(drawDistance.Get());
        entity_->setCastShadows(castShadows.Get());
        entity_->setUserAny(Ogre::Any(static_cast<IComponent *>(this)));
        // Set UserAny also on subentities
        for(uint i = 0; i < entity_->getNumSubEntities(); ++i)
            entity_->getSubEntity(i)->setUserAny(entity_->getUserAny());

        if (entity_->hasSkeleton())
        {
            Ogre::SkeletonInstance* skel = entity_->getSkeleton();
            // Enable cumulative mode on skeletal animations
            if (skel)
                skel->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE);
        }
        
        // Make sure adjustment node is uptodate
        Transform newTransform = nodeTransformation.Get();
        adjustment_node_->setPosition(newTransform.pos);
        adjustment_node_->setOrientation(newTransform.Orientation());
        
        // Prevent Ogre exception from zero scale
        adjustment_node_->setScale(Max(newTransform.scale, float3::FromScalar(0.0000001f)));
            
        // Force a re-apply of all materials to this new mesh.
        ApplyMaterial();
    }
    catch(Ogre::Exception& e)
    {
        LogError("EC_Mesh::SetMesh: Could not set mesh " + mesh_name + ": " + std::string(e.what()));
        return false;
    }
    
    AttachEntity();
    emit MeshChanged();
    
    return true;
}
Exemple #26
0
void EC_Placeable::AttachNode()
{
    if (world_.expired())
    {
        LogError("EC_Placeable::AttachNode: No OgreWorld available to call this function!");
        return;
    }
    OgreWorldPtr world = world_.lock();
    
    try
    {
        // If already attached, detach first
        if (attached_)
            DetachNode();
        
        Ogre::SceneManager* sceneMgr = world->GetSceneManager();
        Ogre::SceneNode* root_node = sceneMgr->getRootSceneNode();
        
        // Three possible cases
        // 1) attach to scene root node
        // 2) attach to another EC_Placeable's scene node
        // 3) attach to a bone on a skeletal mesh
        // Disconnect from the EntityCreated & ParentMeshChanged signals, as responding to them might not be needed anymore.
        // We will reconnect signals as necessary
        disconnect(this, SLOT(CheckParentEntityCreated(Entity*, AttributeChange::Type)));
        disconnect(this, SLOT(OnParentMeshChanged()));
        disconnect(this, SLOT(OnComponentAdded(IComponent*, AttributeChange::Type)));
        
        // Try to attach to another entity if the parent ref is non-empty
        // Make sure we're not trying to attach to ourselves as the parent
        const EntityReference& parent = parentRef.Get();
        if (!parent.IsEmpty())
        {
            Entity* ownEntity = ParentEntity();
            if (!ownEntity)
                return;
            Scene* scene = ownEntity->ParentScene();
            if (!scene)
                return;

            Entity* parentEntity = parent.Lookup(scene).get();
            if (parentEntity == ownEntity)
            {
                // If we refer to self, attach to the root
                root_node->addChild(sceneNode_);
                attached_ = true;
                return;
            }
            
            if (parentEntity)
            {
                // Note: if we don't find the correct bone, we attach to the root
                QString boneName = parentBone.Get();
                if (!boneName.isEmpty())
                {
                    EC_Mesh* parentMesh = parentEntity->GetComponent<EC_Mesh>().get();
                    if (parentMesh)
                    {
                        Ogre::Bone* bone = parentMesh->GetBone(boneName);
                        if (bone)
                        {
                            // Create the node for bone attachment if it did not exist already
                            if (!boneAttachmentNode_)
                            {
                                boneAttachmentNode_ = sceneMgr->createSceneNode(world->GetUniqueObjectName("EC_Placeable_BoneAttachmentNode"));
                                root_node->addChild(boneAttachmentNode_);
                            }
                            
                            // Setup manual bone tracking, as Ogre does not allow to attach scene nodes to bones
                            attachmentListener.AddAttachment(parentMesh->GetEntity(), bone, this);
                            
                            boneAttachmentNode_->addChild(sceneNode_);
                            
                            parentBone_ = bone;
                            parentMesh_ = parentMesh;
                            connect(parentMesh, SIGNAL(MeshAboutToBeDestroyed()), this, SLOT(OnParentMeshDestroyed()), Qt::UniqueConnection);
                            attached_ = true;
                            return;
                        }
                        else
                        {
                            // Could not find the bone. Connect to the parent mesh MeshChanged signal to wait for the proper mesh to be assigned.
                            connect(parentMesh, SIGNAL(MeshChanged()), this, SLOT(OnParentMeshChanged()), Qt::UniqueConnection);
                            return;
                        }
                    }
                    else
                    {
                        // If can't find the mesh component yet, wait for it to be created
                        connect(parentEntity, SIGNAL(ComponentAdded(IComponent*, AttributeChange::Type)), this, SLOT(OnComponentAdded(IComponent*, AttributeChange::Type)), Qt::UniqueConnection);
                        return;
                    }
                }
                
                parentPlaceable_ = parentEntity->GetComponent<EC_Placeable>().get();
                if (parentPlaceable_)
                {
                    parentPlaceable_->GetSceneNode()->addChild(sceneNode_);
                    
                    // Connect to destruction of the placeable to be able to detach gracefully
                    connect(parentPlaceable_, SIGNAL(AboutToBeDestroyed()), this, SLOT(OnParentPlaceableDestroyed()), Qt::UniqueConnection);
                    attached_ = true;
                    return;
                }
                else
                {
                    // If can't find the placeable component yet, wait for it to be created
                    connect(parentEntity, SIGNAL(ComponentAdded(IComponent*, AttributeChange::Type)), this, SLOT(OnComponentAdded(IComponent*, AttributeChange::Type)), Qt::UniqueConnection);
                    return;
                }
            }
            else
            {
                // Could not find parent entity. Check for it later, when new entities are created into the scene
                connect(scene, SIGNAL(EntityCreated(Entity*, AttributeChange::Type)), this, SLOT(CheckParentEntityCreated(Entity*, AttributeChange::Type)), Qt::UniqueConnection);
                return;
            }
        }