QGIS API Documentation  3.2.0-Bonn (bc43194)
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/QAttribute>
19 #include <Qt3DRender/QBuffer>
20 #include <Qt3DRender/QGeometry>
21 #include <Qt3DRender/QGeometryRenderer>
22 #include <Qt3DExtras/QPhongMaterial>
23 
24 #include "qgsaabb.h"
25 
26 
28 
29 class LineMeshGeometry : public Qt3DRender::QGeometry
30 {
31  public:
32  LineMeshGeometry( Qt3DCore::QNode *parent = nullptr );
33 
34  int vertexCount()
35  {
36  return mVertices.size();
37  }
38 
39  void setVertices( QList<QVector3D> vertices );
40 
41  private:
42  Qt3DRender::QAttribute *mPositionAttribute = nullptr;
43  Qt3DRender::QBuffer *mVertexBuffer = nullptr;
44  QList<QVector3D> mVertices;
45 
46 };
47 
48 
49 LineMeshGeometry::LineMeshGeometry( Qt3DCore::QNode *parent )
50  : Qt3DRender::QGeometry( parent )
51  , mPositionAttribute( new Qt3DRender::QAttribute( this ) )
52  , mVertexBuffer( new Qt3DRender::QBuffer( Qt3DRender::QBuffer::VertexBuffer, this ) )
53 {
54  mPositionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
55  mPositionAttribute->setBuffer( mVertexBuffer );
56  mPositionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
57  mPositionAttribute->setVertexSize( 3 );
58  mPositionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
59 
60  addAttribute( mPositionAttribute );
61 }
62 
63 void LineMeshGeometry::setVertices( QList<QVector3D> vertices )
64 {
65  QByteArray vertexBufferData;
66  vertexBufferData.resize( vertices.size() * 3 * sizeof( float ) );
67  float *rawVertexArray = reinterpret_cast<float *>( vertexBufferData.data() );
68  int idx = 0;
69  for ( const auto &v : vertices )
70  {
71  rawVertexArray[idx++] = v.x();
72  rawVertexArray[idx++] = v.y();
73  rawVertexArray[idx++] = v.z();
74  mVertices.append( v );
75  }
76 
77  mVertexBuffer->setData( vertexBufferData );
78 }
79 
80 
81 // ----------------
82 
83 
85 class AABBMesh : public Qt3DRender::QGeometryRenderer
86 {
87  public:
88  AABBMesh( Qt3DCore::QNode *parent = nullptr );
89 
90  void setBoxes( const QList<QgsAABB> &bboxes );
91 
92  private:
93  LineMeshGeometry *mLineMeshGeo = nullptr;
94 };
95 
96 
97 AABBMesh::AABBMesh( Qt3DCore::QNode *parent )
98  : Qt3DRender::QGeometryRenderer( parent )
99 {
100  setInstanceCount( 1 );
101  setIndexOffset( 0 );
102  setFirstInstance( 0 );
103  setPrimitiveType( Qt3DRender::QGeometryRenderer::Lines );
104 
105  mLineMeshGeo = new LineMeshGeometry( this );
106  setGeometry( mLineMeshGeo );
107 }
108 
109 void AABBMesh::setBoxes( const QList<QgsAABB> &bboxes )
110 {
111  QList<QVector3D> vertices;
112  Q_FOREACH ( const QgsAABB &bbox, bboxes )
113  vertices << bbox.verticesForLines();
114  mLineMeshGeo->setVertices( vertices );
115  setVertexCount( mLineMeshGeo->vertexCount() );
116 }
117 
118 
119 // ----------------
120 
121 
122 QgsChunkBoundsEntity::QgsChunkBoundsEntity( Qt3DCore::QNode *parent )
123  : Qt3DCore::QEntity( parent )
124 {
125  mAabbMesh = new AABBMesh;
126  addComponent( mAabbMesh );
127 
128  Qt3DExtras::QPhongMaterial *bboxesMaterial = new Qt3DExtras::QPhongMaterial;
129  bboxesMaterial->setAmbient( Qt::red );
130  addComponent( bboxesMaterial );
131 }
132 
133 void QgsChunkBoundsEntity::setBoxes( const QList<QgsAABB> &bboxes )
134 {
135  mAabbMesh->setBoxes( bboxes );
136 }
137 
3 Axis-aligned bounding box - in world coords.
Definition: qgsaabb.h:30