QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
Loading...
Searching...
No Matches
qgslinematerial_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslinematerial_p.cpp
3 --------------------------------------
4 Date : Apr 2019
5 Copyright : (C) 2019 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 "qgslinematerial_p.h"
17
18#include <QColor>
19#include <QSizeF>
20#include <QUrl>
21#include <QVector3D>
22
23#include <Qt3DRender/QBlendEquation>
24#include <Qt3DRender/QBlendEquationArguments>
25#include <Qt3DRender/QCamera>
26#include <Qt3DRender/QEffect>
27#include <Qt3DRender/QGraphicsApiFilter>
28#include <Qt3DRender/QParameter>
29#include <Qt3DRender/QRenderPass>
30#include <Qt3DRender/QTechnique>
31
33
34
35QgsLineMaterial::QgsLineMaterial()
36 : mParameterThickness( new Qt3DRender::QParameter( "THICKNESS", 10, this ) )
37 , mParameterMiterLimit( new Qt3DRender::QParameter( "MITER_LIMIT", -1, this ) ) // 0.75
38 , mParameterLineColor( new Qt3DRender::QParameter( "lineColor", QColor( 0, 255, 0 ), this ) )
39 , mParameterUseVertexColors( new Qt3DRender::QParameter( "useVertexColors", false, this ) )
40 , mParameterWindowScale( new Qt3DRender::QParameter( "WIN_SCALE", QSizeF(), this ) )
41{
42 addParameter( mParameterThickness );
43 addParameter( mParameterMiterLimit );
44 addParameter( mParameterLineColor );
45 addParameter( mParameterUseVertexColors );
46 addParameter( mParameterWindowScale );
47
48 //Parameter { name: "tex0"; value: txt },
49 //Parameter { name: "useTex"; value: false },
50
51 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram( this );
52 shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/lines.vert" ) ) ) );
53 shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/lines.frag" ) ) ) );
54 shaderProgram->setGeometryShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/lines.geom" ) ) ) );
55
56 Qt3DRender::QBlendEquation *blendEquation = new Qt3DRender::QBlendEquation( this );
57 blendEquation->setBlendFunction( Qt3DRender::QBlendEquation::Add );
58
59 Qt3DRender::QBlendEquationArguments *blendEquationArgs = new Qt3DRender::QBlendEquationArguments( this );
60 blendEquationArgs->setSourceRgb( Qt3DRender::QBlendEquationArguments::SourceAlpha );
61 blendEquationArgs->setDestinationRgb( Qt3DRender::QBlendEquationArguments::OneMinusSourceAlpha );
62
63 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass( this );
64 renderPass->setShaderProgram( shaderProgram );
65 renderPass->addRenderState( blendEquation );
66 renderPass->addRenderState( blendEquationArgs );
67
68 // without this filter the default forward renderer would not render this
69 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey;
70 filterKey->setName( QStringLiteral( "renderingStyle" ) );
71 filterKey->setValue( "forward" );
72
73 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
74 technique->addFilterKey( filterKey );
75 technique->addRenderPass( renderPass );
76 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
77 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
78 technique->graphicsApiFilter()->setMajorVersion( 3 );
79 technique->graphicsApiFilter()->setMinorVersion( 1 );
80
81 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( this );
82 effect->addTechnique( technique );
83
84 setEffect( effect );
85}
86
87void QgsLineMaterial::setLineColor( const QColor &color )
88{
89 mParameterLineColor->setValue( color );
90}
91
92QColor QgsLineMaterial::lineColor() const
93{
94 return mParameterLineColor->value().value<QColor>();
95}
96
97void QgsLineMaterial::setUseVertexColors( bool enabled )
98{
99 mParameterUseVertexColors->setValue( enabled );
100}
101
102bool QgsLineMaterial::useVertexColors() const
103{
104 return mParameterUseVertexColors->value().toBool();
105}
106
107void QgsLineMaterial::setLineWidth( float width )
108{
109 mParameterThickness->setValue( width );
110}
111
112float QgsLineMaterial::lineWidth() const
113{
114 return mParameterThickness->value().toFloat();
115}
116
117void QgsLineMaterial::setViewportSize( const QSizeF &viewportSize )
118{
119 mParameterWindowScale->setValue( viewportSize );
120}
121