Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using Qt3DRender::QAttribute

Using Qt3DRender::QAttribute

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 396 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    grsabourin
    wrote on last edited by
    #1

    Hi,

    I would like to render a 3D model from an STL file inside a widget in my GUI. I have trouble using the QAttribute. I have used one of the QT3D example rendering multi color pyramid as an example. My issue is probably related to the fact that all my buffers do not have the right size according the number of vertices and faces. This how I use QAttribute in my code

    // Root entity
    Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
    
    // background color
    //view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x575757)));
    
    // Camera
    Qt3DRender::QCamera *cameraEntity = m_view->camera();
    
    cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f / 9.0f, 0.1f, 1000.0f);
    cameraEntity->setPosition(QVector3D(0, 0, -40.0f));
    cameraEntity->setUpVector(QVector3D(0, 1, 0));
    cameraEntity->setViewCenter(QVector3D(0, 0, 0));
    
    // For camera controls
    Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
    camController->setCamera(cameraEntity);
    
    // Material
    Qt3DRender::QMaterial *material = new Qt3DExtras::QPerVertexColorMaterial(rootEntity);
    
    // Torus
    Qt3DCore::QEntity *meshEntity = new Qt3DCore::QEntity(rootEntity);
    
    // Transform
    Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
    transform->setScale(8.0f);
    
    // Create the mesh to render
    Qt3DRender::QGeometryRenderer *meshRenderer = new Qt3DRender::QGeometryRenderer;
    Qt3DRender::QGeometry *meshGeometry = new Qt3DRender::QGeometry(meshRenderer);
    
    Qt3DRender::QBuffer *vertexDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, meshGeometry);
    Qt3DRender::QBuffer *indexDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::IndexBuffer, meshGeometry);
    
    QByteArray vertexBufferData;
    vertexBufferData.resize (  numberOfVertices * (3 + 3 + 3) * sizeof(float) );
    
    // Faces Indices
    QByteArray indexBufferData;
    indexBufferData.resize( numberOfFaces * 3 * sizeof(ushort));
    ushort *rawIndexArray = reinterpret_cast<ushort *>(indexBufferData.data());
    
    // Colors
    QVector3D red(1.0f, 0.0f, 0.0f);
    
    
       // Filling the buffer from the mesh tructure
    QVector<QVector3D> vertices;
    vertices.reserve( numberOfVertices * 3);
    OM_Mesh::FaceIter f_it;
    size_t faceIndex = 0;
    for (f_it = m_mesh.faces_begin(); f_it != m_mesh.faces_end(); ++f_it)
    {
    	rawIndexArray[faceIndex] = faceIndex;
    	++faceIndex;
    
    	OM_Mesh::FaceHandle fh = m_mesh.face_handle(f_it->idx());
    	OM_Mesh::ConstFaceVertexIter fv_it;
    	size_t itVertex = 0;
    	for (fv_it = m_mesh.cfv_begin(fh); fv_it != m_mesh.cfv_end(fh); ++fv_it, ++itVertex)
    	{
    		OM_Mesh::Point vNTmp = m_mesh.normal(fv_it);
    		QVector3D vertexNormalTmp( vNTmp[0], vNTmp[1], vNTmp[2]);
    
    		OM_Mesh::Point pTmp = m_mesh.point(fv_it);
    		QVector3D vertexPointTmp  (pTmp[0], pTmp[1], pTmp[2]);
    
    		vertices.push_back ( vertexPointTmp);
    		vertices.push_back(vertexNormalTmp);
    		vertices.push_back(red);
    	}
    }
    
    float *rawVertexArray = reinterpret_cast<float *>(vertexBufferData.data());
    size_t idx = 0;
    for (const QVector3D &v : vertices)
    {
    	rawVertexArray[idx++] = v.x();
    	rawVertexArray[idx++] = v.y();
    	rawVertexArray[idx++] = v.z();
    }
    
    vertexDataBuffer->setData(vertexBufferData);
    indexDataBuffer->setData(indexBufferData);
    
    // Attributes
    // Attributes
    Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute();
    positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    positionAttribute->setBuffer(vertexDataBuffer);
    positionAttribute->setDataType(Qt3DRender::QAttribute::Float);
    positionAttribute->setDataSize(3);
    positionAttribute->setByteOffset(0);
    positionAttribute->setByteStride(9 * sizeof(float));
    positionAttribute->setCount( numberOfVertices );
    positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
    
    Qt3DRender::QAttribute *normalAttribute = new Qt3DRender::QAttribute();
    normalAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    normalAttribute->setBuffer(vertexDataBuffer);
    normalAttribute->setDataType(Qt3DRender::QAttribute::Float);
    normalAttribute->setDataSize(3);
    normalAttribute->setByteOffset(3 * sizeof(float));
    normalAttribute->setByteStride(9 * sizeof(float));
    normalAttribute->setCount( numberOfVertices );
    normalAttribute->setName(Qt3DRender::QAttribute::defaultNormalAttributeName());
    
    Qt3DRender::QAttribute *colorAttribute = new Qt3DRender::QAttribute();
    colorAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    colorAttribute->setBuffer(vertexDataBuffer);
    colorAttribute->setDataType(Qt3DRender::QAttribute::Float);
    colorAttribute->setDataSize(3);
    colorAttribute->setByteOffset(6 * sizeof(float));
    colorAttribute->setByteStride(9 * sizeof(float));
    colorAttribute->setCount( numberOfVertices );
    colorAttribute->setName(Qt3DRender::QAttribute::defaultColorAttributeName());
    
    
    Qt3DRender::QAttribute *indexAttribute = new Qt3DRender::QAttribute();
    indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
    indexAttribute->setBuffer(indexDataBuffer);
    indexAttribute->setDataType(Qt3DRender::QAttribute::UnsignedShort);
    indexAttribute->setDataSize(1);
    indexAttribute->setByteOffset(0);
    indexAttribute->setByteStride(0);
    indexAttribute->setCount(  numberOfFaces );
    
    meshGeometry->addAttribute(positionAttribute);
    meshGeometry->addAttribute(normalAttribute);
    meshGeometry->addAttribute(colorAttribute);
    meshGeometry->addAttribute(indexAttribute);
    
    meshRenderer->setInstanceCount(1);
    meshRenderer->setFirstVertex(0);
    meshRenderer->setFirstInstance(0);
    meshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
    meshRenderer->setGeometry(meshGeometry);
    meshRenderer->setVertexCount(  numberOfVertices );
    
    
    meshEntity->addComponent(meshRenderer);
    meshEntity->addComponent(transform);
    meshEntity->addComponent(material);
    
    m_view->setRootEntity(rootEntity);
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved