|
| 1 | +/*************************************************************************** |
| 2 | + qgsmesh3dsymbol_p.cpp |
| 3 | + --------------------- |
| 4 | + Date : January 2019 |
| 5 | + Copyright : (C) 2019 by Peter Petrik |
| 6 | + Email : zilolv at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgsmesh3dsymbol_p.h" |
| 17 | + |
| 18 | +#include "qgsmesh3dsymbol.h" |
| 19 | +#include "qgstessellatedpolygongeometry.h" |
| 20 | +#include "qgs3dmapsettings.h" |
| 21 | +#include "qgs3dutils.h" |
| 22 | + |
| 23 | +#include <Qt3DCore/QTransform> |
| 24 | +#include <Qt3DRender/QEffect> |
| 25 | +#include <Qt3DRender/QTechnique> |
| 26 | +#include <Qt3DRender/QCullFace> |
| 27 | + |
| 28 | +#include "qgsmultipolygon.h" |
| 29 | +#include "qgsmeshlayer.h" |
| 30 | +#include "qgstriangularmesh.h" |
| 31 | + |
| 32 | +static QgsExpressionContext _expressionContext3D() |
| 33 | +{ |
| 34 | + QgsExpressionContext ctx; |
| 35 | + ctx << QgsExpressionContextUtils::globalScope() |
| 36 | + << QgsExpressionContextUtils::projectScope( QgsProject::instance() ); |
| 37 | + return ctx; |
| 38 | +} |
| 39 | + |
| 40 | +/// @cond PRIVATE |
| 41 | + |
| 42 | +QgsMesh3DSymbolEntity::QgsMesh3DSymbolEntity( const Qgs3DMapSettings &map, |
| 43 | + QgsMeshLayer *layer, |
| 44 | + const QgsMesh3DSymbol &symbol, |
| 45 | + Qt3DCore::QNode *parent ) |
| 46 | + : Qt3DCore::QEntity( parent ) |
| 47 | +{ |
| 48 | + // build the default material |
| 49 | + Qt3DExtras::QPhongMaterial *mat = material( symbol ); |
| 50 | + |
| 51 | + // build a transform function |
| 52 | + Qt3DCore::QTransform *tform = new Qt3DCore::QTransform; |
| 53 | + tform->setTranslation( QVector3D( 0, 0, 0 ) ); |
| 54 | + |
| 55 | + // build the entity |
| 56 | + QgsMesh3DSymbolEntityNode *entity = new QgsMesh3DSymbolEntityNode( map, layer, symbol ); |
| 57 | + entity->findChild<Qt3DRender::QGeometryRenderer *>()->setObjectName( QStringLiteral( "main" ) ); // temporary measure to distinguish between "selected" and "main" |
| 58 | + entity->addComponent( mat ); |
| 59 | + entity->addComponent( tform ); |
| 60 | + entity->setParent( this ); |
| 61 | +} |
| 62 | + |
| 63 | +Qt3DExtras::QPhongMaterial *QgsMesh3DSymbolEntity::material( const QgsMesh3DSymbol &symbol ) const |
| 64 | +{ |
| 65 | + Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial; |
| 66 | + |
| 67 | + // front/back side culling |
| 68 | + auto techniques = material->effect()->techniques(); |
| 69 | + for ( auto tit = techniques.constBegin(); tit != techniques.constEnd(); ++tit ) |
| 70 | + { |
| 71 | + auto renderPasses = ( *tit )->renderPasses(); |
| 72 | + for ( auto rpit = renderPasses.begin(); rpit != renderPasses.end(); ++rpit ) |
| 73 | + { |
| 74 | + Qt3DRender::QCullFace *cullFace = new Qt3DRender::QCullFace; |
| 75 | + cullFace->setMode( Qt3DRender::QCullFace::Back ); |
| 76 | + ( *rpit )->addRenderState( cullFace ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + material->setAmbient( symbol.material().ambient() ); |
| 81 | + material->setDiffuse( symbol.material().diffuse() ); |
| 82 | + material->setSpecular( symbol.material().specular() ); |
| 83 | + material->setShininess( symbol.material().shininess() ); |
| 84 | + return material; |
| 85 | +} |
| 86 | + |
| 87 | +QgsMesh3DSymbolEntityNode::QgsMesh3DSymbolEntityNode( const Qgs3DMapSettings &map, |
| 88 | + QgsMeshLayer *layer, |
| 89 | + const QgsMesh3DSymbol &symbol, |
| 90 | + Qt3DCore::QNode *parent ) : Qt3DCore::QEntity( parent ) |
| 91 | +{ |
| 92 | + addComponent( renderer( map, symbol, layer ) ); |
| 93 | +} |
| 94 | + |
| 95 | +Qt3DRender::QGeometryRenderer *QgsMesh3DSymbolEntityNode::renderer( const Qgs3DMapSettings &map, |
| 96 | + const QgsMesh3DSymbol &symbol, |
| 97 | + const QgsMeshLayer *layer ) |
| 98 | +{ |
| 99 | + QgsPointXY origin( map.origin().x(), map.origin().y() ); |
| 100 | + QList<QgsPolygon *> polygons; |
| 101 | + QList<QgsFeatureId> fids; |
| 102 | + |
| 103 | + QgsExpressionContext ctx( _expressionContext3D() ); |
| 104 | + const QgsPropertyCollection &ddp = symbol.dataDefinedProperties(); |
| 105 | + bool hasDDHeight = ddp.isActive( QgsAbstract3DSymbol::PropertyHeight ); |
| 106 | + float height = symbol.height(); |
| 107 | + if ( hasDDHeight ) |
| 108 | + { |
| 109 | + height = static_cast<float>( ddp.valueAsDouble( QgsAbstract3DSymbol::PropertyHeight, |
| 110 | + ctx, |
| 111 | + static_cast<double>( height ) |
| 112 | + ) |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + const QgsTriangularMesh *mesh = layer->triangularMesh(); |
| 117 | + if ( layer && mesh ) |
| 118 | + { |
| 119 | + const QVector<QgsMeshFace> &triangles = mesh->triangles(); |
| 120 | + const QVector<QgsMeshVertex> &vertices = mesh->vertices(); |
| 121 | + for ( int i = 0; i < triangles.size(); ++i ) |
| 122 | + { |
| 123 | + const QgsMeshFace &triangle = triangles.at( i ); |
| 124 | + Q_ASSERT( triangle.size() == 3 ); |
| 125 | + std::unique_ptr< QgsPolygon > polygon = QgsMeshUtils::toPolygon( triangle, vertices ); |
| 126 | + Qgs3DUtils::clampAltitudes( polygon.get(), |
| 127 | + symbol.altitudeClamping(), |
| 128 | + Qgs3DTypes::AltitudeBinding::AltBindVertex, |
| 129 | + height, |
| 130 | + map ); |
| 131 | + polygons.append( polygon.release() ); |
| 132 | + fids.append( i ); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // Polygons from mesh are already triangles, but |
| 137 | + // call QgsTessellatedPolygonGeometry to |
| 138 | + // use symbol settings for back faces, normals, etc |
| 139 | + mGeometry = new QgsTessellatedPolygonGeometry; |
| 140 | + mGeometry->setInvertNormals( false ); |
| 141 | + mGeometry->setAddBackFaces( symbol.addBackFaces() ); |
| 142 | + QList<float> extrusionHeightPerPolygon; |
| 143 | + mGeometry->setPolygons( polygons, fids, origin, 0.0, extrusionHeightPerPolygon ); |
| 144 | + |
| 145 | + Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer; |
| 146 | + renderer->setGeometry( mGeometry ); |
| 147 | + |
| 148 | + return renderer; |
| 149 | +} |
| 150 | +/// @endcond |
0 commit comments