Skip to content

Commit

Permalink
Added terrain texture resolution spinbox
Browse files Browse the repository at this point in the history
  • Loading branch information
NEDJIMAbelgacem committed Jul 8, 2020
1 parent 294c507 commit 2edd11c
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 61 deletions.
11 changes: 6 additions & 5 deletions src/3d/qgs3dmapexportsettings.h
Expand Up @@ -47,6 +47,8 @@ class _3D_EXPORT Qgs3DMapExportSettings : public QObject
bool exportNormals() const { return mExportNormals; }
//! Returns whether textures will be exported
bool exportTextures() const { return mExportTextures; }
//! Returns the terrain texture resolution
int terrainTextureResolution() const { return mTerrainTextureResolution; }

//! Sets the scene name
void setSceneName( const QString &sceneName ) { mSceneName = sceneName; }
Expand All @@ -60,18 +62,17 @@ class _3D_EXPORT Qgs3DMapExportSettings : public QObject
void setExportNormals( bool exportNormals ) { mExportNormals = exportNormals; }
//! Sets whether textures will be exported
void setExportTextures( bool exportTextures ) { mExportTextures = exportTextures; }
//! Returns a path to a file that with the "name.extension" in the export folder
QString getFilePath( const QString &name, const QString &extension ) const
{
return QDir( mSceneFolderPath ).filePath( name + "." + extension );
}
//! Sets the terrain texture resolution
void setTerrainTextureResolution( int resolution ) { mTerrainTextureResolution = resolution; }

private:
QString mSceneName = QString( "Scene" );
QString mSceneFolderPath = QDir::homePath();
int mTerrainResolution = 128;
bool mSmoothEdges = false;
bool mExportNormals = true;
bool mExportTextures = false;
int mTerrainTextureResolution = 512;
};

#endif // QGS3DMAPEXPORTSETTINGS_H
2 changes: 1 addition & 1 deletion src/3d/qgs3dmapscene.cpp
Expand Up @@ -775,13 +775,13 @@ void Qgs3DMapScene::exportScene( const Qgs3DMapExportSettings &exportSettings )
exporter.setSmoothEdges( exportSettings.smoothEdges() );
exporter.setExportNormals( exportSettings.exportNormals() );
exporter.setExportTextures( exportSettings.exportTextures() );
exporter.setTerrainTextureResolution( exportSettings.terrainTextureResolution() );

for ( QgsMapLayer *layer : mLayerEntities.keys() )
{
exporter.parseEntity( mLayerEntities[layer] );
}
exporter.parseEntity( mTerrain );

QString filePath = exportSettings.getFilePath( exportSettings.sceneName(), "obj" );
exporter.save( exportSettings.sceneName(), exportSettings.sceneFolderPath() );
}
29 changes: 8 additions & 21 deletions src/3d/qgs3dsceneexporter.cpp
Expand Up @@ -59,11 +59,6 @@ QVector<T> getAttributeData( Qt3DRender::QAttribute *attribute )
uint bytesStride = attribute->byteStride();
uint vertexSize = attribute->vertexSize();

qDebug() << "bytesOffset: " << bytesOffset;
qDebug() << "bytesStride: " << bytesStride;
qDebug() << "VertexSize: " << vertexSize;
qDebug() << "Data size: " << data.size();

QVector<T> result;
for ( int i = bytesOffset; i < data.size(); i += bytesStride )
{
Expand Down Expand Up @@ -103,7 +98,6 @@ QVector<unsigned int> getAttributeData<unsigned int>( Qt3DRender::QAttribute *at
return result;
}


QVector<float> createPlaneVertexData( float w, float h, const QSize &resolution )
{
// Taken from Qt3D source with some modifications
Expand Down Expand Up @@ -241,9 +235,7 @@ void Qgs3DSceneExporter::parseEntity( Qt3DCore::QEntity *entity )
QgsTessellatedPolygonGeometry *tessellated = qobject_cast<QgsTessellatedPolygonGeometry *>( geom );
if ( tessellated != nullptr )
{
qDebug() << "Processing QgsTessellatedPolygonGeometry started";
process( tessellated );
qDebug() << "Processing QgsTessellatedPolygonGeometry ended";
continue;
}

Expand Down Expand Up @@ -291,27 +283,23 @@ void Qgs3DSceneExporter::parseEntity( QgsTerrainEntity *terrain )

QgsTerrainGenerator *generator = settings.terrainGenerator();
QgsTerrainTileEntity *terrainTile = nullptr;
QgsTerrainTextureGenerator *textureGenerator = terrain->textureGenerator();
QSize oldResolution = textureGenerator->textureSize();
textureGenerator->setTextureSize( QSize( mTerrainTextureResolution, mTerrainTextureResolution ) );
switch ( generator->type() )
{
case QgsTerrainGenerator::Dem:
for ( QgsChunkNode *node : leafs )
{
terrainTile = getDemTerrainEntity( terrain, node );
QgsTerrainTextureGenerator *textureGenerator = terrain->textureGenerator();
QSize oldResolution = textureGenerator->textureSize();
textureGenerator->setTextureSize( QSize( mTerrainTextureResolution, mTerrainTextureResolution ) );
this->parseDemTile( terrainTile, textureGenerator );
textureGenerator->setTextureSize( oldResolution );
}
break;
case QgsTerrainGenerator::Flat:
for ( QgsChunkNode *node : leafs )
{
terrainTile = getFlatTerrainEntity( terrain, node );
QgsTerrainTextureGenerator *textureGenerator = terrain->textureGenerator();
QSize oldResolution = textureGenerator->textureSize();
this->parseFlatTile( terrainTile, textureGenerator );
textureGenerator->setTextureSize( oldResolution );
}
break;
// TODO: implement other terrain types
Expand All @@ -320,6 +308,7 @@ void Qgs3DSceneExporter::parseEntity( QgsTerrainEntity *terrain )
case QgsTerrainGenerator::Online:
break;
}
textureGenerator->setTextureSize( oldResolution );
}

QgsTerrainTileEntity *Qgs3DSceneExporter::getFlatTerrainEntity( QgsTerrainEntity *terrain, QgsChunkNode *node )
Expand Down Expand Up @@ -397,10 +386,8 @@ void Qgs3DSceneExporter::parseFlatTile( QgsTerrainTileEntity *tileEntity, QgsTer

if ( mExportTextures )
{
qDebug() << "Generating texCoords";
QVector<float> texCoords = createPlaneTexCoordsData( 1.0f, 1.0f, tileGeometry->resolution(), false );
object->setupTextureCoordinates( texCoords );
qDebug() << "Generated texCoords";

QImage img = textureGenerator->renderSynchronously( tileEntity->textureImage()->imageExtent(), tileEntity->textureImage()->imageDebugText() );
object->setTextureImage( img );
Expand Down Expand Up @@ -491,10 +478,10 @@ void Qgs3DSceneExporter::save( const QString &sceneName, const QString &sceneFol
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;
Expand Down
2 changes: 2 additions & 0 deletions src/3d/terrain/qgsflatterraingenerator.h
Expand Up @@ -57,6 +57,8 @@ class _3D_EXPORT QgsFlatTerrainGenerator : public QgsTerrainGenerator
QgsFlatTerrainGenerator() = default;

QgsChunkLoader *createChunkLoader( QgsChunkNode *node ) const override SIP_FACTORY;

//! create a chunk loader that loads synchronously
QgsChunkLoader *createSynchronousChunkLoader( QgsChunkNode *node ) const;

QgsTerrainGenerator *clone() const override SIP_FACTORY;
Expand Down
2 changes: 1 addition & 1 deletion src/3d/terrain/qgsterraintexturegenerator_p.h
Expand Up @@ -88,7 +88,6 @@ class QgsTerrainTextureGenerator : public QObject
QgsMapSettings baseMapSettings();

const Qgs3DMapSettings &mMap;
QSize mTextureSize;

struct JobData
{
Expand All @@ -101,6 +100,7 @@ class QgsTerrainTextureGenerator : public QObject

QHash<QgsMapRendererSequentialJob *, JobData> mJobs;
int mLastJobId;
QSize mTextureSize;
};

/// @endcond
Expand Down
2 changes: 2 additions & 0 deletions src/app/3d/qgsmap3dexportwidget.cpp
Expand Up @@ -49,6 +49,7 @@ QgsMap3DExportWidget::QgsMap3DExportWidget( Qgs3DMapScene *scene, Qgs3DMapExport
connect( ui->terrainResolutionSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), [ = ]( int ) { settingsChanged(); } );
connect( ui->exportNormalsCheckBox, &QCheckBox::stateChanged, [ = ]( int ) { settingsChanged(); } );
connect( ui->exportTexturesCheckBox, &QCheckBox::stateChanged, [ = ]( int ) { settingsChanged(); } );
connect( ui->terrainTextureResolutionSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), [ = ]( int ) { settingsChanged(); } );

// sets the export settings to whatever is on the scene
settingsChanged();
Expand All @@ -67,6 +68,7 @@ void QgsMap3DExportWidget::settingsChanged()
mExportSettings->setSmoothEdges( ui->smoothEdgesCheckBox->isChecked() );
mExportSettings->setExportNormals( ui->exportNormalsCheckBox->isChecked() );
mExportSettings->setExportTextures( ui->exportTexturesCheckBox->isChecked() );
mExportSettings->setTerrainTextureResolution( ui->terrainTextureResolutionSpinBox->value() );
}

void QgsMap3DExportWidget::exportScene()
Expand Down
86 changes: 53 additions & 33 deletions src/ui/3d/map3dexportwidget.ui
Expand Up @@ -13,30 +13,31 @@
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<widget class="QLabel" name="label">
<item row="4" column="0" colspan="3">
<widget class="QCheckBox" name="smoothEdgesCheckBox">
<property name="text">
<string>Terrain resolution</string>
<string>Smooth Edges</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QgsFileWidget" name="selectFolderWidget" native="true"/>
</item>
<item row="2" column="1" colspan="2">
<widget class="QSpinBox" name="terrainResolutionSpinBox">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>4096</number>
<item row="1" column="0">
<widget class="QLabel" name="folderLAbel">
<property name="text">
<string>Folder</string>
</property>
<property name="value">
<number>1</number>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="sceneNameLabel">
<property name="text">
<string>Scene name</string>
</property>
</widget>
</item>
<item row="6" column="1">
<item row="1" column="1" colspan="2">
<widget class="QgsFileWidget" name="selectFolderWidget" native="true"/>
</item>
<item row="7" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -49,7 +50,27 @@
</property>
</spacer>
</item>
<item row="4" column="0" colspan="3">
<item row="6" column="0" colspan="3">
<widget class="QCheckBox" name="exportTexturesCheckBox">
<property name="text">
<string>Export textures</string>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<widget class="QSpinBox" name="terrainResolutionSpinBox">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>4096</number>
</property>
<property name="value">
<number>1</number>
</property>
</widget>
</item>
<item row="5" column="0" colspan="3">
<widget class="QCheckBox" name="exportNormalsCheckBox">
<property name="text">
<string>Export Normals</string>
Expand All @@ -62,34 +83,33 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="3">
<widget class="QCheckBox" name="smoothEdgesCheckBox">
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Smooth Edges</string>
<string>Terrain resolution</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QLineEdit" name="sceneNameLineEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="folderLAbel">
<property name="text">
<string>Folder</string>
<item row="3" column="1" colspan="2">
<widget class="QSpinBox" name="terrainTextureResolutionSpinBox">
<property name="minimum">
<number>16</number>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="sceneNameLabel">
<property name="text">
<string>Scene name</string>
<property name="maximum">
<number>4096</number>
</property>
<property name="value">
<number>512</number>
</property>
</widget>
</item>
<item row="5" column="0" colspan="3">
<widget class="QCheckBox" name="exportTexturesCheckBox">
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Export textures</string>
<string>Terrain texture resolution</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit 2edd11c

Please sign in to comment.