Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void BaseGeometryLoader::generateGeometry()
- {
- QByteArray bufferBytes;
- const int count = m_points.size();
- const quint32 elementSize = 3 + (hasTextureCoordinates() ? 2 : 0)
- + (hasNormals() ? 3 : 0)
- + (hasTangents() ? 4 : 0);
- const quint32 stride = elementSize * sizeof(float);
- bufferBytes.resize(stride * count);
- float *fptr = reinterpret_cast<float*>(bufferBytes.data());
- for (int index = 0; index < count; ++index) {
- *fptr++ = m_points.at(index).x();
- *fptr++ = m_points.at(index).y();
- *fptr++ = m_points.at(index).z();
- if (hasTextureCoordinates()) {
- *fptr++ = m_texCoords.at(index).x();
- *fptr++ = m_texCoords.at(index).y();
- }
- if (hasNormals()) {
- *fptr++ = m_normals.at(index).x();
- *fptr++ = m_normals.at(index).y();
- *fptr++ = m_normals.at(index).z();
- }
- if (hasTangents()) {
- *fptr++ = m_tangents.at(index).x();
- *fptr++ = m_tangents.at(index).y();
- *fptr++ = m_tangents.at(index).z();
- *fptr++ = m_tangents.at(index).w();
- }
- } // of buffer filling loop
- QBuffer *buf = new QBuffer();
- buf->setType(QBuffer::VertexBuffer);
- buf->setData(bufferBytes);
- if (m_geometry)
- qDebug(BaseGeometryLoaderLog, "Existing geometry instance getting overridden.");
- m_geometry = new QGeometry();
- QAttribute *positionAttribute = new QAttribute(buf, QAttribute::defaultPositionAttributeName(), QAttribute::Float, 3, count, 0, stride);
- m_geometry->addAttribute(positionAttribute);
- quint32 offset = sizeof(float) * 3;
- if (hasTextureCoordinates()) {
- QAttribute *texCoordAttribute = new QAttribute(buf, QAttribute::defaultTextureCoordinateAttributeName(), QAttribute::Float, 2, count, offset, stride);
- m_geometry->addAttribute(texCoordAttribute);
- offset += sizeof(float) * 2;
- }
- if (hasNormals()) {
- QAttribute *normalAttribute = new QAttribute(buf, QAttribute::defaultNormalAttributeName(), QAttribute::Float, 3, count, offset, stride);
- m_geometry->addAttribute(normalAttribute);
- offset += sizeof(float) * 3;
- }
- if (hasTangents()) {
- QAttribute *tangentAttribute = new QAttribute(buf, QAttribute::defaultTangentAttributeName(),QAttribute::Float, 4, count, offset, stride);
- m_geometry->addAttribute(tangentAttribute);
- offset += sizeof(float) * 4;
- }
- QByteArray indexBytes;
- QAttribute::VertexBaseType ty;
- if (m_indices.size() < 65536) {
- // we can use USHORT
- ty = QAttribute::UnsignedShort;
- indexBytes.resize(m_indices.size() * sizeof(quint16));
- quint16 *usptr = reinterpret_cast<quint16*>(indexBytes.data());
- for (int i = 0; i < m_indices.size(); ++i)
- *usptr++ = static_cast<quint16>(m_indices.at(i));
- } else {
- // use UINT - no conversion needed, but let's ensure int is 32-bit!
- ty = QAttribute::UnsignedInt;
- Q_ASSERT(sizeof(int) == sizeof(quint32));
- indexBytes.resize(m_indices.size() * sizeof(quint32));
- memcpy(indexBytes.data(), reinterpret_cast<const char*>(m_indices.data()), indexBytes.size());
- }
- QBuffer *indexBuffer = new QBuffer();
- indexBuffer->setType(QBuffer::IndexBuffer);
- indexBuffer->setData(indexBytes);
- QAttribute *indexAttribute = new QAttribute(indexBuffer, ty, 1, m_indices.size());
- indexAttribute->setAttributeType(QAttribute::IndexAttribute);
- m_geometry->addAttribute(indexAttribute);
- }
Advertisement
Add Comment
Please, Sign In to add comment