moved more common members to ModelBaker parent class

This commit is contained in:
utkarshgautamnyu 2017-10-25 18:31:50 -07:00
parent 854c190f32
commit 10eb258d3b
6 changed files with 60 additions and 72 deletions

View file

@ -41,12 +41,12 @@ FBXBaker::FBXBaker(const QUrl& fbxURL, TextureBakerThreadGetter textureThreadGet
} }
FBXBaker::~FBXBaker() { FBXBaker::~FBXBaker() {
if (_tempDir.exists()) { if (modelTempDir.exists()) {
if (!_tempDir.remove(_originalFBXFilePath)) { if (!modelTempDir.remove(originalModelFilePath)) {
qCWarning(model_baking) << "Failed to remove temporary copy of fbx file:" << _originalFBXFilePath; qCWarning(model_baking) << "Failed to remove temporary copy of fbx file:" << originalModelFilePath;
} }
if (!_tempDir.rmdir(".")) { if (!modelTempDir.rmdir(".")) {
qCWarning(model_baking) << "Failed to remove temporary directory:" << _tempDir; qCWarning(model_baking) << "Failed to remove temporary directory:" << modelTempDir;
} }
} }
} }
@ -56,12 +56,12 @@ void FBXBaker::abort() {
// tell our underlying TextureBaker instances to abort // tell our underlying TextureBaker instances to abort
// the FBXBaker will wait until all are aborted before emitting its own abort signal // the FBXBaker will wait until all are aborted before emitting its own abort signal
for (auto& textureBaker : _bakingTextures) { for (auto& textureBaker : bakingTextures) {
textureBaker->abort(); textureBaker->abort();
} }
} }
void FBXBaker::bake() { void FBXBaker::bake() {
qDebug() << "FBXBaker" << modelURL << "bake starting"; qDebug() << "FBXBaker" << modelURL << "bake starting";
auto tempDir = PathUtils::generateTemporaryDir(); auto tempDir = PathUtils::generateTemporaryDir();
@ -71,11 +71,11 @@ void FBXBaker::bake() {
return; return;
} }
_tempDir = tempDir; modelTempDir = tempDir;
_originalFBXFilePath = _tempDir.filePath(modelURL.fileName()); originalModelFilePath = modelTempDir.filePath(modelURL.fileName());
qDebug() << "Made temporary dir " << _tempDir; qDebug() << "Made temporary dir " << modelTempDir;
qDebug() << "Origin file path: " << _originalFBXFilePath; qDebug() << "Origin file path: " << originalModelFilePath;
// setup the output folder for the results of this bake // setup the output folder for the results of this bake
setupOutputFolder(); setupOutputFolder();
@ -148,7 +148,7 @@ void FBXBaker::loadSourceFBX() {
// load up the local file // load up the local file
QFile localFBX { modelURL.toLocalFile() }; QFile localFBX { modelURL.toLocalFile() };
qDebug() << "Local file url: " << modelURL << modelURL.toString() << modelURL.toLocalFile() << ", copying to: " << _originalFBXFilePath; qDebug() << "Local file url: " << modelURL << modelURL.toString() << modelURL.toLocalFile() << ", copying to: " << originalModelFilePath;
if (!localFBX.exists()) { if (!localFBX.exists()) {
//QMessageBox::warning(this, "Could not find " + _fbxURL.toString(), ""); //QMessageBox::warning(this, "Could not find " + _fbxURL.toString(), "");
@ -162,7 +162,7 @@ void FBXBaker::loadSourceFBX() {
localFBX.copy(originalOutputDir + "/" + modelURL.fileName()); localFBX.copy(originalOutputDir + "/" + modelURL.fileName());
} }
localFBX.copy(_originalFBXFilePath); localFBX.copy(originalModelFilePath);
// emit our signal to start the import of the FBX source copy // emit our signal to start the import of the FBX source copy
emit sourceCopyReadyToLoad(); emit sourceCopyReadyToLoad();
@ -193,13 +193,13 @@ void FBXBaker::handleFBXNetworkReply() {
qCDebug(model_baking) << "Downloaded" << modelURL; qCDebug(model_baking) << "Downloaded" << modelURL;
// grab the contents of the reply and make a copy in the output folder // grab the contents of the reply and make a copy in the output folder
QFile copyOfOriginal(_originalFBXFilePath); QFile copyOfOriginal(originalModelFilePath);
qDebug(model_baking) << "Writing copy of original FBX to" << _originalFBXFilePath << copyOfOriginal.fileName(); qDebug(model_baking) << "Writing copy of original FBX to" << originalModelFilePath << copyOfOriginal.fileName();
if (!copyOfOriginal.open(QIODevice::WriteOnly)) { if (!copyOfOriginal.open(QIODevice::WriteOnly)) {
// add an error to the error list for this FBX stating that a duplicate of the original FBX could not be made // add an error to the error list for this FBX stating that a duplicate of the original FBX could not be made
handleError("Could not create copy of " + modelURL.toString() + " (Failed to open " + _originalFBXFilePath + ")"); handleError("Could not create copy of " + modelURL.toString() + " (Failed to open " + originalModelFilePath + ")");
return; return;
} }
if (copyOfOriginal.write(requestReply->readAll()) == -1) { if (copyOfOriginal.write(requestReply->readAll()) == -1) {
@ -223,11 +223,11 @@ void FBXBaker::handleFBXNetworkReply() {
} }
void FBXBaker::importScene() { void FBXBaker::importScene() {
qDebug() << "file path: " << _originalFBXFilePath.toLocal8Bit().data() << QDir(_originalFBXFilePath).exists(); qDebug() << "file path: " << originalModelFilePath.toLocal8Bit().data() << QDir(originalModelFilePath).exists();
QFile fbxFile(_originalFBXFilePath); QFile fbxFile(originalModelFilePath);
if (!fbxFile.open(QIODevice::ReadOnly)) { if (!fbxFile.open(QIODevice::ReadOnly)) {
handleError("Error opening " + _originalFBXFilePath + " for reading"); handleError("Error opening " + originalModelFilePath + " for reading");
return; return;
} }
@ -402,20 +402,20 @@ void FBXBaker::exportScene() {
auto baseName = fileName.left(fileName.lastIndexOf('.')); auto baseName = fileName.left(fileName.lastIndexOf('.'));
auto bakedFilename = baseName + BAKED_FBX_EXTENSION; auto bakedFilename = baseName + BAKED_FBX_EXTENSION;
_bakedFBXFilePath = bakedOutputDir + "/" + bakedFilename; bakedModelFilePath = bakedOutputDir + "/" + bakedFilename;
auto fbxData = FBXWriter::encodeFBX(_rootNode); auto fbxData = FBXWriter::encodeFBX(_rootNode);
QFile bakedFile(_bakedFBXFilePath); QFile bakedFile(bakedModelFilePath);
if (!bakedFile.open(QIODevice::WriteOnly)) { if (!bakedFile.open(QIODevice::WriteOnly)) {
handleError("Error opening " + _bakedFBXFilePath + " for writing"); handleError("Error opening " + bakedModelFilePath + " for writing");
return; return;
} }
bakedFile.write(fbxData); bakedFile.write(fbxData);
_outputFiles.push_back(_bakedFBXFilePath); _outputFiles.push_back(bakedModelFilePath);
qCDebug(model_baking) << "Exported" << modelURL << "with re-written paths to" << _bakedFBXFilePath; qCDebug(model_baking) << "Exported" << modelURL << "with re-written paths to" << bakedModelFilePath;
} }

View file

@ -38,7 +38,7 @@ public:
~FBXBaker() override; ~FBXBaker() override;
QUrl getFBXUrl() const { return modelURL; } QUrl getFBXUrl() const { return modelURL; }
QString getBakedFBXFilePath() const { return _bakedFBXFilePath; } QString getBakedFBXFilePath() const { return bakedModelFilePath; }
public slots: public slots:
virtual void bake() override; virtual void bake() override;
@ -64,13 +64,6 @@ private:
FBXNode _rootNode; FBXNode _rootNode;
FBXGeometry* _geometry; FBXGeometry* _geometry;
QString _bakedFBXFilePath;
QDir _tempDir;
QString _originalFBXFilePath;
QMultiHash<QUrl, QSharedPointer<TextureBaker>> _bakingTextures;
QHash<QString, int> _textureNameMatchCount; QHash<QString, int> _textureNameMatchCount;
QHash<QUrl, QString> _remappedTexturePaths; QHash<QUrl, QString> _remappedTexturePaths;

View file

@ -268,7 +268,7 @@ QByteArray* ModelBaker::compressTexture(QString modelTextureFileName, getTexture
textureChild = bakedTextureFileName.toLocal8Bit(); textureChild = bakedTextureFileName.toLocal8Bit();
if (!_bakingTextures.contains(urlToTexture)) { if (!bakingTextures.contains(urlToTexture)) {
_outputFiles.push_back(bakedTextureFilePath); _outputFiles.push_back(bakedTextureFilePath);
// bake this texture asynchronously // bake this texture asynchronously
@ -325,7 +325,7 @@ void ModelBaker::bakeTexture(const QUrl& textureURL, image::TextureUsage::Type t
connect(bakingTexture.data(), &TextureBaker::aborted, this, &ModelBaker::handleAbortedTexture); connect(bakingTexture.data(), &TextureBaker::aborted, this, &ModelBaker::handleAbortedTexture);
// keep a shared pointer to the baking texture // keep a shared pointer to the baking texture
_bakingTextures.insert(textureURL, bakingTexture); bakingTextures.insert(textureURL, bakingTexture);
// start baking the texture on one of our available worker threads // start baking the texture on one of our available worker threads
bakingTexture->moveToThread(textureThreadGetter()); bakingTexture->moveToThread(textureThreadGetter());
@ -376,7 +376,7 @@ void ModelBaker::handleBakedTexture() {
// now that this texture has been baked and handled, we can remove that TextureBaker from our hash // now that this texture has been baked and handled, we can remove that TextureBaker from our hash
_bakingTextures.remove(bakedTexture->getTextureURL()); bakingTextures.remove(bakedTexture->getTextureURL());
checkIfTexturesFinished(); checkIfTexturesFinished();
} else { } else {
@ -387,10 +387,10 @@ void ModelBaker::handleBakedTexture() {
_pendingErrorEmission = true; _pendingErrorEmission = true;
// now that this texture has been baked, even though it failed, we can remove that TextureBaker from our list // now that this texture has been baked, even though it failed, we can remove that TextureBaker from our list
_bakingTextures.remove(bakedTexture->getTextureURL()); bakingTextures.remove(bakedTexture->getTextureURL());
// abort any other ongoing texture bakes since we know we'll end up failing // abort any other ongoing texture bakes since we know we'll end up failing
for (auto& bakingTexture : _bakingTextures) { for (auto& bakingTexture : bakingTextures) {
bakingTexture->abort(); bakingTexture->abort();
} }
@ -400,7 +400,7 @@ void ModelBaker::handleBakedTexture() {
// we have errors to attend to, so we don't do extra processing for this texture // we have errors to attend to, so we don't do extra processing for this texture
// but we do need to remove that TextureBaker from our list // but we do need to remove that TextureBaker from our list
// and then check if we're done with all textures // and then check if we're done with all textures
_bakingTextures.remove(bakedTexture->getTextureURL()); bakingTextures.remove(bakedTexture->getTextureURL());
checkIfTexturesFinished(); checkIfTexturesFinished();
} }
@ -424,7 +424,7 @@ void ModelBaker::checkIfTexturesFinished() {
// check if we're done everything we need to do for this model // check if we're done everything we need to do for this model
// and emit our finished signal if we're done // and emit our finished signal if we're done
if (_bakingTextures.isEmpty()) { if (bakingTextures.isEmpty()) {
if (shouldStop()) { if (shouldStop()) {
// if we're checking for completion but we have errors // if we're checking for completion but we have errors
// that means one or more of our texture baking operations failed // that means one or more of our texture baking operations failed
@ -447,14 +447,14 @@ void ModelBaker::handleAbortedTexture() {
TextureBaker* bakedTexture = qobject_cast<TextureBaker*>(sender()); TextureBaker* bakedTexture = qobject_cast<TextureBaker*>(sender());
if (bakedTexture) { if (bakedTexture) {
_bakingTextures.remove(bakedTexture->getTextureURL()); bakingTextures.remove(bakedTexture->getTextureURL());
} }
// since a texture we were baking aborted, our status is also aborted // since a texture we were baking aborted, our status is also aborted
_shouldAbort.store(true); _shouldAbort.store(true);
// abort any other ongoing texture bakes since we know we'll end up failing // abort any other ongoing texture bakes since we know we'll end up failing
for (auto& bakingTexture : _bakingTextures) { for (auto& bakingTexture : bakingTextures) {
bakingTexture->abort(); bakingTexture->abort();
} }

View file

@ -39,7 +39,7 @@ public:
bool compressMesh(FBXMesh& mesh, bool hasDeformers, FBXNode& dracoMeshNode, getMaterialIDCallback materialIDCallback = NULL); bool compressMesh(FBXMesh& mesh, bool hasDeformers, FBXNode& dracoMeshNode, getMaterialIDCallback materialIDCallback = NULL);
QByteArray* compressTexture(QString textureFileName, getTextureTypeCallback textureTypeCallback = NULL); QByteArray* compressTexture(QString textureFileName, getTextureTypeCallback textureTypeCallback = NULL);
virtual void setWasAborted(bool wasAborted) override; virtual void setWasAborted(bool wasAborted) override;
protected: protected:
void checkIfTexturesFinished(); void checkIfTexturesFinished();
@ -48,6 +48,10 @@ protected:
QString originalOutputDir; QString originalOutputDir;
TextureBakerThreadGetter textureThreadGetter; TextureBakerThreadGetter textureThreadGetter;
QUrl modelURL; QUrl modelURL;
QString bakedModelFilePath;
QDir modelTempDir;
QString originalModelFilePath;
QMultiHash<QUrl, QSharedPointer<TextureBaker>> bakingTextures;
public slots: public slots:
virtual void bake() override; virtual void bake() override;
@ -66,10 +70,6 @@ private:
QHash<QString, int> _textureNameMatchCount; QHash<QString, int> _textureNameMatchCount;
QHash<QUrl, QString> _remappedTexturePaths; QHash<QUrl, QString> _remappedTexturePaths;
//QUrl _modelURL;
QMultiHash<QUrl, QSharedPointer<TextureBaker>> _bakingTextures;
//TextureBakerThreadGetter _textureThreadGetter;
//QString _originalOutputDir;
bool _pendingErrorEmission{ false }; bool _pendingErrorEmission{ false };
}; };

View file

@ -42,12 +42,12 @@ OBJBaker::OBJBaker(const QUrl& objURL, TextureBakerThreadGetter textureThreadGet
} }
OBJBaker::~OBJBaker() { OBJBaker::~OBJBaker() {
if (_tempDir.exists()) { if (modelTempDir.exists()) {
if (!_tempDir.remove(_originalOBJFilePath)) { if (!modelTempDir.remove(originalModelFilePath)) {
qCWarning(model_baking) << "Failed to remove temporary copy of OBJ file:" << _originalOBJFilePath; qCWarning(model_baking) << "Failed to remove temporary copy of OBJ file:" << originalModelFilePath;
} }
if (!_tempDir.rmdir(".")) { if (!modelTempDir.rmdir(".")) {
qCWarning(model_baking) << "Failed to remove temporary directory:" << _tempDir; qCWarning(model_baking) << "Failed to remove temporary directory:" << modelTempDir;
} }
} }
} }
@ -62,11 +62,11 @@ void OBJBaker::bake() {
return; return;
} }
_tempDir = tempDir; modelTempDir = tempDir;
_originalOBJFilePath = _tempDir.filePath(modelURL.fileName()); originalModelFilePath = modelTempDir.filePath(modelURL.fileName());
qDebug() << "Made temporary dir " << _tempDir; qDebug() << "Made temporary dir " << modelTempDir;
qDebug() << "Origin file path: " << _originalOBJFilePath; qDebug() << "Origin file path: " << originalModelFilePath;
// trigger bakeOBJ once OBJ is loaded // trigger bakeOBJ once OBJ is loaded
connect(this, &OBJBaker::OBJLoaded, this, &OBJBaker::bakeOBJ); connect(this, &OBJBaker::OBJLoaded, this, &OBJBaker::bakeOBJ);
@ -81,7 +81,7 @@ void OBJBaker::loadOBJ() {
// loading the local OBJ // loading the local OBJ
QFile localOBJ{ modelURL.toLocalFile() }; QFile localOBJ{ modelURL.toLocalFile() };
qDebug() << "Local file url: " << modelURL << modelURL.toString() << modelURL.toLocalFile() << ", copying to: " << _originalOBJFilePath; qDebug() << "Local file url: " << modelURL << modelURL.toString() << modelURL.toLocalFile() << ", copying to: " << originalModelFilePath;
if (!localOBJ.exists()) { if (!localOBJ.exists()) {
handleError("Could not find " + modelURL.toString()); handleError("Could not find " + modelURL.toString());
@ -94,7 +94,7 @@ void OBJBaker::loadOBJ() {
localOBJ.copy(originalOutputDir + "/" + modelURL.fileName()); localOBJ.copy(originalOutputDir + "/" + modelURL.fileName());
} }
localOBJ.copy(_originalOBJFilePath); localOBJ.copy(originalModelFilePath);
// local OBJ is loaded emit signal to trigger its baking // local OBJ is loaded emit signal to trigger its baking
emit OBJLoaded(); emit OBJLoaded();
@ -124,13 +124,13 @@ void OBJBaker::handleOBJNetworkReply() {
qCDebug(model_baking) << "Downloaded" << modelURL; qCDebug(model_baking) << "Downloaded" << modelURL;
// grab the contents of the reply and make a copy in the output folder // grab the contents of the reply and make a copy in the output folder
QFile copyOfOriginal(_originalOBJFilePath); QFile copyOfOriginal(originalModelFilePath);
qDebug(model_baking) << "Writing copy of original obj to" << _originalOBJFilePath << copyOfOriginal.fileName(); qDebug(model_baking) << "Writing copy of original obj to" << originalModelFilePath << copyOfOriginal.fileName();
if (!copyOfOriginal.open(QIODevice::WriteOnly)) { if (!copyOfOriginal.open(QIODevice::WriteOnly)) {
// add an error to the error list for this obj stating that a duplicate of the original obj could not be made // add an error to the error list for this obj stating that a duplicate of the original obj could not be made
handleError("Could not create copy of " + modelURL.toString() + " (Failed to open " + _originalOBJFilePath + ")"); handleError("Could not create copy of " + modelURL.toString() + " (Failed to open " + originalModelFilePath + ")");
return; return;
} }
if (copyOfOriginal.write(requestReply->readAll()) == -1) { if (copyOfOriginal.write(requestReply->readAll()) == -1) {
@ -156,9 +156,9 @@ void OBJBaker::handleOBJNetworkReply() {
void OBJBaker::bakeOBJ() { void OBJBaker::bakeOBJ() {
// Read the OBJ file // Read the OBJ file
QFile objFile(_originalOBJFilePath); QFile objFile(originalModelFilePath);
if (!objFile.open(QIODevice::ReadOnly)) { if (!objFile.open(QIODevice::ReadOnly)) {
handleError("Error opening " + _originalOBJFilePath + " for reading"); handleError("Error opening " + originalModelFilePath + " for reading");
return; return;
} }
@ -180,20 +180,20 @@ void OBJBaker::bakeOBJ() {
auto baseName = fileName.left(fileName.lastIndexOf('.')); auto baseName = fileName.left(fileName.lastIndexOf('.'));
auto bakedFilename = baseName + ".baked.fbx"; auto bakedFilename = baseName + ".baked.fbx";
_bakedOBJFilePath = bakedOutputDir + "/" + bakedFilename; bakedModelFilePath = bakedOutputDir + "/" + bakedFilename;
QFile bakedFile; QFile bakedFile;
bakedFile.setFileName(_bakedOBJFilePath); bakedFile.setFileName(bakedModelFilePath);
if (!bakedFile.open(QIODevice::WriteOnly)) { if (!bakedFile.open(QIODevice::WriteOnly)) {
handleError("Error opening " + _bakedOBJFilePath + " for writing"); handleError("Error opening " + bakedModelFilePath + " for writing");
return; return;
} }
bakedFile.write(encodedFBX); bakedFile.write(encodedFBX);
// Export successful // Export successful
_outputFiles.push_back(_bakedOBJFilePath); _outputFiles.push_back(bakedModelFilePath);
qCDebug(model_baking) << "Exported" << modelURL << "to" << _bakedOBJFilePath; qCDebug(model_baking) << "Exported" << modelURL << "to" << bakedModelFilePath;
// Export done emit finished // Export done emit finished
emit finished(); emit finished();

View file

@ -45,11 +45,6 @@ private slots:
private: private:
qlonglong _nodeID = 0; qlonglong _nodeID = 0;
QString _bakedOBJFilePath;
QDir _tempDir;
QString _originalOBJFilePath;
QMultiHash<QUrl, QSharedPointer<TextureBaker>> _bakingTextures;
qlonglong _geometryID; qlonglong _geometryID;
qlonglong _modelID; qlonglong _modelID;
std::vector<qlonglong> _materialIDs; std::vector<qlonglong> _materialIDs;