QGIS API Documentation 3.99.0-Master (09f76ad7019)
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 <QString>
24#include <QUrl>
25#include <Qt3DRender/QBlendEquation>
26#include <Qt3DRender/QBlendEquationArguments>
27#include <Qt3DRender/QEffect>
28#include <Qt3DRender/QGraphicsApiFilter>
29#include <Qt3DRender/QNoDepthMask>
30#include <Qt3DRender/QParameter>
31#include <Qt3DRender/QRenderPass>
32#include <Qt3DRender/QShaderProgram>
33#include <Qt3DRender/QTechnique>
34
35#include "moc_qgspoint3dbillboardmaterial.cpp"
36
37using namespace Qt::StringLiterals;
38
40 : mSize( new Qt3DRender::QParameter( "BB_SIZE", QSizeF( 100, 100 ), this ) )
41 , mViewportSize( new Qt3DRender::QParameter( "WIN_SCALE", QSizeF( 800, 600 ), this ) )
42{
43 addParameter( mSize );
44 addParameter( mViewportSize );
45
46 // Initialize with empty parameter.
47 mTexture2D = new Qt3DRender::QParameter( "tex0", QVariant(), this );
48 addParameter( mTexture2D );
49
50 // Blending for handling transparency
51 Qt3DRender::QBlendEquationArguments *blendState = new Qt3DRender::QBlendEquationArguments;
52 blendState->setSourceRgb( Qt3DRender::QBlendEquationArguments::SourceAlpha );
53 blendState->setDestinationRgb( Qt3DRender::QBlendEquationArguments::OneMinusSourceAlpha );
54
55 Qt3DRender::QBlendEquation *blendEquation = new Qt3DRender::QBlendEquation;
56 blendEquation->setBlendFunction( Qt3DRender::QBlendEquation::Add );
57
58 // Shader program
59 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram( this );
60
61 const QUrl urlVert( u"qrc:/shaders/billboards.vert"_s );
62 const QUrl urlGeom( u"qrc:/shaders/billboards.geom"_s );
63
64 switch ( mode )
65 {
67 {
68 shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( urlVert ) );
69 shaderProgram->setGeometryShaderCode( Qt3DRender::QShaderProgram::loadSource( urlGeom ) );
70 break;
71 }
73 {
74 const QByteArray vertexShaderCode = Qt3DRender::QShaderProgram::loadSource( urlVert );
75 const QByteArray finalVertexShaderCode = Qgs3DUtils::addDefinesToShaderCode( vertexShaderCode, QStringList( { "TEXTURE_ATLAS" } ) );
76 shaderProgram->setVertexShaderCode( finalVertexShaderCode );
77
78 const QByteArray geomShaderCode = Qt3DRender::QShaderProgram::loadSource( urlGeom );
79 const QByteArray finalGeomShaderCode = Qgs3DUtils::addDefinesToShaderCode( geomShaderCode, QStringList( { "TEXTURE_ATLAS" } ) );
80 shaderProgram->setGeometryShaderCode( finalGeomShaderCode );
81 break;
82 }
84 {
85 const QByteArray vertexShaderCode = Qt3DRender::QShaderProgram::loadSource( urlVert );
86 const QByteArray finalVertexShaderCode = Qgs3DUtils::addDefinesToShaderCode( vertexShaderCode, QStringList( { "TEXTURE_ATLAS", "TEXTURE_ATLAS_PIXEL_OFFSETS" } ) );
87 shaderProgram->setVertexShaderCode( finalVertexShaderCode );
88
89 const QByteArray geomShaderCode = Qt3DRender::QShaderProgram::loadSource( urlGeom );
90 const QByteArray finalGeomShaderCode = Qgs3DUtils::addDefinesToShaderCode( geomShaderCode, QStringList( { "TEXTURE_ATLAS", "TEXTURE_ATLAS_PIXEL_OFFSETS" } ) );
91 shaderProgram->setGeometryShaderCode( finalGeomShaderCode );
92 break;
93 }
94 }
95 shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( u"qrc:/shaders/billboards.frag"_s ) ) );
96
97 // Render Pass
98 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass( this );
99 renderPass->setShaderProgram( shaderProgram );
100 renderPass->addRenderState( blendState );
101 renderPass->addRenderState( blendEquation );
102
103 // without this filter the default forward renderer would not render this
104 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey;
105 filterKey->setName( u"renderingStyle"_s );
106 filterKey->setValue( "forward" );
107
108 // Technique
109 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
110 technique->addRenderPass( renderPass );
111 technique->addFilterKey( filterKey );
112 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
113 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
114 technique->graphicsApiFilter()->setMajorVersion( 3 );
115 technique->graphicsApiFilter()->setMinorVersion( 1 );
116
117 // Effect
118 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( this );
119 effect->addTechnique( technique );
120
121 setEffect( effect );
122}
123
125
127{
128 mSize->setValue( size );
129}
130
132{
133 return mSize->value().value<QSizeF>();
134}
135
137{
138 mViewportSize->setValue( size );
139}
140
142{
143 return mViewportSize->value().value<QSizeF>();
144}
145
147{
148 // Create texture image
149 QgsImageTexture *textureImage = new QgsImageTexture( image );
150 setTexture2DFromTextureImage( textureImage );
151
152 setSize( QSizeF( image.size().width(), image.size().height() ) );
153}
154
156{
157 // Default texture
158 const std::unique_ptr<QgsMarkerSymbol> defaultSymbol( static_cast<QgsMarkerSymbol *>( QgsSymbol::defaultSymbol( Qgis::GeometryType::Point ) ) );
159 setTexture2DFromSymbol( defaultSymbol.get(), context, selected );
160}
161
162QImage QgsPoint3DBillboardMaterial::renderSymbolToImage( const QgsMarkerSymbol *markerSymbol, const Qgs3DRenderContext &context, bool selected )
163{
164 QgsRenderContext context2D;
165 context2D.setSelectionColor( context.selectionColor() );
166 context2D.setScaleFactor( context.outputDpi() / 25.4 );
169
170 std::unique_ptr< QgsMarkerSymbol > clonedSymbol( markerSymbol->clone() );
171 clonedSymbol->startRender( context2D );
172
173 constexpr int BUFFER_SIZE_PIXELS = 2;
174
175 const QRectF bounds = markerSymbol->bounds( QPointF( 0, 0 ), context2D );
176
177 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 );
178 image.fill( Qt::transparent );
179
180 QPainter painter( &image );
181 context2D.setPainter( &painter );
182
183 clonedSymbol->renderPoint( QPointF( -bounds.left() + BUFFER_SIZE_PIXELS, -bounds.top() + BUFFER_SIZE_PIXELS ), nullptr, context2D, -1, selected );
184
185 painter.end();
186
187 clonedSymbol->stopRender( context2D );
188 return image;
189}
190
191void QgsPoint3DBillboardMaterial::setTexture2DFromSymbol( const QgsMarkerSymbol *markerSymbol, const Qgs3DRenderContext &context, bool selected )
192{
193 const QImage symbolImage = renderSymbolToImage( markerSymbol, context, selected );
194 setTexture2DFromImage( symbolImage );
195}
196
197void QgsPoint3DBillboardMaterial::setTexture2DFromTextureImage( Qt3DRender::QAbstractTextureImage *textureImage )
198{
199 // Texture2D
200 Qt3DRender::QTexture2D *texture2D = new Qt3DRender::QTexture2D( this );
201 texture2D->setGenerateMipMaps( false );
202 texture2D->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
203 texture2D->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
204
205 // The textureImage gets parented to texture2D here
206 texture2D->addTextureImage( textureImage );
207
208 mTexture2D->setValue( QVariant::fromValue( texture2D ) );
209}
@ Point
Points.
Definition qgis.h:366
@ Antialiasing
Use antialiasing while drawing.
Definition qgis.h:2814
@ HighQualityImageTransforms
Enable high quality image transformations, which results in better appearance of scaled or rotated ra...
Definition qgis.h:2824
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.