QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include <QVector2D>
28 
29 QgsPreviewQuad::QgsPreviewQuad( Qt3DRender::QAbstractTexture *texture,
30  const QPointF &centerTexCoords, const QSizeF &sizeTexCoords,
31  QVector<Qt3DRender::QParameter *> additionalShaderParameters,
32  Qt3DCore::QEntity *parent )
33  : Qt3DCore::QEntity( parent )
34 {
35  setObjectName( "Preview Quad" );
36  Qt3DRender::QGeometry *geom = new Qt3DRender::QGeometry;
37  Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute;
38  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 };
39 
40  const QByteArray vertexArr( ( const char * ) vert.constData(), vert.size() * sizeof( float ) );
41  Qt3DRender::QBuffer *vertexBuffer = nullptr;
42  vertexBuffer = new Qt3DRender::QBuffer( this );
43  vertexBuffer->setData( vertexArr );
44 
45  positionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
46  positionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
47  positionAttribute->setVertexSize( 3 );
48  positionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
49  positionAttribute->setBuffer( vertexBuffer );
50  positionAttribute->setByteOffset( 0 );
51  positionAttribute->setByteStride( 3 * sizeof( float ) );
52  positionAttribute->setCount( 6 );
53 
54  geom->addAttribute( positionAttribute );
55 
56  Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
57  renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
58  renderer->setGeometry( geom );
59 
60  addComponent( renderer );
61 
62  mMaterial = new QgsPreviewQuadMaterial( texture, additionalShaderParameters );
63 
64  addComponent( mMaterial );
65 
66  setViewPort( centerTexCoords, sizeTexCoords );
67 }
68 
69 void QgsPreviewQuad::setViewPort( const QPointF &centerTexCoords, const QSizeF &sizeTexCoords )
70 {
71  mMaterial->setViewPort( QVector2D( centerTexCoords.x(), centerTexCoords.y() ), QVector2D( sizeTexCoords.width(), sizeTexCoords.height() ) );
72 }
73 
74 QgsPreviewQuadMaterial::QgsPreviewQuadMaterial( Qt3DRender::QAbstractTexture *texture, QVector<Qt3DRender::QParameter *> additionalShaderParameters, QNode *parent )
75  : Qt3DRender::QMaterial( parent )
76 {
77  mTextureParameter = new Qt3DRender::QParameter( "previewTexture", texture );
78  mCenterTextureCoords = new Qt3DRender::QParameter( "centerTexCoords", QVector2D( 0, 0 ) );
79  mSizeTextureCoords = new Qt3DRender::QParameter( "sizeTexCoords", QVector2D( 1, 1 ) );
80  addParameter( mTextureParameter );
81  addParameter( mCenterTextureCoords );
82  addParameter( mSizeTextureCoords );
83  for ( Qt3DRender::QParameter *parameter : additionalShaderParameters ) addParameter( parameter );
84 
85  mEffect = new Qt3DRender::QEffect;
86 
87  Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
88 
89  Qt3DRender::QGraphicsApiFilter *graphicsApiFilter = technique->graphicsApiFilter();
90  graphicsApiFilter->setApi( Qt3DRender::QGraphicsApiFilter::Api::OpenGL );
91  graphicsApiFilter->setProfile( Qt3DRender::QGraphicsApiFilter::OpenGLProfile::CoreProfile );
92  graphicsApiFilter->setMajorVersion( 1 );
93  graphicsApiFilter->setMinorVersion( 5 );
94 
95  Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass;
96 
97  Qt3DRender::QShaderProgram *shader = new Qt3DRender::QShaderProgram;
98  shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( "qrc:/shaders/preview.vert" ) ) );
99  shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( "qrc:/shaders/preview.frag" ) ) );
100  renderPass->setShaderProgram( shader );
101 
102  technique->addRenderPass( renderPass );
103 
104  mEffect->addTechnique( technique );
105  setEffect( mEffect );
106 }
107 
108 void QgsPreviewQuadMaterial::setViewPort( QVector2D centerTexCoords, QVector2D sizeTexCoords )
109 {
110  mCenterTextureCoords->setValue( centerTexCoords );
111  mSizeTextureCoords->setValue( sizeTexCoords );
112 }
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:29
QgsPreviewQuadMaterial
Material component for QgsPreviewQuad object.
Definition: qgspreviewquad.h:33
QgsPreviewQuadMaterial::setViewPort
void setViewPort(QVector2D centerTexCoords, QVector2D sizeTexCoords)
Sets the view port of the quad.
Definition: qgspreviewquad.cpp:108
Qt3DCore
Definition: qgsabstract3drenderer.h:30
Qt3DRender
Definition: qgs3dmapscene.h:28
QgsPreviewQuad::setViewPort
void setViewPort(const QPointF &centerNDC, const QSizeF &size)
Sets where the quad will be located on the scene.
Definition: qgspreviewquad.cpp:69
QgsPreviewQuadMaterial::QgsPreviewQuadMaterial
QgsPreviewQuadMaterial(Qt3DRender::QAbstractTexture *texture, QVector< Qt3DRender::QParameter * > additionalShaderParameters=QVector< Qt3DRender::QParameter * >(), QNode *parent=nullptr)
Constructor.
Definition: qgspreviewquad.cpp:74