bool EC_OgreCustomObject::CommitChanges(Ogre::ManualObject* object) { if (!object) return false; if (renderer_.expired()) return false; RendererPtr renderer = renderer_.lock(); DestroyEntity(); // If placeable is not set yet, set it manually by searching it from the parent entity if (!placeable_) { Scene::Entity* entity = GetParentEntity(); if (entity) { ComponentPtr placeable = entity->GetComponent(EC_Placeable::TypeNameStatic()); if (placeable) placeable_ = placeable; } } if (!object->getNumSections()) return true; try { std::string mesh_name = renderer->GetUniqueObjectName("EC_OgreCustomObject_mesh"); object->convertToMesh(mesh_name); object->clear(); Ogre::SceneManager* scene_mgr = renderer->GetSceneManager(); entity_ = scene_mgr->createEntity(renderer->GetUniqueObjectName("EC_OgreCustomObject_entity"), mesh_name); if (entity_) { AttachEntity(); entity_->setRenderingDistance(draw_distance_); entity_->setCastShadows(cast_shadows_); entity_->setUserAny(Ogre::Any(GetParentEntity())); // Set UserAny also on subentities for (uint i = 0; i < entity_->getNumSubEntities(); ++i) entity_->getSubEntity(i)->setUserAny(entity_->getUserAny()); } else { OgreRenderingModule::LogError("Could not create entity from manualobject mesh"); return false; } } catch (Ogre::Exception& e) { OgreRenderingModule::LogError("Could not convert manualobject to mesh: " + std::string(e.what())); return false; } return true; }
void EC_Mesh::AutoSetPlaceable() { Scene::Entity* entity = GetParentEntity(); if (entity) { ComponentPtr placeable = entity->GetComponent(EC_Placeable::TypeNameStatic()); if (placeable) SetPlaceable(placeable); } }
void EC_Clone::Create() { if (renderer_.expired()) return; Ogre::SceneManager *scene = renderer_.lock()->GetSceneManager(); assert(scene); if (!scene) return; Scene::Entity *entity = GetParentEntity(); assert(entity); if (!entity) return; OgreRenderer::EC_OgrePlaceable *placeable = entity->GetComponent<OgreRenderer::EC_OgrePlaceable>().get(); assert(placeable); if (!placeable) return; // Check out if this entity has EC_OgreMesh or EC_OgreCustomObject. Ogre::Entity *originalEntity = 0; if (entity->GetComponent(OgreRenderer::EC_OgreMesh::NameStatic())) { OgreRenderer::EC_OgreMesh *ec_mesh= entity->GetComponent<OgreRenderer::EC_OgreMesh>().get(); assert(ec_mesh); originalEntity = ec_mesh->GetEntity(); sceneNode_ = ec_mesh->GetAdjustmentSceneNode(); } else if(entity->GetComponent(OgreRenderer::EC_OgreCustomObject::NameStatic())) { OgreRenderer::EC_OgreCustomObject *ec_custom = entity->GetComponent<OgreRenderer::EC_OgreCustomObject>().get(); assert(ec_custom); if (!ec_custom->IsCommitted()) { LogError("Mesh entity have not been created for the target primitive. Cannot create EC_Highlight."); return; } originalEntity = ec_custom->GetEntity(); sceneNode_ = placeable->GetSceneNode(); } else { LogError("This entity doesn't have either EC_OgreMesh or EC_OgreCustomObject present. Cannot create EC_Highlight."); return; } assert(originalEntity); if (!originalEntity) return; assert(sceneNode_); if (!sceneNode_) return; // Clone the Ogre entity. cloneName_ = std::string("entity") + renderer_.lock()->GetUniqueObjectName(); entityClone_ = originalEntity->clone(cloneName_); assert(entityClone_); // Disable casting of shadows for the clone. entityClone_->setCastShadows(false); ///\todo If original entity has skeleton, (try to) link it to the clone. /* if (originalEntity->hasSkeleton()) { Ogre::SkeletonInstance *skel = originalEntity->getSkeleton(); // 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 = originalEntity->getMesh()->getSkeleton(); if (entity_skel.isNull()) { LogError("Cannot share skeleton for attachment, not found"); } else { try { entityClone_->getMesh()->_notifySkeleton(entity_skel); } catch (Ogre::Exception &e) { LogError("Could not set shared skeleton for attachment: " + std::string(e.what())); } } } */ std::string newMatName = std::string("HighlightMaterial") + renderer_.lock()->GetUniqueObjectName(); try { Ogre::MaterialPtr highlightMaterial = OgreRenderer::CloneMaterial("Highlight", newMatName); entityClone_->setMaterialName(newMatName); } catch (Ogre::Exception &e) { LogError("Could not set material \"" + newMatName + "\": " + std::string(e.what())); return; } sceneNode_->attachObject(entityClone_); }
void EC_VideoSource::UpdateCanvas() { if (!video_widget_ || !media_object_) return; Scene::Entity* entity = GetParentEntity(); if (!entity) { LogError("No parent entity, cannot create/update 3DCanvas"); return; } // If entity has no valid mesh or prim yet, start a retry timer and try to set the canvas later if ((!entity->GetComponent(EC_Mesh::TypeNameStatic())) && (!entity->GetComponent(EC_OgreCustomObject::TypeNameStatic()))) { LogDebug("Mesh or prim did not exist yet, retrying"); QTimer::singleShot(500, this, SLOT(UpdateCanvas())); return; } canvas_ = Get3DCanvas(); if (!canvas_) { LogError("Could not get 3D Canvas component"); return; } // Update widget if (canvas_->GetWidget() != player_) { canvas_->SetWidget(player_); LogDebug("Widget set"); } // Update submesh int submesh = getsubmeshIndex(); if (submesh < 0) submesh = 0; if (!canvas_->GetSubMeshes().contains(submesh)) { canvas_->SetSubmesh(submesh); LogDebug("Submesh set"); } // Update refresh rate if (getrefreshRate() > 0) { int ref_rate_msec = 1000 / getrefreshRate(); if (canvas_->GetRefreshRate() != ref_rate_msec) { canvas_->SetRefreshRate(getrefreshRate()); LogDebug("Refresh rate set"); } } else canvas_->SetRefreshRate(0); // Scale down widget if (getscaleDown() && player_) { if (original_size_.isNull()) { original_size_ = player_->size(); if (original_size_.width() < 1 && original_size_.height() < 1) original_size_ = QSize(0,0); else qDebug() << "Original size set: " << original_size_; } if (!original_size_.isNull()) { QSize scale_down_size(320, 240); if (player_->size().width() > scale_down_size.width() && player_->size().height() > scale_down_size.height()) { //player_->resize(scale_down_size); //LogDebug("Scaled video widget down to 320x240 for performance"); } } } if (start_canvas_) { canvas_->Start(); start_canvas_ = false; playing_canvas_ = true; LogDebug("Started rendering updates"); if (ready_poller_->isActive()) ready_poller_->stop(); } if (stop_canvas_) { canvas_->Stop(); start_canvas_ = false; stop_canvas_ = false; playing_canvas_ = false; LogDebug("Stoppend rendering updates"); } }
bool EC_Mesh::SetMesh(QString meshResourceName, bool clone) { if (!ViewEnabled()) return false; if (renderer_.expired()) return false; RendererPtr renderer = renderer_.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_) { Scene::Entity* entity = GetParentEntity(); if (entity) { ComponentPtr placeable = entity->GetComponent(EC_Placeable::TypeNameStatic()); if (placeable) placeable_ = placeable; } } Ogre::SceneManager* scene_mgr = renderer->GetSceneManager(); Ogre::Mesh* mesh = PrepareMesh(mesh_name, clone); if (!mesh) return false; try { entity_ = scene_mgr->createEntity(renderer->GetUniqueObjectName("EC_Mesh_entity"), mesh->getName()); if (!entity_) { LogError("Could not set mesh " + mesh_name); return false; } entity_->setRenderingDistance(drawDistance.Get()); entity_->setCastShadows(castShadows.Get()); entity_->setUserAny(Ogre::Any(GetParentEntity())); // 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 if (adjustment_node_) { Transform newTransform = nodeTransformation.Get(); adjustment_node_->setPosition(newTransform.position.x, newTransform.position.y, newTransform.position.z); Quaternion adjust(DEGTORAD * newTransform.rotation.x, DEGTORAD * newTransform.rotation.y, DEGTORAD * newTransform.rotation.z); adjustment_node_->setOrientation(Ogre::Quaternion(adjust.w, adjust.x, adjust.y, adjust.z)); // Prevent Ogre exception from zero scale if (newTransform.scale.x < 0.0000001f) newTransform.scale.x = 0.0000001f; if (newTransform.scale.y < 0.0000001f) newTransform.scale.y = 0.0000001f; if (newTransform.scale.z < 0.0000001f) newTransform.scale.z = 0.0000001f; adjustment_node_->setScale(newTransform.scale.x, newTransform.scale.y, newTransform.scale.z); } // Force a re-apply of all materials to this new mesh. ApplyMaterial(); } catch (Ogre::Exception& e) { LogError("Could not set mesh " + mesh_name + ": " + std::string(e.what())); return false; } AttachEntity(); emit MeshChanged(); return true; }