QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgspreviewquad.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspreviewquad.cpp
3  --------------------------------------
4  Date : August 2020
5  Copyright : (C) 2020 by Belgacem Nedjima
6  Email : gb underscore nedjima at esi dot dz
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 "qgspreviewquad.h"
17 
18 #include <Qt3DRender/QGeometry>
19 #include <Qt3DRender/QAttribute>
20 #include <Qt3DRender/QBuffer>
21 #include <Qt3DRender/QGeometryRenderer>
22 #include <Qt3DRender/QTechnique>
23 #include <Qt3DRender/QGraphicsApiFilter>
24 #include <Qt3DRender/QShaderProgram>
25 #include <QMatrix4x4>
26 #include <QUrl>
27 
28 QgsPreviewQuad::QgsPreviewQuad( Qt3DRender::QAbstractTexture *texture,
29  const QPointF &centerNDC, const QSizeF &size,
30  QVector<Qt3DRender::QParameter *> additionalShaderParameters,
31  Qt3DCore::QEntity *parent )
32  : Qt3DCore::QEntity( parent )
33 {
34  setObjectName( "Preview Quad" );
35  Qt3DRender::QGeometry *geom = new Qt3DRender::QGeometry;
36  Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute;
37  QVector<float> vert = { -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
38 
39  QByteArray vertexArr( ( const char * ) vert.constData(), vert.size() * sizeof( float ) );
40  Qt3DRender::QBuffer *vertexBuffer = nullptr;
41 #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
42  vertexBuffer = new Qt3DRender::QBuffer( Qt3DRender::QBuffer::VertexBuffer, this );
43 #else
44  vertexBuffer = new Qt3DRender::QBuffer( this );
45 #endif
46  vertexBuffer->setData( vertexArr );
47 
48  positionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
49  positionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
50  positionAttribute->setVertexSize( 3 );
51  positionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
52  positionAttribute->setBuffer( vertexBuffer );
53  positionAttribute->setByteOffset( 0 );
54  positionAttribute->setByteStride( 3 * sizeof( float ) );
55  positionAttribute->setCount( 6 );
56 
57  geom->addAttribute( positionAttribute );
58 
59  Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
60  renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
61  renderer->setGeometry( geom );
62 
63  addComponent( renderer );
64 
65  QMatrix4x4 modelMatrix;
66  modelMatrix.setToIdentity();
67  modelMatrix.translate( centerNDC.x(), centerNDC.y() );
68  modelMatrix.scale( size.width(), size.height() );
69  mMaterial = new QgsPreviewQuadMaterial( texture, modelMatrix, additionalShaderParameters );
70 
71  addComponent( mMaterial );
72 }
73 
74 QgsPreviewQuadMaterial::QgsPreviewQuadMaterial( Qt3DRender::QAbstractTexture *texture, const QMatrix4x4 &modelMatrix, QVector<Qt3DRender::QParameter *> additionalShaderParameters, QNode *parent )
75  : Qt3DRender::QMaterial( parent )
76 {
77  mTextureParameter = new Qt3DRender::QParameter( "previewTexture", texture );
78  mTextureTransformParameter = new Qt3DRender::QParameter( "modelMatrix", QVariant::fromValue( modelMatrix ) );
79  addParameter( mTextureParameter );
80  addParameter( mTextureTransformParameter );
81  for ( Qt3DRender::QParameter *parameter : additionalShaderParameters ) addParameter( parameter );
82 
83  mEffect = new Qt3DRender::QEffect;
84 
85  Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
86 
87  Qt3DRender::QGraphicsApiFilter *graphicsApiFilter = technique->graphicsApiFilter();
88  graphicsApiFilter->setApi( Qt3DRender::QGraphicsApiFilter::Api::OpenGL );
89  graphicsApiFilter->setProfile( Qt3DRender::QGraphicsApiFilter::OpenGLProfile::CoreProfile );
90  graphicsApiFilter->setMajorVersion( 1 );
91  graphicsApiFilter->setMinorVersion( 5 );
92 
93  Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass;
94 
95  Qt3DRender::QShaderProgram *shader = new Qt3DRender::QShaderProgram;
96  shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( "qrc:/shaders/preview.vert" ) ) );
97  shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( "qrc:/shaders/preview.frag" ) ) );
98  renderPass->setShaderProgram( shader );
99 
100  technique->addRenderPass( renderPass );
101 
102  mEffect->addTechnique( technique );
103  setEffect( mEffect );
104 }
105 
qgspreviewquad.h
QgsPreviewQuad::QgsPreviewQuad
QgsPreviewQuad(Qt3DRender::QAbstractTexture *texture, const QPointF &centerNDC, const QSizeF &size, QVector< Qt3DRender::QParameter * > additionalShaderParameters=QVector< Qt3DRender::QParameter * >(), Qt3DCore::QEntity *parent=nullptr)
Construct an object that displays a texture for debugging purposes (example: depth buffer)
Definition: qgspreviewquad.cpp:28
QgsPreviewQuadMaterial
3 Material component for QgsPreviewQuad object
Definition: qgspreviewquad.h:34
Qt3DCore
Definition: qgsabstract3drenderer.h:31
Qt3DRender
Definition: qgs3dmapscene.h:27
QgsPreviewQuadMaterial::QgsPreviewQuadMaterial
QgsPreviewQuadMaterial(Qt3DRender::QAbstractTexture *texture, const QMatrix4x4 &modelMatrix, QVector< Qt3DRender::QParameter * > additionalShaderParameters=QVector< Qt3DRender::QParameter * >(), QNode *parent=nullptr)
Constructor.
Definition: qgspreviewquad.cpp:74