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>
30 const QPointF ¢erTexCoords,
const QSizeF &sizeTexCoords,
31 QVector<Qt3DRender::QParameter *> additionalShaderParameters,
32 Qt3DCore::QEntity *parent )
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 };
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 );
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 );
54 geom->addAttribute( positionAttribute );
56 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
57 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
58 renderer->setGeometry( geom );
60 addComponent( renderer );
64 addComponent( mMaterial );
71 mMaterial->
setViewPort( QVector2D( centerTexCoords.x(), centerTexCoords.y() ), QVector2D( sizeTexCoords.width(), sizeTexCoords.height() ) );
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 );
85 mEffect =
new Qt3DRender::QEffect;
87 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
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 );
95 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass;
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 );
102 technique->addRenderPass( renderPass );
104 mEffect->addTechnique( technique );
105 setEffect( mEffect );
110 mCenterTextureCoords->setValue( centerTexCoords );
111 mSizeTextureCoords->setValue( sizeTexCoords );
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 ¢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)