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