18 #include <Qt3DRender/QAttribute> 19 #include <Qt3DRender/QBuffer> 20 #include <Qt3DRender/QEffect> 21 #include <Qt3DRender/QGraphicsApiFilter> 22 #include <Qt3DRender/QParameter> 23 #include <Qt3DRender/QTechnique> 25 #include <Qt3DExtras/QCylinderGeometry> 26 #include <Qt3DExtras/QConeGeometry> 27 #include <Qt3DExtras/QCuboidGeometry> 28 #include <Qt3DExtras/QPlaneGeometry> 29 #include <Qt3DExtras/QSphereGeometry> 30 #include <Qt3DExtras/QTorusGeometry> 31 #include <Qt3DExtras/QPhongMaterial> 32 #include <Qt3DRender/QSceneLoader> 34 #include <Qt3DRender/QMesh> 36 #if QT_VERSION >= 0x050900 37 #include <Qt3DExtras/QExtrudedTextGeometry> 57 QgsPoint3DSymbolModelEntityFactory::addEntitiesForSelectedPoints( map, layer, symbol,
this );
58 QgsPoint3DSymbolModelEntityFactory::addEntitiesForNotSelectedPoints( map, layer, symbol,
this );
62 QgsPoint3DSymbolInstancedEntityFactory::addEntityForNotSelectedPoints( map, layer, symbol,
this );
63 QgsPoint3DSymbolInstancedEntityFactory::addEntityForSelectedPoints( map, layer, symbol,
this );
69 Qt3DRender::QMaterial *QgsPoint3DSymbolInstancedEntityFactory::material(
const QgsPoint3DSymbol &symbol )
71 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey;
72 filterKey->setName( QStringLiteral(
"renderingStyle" ) );
73 filterKey->setValue(
"forward" );
78 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram;
79 shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/instanced.vert" ) ) ) );
80 shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/instanced.frag" ) ) ) );
82 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass;
83 renderPass->setShaderProgram( shaderProgram );
85 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
86 technique->addFilterKey( filterKey );
87 technique->addRenderPass( renderPass );
88 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
89 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
90 technique->graphicsApiFilter()->setMajorVersion( 3 );
91 technique->graphicsApiFilter()->setMinorVersion( 2 );
93 Qt3DRender::QParameter *ambientParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ka" ), QColor::fromRgbF( 0.05f, 0.05f, 0.05f, 1.0f ) );
94 Qt3DRender::QParameter *diffuseParameter =
new Qt3DRender::QParameter( QStringLiteral(
"kd" ), QColor::fromRgbF( 0.7f, 0.7f, 0.7f, 1.0f ) );
95 Qt3DRender::QParameter *specularParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ks" ), QColor::fromRgbF( 0.01f, 0.01f, 0.01f, 1.0f ) );
96 Qt3DRender::QParameter *shininessParameter =
new Qt3DRender::QParameter( QStringLiteral(
"shininess" ), 150.0f );
103 QMatrix4x4 transformMatrix = symbol.
transform();
104 QMatrix3x3 normalMatrix = transformMatrix.normalMatrix();
107 float *n = normalMatrix.data();
108 QMatrix4x4 normalMatrix4(
114 Qt3DRender::QParameter *paramInst =
new Qt3DRender::QParameter;
115 paramInst->setName( QStringLiteral(
"inst" ) );
116 paramInst->setValue( transformMatrix );
118 Qt3DRender::QParameter *paramInstNormal =
new Qt3DRender::QParameter;
119 paramInstNormal->setName( QStringLiteral(
"instNormal" ) );
120 paramInstNormal->setValue( normalMatrix4 );
122 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect;
123 effect->addTechnique( technique );
124 effect->addParameter( paramInst );
125 effect->addParameter( paramInstNormal );
127 effect->addParameter( ambientParameter );
128 effect->addParameter( diffuseParameter );
129 effect->addParameter( specularParameter );
130 effect->addParameter( shininessParameter );
132 Qt3DRender::QMaterial *
material =
new Qt3DRender::QMaterial;
133 material->setEffect( effect );
141 Qt3DRender::QMaterial *mat =
material( symbol );
144 Q_FOREACH ( Qt3DRender::QParameter *param, mat->effect()->parameters() )
146 if ( param->name() == QLatin1String(
"kd" ) )
148 else if ( param->name() == QLatin1String(
"ka" ) )
159 QgsPoint3DSymbolInstancedEntityNode *entity =
new QgsPoint3DSymbolInstancedEntityNode( map, layer, symbol, req );
160 entity->addComponent( mat );
161 entity->setParent( parent );
167 Qt3DRender::QMaterial *mat =
material( symbol );
179 QgsPoint3DSymbolInstancedEntityNode *entity =
new QgsPoint3DSymbolInstancedEntityNode( map, layer, symbol, req );
180 entity->addComponent( mat );
181 entity->setParent( parent );
188 addComponent( renderer( symbol, pos ) );
191 Qt3DRender::QGeometryRenderer *QgsPoint3DSymbolInstancedEntityNode::renderer(
const QgsPoint3DSymbol &symbol,
const QList<QVector3D> &positions )
const 193 int count = positions.count();
196 ba.resize( count *
sizeof( QVector3D ) );
197 QVector3D *posData =
reinterpret_cast<QVector3D *
>( ba.data() );
198 for (
int j = 0; j < count; ++j )
200 *posData = positions[j];
204 Qt3DRender::QBuffer *instanceBuffer =
new Qt3DRender::QBuffer( Qt3DRender::QBuffer::VertexBuffer );
205 instanceBuffer->setData( ba );
207 Qt3DRender::QAttribute *instanceDataAttribute =
new Qt3DRender::QAttribute;
208 instanceDataAttribute->setName( QStringLiteral(
"pos" ) );
209 instanceDataAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
210 instanceDataAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
211 instanceDataAttribute->setVertexSize( 3 );
212 instanceDataAttribute->setDivisor( 1 );
213 instanceDataAttribute->setBuffer( instanceBuffer );
214 instanceDataAttribute->setCount( count );
215 instanceDataAttribute->setByteStride( 3 *
sizeof(
float ) );
218 geometry->addAttribute( instanceDataAttribute );
219 geometry->setBoundingVolumePositionAttribute( instanceDataAttribute );
221 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
222 renderer->setGeometry( geometry );
223 renderer->setInstanceCount( count );
234 float radius = shapeProperties[QStringLiteral(
"radius" )].toFloat();
235 float length = shapeProperties[QStringLiteral(
"length" )].toFloat();
236 Qt3DExtras::QCylinderGeometry *g =
new Qt3DExtras::QCylinderGeometry;
239 g->setRadius( radius ? radius : 10 );
240 g->setLength( length ? length : 10 );
246 float radius = shapeProperties[QStringLiteral(
"radius" )].toFloat();
247 Qt3DExtras::QSphereGeometry *g =
new Qt3DExtras::QSphereGeometry;
248 g->setRadius( radius ? radius : 10 );
254 float length = shapeProperties[QStringLiteral(
"length" )].toFloat();
255 float bottomRadius = shapeProperties[QStringLiteral(
"bottomRadius" )].toFloat();
256 float topRadius = shapeProperties[QStringLiteral(
"topRadius" )].toFloat();
257 Qt3DExtras::QConeGeometry *g =
new Qt3DExtras::QConeGeometry;
258 g->setLength( length ? length : 10 );
259 g->setBottomRadius( bottomRadius );
260 g->setTopRadius( topRadius );
268 float size = shapeProperties[QStringLiteral(
"size" )].toFloat();
269 Qt3DExtras::QCuboidGeometry *g =
new Qt3DExtras::QCuboidGeometry;
270 g->setXExtent( size ? size : 10 );
271 g->setYExtent( size ? size : 10 );
272 g->setZExtent( size ? size : 10 );
278 float radius = shapeProperties[QStringLiteral(
"radius" )].toFloat();
279 float minorRadius = shapeProperties[QStringLiteral(
"minorRadius" )].toFloat();
280 Qt3DExtras::QTorusGeometry *g =
new Qt3DExtras::QTorusGeometry;
281 g->setRadius( radius ? radius : 10 );
282 g->setMinorRadius( minorRadius ? minorRadius : 5 );
288 float size = shapeProperties[QStringLiteral(
"size" )].toFloat();
289 Qt3DExtras::QPlaneGeometry *g =
new Qt3DExtras::QPlaneGeometry;
290 g->setWidth( size ? size : 10 );
291 g->setHeight( size ? size : 10 );
295 #if QT_VERSION >= 0x050900 298 float depth = shapeProperties[QStringLiteral(
"depth" )].toFloat();
299 QString text = shapeProperties[QStringLiteral(
"text" )].toString();
300 Qt3DExtras::QExtrudedTextGeometry *g =
new Qt3DExtras::QExtrudedTextGeometry;
301 g->setDepth( depth ? depth : 1 );
315 static Qt3DExtras::QPhongMaterial *phongMaterial(
const QgsPoint3DSymbol &symbol )
317 Qt3DExtras::QPhongMaterial *phong =
new Qt3DExtras::QPhongMaterial;
334 addMeshEntities( map, layer, req, symbol, parent,
true );
349 if ( symbol.
shapeProperties()[QStringLiteral(
"overwriteMaterial" )].toBool() )
351 addMeshEntities( map, layer, req, symbol, parent,
false );
355 addSceneEntities( map, layer, req, symbol, parent );
362 Q_FOREACH (
const QVector3D &position, positions )
365 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
367 QUrl url = QUrl::fromLocalFile( symbol.
shapeProperties()[QStringLiteral(
"model" )].toString() );
368 Qt3DRender::QSceneLoader *modelLoader =
new Qt3DRender::QSceneLoader;
369 modelLoader->setSource( url );
371 entity->addComponent( modelLoader );
372 entity->addComponent(
transform( position, symbol ) );
373 entity->setParent( parent );
380 Qt3DExtras::QPhongMaterial *mat = phongMaterial( symbol );
390 Q_FOREACH (
const QVector3D &position, positions )
393 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
395 QUrl url = QUrl::fromLocalFile( symbol.
shapeProperties()[QStringLiteral(
"model" )].toString() );
396 Qt3DRender::QMesh *mesh =
new Qt3DRender::QMesh;
397 mesh->setSource( url );
399 entity->addComponent( mesh );
400 entity->addComponent( mat );
401 entity->addComponent(
transform( position, symbol ) );
402 entity->setParent( parent );
406 Qt3DCore::QTransform *QgsPoint3DSymbolModelEntityFactory::transform( QVector3D position,
const QgsPoint3DSymbol &symbol )
408 Qt3DCore::QTransform *tr =
new Qt3DCore::QTransform;
410 tr->setTranslation( position + tr->translation() );
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
float shininess() const
Returns shininess of the surface.
QSet< QgsFeatureId > QgsFeatureIds
QVariantMap shapeProperties() const
Returns a key-value dictionary of point shape properties.
virtual QgsFeatureIds allFeatureIds() const
Returns a list of all feature IDs for features present in the source.
3 Definition of the world
QColor selectionColor() const
Returns color used for selected features.
QgsPhongMaterialSettings material() const
Returns material used for shading of the symbol.
QColor specular() const
Returns specular color component.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
3 3D symbol that draws point geometries as 3D objects using one of the predefined shapes...
Shape shape() const
Returns 3D shape for points.
QMatrix4x4 transform() const
Returns transform for individual objects represented by the symbol.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Shape
3D shape types supported by the symbol
QColor ambient() const
Returns ambient color component.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
Qgs3DTypes::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain)
static QList< QVector3D > positions(const Qgs3DMapSettings &map, QgsVectorLayer *layer, const QgsFeatureRequest &req, Qgs3DTypes::AltitudeClamping altClamp)
Calculates (x,y,z) positions of a (multi)point in the Point vector layers.
Represents a vector layer which manages a vector based data sets.
QColor diffuse() const
Returns diffuse color component.