Skip to content
Permalink
Browse files

Added initial mtl implementation

  • Loading branch information
NEDJIMAbelgacem committed Jul 8, 2020
1 parent 2edd11c commit 00cbe5d12c041b01813e25c66dba78d221785400
Showing with 24 additions and 9 deletions.
  1. +7 −2 src/3d/qgs3dexportobject.cpp
  2. +1 −1 src/3d/qgs3dexportobject.h
  3. +16 −6 src/3d/qgs3dsceneexporter.cpp
@@ -146,9 +146,14 @@ void Qgs3DExportObject::saveTo( QTextStream &out, float scale, const QVector3D &
}
}

void Qgs3DExportObject::saveMaterial( const QString &textureName, const QString &folderPath )
QString Qgs3DExportObject::saveMaterial( QTextStream &mtlOut, const QString &folderPath )
{
if ( mTexturesUV.size() == 0 ) return;
QString textureName = mName + "_material";
if ( mTexturesUV.size() == 0 ) return QString();
QString filePath = QDir( folderPath ).filePath( textureName + ".jpg" );
mTextureImage.save( filePath, "JPG" );

mtlOut << "newmtl " << textureName << "\n";
mtlOut << "\tmap_Kd " << textureName << ".jpg" << "\n";
return textureName;
}
@@ -79,7 +79,7 @@ class Qgs3DExportObject : public QObject
//! Saves the current object to the output stream while scaling the object and centering it to be visible in exported scene
void saveTo( QTextStream &out, float scale, const QVector3D &center );
//! saves the texture of the object and material informations
void saveMaterial( const QString &textureName, const QString &folder );
QString saveMaterial( QTextStream &mtlOut, const QString &folder );
private:
QString mName;
QString mParentName;
@@ -473,29 +473,39 @@ void Qgs3DSceneExporter::save( const QString &sceneName, const QString &sceneFol
QFile file( objFilePath );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
return;
QFile mtlFile( mtlFilePath );
if ( !mtlFile.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
return;

float maxfloat = std::numeric_limits<float>::max(), minFloat = std::numeric_limits<float>::lowest();
float minX = maxfloat, minY = maxfloat, minZ = maxfloat, maxX = minFloat, maxY = minFloat, maxZ = minFloat;
for ( Qgs3DExportObject *obj : mObjects ) obj->objectBounds( minX, minY, minZ, maxX, maxY, maxZ );

// float diffX = 1.0f, diffY = 1.0f, diffZ = 1.0f;
// diffX = maxX - minX;
// diffY = maxY - minY;
// diffZ = maxZ - minZ;
float diffX = 1.0f, diffY = 1.0f, diffZ = 1.0f;
diffX = maxX - minX;
diffY = maxY - minY;
diffZ = maxZ - minZ;

float centerX = ( minX + maxX ) / 2.0f;
float centerY = ( minY + maxY ) / 2.0f;
float centerZ = ( minZ + maxZ ) / 2.0f;

float scale = 1.0f;//std::max( diffX, std::max( diffY, diffZ ) );
float scale = std::max( diffX, std::max( diffY, diffZ ) );

QTextStream out( &file );
// set material library name
QString mtlLibName = sceneName + ".mtl";
out << "mtllib " << mtlLibName << "\n";

QTextStream mtlOut( &mtlFile );
for ( Qgs3DExportObject *obj : mObjects )
{
// Set object name
QString material = obj->saveMaterial( mtlOut, sceneFolderPath );
out << "o " << obj->name() << "\n";
if ( material != QString() )
out << "usemtl " << material << "\n";
obj->saveTo( out, scale, QVector3D( centerX, centerY, centerZ ) );
obj->saveMaterial( obj->name(), sceneFolderPath );
}
}

0 comments on commit 00cbe5d

Please sign in to comment.
You can’t perform that action at this time.