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. QT3D how to render stl file

QT3D how to render stl file

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.7k Views
  • 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.
  • J Offline
    J Offline
    jimfar
    wrote on 28 Oct 2017, 08:47 last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jimfar
      wrote on 28 Oct 2017, 13:41 last edited by
      #2

      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

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on 28 Oct 2017, 17:45 last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jimfar
          wrote on 29 Oct 2017, 07:04 last edited by
          #4

          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

          1 Reply Last reply
          0

          3/4

          28 Oct 2017, 17:45

          • Login

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