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