QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  , mVertexBuffer( new Qt3DRender::QBuffer( this ) )
30 {
31  mPositionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
32  mPositionAttribute->setBuffer( mVertexBuffer );
33  mPositionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
34  mPositionAttribute->setVertexSize( 3 );
35  mPositionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
36 
37  addAttribute( mPositionAttribute );
38 }
39 
40 void LineMeshGeometry::setVertices( const QList<QVector3D> &vertices )
41 {
42  QByteArray vertexBufferData;
43  vertexBufferData.resize( vertices.size() * 3 * sizeof( float ) );
44  float *rawVertexArray = reinterpret_cast<float *>( vertexBufferData.data() );
45  int idx = 0;
46  for ( const auto &v : vertices )
47  {
48  rawVertexArray[idx++] = v.x();
49  rawVertexArray[idx++] = v.y();
50  rawVertexArray[idx++] = v.z();
51  }
52 
53  mVertexCount = vertices.count();
54  mVertexBuffer->setData( vertexBufferData );
55 }
56 
57 
58 // ----------------
59 
60 
61 AABBMesh::AABBMesh( Qt3DCore::QNode *parent )
62  : Qt3DRender::QGeometryRenderer( parent )
63 {
64  setInstanceCount( 1 );
65  setIndexOffset( 0 );
66  setFirstInstance( 0 );
67  setPrimitiveType( Qt3DRender::QGeometryRenderer::Lines );
68 
69  mLineMeshGeo = new LineMeshGeometry( this );
70  setGeometry( mLineMeshGeo );
71 }
72 
73 void AABBMesh::setBoxes( const QList<QgsAABB> &bboxes )
74 {
75  QList<QVector3D> vertices;
76  for ( const QgsAABB &bbox : bboxes )
77  vertices << bbox.verticesForLines();
78  mLineMeshGeo->setVertices( vertices );
79  setVertexCount( mLineMeshGeo->vertexCount() );
80 }
81 
82 
83 // ----------------
84 
85 
86 QgsChunkBoundsEntity::QgsChunkBoundsEntity( Qt3DCore::QNode *parent )
87  : Qt3DCore::QEntity( parent )
88 {
89  mAabbMesh = new AABBMesh;
90  addComponent( mAabbMesh );
91 
92  Qt3DExtras::QPhongMaterial *bboxesMaterial = new Qt3DExtras::QPhongMaterial;
93  bboxesMaterial->setAmbient( Qt::red );
94  addComponent( bboxesMaterial );
95 }
96 
97 void QgsChunkBoundsEntity::setBoxes( const QList<QgsAABB> &bboxes )
98 {
99  mAabbMesh->setBoxes( bboxes );
100 }
101 
qgsaabb.h
Qt3DCore
Definition: qgsabstract3drenderer.h:30
Qt3DRender
Definition: qgs3dmapscene.h:28
QgsAABB
Axis-aligned bounding box - in world coords.
Definition: qgsaabb.h:33
qgschunkboundsentity_p.h