DYTSrouce/src/entities/MeshComponent.cpp
2025-01-09 08:32:05 +08:00

59 lines
1.4 KiB
C++

#include "entities/MeshComponent.h"
#include "common/SpdLogger.h"
#include "scene/MeshManager.h"
MeshComponent::MeshComponent(SceneComponent* parent)
: SceneComponent(parent) {
}
MeshComponent::~MeshComponent() {
}
std::string MeshComponent::GetTypeName() {
return "MeshComponent";
}
bool MeshComponent::SetAttribute(const char* name, const char* value) {
if (0 == strcmp(name, "mesh")) {
return LoadNode(value);
}
return SceneComponent::SetAttribute(name, value);
}
bool MeshComponent::SaveAttribute(tinyxml2::XMLElement* element) {
element->SetAttribute("mesh", mesh_.c_str());
return SceneComponent::SaveAttribute(element);
}
std::string MeshComponent::GetSelfTypeName() {
return MeshComponent::GetTypeName();
}
void MeshComponent::Begin() {
SceneComponent::Begin();
}
bool MeshComponent::LoadNode(const char* mesh) {
mesh_ = mesh;
osg::ref_ptr<osg::MatrixTransform> mt = MeshManager::Get().ReadNode(mesh_);
if (mt_.valid()) {
int count = mt_->getNumParents();
for (int i = 0; i < count; ++i) {
osg::Group* parent = mt_->getParent(i)->asGroup();
if (nullptr != parent) {
parent->removeChild(mt_);
parent->addChild(mt);
}
}
}
mt_ = mt;
//mt_->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
UpdateLocationAndRotation();
return true;
}