QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgspoint3dbillboardmaterial.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspoint3dbillboardmaterial.h
3 --------------------------------------
4 Date : Jul 2019
5 Copyright : (C) 2019 by Ismail Sunni
6 Email : imajimatika 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 ***************************************************************************/
16
17#include "qgs3drendercontext.h"
18#include "qgs3dutils.h"
19#include "qgsimagetexture.h"
20#include "qgsmarkersymbol.h"
21#include "qgssymbollayerutils.h"
22
23#include <QUrl>
24#include <Qt3DRender/QBlendEquation>
25#include <Qt3DRender/QBlendEquationArguments>
26#include <Qt3DRender/QEffect>
27#include <Qt3DRender/QGraphicsApiFilter>
28#include <Qt3DRender/QNoDepthMask>
29#include <Qt3DRender/QParameter>
30#include <Qt3DRender/QRenderPass>
31#include <Qt3DRender/QShaderProgram>
32#include <Qt3DRender/QTechnique>
33
34#include "moc_qgspoint3dbillboardmaterial.cpp"
35
37 : mSize( new Qt3DRender::QParameter( "BB_SIZE", QSizeF( 100, 100 ), this ) )
38 , mViewportSize( new Qt3DRender::QParameter( "WIN_SCALE", QSizeF( 800, 600 ), this ) )
39{
40 addParameter( mSize );
41 addParameter( mViewportSize );
42
43 // Initialize with empty parameter.
44 mTexture2D = new Qt3DRender::QParameter( "tex0", QVariant(), this );
45 addParameter( mTexture2D );
46
47 // Blending for handling transparency
48 Qt3DRender::QBlendEquationArguments *blendState = new Qt3DRender::QBlendEquationArguments;
49 blendState->setSourceRgb( Qt3DRender::QBlendEquationArguments::SourceAlpha );
50 blendState->setDestinationRgb( Qt3DRender::QBlendEquationArguments::OneMinusSourceAlpha );
51
52 Qt3DRender::QBlendEquation *blendEquation = new Qt3DRender::QBlendEquation;
53 blendEquation->setBlendFunction( Qt3DRender::QBlendEquation::Add );
54
55 // Shader program
56 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram( this );
57
58 const QUrl urlVert( QStringLiteral( "qrc:/shaders/billboards.vert" ) );
59 const QUrl urlGeom( QStringLiteral( "qrc:/shaders/billboards.geom" ) );
60
61 switch ( mode )
62 {
64 {
65 shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( urlVert ) );
66 shaderProgram->setGeometryShaderCode( Qt3DRender::QShaderProgram::loadSource( urlGeom ) );
67 break;
68 }
70 {
71 const QByteArray vertexShaderCode = Qt3DRender::QShaderProgram::loadSource( urlVert );
72 const QByteArray finalVertexShaderCode = Qgs3DUtils::addDefinesToShaderCode( vertexShaderCode, QStringList( { "TEXTURE_ATLAS" } ) );
73 shaderProgram->setVertexShaderCode( finalVertexShaderCode );
74
75 const QByteArray geomShaderCode = Qt3DRender::QShaderProgram::loadSource( urlGeom );
76 const QByteArray finalGeomShaderCode = Qgs3DUtils::addDefinesToShaderCode( geomShaderCode, QStringList( { "TEXTURE_ATLAS" } ) );
77 shaderProgram->setGeometryShaderCode( finalGeomShaderCode );
78 break;
79 }
81 {
82 const QByteArray vertexShaderCode = Qt3DRender::QShaderProgram::loadSource( urlVert );
83 const QByteArray finalVertexShaderCode = Qgs3DUtils::addDefinesToShaderCode( vertexShaderCode, QStringList( { "TEXTURE_ATLAS", "TEXTURE_ATLAS_PIXEL_OFFSETS" } ) );
84 shaderProgram->setVertexShaderCode( finalVertexShaderCode );
85
86 const QByteArray geomShaderCode = Qt3DRender::QShaderProgram::loadSource( urlGeom );
87 const QByteArray finalGeomShaderCode = Qgs3DUtils::addDefinesToShaderCode( geomShaderCode, QStringList( { "TEXTURE_ATLAS", "TEXTURE_ATLAS_PIXEL_OFFSETS" } ) );
88 shaderProgram->setGeometryShaderCode( finalGeomShaderCode );
89 break;
90 }
91 }
92 shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/billboards.frag" ) ) ) );
93
94 // Render Pass
95 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass( this );
96 renderPass->setShaderProgram( shaderProgram );
97 renderPass->addRenderState( blendState );
98 renderPass->addRenderState( blendEquation );
99
100 // without this filter the default forward renderer would not render this
101 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey;
102 filterKey->setName( QStringLiteral( "renderingStyle" ) );
103 filterKey->setValue( "forward" );
104
105 // Technique
106 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
107 technique->addRenderPass( renderPass );
108 technique->addFilterKey( filterKey );
109 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
110 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
111 technique->graphicsApiFilter()->setMajorVersion( 3 );
112 technique->graphicsApiFilter()->setMinorVersion( 1 );
113
114 // Effect
115 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( this );
116 effect->addTechnique( technique );
117
118 setEffect( effect );
119}
120
122
124{
125 mSize->setValue( size );
126}
127
129{
130 return mSize->value().value<QSizeF>();
131}
132
134{
135 mViewportSize->setValue( size );
136}
137
139{
140 return mViewportSize->value().value<QSizeF>();
141}
142
144{
145 // Create texture image
146 QgsImageTexture *textureImage = new QgsImageTexture( image );
147 setTexture2DFromTextureImage( textureImage );
148
149 setSize( QSizeF( image.size().width(), image.size().height() ) );
150}
151
153{
154 // Default texture
155 const std::unique_ptr<QgsMarkerSymbol> defaultSymbol( static_cast<QgsMarkerSymbol *>( QgsSymbol::defaultSymbol( Qgis::GeometryType::Point ) ) );
156 setTexture2DFromSymbol( defaultSymbol.get(), context, selected );
157}
158
159QImage QgsPoint3DBillboardMaterial::renderSymbolToImage( const QgsMarkerSymbol *markerSymbol, const Qgs3DRenderContext &context, bool selected )
160{
161 QgsRenderContext context2D;
162 context2D.setSelectionColor( context.selectionColor() );
163 context2D.setScaleFactor( context.outputDpi() / 25.4 );
166
167 std::unique_ptr< QgsMarkerSymbol > clonedSymbol( markerSymbol->clone() );
168 clonedSymbol->startRender( context2D );
169
170 constexpr int BUFFER_SIZE_PIXELS = 2;
171
172 const QRectF bounds = markerSymbol->bounds( QPointF( 0, 0 ), context2D );
173
174 QImage image( static_cast< int >( std::ceil( bounds.size().width() ) ) + 2 * BUFFER_SIZE_PIXELS, static_cast< int >( std::ceil( bounds.size().height() ) ) + 2 * BUFFER_SIZE_PIXELS, QImage::Format_ARGB32_Premultiplied );
175 image.fill( Qt::transparent );
176
177 QPainter painter( &image );
178 context2D.setPainter( &painter );
179
180 clonedSymbol->renderPoint( QPointF( -bounds.left() + BUFFER_SIZE_PIXELS, -bounds.top() + BUFFER_SIZE_PIXELS ), nullptr, context2D, -1, selected );
181
182 painter.end();
183
184 clonedSymbol->stopRender( context2D );
185 return image;
186}
187
188void QgsPoint3DBillboardMaterial::setTexture2DFromSymbol( const QgsMarkerSymbol *markerSymbol, const Qgs3DRenderContext &context, bool selected )
189{
190 const QImage symbolImage = renderSymbolToImage( markerSymbol, context, selected );
191 setTexture2DFromImage( symbolImage );
192}
193
194void QgsPoint3DBillboardMaterial::setTexture2DFromTextureImage( Qt3DRender::QAbstractTextureImage *textureImage )
195{
196 // Texture2D
197 Qt3DRender::QTexture2D *texture2D = new Qt3DRender::QTexture2D( this );
198 texture2D->setGenerateMipMaps( false );
199 texture2D->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
200 texture2D->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
201
202 // The textureImage gets parented to texture2D here
203 texture2D->addTextureImage( textureImage );
204
205 mTexture2D->setValue( QVariant::fromValue( texture2D ) );
206}
@ Point
Points.
Definition qgis.h:359
@ Antialiasing
Use antialiasing while drawing.
Definition qgis.h:2756
@ HighQualityImageTransforms
Enable high quality image transformations, which results in better appearance of scaled or rotated ra...
Definition qgis.h:2766
Rendering context for preparation of 3D entities.
QColor selectionColor() const
Returns color used for selected features.
double outputDpi() const
Returns DPI used for conversion between real world units (e.g.
static QByteArray addDefinesToShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Inserts some define macros into a shader source code.
Holds an image that can be used as a texture in the 3D view.
A marker symbol type, for rendering Point and MultiPoint geometries.
QgsMarkerSymbol * clone() const override
Returns a deep copy of this symbol.
QRectF bounds(QPointF point, QgsRenderContext &context, const QgsFeature &feature=QgsFeature()) const
Returns the approximate bounding box of the marker symbol, which includes the bounding box of all sym...
void useDefaultSymbol(const Qgs3DRenderContext &context, bool selected=false)
Set default symbol for the texture with context and selected parameter for rendering.
static QImage renderSymbolToImage(const QgsMarkerSymbol *markerSymbol, const Qgs3DRenderContext &context, bool selected=false)
Renders a marker symbol to an image.
@ SingleTexture
Use a single repeated texture for all billboards. Billboard positions should be set using QgsBillboar...
@ AtlasTexture
Use a texture atlas, so each billboard has a different texture. Billboard positions and texture data ...
@ AtlasTextureWithPixelOffsets
Use a texture atlas, so each billboard has a different texture. Billboards have pixel-sized offsets f...
QSizeF windowSize() const
Returns the size of the view port.
void setTexture2DFromImage(const QImage &image)
Set the texture2D of the billboard from an image.
~QgsPoint3DBillboardMaterial() override
QSizeF size() const
Returns the billboard size.
void setTexture2DFromSymbol(const QgsMarkerSymbol *markerSymbol, const Qgs3DRenderContext &context, bool selected=false)
Set markerSymbol for the texture with context and selected parameter for rendering.
void setViewportSize(const QSizeF size)
Set the size of the view port.
void setSize(const QSizeF size)
Set the billboard size.
QgsPoint3DBillboardMaterial(Mode mode=Mode::SingleTexture)
Constructor for QgsPoint3DBillboardMaterial, using the specified mode.
Contains information about the context of a rendering operation.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
void setSelectionColor(const QColor &color)
Sets the color to use when rendering selected features.
static QgsSymbol * defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.