Qt3D Simple Example



  • Hi,

    I'm using Qt5.8 and Win10. I'm working on a solution to display a pointcloud in a widget based application using qt3D.

    I'm new to Qt3D, but not to Qt framework. I can't have a point displayed, and I think that I'm missing something somewhere.
    (For the following code, I'm just showing the important pieces).

    Is the following process the right one ? It seems right, but I obvisouly am missing something...

    1. I create a very basic data sample

      // /////////////////////////////////////////////////////////////////////////
      QByteArray *ba_positions = new QByteArray;
      QDataStream stream_position(ba_positions, QIODevice::WriteOnly);
      stream_position << 10.0f << 10.0f << 10.0f;
      stream_position << 9.0f << 9.0f << 9.0f;
      stream_position << 8.0f << 8.0f << 8.0f;
      stream_position << 7.0f << 7.0f << 7.0f;
      stream_position << 6.0f << 6.0f << 6.0f;
      stream_position << 5.0f << 5.0f << 5.0f;
      d->vertexBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer);
      d->vertexBuffer->setData(*ba_positions);
      
      // /////////////////////////////////////////////////////////////////////////
      QByteArray *ba_colors = new QByteArray;
      QDataStream stream_colors(ba_colors, QIODevice::WriteOnly);
      stream_colors << 1.0f << 1.0f << 1.0f;
      stream_colors << 1.0f << 1.0f << 1.0f;
      stream_colors << 1.0f << 1.0f << 1.0f;
      stream_colors << 1.0f << 1.0f << 1.0f;
      stream_colors << 1.0f << 1.0f << 1.0f;
      stream_colors << 1.0f << 1.0f << 1.0f;
      
      d->colorBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer);
      d->colorBuffer->setData(*ba_colors);
      
      // /////////////////////////////////////////////////////////////////////////
      QByteArray *ba_normals = new QByteArray;
      QDataStream stream_normals(ba_normals, QIODevice::WriteOnly);
      stream_normals << 0.0f << 1.0f << 0.0f;
      stream_normals << 0.0f << 1.0f << 0.0f;
      stream_normals << 0.0f << 1.0f << 0.0f;
      stream_normals << 0.0f << 1.0f << 0.0f;
      stream_normals << 0.0f << 1.0f << 0.0f;
      stream_normals << 0.0f << 1.0f << 0.0f;
      
      d->normalBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer);
      d->normalBuffer->setData(*ba_normals);
      
      
    2. I create a geometry renderer

           	d->geometry = new Qt3DRender::QGeometry;
    	d->renderer = new Qt3DRender::QGeometryRenderer;
            Qt3DRender::QAttribute  *attributePosition = new Qt3DRender::QAttribute(d->geometry);
    	attributePosition->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
    	attributePosition->setDataType(Qt3DRender::QAttribute::Float);
    	attributePosition->setDataSize(3);
    	attributePosition->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    	attributePosition->setBuffer(d->cloud->vertices());
    	attributePosition->setByteOffset(0);
    	attributePosition->setByteStride(0);
    	attributePosition->setCount(6);
    
    	Qt3DRender::QAttribute  *attributeColor = new Qt3DRender::QAttribute(d->geometry);
    	attributeColor->setName(Qt3DRender::QAttribute::defaultColorAttributeName());
    	attributeColor->setDataType(Qt3DRender::QAttribute::Float);
    	attributeColor->setDataSize(3);
    	attributeColor->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    	attributeColor->setBuffer(d->cloud->colors());
    	attributeColor->setByteStride(0);
    	attributeColor->setByteOffset(0);
    	attributeColor->setCount(6);
    
    	Qt3DRender::QAttribute  *attributeNormal = new Qt3DRender::QAttribute(d->geometry);
    	attributeNormal->setName(Qt3DRender::QAttribute::defaultNormalAttributeName());
    	attributeNormal->setDataType(Qt3DRender::QAttribute::Float);
    	attributeNormal->setDataSize(3);
    	attributeNormal->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    	attributeNormal->setBuffer(d->cloud->normals());
    	attributeNormal->setByteStride(0);
    	attributeNormal->setByteOffset(0);
    	attributeNormal->setCount(6);
    
    	d->geometry->addAttribute(attributePosition);
    	d->geometry->addAttribute(attributeColor);
    	d->geometry->addAttribute(attributeNormal);
    	d->geometry->setBoundingVolumePositionAttribute(attributePosition);
    
    	d->renderer = new Qt3DRender::QGeometryRenderer;
    	d->renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Points);
    	d->renderer->setGeometry(d->geometry);
    
    1. I create ascene to display my elements
          //  Window //////////////////////////////////////////////////////////////////
      d->window = new Qt3DExtras::Qt3DWindow;
          // d->window->defaultFrameGraph()->setClearColor(QColor(QRgb(0x332e2f)));
      d->window->defaultFrameGraph()->setClearColor(QColor(QRgb(0x000000)));
      d->windowContainer = QWidget::createWindowContainer(d->window);
      
      // Root entity
      d->rootEntity = new Qt3DCore::QEntity();
      
      d->window->setRootEntity(d->rootEntity);
      
    
    4) I create my element
    
        Qt3DCore::QEntity *cloudEntity = new Qt3DCore::QEntity(d->rootEntity);
    cloudEntity->setParent(d->rootEntity); 
    cloudEntity->addComponent(geometry->renderer());
    
    
    --------------------------------------------------------
    I have tried to add a material, and a transform to enhance my element : 
    
    //	// Cloud Material //////////////////////////////////////////////////////////
    	Qt3DCore::QEntity *cloudEntity = new Qt3DCore::QEntity(d->rootEntity);
    	cloudEntity->setParent(d->rootEntity);
    
    	Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram(cloudEntity);
    	shaderProgram->setFragmentShaderCode(Qt3DRender::QShaderProgram::loadSource( QUrl("qrc:/shaders/shaders/pointcloud.frag")));
    	shaderProgram->setVertexShaderCode(Qt3DRender::QShaderProgram::loadSource(QUrl("qrc:/shaders/shaders/pointcloud.vert")));
    
    	Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass(cloudEntity);
    	renderPass->setShaderProgram(shaderProgram);
    
    	Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique(cloudEntity);
    	technique->addRenderPass(renderPass);
    
    	Qt3DRender::QEffect *effect = new Qt3DRender::QEffect(cloudEntity);
    	effect->addTechnique(technique);
    
    	Qt3DRender::QParameter *parameter = new Qt3DRender::QParameter("pointSize", 1.0);
    
    	Qt3DRender::QMaterial *cloudMaterial = new Qt3DRender::QMaterial(cloudEntity);
    	cloudMaterial->setEffect(effect);
    	cloudMaterial->addParameter(parameter);
    
    	// Cloud Transform /////////////////////////////////////////////////////////
    	Qt3DCore::QTransform *cloudTransform = new Qt3DCore::QTransform(cloudEntity);
    	cloudTransform->setScale(2);
    	cloudTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0.0f, 1.0f, 0.0f), 25.0f));
    	cloudTransform->setTranslation(QVector3D(5,4,0));
    
    	// Cloud Entity ////////////////////////////////////////////////////////////
    	cloudEntity->addComponent(cloudTransform);
    	cloudEntity->addComponent(geometry->renderer());
    	cloudEntity->addComponent(cloudMaterial);

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.