Make HFMSerializer return a smart pointer to an HFMModel

This commit is contained in:
sabrina-shanman 2018-11-09 09:21:02 -08:00
parent 5a9af5b2c3
commit f971294d6d
14 changed files with 21 additions and 22 deletions

View file

@ -109,7 +109,7 @@ bool ModelPackager::loadModel() {
qCDebug(interfaceapp) << "Reading FBX file : " << _fbxInfo.filePath();
QByteArray fbxContents = fbx.readAll();
_hfmModel.reset(FBXSerializer().read(fbxContents, QVariantHash(), _fbxInfo.filePath()));
_hfmModel = FBXSerializer().read(fbxContents, QVariantHash(), _fbxInfo.filePath());
// make sure we have some basic mappings
populateBasicMapping(_mapping, _fbxInfo.filePath(), *_hfmModel);

View file

@ -46,7 +46,7 @@ private:
QString _scriptDir;
QVariantHash _mapping;
std::unique_ptr<hfm::Model> _hfmModel;
std::shared_ptr<hfm::Model> _hfmModel;
QStringList _textures;
QStringList _scripts;
};

View file

@ -71,7 +71,7 @@ void AnimationReader::run() {
// Parse the FBX directly from the QNetworkReply
HFMModel::Pointer hfmModel;
if (_url.path().toLower().endsWith(".fbx")) {
hfmModel.reset(FBXSerializer().read(_data, QVariantHash(), _url.path()));
hfmModel = FBXSerializer().read(_data, QVariantHash(), _url.path());
} else {
QString errorStr("usupported format");
emit onError(299, errorStr);

View file

@ -1796,10 +1796,10 @@ HFMModel* FBXSerializer::extractHFMModel(const QVariantHash& mapping, const QStr
return hfmModelPtr;
}
HFMModel* FBXSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) {
HFMModel::Pointer FBXSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) {
QBuffer buffer(const_cast<QByteArray*>(&data));
buffer.open(QIODevice::ReadOnly);
return read(&buffer, mapping, url);
return HFMModel::Pointer(read(&buffer, mapping, url));
}
HFMModel* FBXSerializer::read(QIODevice* device, const QVariantHash& mapping, const QUrl& url) {

View file

@ -103,7 +103,7 @@ public:
HFMModel* _hfmModel;
/// Reads HFMModel from the supplied model and mapping data.
/// \exception QString if an error occurs in parsing
HFMModel* read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url = QUrl()) override;
HFMModel::Pointer read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url = QUrl()) override;
HFMModel* read(QIODevice* device, const QVariantHash& mapping, const QUrl& url = QUrl());
FBXNode _rootNode;

View file

@ -910,7 +910,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const QUrl& url) {
return true;
}
HFMModel* GLTFSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) {
HFMModel::Pointer GLTFSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) {
_url = url;
@ -923,7 +923,7 @@ HFMModel* GLTFSerializer::read(const QByteArray& data, const QVariantHash& mappi
parseGLTF(data);
//_file.dump();
HFMModel* hfmModelPtr = new HFMModel();
auto hfmModelPtr = std::make_shared<HFMModel>();
HFMModel& hfmModel = *hfmModelPtr;
buildGeometry(hfmModel, _url);

View file

@ -707,7 +707,7 @@ class GLTFSerializer : public QObject, public HFMSerializer {
Q_OBJECT
public:
GLTFSerializer();
HFMModel* read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url = QUrl()) override;
HFMModel::Pointer read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url = QUrl()) override;
private:
GLTFFile _file;
QUrl _url;

View file

@ -652,12 +652,12 @@ done:
}
HFMModel* OBJSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) {
HFMModel::Pointer OBJSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) {
PROFILE_RANGE_EX(resource_parse, __FUNCTION__, 0xffff0000, nullptr);
QBuffer buffer { const_cast<QByteArray*>(&data) };
buffer.open(QIODevice::ReadOnly);
auto hfmModelPtr = new HFMModel();
auto hfmModelPtr = std::make_shared<HFMModel>();
HFMModel& hfmModel { *hfmModelPtr };
OBJTokenizer tokenizer { &buffer };
float scaleGuess = 1.0f;

View file

@ -101,7 +101,7 @@ public:
QString currentMaterialName;
QHash<QString, OBJMaterial> materials;
HFMModel* read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url = QUrl()) override;
HFMModel::Pointer read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url = QUrl()) override;
private:
QUrl _url;

View file

@ -21,7 +21,7 @@
namespace hfm {
class Serializer {
virtual Model* read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url = QUrl()) = 0;
virtual Model::Pointer read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url = QUrl()) = 0;
};
};

View file

@ -199,22 +199,22 @@ void GeometryReader::run() {
serializerMapping["combineParts"] = _combineParts;
if (_url.path().toLower().endsWith(".fbx")) {
hfmModel.reset(FBXSerializer().read(_data, serializerMapping, _url));
hfmModel = FBXSerializer().read(_data, serializerMapping, _url);
if (hfmModel->meshes.size() == 0 && hfmModel->joints.size() == 0) {
throw QString("empty geometry, possibly due to an unsupported FBX version");
}
} else if (_url.path().toLower().endsWith(".obj")) {
hfmModel.reset(OBJSerializer().read(_data, serializerMapping, _url));
hfmModel = OBJSerializer().read(_data, serializerMapping, _url);
} else if (_url.path().toLower().endsWith(".obj.gz")) {
QByteArray uncompressedData;
if (gunzip(_data, uncompressedData)){
hfmModel.reset(OBJSerializer().read(uncompressedData, serializerMapping, _url));
hfmModel = OBJSerializer().read(uncompressedData, serializerMapping, _url);
} else {
throw QString("failed to decompress .obj.gz");
}
} else if (_url.path().toLower().endsWith(".gltf")) {
hfmModel.reset(GLTFSerializer().read(_data, serializerMapping, _url));
hfmModel = GLTFSerializer().read(_data, serializerMapping, _url);
if (hfmModel->meshes.size() == 0 && hfmModel->joints.size() == 0) {
throw QString("empty geometry, possibly due to an unsupported GLTF version");
}

View file

@ -100,7 +100,7 @@ bool TestFbx::isReady() const {
void TestFbx::parseFbx(const QByteArray& fbxData) {
QVariantHash mapping;
HFMModel* hfmModel = FBXSerializer().read(fbxData, mapping);
HFMModel::Pointer hfmModel = FBXSerializer().read(fbxData, mapping);
size_t totalVertexCount = 0;
size_t totalIndexCount = 0;
size_t totalPartCount = 0;
@ -163,7 +163,6 @@ void TestFbx::parseFbx(const QByteArray& fbxData) {
_vertexBuffer->append(vertices);
_indexBuffer->append(indices);
_indirectBuffer->append(parts);
delete hfmModel;
}
void TestFbx::renderTest(size_t testId, RenderArgs* args) {

View file

@ -54,7 +54,7 @@ SkeletonDumpApp::SkeletonDumpApp(int argc, char* argv[]) : QCoreApplication(argc
return;
}
QByteArray blob = file.readAll();
std::unique_ptr<HFMModel> geometry(FBXSerializer().read(blob, QVariantHash()));
HFMModel::Pointer geometry = FBXSerializer().read(blob, QVariantHash());
std::unique_ptr<AnimSkeleton> skeleton(new AnimSkeleton(*geometry));
skeleton->dump(verbose);
}

View file

@ -43,9 +43,9 @@ bool vhacd::VHACDUtil::loadFBX(const QString filename, HFMModel& result) {
QByteArray fbxContents = fbx.readAll();
HFMModel::Pointer hfmModel;
if (filename.toLower().endsWith(".obj")) {
hfmModel.reset(OBJSerializer().read(fbxContents, QVariantHash(), filename));
hfmModel = OBJSerializer().read(fbxContents, QVariantHash(), filename);
} else if (filename.toLower().endsWith(".fbx")) {
hfmModel.reset(FBXSerializer().read(fbxContents, QVariantHash(), filename));
hfmModel = FBXSerializer().read(fbxContents, QVariantHash(), filename);
} else {
qWarning() << "file has unknown extension" << filename;
return false;