Moved common variables between base and derived classes to base class

This commit is contained in:
utkarshgautamnyu 2017-10-25 16:13:06 -07:00
parent 54f9d52aa1
commit d7f2e21dca
6 changed files with 68 additions and 79 deletions

View file

@ -34,14 +34,12 @@
#include "FBXBaker.h"
FBXBaker::FBXBaker(const QUrl& fbxURL, TextureBakerThreadGetter textureThreadGetter,
const QString& bakedOutputDir, const QString& originalOutputDir) :
_fbxURL(fbxURL),
_bakedOutputDir(bakedOutputDir),
_originalOutputDir(originalOutputDir),
_textureThreadGetter(textureThreadGetter) {
const QString& bakedOutputDir, const QString& originalOutputDir) :
ModelBaker(fbxURL, textureThreadGetter, bakedOutputDir, originalOutputDir)
{
}
FBXBaker::~FBXBaker() {
if (_tempDir.exists()) {
if (!_tempDir.remove(_originalFBXFilePath)) {
@ -64,7 +62,7 @@ void FBXBaker::abort() {
}
void FBXBaker::bake() {
qDebug() << "FBXBaker" << _fbxURL << "bake starting";
qDebug() << "FBXBaker" << _modelURL << "bake starting";
auto tempDir = PathUtils::generateTemporaryDir();
@ -75,7 +73,7 @@ void FBXBaker::bake() {
_tempDir = tempDir;
_originalFBXFilePath = _tempDir.filePath(_fbxURL.fileName());
_originalFBXFilePath = _tempDir.filePath(_modelURL.fileName());
qDebug() << "Made temporary dir " << _tempDir;
qDebug() << "Origin file path: " << _originalFBXFilePath;
@ -146,22 +144,22 @@ void FBXBaker::setupOutputFolder() {
void FBXBaker::loadSourceFBX() {
// check if the FBX is local or first needs to be downloaded
if (_fbxURL.isLocalFile()) {
if (_modelURL.isLocalFile()) {
// load up the local file
QFile localFBX { _fbxURL.toLocalFile() };
QFile localFBX { _modelURL.toLocalFile() };
qDebug() << "Local file url: " << _fbxURL << _fbxURL.toString() << _fbxURL.toLocalFile() << ", copying to: " << _originalFBXFilePath;
qDebug() << "Local file url: " << _modelURL << _modelURL.toString() << _modelURL.toLocalFile() << ", copying to: " << _originalFBXFilePath;
if (!localFBX.exists()) {
//QMessageBox::warning(this, "Could not find " + _fbxURL.toString(), "");
handleError("Could not find " + _fbxURL.toString());
handleError("Could not find " + _modelURL.toString());
return;
}
// make a copy in the output folder
if (!_originalOutputDir.isEmpty()) {
qDebug() << "Copying to: " << _originalOutputDir << "/" << _fbxURL.fileName();
localFBX.copy(_originalOutputDir + "/" + _fbxURL.fileName());
qDebug() << "Copying to: " << _originalOutputDir << "/" << _modelURL.fileName();
localFBX.copy(_originalOutputDir + "/" + _modelURL.fileName());
}
localFBX.copy(_originalFBXFilePath);
@ -179,9 +177,9 @@ void FBXBaker::loadSourceFBX() {
networkRequest.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork);
networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
networkRequest.setUrl(_fbxURL);
networkRequest.setUrl(_modelURL);
qCDebug(model_baking) << "Downloading" << _fbxURL;
qCDebug(model_baking) << "Downloading" << _modelURL;
auto networkReply = networkAccessManager.get(networkRequest);
connect(networkReply, &QNetworkReply::finished, this, &FBXBaker::handleFBXNetworkReply);
@ -192,7 +190,7 @@ void FBXBaker::handleFBXNetworkReply() {
auto requestReply = qobject_cast<QNetworkReply*>(sender());
if (requestReply->error() == QNetworkReply::NoError) {
qCDebug(model_baking) << "Downloaded" << _fbxURL;
qCDebug(model_baking) << "Downloaded" << _modelURL;
// grab the contents of the reply and make a copy in the output folder
QFile copyOfOriginal(_originalFBXFilePath);
@ -201,11 +199,11 @@ void FBXBaker::handleFBXNetworkReply() {
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 " + _fbxURL.toString() + " (Failed to open " + _originalFBXFilePath + ")");
handleError("Could not create copy of " + _modelURL.toString() + " (Failed to open " + _originalFBXFilePath + ")");
return;
}
if (copyOfOriginal.write(requestReply->readAll()) == -1) {
handleError("Could not create copy of " + _fbxURL.toString() + " (Failed to write)");
handleError("Could not create copy of " + _modelURL.toString() + " (Failed to write)");
return;
}
@ -213,14 +211,14 @@ void FBXBaker::handleFBXNetworkReply() {
copyOfOriginal.close();
if (!_originalOutputDir.isEmpty()) {
copyOfOriginal.copy(_originalOutputDir + "/" + _fbxURL.fileName());
copyOfOriginal.copy(_originalOutputDir + "/" + _modelURL.fileName());
}
// emit our signal to start the import of the FBX source copy
emit sourceCopyReadyToLoad();
} else {
// add an error to our list stating that the FBX could not be downloaded
handleError("Failed to download " + _fbxURL.toString());
handleError("Failed to download " + _modelURL.toString());
}
}
@ -235,9 +233,9 @@ void FBXBaker::importScene() {
FBXReader reader;
qCDebug(model_baking) << "Parsing" << _fbxURL;
qCDebug(model_baking) << "Parsing" << _modelURL;
_rootNode = reader._rootNode = reader.parseFBX(&fbxFile);
_geometry = reader.extractFBXGeometry({}, _fbxURL.toString());
_geometry = reader.extractFBXGeometry({}, _modelURL.toString());
_textureContent = reader._textureContent;
}
@ -375,8 +373,7 @@ void FBXBaker::rewriteAndBakeSceneTextures() {
};
// Compress the texture information and return the new filename to be added into the FBX scene
QByteArray* bakedTextureFile = this->compressTexture(fbxTextureFileName, _fbxURL, _bakedOutputDir, _textureThreadGetter,
textureTypeCallback, _originalOutputDir);
QByteArray* bakedTextureFile = this->compressTexture(fbxTextureFileName, textureTypeCallback);
// If no errors or warnings have occurred during texture compression add the filename to the FBX scene
if (bakedTextureFile) {
@ -407,7 +404,7 @@ void FBXBaker::rewriteAndBakeSceneTextures() {
void FBXBaker::exportScene() {
// save the relative path to this FBX inside our passed output folder
auto fileName = _fbxURL.fileName();
auto fileName = _modelURL.fileName();
auto baseName = fileName.left(fileName.lastIndexOf('.'));
auto bakedFilename = baseName + BAKED_FBX_EXTENSION;
@ -426,5 +423,5 @@ void FBXBaker::exportScene() {
_outputFiles.push_back(_bakedFBXFilePath);
qCDebug(model_baking) << "Exported" << _fbxURL << "with re-written paths to" << _bakedFBXFilePath;
qCDebug(model_baking) << "Exported" << _modelURL << "with re-written paths to" << _bakedFBXFilePath;
}

View file

@ -37,7 +37,7 @@ public:
const QString& bakedOutputDir, const QString& originalOutputDir = "");
~FBXBaker() override;
QUrl getFBXUrl() const { return _fbxURL; }
QUrl getFBXUrl() const { return _modelURL; }
QString getBakedFBXFilePath() const { return _bakedFBXFilePath; }
public slots:
@ -62,18 +62,11 @@ private:
void exportScene();
void removeEmbeddedMediaFolder();
QUrl _fbxURL;
FBXNode _rootNode;
FBXGeometry* _geometry;
QString _bakedFBXFilePath;
QString _bakedOutputDir;
// If set, the original FBX and textures will also be copied here
QString _originalOutputDir;
QDir _tempDir;
QString _originalFBXFilePath;
@ -81,8 +74,6 @@ private:
QHash<QString, int> _textureNameMatchCount;
QHash<QUrl, QString> _remappedTexturePaths;
TextureBakerThreadGetter _textureThreadGetter;
bool _pendingErrorEmission { false };
};

View file

@ -29,7 +29,15 @@
#pragma warning( pop )
#endif
ModelBaker::ModelBaker() {}
ModelBaker::ModelBaker(const QUrl& modelURL, TextureBakerThreadGetter textureThreadGetter,
const QString& bakedOutputDir, const QString& originalOutputDir) :
_modelURL(modelURL),
_textureThreadGetter(textureThreadGetter),
_bakedOutputDir(bakedOutputDir),
_originalOutputDir(originalOutputDir)
{
}
void ModelBaker::bake() {}
@ -206,12 +214,7 @@ bool ModelBaker::compressMesh(FBXMesh& mesh, bool hasDeformers,FBXNode& dracoMes
return true;
}
QByteArray* ModelBaker::compressTexture(QString modelTextureFileName, QUrl modelURL, QString bakedOutputDir, TextureBakerThreadGetter textureThreadGetter,
getTextureTypeCallback textureTypeCallback, const QString& originalOutputDir) {
_modelURL = modelURL;
_textureThreadGetter = textureThreadGetter;
_originalOutputDir = originalOutputDir;
QByteArray* ModelBaker::compressTexture(QString modelTextureFileName, getTextureTypeCallback textureTypeCallback) {
static QByteArray textureChild;
QByteArray textureContent;
image::TextureUsage::Type textureType;
@ -260,7 +263,7 @@ QByteArray* ModelBaker::compressTexture(QString modelTextureFileName, QUrl model
<< "to" << bakedTextureFileName;
QString bakedTextureFilePath{
bakedOutputDir + "/" + bakedTextureFileName
_bakedOutputDir + "/" + bakedTextureFileName
};
textureChild = bakedTextureFileName.toLocal8Bit();
@ -269,7 +272,7 @@ QByteArray* ModelBaker::compressTexture(QString modelTextureFileName, QUrl model
_outputFiles.push_back(bakedTextureFilePath);
// bake this texture asynchronously
bakeTexture(urlToTexture, textureType, bakedOutputDir, bakedTextureFileName, textureContent);
bakeTexture(urlToTexture, textureType, _bakedOutputDir, bakedTextureFileName, textureContent);
}
}

View file

@ -34,15 +34,20 @@ class ModelBaker : public Baker{
Q_OBJECT
public:
ModelBaker();
ModelBaker(const QUrl& modelURL, TextureBakerThreadGetter textureThreadGetter,
const QString& bakedOutputDir, const QString& originalOutputDir);
bool compressMesh(FBXMesh& mesh, bool hasDeformers, FBXNode& dracoMeshNode, getMaterialIDCallback materialIDCallback = NULL);
QByteArray* compressTexture(QString textureFileName, QUrl modelUrl, QString bakedOutputDir, TextureBakerThreadGetter textureThreadGetter,
getTextureTypeCallback textureTypeCallback = NULL, const QString& originalOutputDir = "");
QByteArray* compressTexture(QString textureFileName, getTextureTypeCallback textureTypeCallback = NULL);
virtual void setWasAborted(bool wasAborted) override;
protected:
void checkIfTexturesFinished();
QHash<QByteArray, QByteArray> _textureContent;
QString _bakedOutputDir;
QString _originalOutputDir;
TextureBakerThreadGetter _textureThreadGetter;
QUrl _modelURL;
public slots:
virtual void bake() override;
@ -61,10 +66,10 @@ private:
QHash<QString, int> _textureNameMatchCount;
QHash<QUrl, QString> _remappedTexturePaths;
QUrl _modelURL;
//QUrl _modelURL;
QMultiHash<QUrl, QSharedPointer<TextureBaker>> _bakingTextures;
TextureBakerThreadGetter _textureThreadGetter;
QString _originalOutputDir;
//TextureBakerThreadGetter _textureThreadGetter;
//QString _originalOutputDir;
bool _pendingErrorEmission{ false };
};

View file

@ -36,10 +36,7 @@ const QByteArray MESH = "Mesh";
OBJBaker::OBJBaker(const QUrl& objURL, TextureBakerThreadGetter textureThreadGetter,
const QString& bakedOutputDir, const QString& originalOutputDir) :
_objURL(objURL),
_bakedOutputDir(bakedOutputDir),
_originalOutputDir(originalOutputDir),
_textureThreadGetter(textureThreadGetter)
ModelBaker(objURL, textureThreadGetter, bakedOutputDir, originalOutputDir)
{
}
@ -56,7 +53,7 @@ OBJBaker::~OBJBaker() {
}
void OBJBaker::bake() {
qDebug() << "OBJBaker" << _objURL << "bake starting";
qDebug() << "OBJBaker" << _modelURL << "bake starting";
auto tempDir = PathUtils::generateTemporaryDir();
@ -67,7 +64,7 @@ void OBJBaker::bake() {
_tempDir = tempDir;
_originalOBJFilePath = _tempDir.filePath(_objURL.fileName());
_originalOBJFilePath = _tempDir.filePath(_modelURL.fileName());
qDebug() << "Made temporary dir " << _tempDir;
qDebug() << "Origin file path: " << _originalOBJFilePath;
@ -80,21 +77,21 @@ void OBJBaker::bake() {
void OBJBaker::loadOBJ() {
// check if the OBJ is local or it needs to be downloaded
if (_objURL.isLocalFile()) {
if (_modelURL.isLocalFile()) {
// loading the local OBJ
QFile localOBJ{ _objURL.toLocalFile() };
QFile localOBJ{ _modelURL.toLocalFile() };
qDebug() << "Local file url: " << _objURL << _objURL.toString() << _objURL.toLocalFile() << ", copying to: " << _originalOBJFilePath;
qDebug() << "Local file url: " << _modelURL << _modelURL.toString() << _modelURL.toLocalFile() << ", copying to: " << _originalOBJFilePath;
if (!localOBJ.exists()) {
handleError("Could not find " + _objURL.toString());
handleError("Could not find " + _modelURL.toString());
return;
}
// make a copy in the output folder
if (!_originalOutputDir.isEmpty()) {
qDebug() << "Copying to: " << _originalOutputDir << "/" << _objURL.fileName();
localOBJ.copy(_originalOutputDir + "/" + _objURL.fileName());
qDebug() << "Copying to: " << _originalOutputDir << "/" << _modelURL.fileName();
localOBJ.copy(_originalOutputDir + "/" + _modelURL.fileName());
}
localOBJ.copy(_originalOBJFilePath);
@ -111,9 +108,9 @@ void OBJBaker::loadOBJ() {
networkRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
networkRequest.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork);
networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
networkRequest.setUrl(_objURL);
networkRequest.setUrl(_modelURL);
qCDebug(model_baking) << "Downloading" << _objURL;
qCDebug(model_baking) << "Downloading" << _modelURL;
auto networkReply = networkAccessManager.get(networkRequest);
connect(networkReply, &QNetworkReply::finished, this, &OBJBaker::handleOBJNetworkReply);
@ -124,7 +121,7 @@ void OBJBaker::handleOBJNetworkReply() {
auto requestReply = qobject_cast<QNetworkReply*>(sender());
if (requestReply->error() == QNetworkReply::NoError) {
qCDebug(model_baking) << "Downloaded" << _objURL;
qCDebug(model_baking) << "Downloaded" << _modelURL;
// grab the contents of the reply and make a copy in the output folder
QFile copyOfOriginal(_originalOBJFilePath);
@ -133,11 +130,11 @@ void OBJBaker::handleOBJNetworkReply() {
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 " + _objURL.toString() + " (Failed to open " + _originalOBJFilePath + ")");
handleError("Could not create copy of " + _modelURL.toString() + " (Failed to open " + _originalOBJFilePath + ")");
return;
}
if (copyOfOriginal.write(requestReply->readAll()) == -1) {
handleError("Could not create copy of " + _objURL.toString() + " (Failed to write)");
handleError("Could not create copy of " + _modelURL.toString() + " (Failed to write)");
return;
}
@ -145,14 +142,14 @@ void OBJBaker::handleOBJNetworkReply() {
copyOfOriginal.close();
if (!_originalOutputDir.isEmpty()) {
copyOfOriginal.copy(_originalOutputDir + "/" + _objURL.fileName());
copyOfOriginal.copy(_originalOutputDir + "/" + _modelURL.fileName());
}
// remote OBJ is loaded emit signal to trigger its baking
emit OBJLoaded();
} else {
// add an error to our list stating that the OBJ could not be downloaded
handleError("Failed to download " + _objURL.toString());
handleError("Failed to download " + _modelURL.toString());
}
}
@ -169,7 +166,7 @@ void OBJBaker::bakeOBJ() {
bool combineParts = true; // set true so that OBJReader reads material info from material library
OBJReader reader;
FBXGeometry* geometry = reader.readOBJ(objData, QVariantHash(), combineParts, _objURL);
FBXGeometry* geometry = reader.readOBJ(objData, QVariantHash(), combineParts, _modelURL);
// Write OBJ Data as FBX tree nodes
FBXNode rootNode;
@ -179,7 +176,7 @@ void OBJBaker::bakeOBJ() {
auto encodedFBX = FBXWriter::encodeFBX(rootNode);
// Export as baked FBX
auto fileName = _objURL.fileName();
auto fileName = _modelURL.fileName();
auto baseName = fileName.left(fileName.lastIndexOf('.'));
auto bakedFilename = baseName + ".baked.fbx";
@ -196,7 +193,7 @@ void OBJBaker::bakeOBJ() {
// Export successful
_outputFiles.push_back(_bakedOBJFilePath);
qCDebug(model_baking) << "Exported" << _objURL << "to" << _bakedOBJFilePath;
qCDebug(model_baking) << "Exported" << _modelURL << "to" << _bakedOBJFilePath;
// Export done emit finished
emit finished();
@ -293,7 +290,7 @@ void OBJBaker::createFBXNodeTree(FBXNode& rootNode, FBXGeometry& geometry) {
};
// Compress the texture using ModelBaker::compressTexture() and store compressed file's name in the node
QByteArray* textureFile = this->compressTexture(textureFileName, _objURL, _bakedOutputDir, _textureThreadGetter, textureContentTypeCallback, _originalOutputDir);
QByteArray* textureFile = this->compressTexture(textureFileName, textureContentTypeCallback);
if (textureFile) {
textureProperty = QVariant::fromValue(QByteArray(textureFile->data(), (int)textureFile->size()));
} else {

View file

@ -45,13 +45,9 @@ private slots:
private:
qlonglong _nodeID = 0;
QUrl _objURL;
QString _bakedOBJFilePath;
QString _bakedOutputDir;
QString _originalOutputDir;
QDir _tempDir;
QString _originalOBJFilePath;
TextureBakerThreadGetter _textureThreadGetter;
QMultiHash<QUrl, QSharedPointer<TextureBaker>> _bakingTextures;
qlonglong _geometryID;