3
0
Fork 0
mirror of https://github.com/lubosz/overte.git synced 2025-04-27 08:55:31 +02:00

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() {
if (_tempDir.exists()) {
if (!_tempDir.remove(_originalFBXFilePath)) {
qCWarning(model_baking) << "Failed to remove temporary copy of fbx file:" << _originalFBXFilePath;
if (modelTempDir.exists()) {
if (!modelTempDir.remove(originalModelFilePath)) {
qCWarning(model_baking) << "Failed to remove temporary copy of fbx file:" << originalModelFilePath;
}
if (!_tempDir.rmdir(".")) {
qCWarning(model_baking) << "Failed to remove temporary directory:" << _tempDir;
if (!modelTempDir.rmdir(".")) {
qCWarning(model_baking) << "Failed to remove temporary directory:" << modelTempDir;
}
}
}
@ -56,12 +56,12 @@ void FBXBaker::abort() {
// tell our underlying TextureBaker instances to abort
// the FBXBaker will wait until all are aborted before emitting its own abort signal
for (auto& textureBaker : _bakingTextures) {
for (auto& textureBaker : bakingTextures) {
textureBaker->abort();
}
}
void FBXBaker::bake() {
void FBXBaker::bake() {
qDebug() << "FBXBaker" << modelURL << "bake starting";
auto tempDir = PathUtils::generateTemporaryDir();
@ -71,11 +71,11 @@ void FBXBaker::bake() {
return;
}
_tempDir = tempDir;
modelTempDir = tempDir;
_originalFBXFilePath = _tempDir.filePath(modelURL.fileName());
qDebug() << "Made temporary dir " << _tempDir;
qDebug() << "Origin file path: " << _originalFBXFilePath;
originalModelFilePath = modelTempDir.filePath(modelURL.fileName());
qDebug() << "Made temporary dir " << modelTempDir;
qDebug() << "Origin file path: " << originalModelFilePath;
// setup the output folder for the results of this bake
setupOutputFolder();
@ -148,7 +148,7 @@ void FBXBaker::loadSourceFBX() {
// load up the local file
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()) {
//QMessageBox::warning(this, "Could not find " + _fbxURL.toString(), "");
@ -162,7 +162,7 @@ void FBXBaker::loadSourceFBX() {
localFBX.copy(originalOutputDir + "/" + modelURL.fileName());
}
localFBX.copy(_originalFBXFilePath);
localFBX.copy(originalModelFilePath);
// emit our signal to start the import of the FBX source copy
emit sourceCopyReadyToLoad();
@ -193,13 +193,13 @@ void FBXBaker::handleFBXNetworkReply() {
qCDebug(model_baking) << "Downloaded" << modelURL;
// 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)) {
// 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;
}
if (copyOfOriginal.write(requestReply->readAll()) == -1) {
@ -223,11 +223,11 @@ void FBXBaker::handleFBXNetworkReply() {
}
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)) {
handleError("Error opening " + _originalFBXFilePath + " for reading");
handleError("Error opening " + originalModelFilePath + " for reading");
return;
}
@ -402,20 +402,20 @@ void FBXBaker::exportScene() {
auto baseName = fileName.left(fileName.lastIndexOf('.'));
auto bakedFilename = baseName + BAKED_FBX_EXTENSION;
_bakedFBXFilePath = bakedOutputDir + "/" + bakedFilename;
bakedModelFilePath = bakedOutputDir + "/" + bakedFilename;
auto fbxData = FBXWriter::encodeFBX(_rootNode);
QFile bakedFile(_bakedFBXFilePath);
QFile bakedFile(bakedModelFilePath);
if (!bakedFile.open(QIODevice::WriteOnly)) {
handleError("Error opening " + _bakedFBXFilePath + " for writing");
handleError("Error opening " + bakedModelFilePath + " for writing");
return;
}
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;
QUrl getFBXUrl() const { return modelURL; }
QString getBakedFBXFilePath() const { return _bakedFBXFilePath; }
QString getBakedFBXFilePath() const { return bakedModelFilePath; }
public slots:
virtual void bake() override;
@ -64,13 +64,6 @@ private:
FBXNode _rootNode;
FBXGeometry* _geometry;
QString _bakedFBXFilePath;
QDir _tempDir;
QString _originalFBXFilePath;
QMultiHash<QUrl, QSharedPointer<TextureBaker>> _bakingTextures;
QHash<QString, int> _textureNameMatchCount;
QHash<QUrl, QString> _remappedTexturePaths;

View file

@ -268,7 +268,7 @@ QByteArray* ModelBaker::compressTexture(QString modelTextureFileName, getTexture
textureChild = bakedTextureFileName.toLocal8Bit();
if (!_bakingTextures.contains(urlToTexture)) {
if (!bakingTextures.contains(urlToTexture)) {
_outputFiles.push_back(bakedTextureFilePath);
// 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);
// 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
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
_bakingTextures.remove(bakedTexture->getTextureURL());
bakingTextures.remove(bakedTexture->getTextureURL());
checkIfTexturesFinished();
} else {
@ -387,10 +387,10 @@ void ModelBaker::handleBakedTexture() {
_pendingErrorEmission = true;
// 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
for (auto& bakingTexture : _bakingTextures) {
for (auto& bakingTexture : bakingTextures) {
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
// but we do need to remove that TextureBaker from our list
// and then check if we're done with all textures
_bakingTextures.remove(bakedTexture->getTextureURL());
bakingTextures.remove(bakedTexture->getTextureURL());
checkIfTexturesFinished();
}
@ -424,7 +424,7 @@ void ModelBaker::checkIfTexturesFinished() {
// check if we're done everything we need to do for this model
// and emit our finished signal if we're done
if (_bakingTextures.isEmpty()) {
if (bakingTextures.isEmpty()) {
if (shouldStop()) {
// if we're checking for completion but we have errors
// that means one or more of our texture baking operations failed
@ -447,14 +447,14 @@ void ModelBaker::handleAbortedTexture() {
TextureBaker* bakedTexture = qobject_cast<TextureBaker*>(sender());
if (bakedTexture) {
_bakingTextures.remove(bakedTexture->getTextureURL());
bakingTextures.remove(bakedTexture->getTextureURL());
}
// since a texture we were baking aborted, our status is also aborted
_shouldAbort.store(true);
// abort any other ongoing texture bakes since we know we'll end up failing
for (auto& bakingTexture : _bakingTextures) {
for (auto& bakingTexture : bakingTextures) {
bakingTexture->abort();
}

View file

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

View file

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

View file

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