osipyonok

GeometryLoader::generateGeometry

Dec 31st, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. void BaseGeometryLoader::generateGeometry()
  2. {
  3.     QByteArray bufferBytes;
  4.     const int count = m_points.size();
  5.     const quint32 elementSize = 3 + (hasTextureCoordinates() ? 2 : 0)
  6.             + (hasNormals() ? 3 : 0)
  7.             + (hasTangents() ? 4 : 0);
  8.     const quint32 stride = elementSize * sizeof(float);
  9.     bufferBytes.resize(stride * count);
  10.     float *fptr = reinterpret_cast<float*>(bufferBytes.data());
  11.  
  12.     for (int index = 0; index < count; ++index) {
  13.         *fptr++ = m_points.at(index).x();
  14.         *fptr++ = m_points.at(index).y();
  15.         *fptr++ = m_points.at(index).z();
  16.  
  17.         if (hasTextureCoordinates()) {
  18.             *fptr++ = m_texCoords.at(index).x();
  19.             *fptr++ = m_texCoords.at(index).y();
  20.         }
  21.  
  22.         if (hasNormals()) {
  23.             *fptr++ = m_normals.at(index).x();
  24.             *fptr++ = m_normals.at(index).y();
  25.             *fptr++ = m_normals.at(index).z();
  26.         }
  27.  
  28.         if (hasTangents()) {
  29.             *fptr++ = m_tangents.at(index).x();
  30.             *fptr++ = m_tangents.at(index).y();
  31.             *fptr++ = m_tangents.at(index).z();
  32.             *fptr++ = m_tangents.at(index).w();
  33.         }
  34.     } // of buffer filling loop
  35.  
  36.     QBuffer *buf = new QBuffer();
  37.     buf->setType(QBuffer::VertexBuffer);
  38.     buf->setData(bufferBytes);
  39.  
  40.     if (m_geometry)
  41.         qDebug(BaseGeometryLoaderLog, "Existing geometry instance getting overridden.");
  42.     m_geometry = new QGeometry();
  43.  
  44.     QAttribute *positionAttribute = new QAttribute(buf, QAttribute::defaultPositionAttributeName(), QAttribute::Float, 3, count, 0, stride);
  45.     m_geometry->addAttribute(positionAttribute);
  46.     quint32 offset = sizeof(float) * 3;
  47.  
  48.     if (hasTextureCoordinates()) {
  49.         QAttribute *texCoordAttribute = new QAttribute(buf, QAttribute::defaultTextureCoordinateAttributeName(),  QAttribute::Float, 2, count, offset, stride);
  50.         m_geometry->addAttribute(texCoordAttribute);
  51.         offset += sizeof(float) * 2;
  52.     }
  53.  
  54.     if (hasNormals()) {
  55.         QAttribute *normalAttribute = new QAttribute(buf, QAttribute::defaultNormalAttributeName(), QAttribute::Float, 3, count, offset, stride);
  56.         m_geometry->addAttribute(normalAttribute);
  57.         offset += sizeof(float) * 3;
  58.     }
  59.  
  60.     if (hasTangents()) {
  61.         QAttribute *tangentAttribute = new QAttribute(buf, QAttribute::defaultTangentAttributeName(),QAttribute::Float, 4, count, offset, stride);
  62.         m_geometry->addAttribute(tangentAttribute);
  63.         offset += sizeof(float) * 4;
  64.     }
  65.  
  66.     QByteArray indexBytes;
  67.     QAttribute::VertexBaseType ty;
  68.     if (m_indices.size() < 65536) {
  69.         // we can use USHORT
  70.         ty = QAttribute::UnsignedShort;
  71.         indexBytes.resize(m_indices.size() * sizeof(quint16));
  72.         quint16 *usptr = reinterpret_cast<quint16*>(indexBytes.data());
  73.         for (int i = 0; i < m_indices.size(); ++i)
  74.             *usptr++ = static_cast<quint16>(m_indices.at(i));
  75.     } else {
  76.         // use UINT - no conversion needed, but let's ensure int is 32-bit!
  77.         ty = QAttribute::UnsignedInt;
  78.         Q_ASSERT(sizeof(int) == sizeof(quint32));
  79.         indexBytes.resize(m_indices.size() * sizeof(quint32));
  80.         memcpy(indexBytes.data(), reinterpret_cast<const char*>(m_indices.data()), indexBytes.size());
  81.     }
  82.  
  83.     QBuffer *indexBuffer = new QBuffer();
  84.     indexBuffer->setType(QBuffer::IndexBuffer);
  85.     indexBuffer->setData(indexBytes);
  86.     QAttribute *indexAttribute = new QAttribute(indexBuffer, ty, 1, m_indices.size());
  87.     indexAttribute->setAttributeType(QAttribute::IndexAttribute);
  88.     m_geometry->addAttribute(indexAttribute);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment