-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsrubberband3d.cpp
133 lines (105 loc) · 3.82 KB
/
qgsrubberband3d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/***************************************************************************
qgsrubberband3d.cpp
--------------------------------------
Date : June 2021
Copyright : (C) 2021 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsrubberband3d.h"
#include "qgscameracontroller.h"
#include "qgslinevertexdata_p.h"
#include "qgslinematerial_p.h"
#include "qgsvertexid.h"
#include "qgslinestring.h"
#include <Qt3DCore/QEntity>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <Qt3DRender/QAttribute>
#include <Qt3DRender/QBuffer>
#include <Qt3DRender/QGeometry>
#else
#include <Qt3DCore/QAttribute>
#include <Qt3DCore/QBuffer>
#include <Qt3DCore/QGeometry>
#endif
#include <Qt3DRender/QGeometryRenderer>
#include <Qt3DRender/QMaterial>
/// @cond PRIVATE
QgsRubberBand3D::QgsRubberBand3D( Qgs3DMapSettings &map, QgsCameraController *cameraController, Qt3DCore::QEntity *parentEntity )
{
mMapSettings = ↦
mEntity = new Qt3DCore::QEntity( parentEntity );
QgsLineVertexData dummyLineData;
mGeometry = dummyLineData.createGeometry( mEntity );
Q_ASSERT( mGeometry->attributes().count() == 2 );
mPositionAttribute = mGeometry->attributes()[0];
mIndexAttribute = mGeometry->attributes()[1];
mGeomRenderer = new Qt3DRender::QGeometryRenderer;
mGeomRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::LineStripAdjacency );
mGeomRenderer->setGeometry( mGeometry );
mGeomRenderer->setPrimitiveRestartEnabled( true );
mGeomRenderer->setRestartIndexValue( 0 );
mEntity->addComponent( mGeomRenderer );
mLineMaterial = new QgsLineMaterial;
mLineMaterial->setLineWidth( 3 );
mLineMaterial->setLineColor( Qt::red );
QObject::connect( cameraController, &QgsCameraController::viewportChanged, mLineMaterial, [this, cameraController]
{
mLineMaterial->setViewportSize( cameraController->viewport().size() );
} );
mLineMaterial->setViewportSize( cameraController->viewport().size() );
mEntity->addComponent( mLineMaterial );
}
QgsRubberBand3D::~QgsRubberBand3D()
{
delete mEntity;
}
float QgsRubberBand3D::width() const
{
return mLineMaterial->lineWidth();
}
void QgsRubberBand3D::setWidth( float width )
{
mLineMaterial->setLineWidth( width );
}
QColor QgsRubberBand3D::color() const
{
return mLineMaterial->lineColor();
}
void QgsRubberBand3D::setColor( QColor color )
{
mLineMaterial->setLineColor( color );
}
void QgsRubberBand3D::reset()
{
mLineString.clear();
updateGeometry();
}
void QgsRubberBand3D::addPoint( const QgsPoint &pt )
{
mLineString.addVertex( pt );
updateGeometry();
}
void QgsRubberBand3D::removeLastPoint()
{
const int lastVertexIndex = mLineString.numPoints() - 1;
mLineString.deleteVertex( QgsVertexId( 0, 0, lastVertexIndex ) );
updateGeometry();
}
void QgsRubberBand3D::updateGeometry()
{
QgsLineVertexData lineData;
lineData.withAdjacency = true;
lineData.init( Qgis::AltitudeClamping::Absolute, Qgis::AltitudeBinding::Vertex, 0, mMapSettings );
lineData.addLineString( mLineString );
mPositionAttribute->buffer()->setData( lineData.createVertexBuffer() );
mIndexAttribute->buffer()->setData( lineData.createIndexBuffer() );
mGeomRenderer->setVertexCount( lineData.indexes.count() );
}
/// @endcond