could anyone told me how to render a stl using qt3d?



  • 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);
    return geometry;



  • @jimfar said in could anyone told me how to render a stl using qt3d?:

    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;
    charpdata = (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(vsize2sizeof(Vector3));
    Vector3vertex = &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);
    return geometry;

    this is sippet for create a geometry,and now it can display a stl object,,but the object show all black...it seems the normal is loaded wrongly..could anyone told me how to render stl file


Log in to reply
 

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