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>
29 const QPointF ¢erNDC,
const QSizeF &size,
30 QVector<Qt3DRender::QParameter *> additionalShaderParameters,
31 Qt3DCore::QEntity *parent )
34 setObjectName(
"Preview Quad" );
35 Qt3DRender::QGeometry *geom =
new Qt3DRender::QGeometry;
36 Qt3DRender::QAttribute *positionAttribute =
new Qt3DRender::QAttribute;
37 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 QByteArray vertexArr( (
const char * ) vert.constData(), vert.size() *
sizeof(
float ) );
40 Qt3DRender::QBuffer *vertexBuffer =
nullptr;
41 #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
42 vertexBuffer =
new Qt3DRender::QBuffer( Qt3DRender::QBuffer::VertexBuffer,
this );
44 vertexBuffer =
new Qt3DRender::QBuffer(
this );
46 vertexBuffer->setData( vertexArr );
48 positionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
49 positionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
50 positionAttribute->setVertexSize( 3 );
51 positionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
52 positionAttribute->setBuffer( vertexBuffer );
53 positionAttribute->setByteOffset( 0 );
54 positionAttribute->setByteStride( 3 *
sizeof(
float ) );
55 positionAttribute->setCount( 6 );
57 geom->addAttribute( positionAttribute );
59 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
60 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
61 renderer->setGeometry( geom );
63 addComponent( renderer );
65 QMatrix4x4 modelMatrix;
66 modelMatrix.setToIdentity();
67 modelMatrix.translate( centerNDC.x(), centerNDC.y() );
68 modelMatrix.scale( size.width(), size.height() );
71 addComponent( mMaterial );
76 QMatrix4x4 modelMatrix;
77 modelMatrix.setToIdentity();
78 modelMatrix.translate( centerNDC.x(), centerNDC.y() );
79 modelMatrix.scale( size.width(), size.height() );
86 mTextureParameter =
new Qt3DRender::QParameter(
"previewTexture", texture );
87 mTextureTransformParameter =
new Qt3DRender::QParameter(
"modelMatrix", QVariant::fromValue( modelMatrix ) );
88 addParameter( mTextureParameter );
89 addParameter( mTextureTransformParameter );
90 for ( Qt3DRender::QParameter *parameter : additionalShaderParameters ) addParameter( parameter );
92 mEffect =
new Qt3DRender::QEffect;
94 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
96 Qt3DRender::QGraphicsApiFilter *graphicsApiFilter = technique->graphicsApiFilter();
97 graphicsApiFilter->setApi( Qt3DRender::QGraphicsApiFilter::Api::OpenGL );
98 graphicsApiFilter->setProfile( Qt3DRender::QGraphicsApiFilter::OpenGLProfile::CoreProfile );
99 graphicsApiFilter->setMajorVersion( 1 );
100 graphicsApiFilter->setMinorVersion( 5 );
102 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass;
104 Qt3DRender::QShaderProgram *shader =
new Qt3DRender::QShaderProgram;
105 shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"qrc:/shaders/preview.vert" ) ) );
106 shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"qrc:/shaders/preview.frag" ) ) );
107 renderPass->setShaderProgram( shader );
109 technique->addRenderPass( renderPass );
111 mEffect->addTechnique( technique );
112 setEffect( mEffect );
117 mTextureTransformParameter->setValue( modelMatrix );
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 ¢erNDC, const QSizeF &size)
Sets where the quad will be located on the scene.
QgsPreviewQuad(Qt3DRender::QAbstractTexture *texture, const QPointF ¢erNDC, 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)