QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgschunkboundsentity_p.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgschunkboundsentity_p.cpp
3  --------------------------------------
4  Date : July 2017
5  Copyright : (C) 2017 by Martin Dobias
6  Email : wonder dot sk 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 "qgschunkboundsentity_p.h"
17 
18 #include <Qt3DRender/QBuffer>
19 #include <Qt3DExtras/QPhongMaterial>
20 
21 #include "qgsaabb.h"
22 
23 
25 
26 LineMeshGeometry::LineMeshGeometry( Qt3DCore::QNode *parent )
27  : Qt3DRender::QGeometry( parent )
28  , mPositionAttribute( new Qt3DRender::QAttribute( this ) )
29 #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
30  , mVertexBuffer( new Qt3DRender::QBuffer( Qt3DRender::QBuffer::VertexBuffer, this ) )
31 #else
32  , mVertexBuffer( new Qt3DRender::QBuffer( this ) )
33 #endif
34 {
35  mPositionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
36  mPositionAttribute->setBuffer( mVertexBuffer );
37  mPositionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
38  mPositionAttribute->setVertexSize( 3 );
39  mPositionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
40 
41  addAttribute( mPositionAttribute );
42 }
43 
44 void LineMeshGeometry::setVertices( const QList<QVector3D> &vertices )
45 {
46  QByteArray vertexBufferData;
47  vertexBufferData.resize( vertices.size() * 3 * sizeof( float ) );
48  float *rawVertexArray = reinterpret_cast<float *>( vertexBufferData.data() );
49  int idx = 0;
50  for ( const auto &v : vertices )
51  {
52  rawVertexArray[idx++] = v.x();
53  rawVertexArray[idx++] = v.y();
54  rawVertexArray[idx++] = v.z();
55  }
56 
57  mVertexCount = vertices.count();
58  mVertexBuffer->setData( vertexBufferData );
59 }
60 
61 
62 // ----------------
63 
64 
65 AABBMesh::AABBMesh( Qt3DCore::QNode *parent )
66  : Qt3DRender::QGeometryRenderer( parent )
67 {
68  setInstanceCount( 1 );
69  setIndexOffset( 0 );
70  setFirstInstance( 0 );
71  setPrimitiveType( Qt3DRender::QGeometryRenderer::Lines );
72 
73  mLineMeshGeo = new LineMeshGeometry( this );
74  setGeometry( mLineMeshGeo );
75 }
76 
77 void AABBMesh::setBoxes( const QList<QgsAABB> &bboxes )
78 {
79  QList<QVector3D> vertices;
80  Q_FOREACH ( const QgsAABB &bbox, bboxes )
81  vertices << bbox.verticesForLines();
82  mLineMeshGeo->setVertices( vertices );
83  setVertexCount( mLineMeshGeo->vertexCount() );
84 }
85 
86 
87 // ----------------
88 
89 
90 QgsChunkBoundsEntity::QgsChunkBoundsEntity( Qt3DCore::QNode *parent )
91  : Qt3DCore::QEntity( parent )
92 {
93  mAabbMesh = new AABBMesh;
94  addComponent( mAabbMesh );
95 
96  Qt3DExtras::QPhongMaterial *bboxesMaterial = new Qt3DExtras::QPhongMaterial;
97  bboxesMaterial->setAmbient( Qt::red );
98  addComponent( bboxesMaterial );
99 }
100 
101 void QgsChunkBoundsEntity::setBoxes( const QList<QgsAABB> &bboxes )
102 {
103  mAabbMesh->setBoxes( bboxes );
104 }
105 
3
Definition: qgsaabb.h:34