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(
"renderingStyle" );
73 filterKey->setValue(
"forward" );
78 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram;
79 shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"qrc:/shaders/instanced.vert" ) ) );
80 shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"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(
"inst" );
116 paramInst->setValue( transformMatrix );
118 Qt3DRender::QParameter *paramInstNormal =
new Qt3DRender::QParameter;
119 paramInstNormal->setName(
"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() ==
"kd" )
148 else if ( param->name() ==
"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(
"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[
"radius"].toFloat();
235 float length = shapeProperties[
"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[
"radius"].toFloat();
247 Qt3DExtras::QSphereGeometry *g =
new Qt3DExtras::QSphereGeometry;
248 g->setRadius( radius ? radius : 10 );
254 float length = shapeProperties[
"length"].toFloat();
255 float bottomRadius = shapeProperties[
"bottomRadius"].toFloat();
256 float topRadius = shapeProperties[
"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[
"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[
"radius"].toFloat();
279 float minorRadius = shapeProperties[
"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[
"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[
"depth"].toFloat();
299 QString text = shapeProperties[
"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 );
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()[
"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()[
"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(
const 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.
QColor selectionColor() const
Returns color used for selected features.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
QSet< QgsFeatureId > QgsFeatureIds
QColor specular() const
Returns specular color component.
Shape shape() const
Returns 3D shape for points.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QVariantMap shapeProperties() const
Returns a key-value dictionary of point shape properties.
3 Definition of the world
QMatrix4x4 transform() const
Returns transform for individual objects represented by the symbol.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
3 3D symbol that draws point geometries as 3D objects using one of the predefined shapes...
QgsPhongMaterialSettings material() const
Returns material used for shading of the symbol.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Shape
3D shape types supported by the symbol
static QList< QVector3D > positions(const Qgs3DMapSettings &map, QgsVectorLayer *layer, const QgsFeatureRequest &req, AltitudeClamping altClamp)
Calculates (x,y,z) positions of a (multi)point in the Point vector layers.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
QColor ambient() const
Returns ambient color component.
QColor diffuse() const
Returns diffuse color component.
QList< int > QgsAttributeList
Represents a vector layer which manages a vector based data sets.
virtual QgsFeatureIds allFeatureIds() const
Returns a list of all feature IDs for features present in the source.
AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain)