QT3D how to render stl file



  • Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry;

    Qt3DRender::QAttribute *m_positionAttr = new Qt3DRender::QAttribute(geometry);
     Qt3DRender::QAttribute *m_NORAttr = new Qt3DRender::QAttribute(geometry);
    
    Qt3DRender::QBuffer *m_positionBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, geometry);
     
    Qt3DRender::QBuffer *m_NORBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, geometry);
    
    int vsize = p3dparser->mmeshs[0].mVertices.size();
    int isize = p3dparser->mmeshs[0].mIndices.size();
    //Configure the attributes access
    QByteArray colorData;
    int size1 = sizeof(QVector3D);
    //positionData.resize(vsize * 3 * sizeof(float));
    QByteArray mNormals;
    char*pdata = (char*)&p3dparser->mmeshs[0].mVertices[0][0];
     
    
    const int nVerts = vsize;
    const int size = nVerts * 3 * sizeof(float);
    QByteArray positionBytes,noramlbytes;
    positionBytes.resize(size);
    colorData.resize(size);
    vsize = nVerts;
    memcpy(positionBytes.data(), pdata, size);
    m_positionBuffer->setData(positionBytes);
    
    
    uint facenum = vsize /3;
    uint j = 0, i = 0;
    mNormals.resize(vsize*2*sizeof(Vector3));
    Vector3*vertex = &p3dparser->mmeshs[0].mVertices[0];
    Vector3*pnormal = (Vector3*)mNormals.data();
    Vector3 n;
    uint cnt = 0;
    for ( j = 0; j <vsize*2; )
    {
    	if (j % 6 == 0) {
    		n = CalcNormal(&vertex[cnt]);
    		//n=Normalize(Cross(vertex[i] - vertex[i + 1], vertex[i+2] - vertex[i + 1]));
    	}
    	 
    	pnormal[j++] = vertex[cnt++];
    	pnormal[j++] = n;
    	
    	
    }
     
    
    
    m_NORBuffer->setData(mNormals);
    const quint32 elementSize = 3 + 3  ;
    const quint32 stride = elementSize * sizeof(float);
    m_colorBuffer->setData(colorData);
    m_positionAttr->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
    m_positionAttr->setVertexBaseType(Qt3DRender::QAttribute::Float);
    m_positionAttr->setDataSize(3);
    m_positionAttr->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    m_positionAttr->setBuffer(m_NORBuffer);
    m_positionAttr->setCount(vsize);
    m_positionAttr->setByteStride(stride);
    //m_positionAttr->setProperty();
    
    m_NORAttr->setName(Qt3DRender::QAttribute::defaultNormalAttributeName());
    m_NORAttr->setVertexBaseType(Qt3DRender::QAttribute::Float);
    m_NORAttr->setDataSize(3);
    m_NORAttr->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    m_NORAttr->setBuffer(m_NORBuffer);
    m_NORAttr->setCount(vsize);
    m_NORAttr->setByteOffset( 3 * sizeof(Vector3));
    m_NORAttr->setByteStride(stride);
    

    geometry->addAttribute(m_positionAttr);
    // geometry->addAttribute(m_colorAttr);
    geometry->addAttribute(m_NORAttr);
    this is snipet for render stl file,it can display object but it is all black cant see the outline ,it seems the normal is wrong,can anyone help me ?



  • i cant use mesh->setsource("***.stl") for some reason ,,,i want to render custom mesh by vertics it can be composed a set of triangle


  • Moderators

    @jimfar said in QT3D how to render stl file:

    i cant use mesh->setsource("***.stl") for some reason

    Hi! What's the type of mesh?



  • the mesh is the same with stl ,3 vertices and 1 normal .. yep i already fix it .3 * sizeof(Vector3))--->modify as 3 * sizeof(float)); it is my careless..thanks ur attention


Log in to reply
 

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