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