Skip to content
Permalink
Browse files

Refactor phong material setup

  • Loading branch information
NEDJIMAbelgacem committed Jul 14, 2020
1 parent 633627f commit dffc35a1bb6892faf4513bc61f6903b29c765bdb
Showing with 29 additions and 49 deletions.
  1. +14 −0 src/3d/qgs3dexportobject.cpp
  2. +4 −0 src/3d/qgs3dexportobject.h
  3. +11 −49 src/3d/qgs3dsceneexporter.cpp
@@ -20,6 +20,8 @@
#include <QDir>
#include <QImage>

#include "qgsphongmaterialsettings.h"

Qgs3DExportObject::Qgs3DExportObject( const QString &name, const QString &parentName, QObject *parent )
: QObject( parent )
, mName( name )
@@ -92,6 +94,18 @@ void Qgs3DExportObject::setupTextureCoordinates( const QVector<float> &texturesB
mTexturesUV << texturesBuffer;
}

void Qgs3DExportObject::setupPhongMaterial( const QgsPhongMaterialSettings &material )
{
QColor diffuse = material.diffuse();
QColor specular = material.specular();
QColor ambient = material.ambient();
float shininess = material.shininess();
setMaterialParameter( QString( "Kd" ), QString( "%1 %2 %3" ).arg( diffuse.redF() ).arg( diffuse.greenF() ).arg( diffuse.blueF() ) );
setMaterialParameter( QString( "Ka" ), QString( "%1 %2 %3" ).arg( ambient.redF() ).arg( ambient.greenF() ).arg( ambient.blueF() ) );
setMaterialParameter( QString( "Ks" ), QString( "%1 %2 %3" ).arg( specular.redF() ).arg( specular.greenF() ).arg( specular.blueF() ) );
setMaterialParameter( QString( "Ns" ), QString( "%1" ).arg( shininess ) );
}

void Qgs3DExportObject::objectBounds( float &minX, float &minY, float &minZ, float &maxX, float &maxY, float &maxZ )
{
for ( unsigned int vertice : mIndexes )
@@ -22,6 +22,8 @@
#include <QVector3D>
#include <QImage>

class QgsPhongMaterialSettings;

/**
* \brief The Qgs3DExportObject class
* Manages the data of each object of the scene (positions, normals, texture coordinates ...) since each object
@@ -70,6 +72,8 @@ class Qgs3DExportObject : public QObject
void setupNormalCoordinates( const QVector<float> &normalsBuffer );
//! Sets texture coordinates for each vertex
void setupTextureCoordinates( const QVector<float> &texturesBuffer );
//! Sets the material parameters (diffuse color, shininess...) from phong material
void setupPhongMaterial( const QgsPhongMaterialSettings &material );

//! Sets the texture image used by the object
void setTextureImage( const QImage &image ) { this->mTextureImage = image; };
@@ -205,11 +205,12 @@ bool Qgs3DSceneExporter::parseVectorLayerEntity( Qt3DCore::QEntity *entity, QgsV
if ( pointSymbol->shape() == QgsPoint3DSymbol::Model )
{
Qt3DRender::QSceneLoader *sceneLoader = entity->findChild<Qt3DRender::QSceneLoader *>();
if (sceneLoader != nullptr) processSceneLoaderGeometry( sceneLoader, pointSymbol );
else {
if ( sceneLoader != nullptr ) processSceneLoaderGeometry( sceneLoader, pointSymbol );
else
{
QList<Qt3DRender::QMesh *> meshes = entity->findChildren<Qt3DRender::QMesh *>();
for (Qt3DRender::QMesh * mesh : meshes)
processMeshGeometry(mesh, pointSymbol);
for ( Qt3DRender::QMesh *mesh : meshes )
processMeshGeometry( mesh, pointSymbol );
}
return true;
}
@@ -406,15 +407,8 @@ void Qgs3DSceneExporter::processPolygonGeometry( QgsTessellatedPolygonGeometry *
QVector<float> normalsData = getAttributeData<float>( normalsAttribute, normalsBytes );
object->setupNormalCoordinates( normalsData );
}
QgsPhongMaterialSettings material = polygonSymbol->material();
QColor diffuse = material.diffuse();
QColor specular = material.specular();
QColor ambient = material.ambient();
float shininess = material.shininess();
object->setMaterialParameter( QString( "Kd" ), QString( "%1 %2 %3" ).arg( diffuse.redF() ).arg( diffuse.greenF() ).arg( diffuse.blueF() ) );
object->setMaterialParameter( QString( "Ka" ), QString( "%1 %2 %3" ).arg( ambient.redF() ).arg( ambient.greenF() ).arg( ambient.blueF() ) );
object->setMaterialParameter( QString( "Ks" ), QString( "%1 %2 %3" ).arg( specular.redF() ).arg( specular.greenF() ).arg( specular.blueF() ) );
object->setMaterialParameter( QString( "Ns" ), QString( "%1" ).arg( shininess ) );

object->setupPhongMaterial( polygonSymbol->material() );
// TODO: handle textures
}

@@ -438,15 +432,7 @@ void Qgs3DSceneExporter::processBufferedLineGeometry( QgsTessellatedPolygonGeome
object->setupNormalCoordinates( normalsData );
}

QgsPhongMaterialSettings material = lineSymbol->material();
QColor diffuse = material.diffuse();
QColor specular = material.specular();
QColor ambient = material.ambient();
float shininess = material.shininess();
object->setMaterialParameter( QString( "Kd" ), QString( "%1 %2 %3" ).arg( diffuse.redF() ).arg( diffuse.greenF() ).arg( diffuse.blueF() ) );
object->setMaterialParameter( QString( "Ka" ), QString( "%1 %2 %3" ).arg( ambient.redF() ).arg( ambient.greenF() ).arg( ambient.blueF() ) );
object->setMaterialParameter( QString( "Ks" ), QString( "%1 %2 %3" ).arg( specular.redF() ).arg( specular.greenF() ).arg( specular.blueF() ) );
object->setMaterialParameter( QString( "Ns" ), QString( "%1" ).arg( shininess ) );
object->setupPhongMaterial( lineSymbol->material() );
}

void Qgs3DSceneExporter::processInstancedPointGeometry( Qt3DCore::QEntity *entity, const QgsPoint3DSymbol *pointSymbol )
@@ -489,15 +475,7 @@ void Qgs3DSceneExporter::processInstancedPointGeometry( Qt3DCore::QEntity *entit
object->setupNormalCoordinates( normalsData );
}

QgsPhongMaterialSettings material = pointSymbol->material();
QColor diffuse = material.diffuse();
QColor specular = material.specular();
QColor ambient = material.ambient();
float shininess = material.shininess();
object->setMaterialParameter( QString( "Kd" ), QString( "%1 %2 %3" ).arg( diffuse.redF() ).arg( diffuse.greenF() ).arg( diffuse.blueF() ) );
object->setMaterialParameter( QString( "Ka" ), QString( "%1 %2 %3" ).arg( ambient.redF() ).arg( ambient.greenF() ).arg( ambient.blueF() ) );
object->setMaterialParameter( QString( "Ks" ), QString( "%1 %2 %3" ).arg( specular.redF() ).arg( specular.greenF() ).arg( specular.blueF() ) );
object->setMaterialParameter( QString( "Ns" ), QString( "%1" ).arg( shininess ) );
object->setupPhongMaterial( pointSymbol->material() );
}
}
}
@@ -569,15 +547,7 @@ void Qgs3DSceneExporter::processSceneLoaderGeometry( Qt3DRender::QSceneLoader *s
object->setupNormalCoordinates( normalsData );
}

QgsPhongMaterialSettings material = pointSymbol->material();
QColor diffuse = material.diffuse();
QColor specular = material.specular();
QColor ambient = material.ambient();
float shininess = material.shininess();
object->setMaterialParameter( QString( "Kd" ), QString( "%1 %2 %3" ).arg( diffuse.redF() ).arg( diffuse.greenF() ).arg( diffuse.blueF() ) );
object->setMaterialParameter( QString( "Ka" ), QString( "%1 %2 %3" ).arg( ambient.redF() ).arg( ambient.greenF() ).arg( ambient.blueF() ) );
object->setMaterialParameter( QString( "Ks" ), QString( "%1 %2 %3" ).arg( specular.redF() ).arg( specular.greenF() ).arg( specular.blueF() ) );
object->setMaterialParameter( QString( "Ns" ), QString( "%1" ).arg( shininess ) );
object->setupPhongMaterial( pointSymbol->material() );
}
}

@@ -635,15 +605,7 @@ void Qgs3DSceneExporter::processMeshGeometry( Qt3DRender::QMesh *mesh, const Qgs
object->setupNormalCoordinates( normalsData );
}

QgsPhongMaterialSettings material = pointSymbol->material();
QColor diffuse = material.diffuse();
QColor specular = material.specular();
QColor ambient = material.ambient();
float shininess = material.shininess();
object->setMaterialParameter( QString( "Kd" ), QString( "%1 %2 %3" ).arg( diffuse.redF() ).arg( diffuse.greenF() ).arg( diffuse.blueF() ) );
object->setMaterialParameter( QString( "Ka" ), QString( "%1 %2 %3" ).arg( ambient.redF() ).arg( ambient.greenF() ).arg( ambient.blueF() ) );
object->setMaterialParameter( QString( "Ks" ), QString( "%1 %2 %3" ).arg( specular.redF() ).arg( specular.greenF() ).arg( specular.blueF() ) );
object->setMaterialParameter( QString( "Ns" ), QString( "%1" ).arg( shininess ) );
object->setupPhongMaterial( pointSymbol->material() );
}

void Qgs3DSceneExporter::save( const QString &sceneName, const QString &sceneFolderPath )

0 comments on commit dffc35a

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