QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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  const 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  const QByteArray vertexArr( ( const char * ) vert.constData(), vert.size() * sizeof( float ) );
40  Qt3DRender::QBuffer *vertexBuffer = nullptr;
41  vertexBuffer = new Qt3DRender::QBuffer( this );
42  vertexBuffer->setData( vertexArr );
43 
44  positionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
45  positionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
46  positionAttribute->setVertexSize( 3 );
47  positionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
48  positionAttribute->setBuffer( vertexBuffer );
49  positionAttribute->setByteOffset( 0 );
50  positionAttribute->setByteStride( 3 * sizeof( float ) );
51  positionAttribute->setCount( 6 );
52 
53  geom->addAttribute( positionAttribute );
54 
55  Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
56  renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
57  renderer->setGeometry( geom );
58 
59  addComponent( renderer );
60 
61  QMatrix4x4 modelMatrix;
62  modelMatrix.setToIdentity();
63  modelMatrix.translate( centerNDC.x(), centerNDC.y() );
64  modelMatrix.scale( size.width(), size.height() );
65  mMaterial = new QgsPreviewQuadMaterial( texture, modelMatrix, additionalShaderParameters );
66 
67  addComponent( mMaterial );
68 }
69 
70 void QgsPreviewQuad::setViewPort( const QPointF &centerNDC, const QSizeF &size )
71 {
72  QMatrix4x4 modelMatrix;
73  modelMatrix.setToIdentity();
74  modelMatrix.translate( centerNDC.x(), centerNDC.y() );
75  modelMatrix.scale( size.width(), size.height() );
76  mMaterial->setModelMatrix( modelMatrix );
77 }
78 
79 QgsPreviewQuadMaterial::QgsPreviewQuadMaterial( Qt3DRender::QAbstractTexture *texture, const QMatrix4x4 &modelMatrix, QVector<Qt3DRender::QParameter *> additionalShaderParameters, QNode *parent )
80  : Qt3DRender::QMaterial( parent )
81 {
82  mTextureParameter = new Qt3DRender::QParameter( "previewTexture", texture );
83  mTextureTransformParameter = new Qt3DRender::QParameter( "modelMatrix", QVariant::fromValue( modelMatrix ) );
84  addParameter( mTextureParameter );
85  addParameter( mTextureTransformParameter );
86  for ( Qt3DRender::QParameter *parameter : additionalShaderParameters ) addParameter( parameter );
87 
88  mEffect = new Qt3DRender::QEffect;
89 
90  Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
91 
92  Qt3DRender::QGraphicsApiFilter *graphicsApiFilter = technique->graphicsApiFilter();
93  graphicsApiFilter->setApi( Qt3DRender::QGraphicsApiFilter::Api::OpenGL );
94  graphicsApiFilter->setProfile( Qt3DRender::QGraphicsApiFilter::OpenGLProfile::CoreProfile );
95  graphicsApiFilter->setMajorVersion( 1 );
96  graphicsApiFilter->setMinorVersion( 5 );
97 
98  Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass;
99 
100  Qt3DRender::QShaderProgram *shader = new Qt3DRender::QShaderProgram;
101  shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( "qrc:/shaders/preview.vert" ) ) );
102  shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( "qrc:/shaders/preview.frag" ) ) );
103  renderPass->setShaderProgram( shader );
104 
105  technique->addRenderPass( renderPass );
106 
107  mEffect->addTechnique( technique );
108  setEffect( mEffect );
109 }
110 
111 void QgsPreviewQuadMaterial::setModelMatrix( const QMatrix4x4 &modelMatrix )
112 {
113  mTextureTransformParameter->setValue( modelMatrix );
114 }
void setModelMatrix(const QMatrix4x4 &modelMatrix)
Sets the model matrix of the quad.
QgsPreviewQuadMaterial(Qt3DRender::QAbstractTexture *texture, const QMatrix4x4 &modelMatrix, QVector< Qt3DRender::QParameter * > additionalShaderParameters=QVector< Qt3DRender::QParameter * >(), QNode *parent=nullptr)
Constructor.
void setViewPort(const QPointF &centerNDC, const QSizeF &size)
Sets where the quad will be located on the scene.
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)