QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
35 QgsLineMaterial::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  , mParameterWindowScale( new Qt3DRender::QParameter( "WIN_SCALE", QSizeF(), this ) )
40 {
41  addParameter( mParameterThickness );
42  addParameter( mParameterMiterLimit );
43  addParameter( mParameterLineColor );
44  addParameter( mParameterWindowScale );
45 
46  //Parameter { name: "tex0"; value: txt },
47  //Parameter { name: "useTex"; value: false },
48 
49  Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram( this );
50  shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/lines.vert" ) ) ) );
51  shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/lines.frag" ) ) ) );
52  shaderProgram->setGeometryShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/lines.geom" ) ) ) );
53 
54  Qt3DRender::QBlendEquation *blendEquation = new Qt3DRender::QBlendEquation( this );
55  blendEquation->setBlendFunction( Qt3DRender::QBlendEquation::Add );
56 
57  Qt3DRender::QBlendEquationArguments *blendEquationArgs = new Qt3DRender::QBlendEquationArguments( this );
58  blendEquationArgs->setSourceRgb( Qt3DRender::QBlendEquationArguments::SourceAlpha );
59  blendEquationArgs->setDestinationRgb( Qt3DRender::QBlendEquationArguments::OneMinusSourceAlpha );
60 
61  Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass( this );
62  renderPass->setShaderProgram( shaderProgram );
63  renderPass->addRenderState( blendEquation );
64  renderPass->addRenderState( blendEquationArgs );
65 
66  // without this filter the default forward renderer would not render this
67  Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey;
68  filterKey->setName( QStringLiteral( "renderingStyle" ) );
69  filterKey->setValue( "forward" );
70 
71  Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
72  technique->addFilterKey( filterKey );
73  technique->addRenderPass( renderPass );
74  technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
75  technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
76  technique->graphicsApiFilter()->setMajorVersion( 3 );
77  technique->graphicsApiFilter()->setMinorVersion( 1 );
78 
79  Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( this );
80  effect->addTechnique( technique );
81 
82  setEffect( effect );
83 }
84 
85 void QgsLineMaterial::setLineColor( const QColor &color )
86 {
87  mParameterLineColor->setValue( color );
88 }
89 
90 QColor QgsLineMaterial::lineColor() const
91 {
92  return mParameterLineColor->value().value<QColor>();
93 }
94 
95 void QgsLineMaterial::setLineWidth( float width )
96 {
97  mParameterThickness->setValue( width );
98 }
99 
100 float QgsLineMaterial::lineWidth() const
101 {
102  return mParameterThickness->value().toFloat();
103 }
104 
105 void QgsLineMaterial::setViewportSize( const QSizeF &viewportSize )
106 {
107  mParameterWindowScale->setValue( viewportSize );
108 }
109