void MeshPlyReaderDelegate::processVertex(const std::string& elementName)
{
	MeshType::VertexType vertex(SurgSim::Math::Vector3d(m_vertexData.x, m_vertexData.y, m_vertexData.z));

	if (hasTextureCoordinates())
	{
		vertex.data.texture.setValue(SurgSim::Math::Vector2d(m_vertexData.s, m_vertexData.t));
	}

	m_mesh->addVertex(vertex);
}
Esempio n. 2
0
void ModelLoader::genVBO(GLfloat* vbo, unsigned int meshId, bool normals, bool texcoords) const
{
    if (!_hasScene) {
        return;
    }

    if (! hasTextureCoordinates(meshId) && texcoords) {
        std::cerr << "Model does not have texture coordinates "
            << "although they are requested." << std::endl;
        return;
    }

    // calculate stride
    unsigned int stride = 4;
    if (normals) {
        stride += 4;
    }
    if (texcoords) {
        stride += 4;
    }

    // copy array and add 4th component
    int unsigned n;
    for (unsigned int i = 0; i < scene->mMeshes[meshId]->mNumVertices; ++i) {
        //std::cout << "read vertex number " << i << std::endl;
        n = 0;
        vbo[i*stride+n++] = scene->mMeshes[meshId]->mVertices[i].x;
        vbo[i*stride+n++] = scene->mMeshes[meshId]->mVertices[i].y;
        vbo[i*stride+n++] = scene->mMeshes[meshId]->mVertices[i].z;
        vbo[i*stride+n++] = 1.0f;

        if (normals) {
            vbo[i*stride+n++] = scene->mMeshes[meshId]->mNormals[i].x;
            vbo[i*stride+n++] = scene->mMeshes[meshId]->mNormals[i].y;
            vbo[i*stride+n++] = scene->mMeshes[meshId]->mNormals[i].z;
            vbo[i*stride+n++] = 1.0f;
        }

        if (texcoords) {
            /* TODO: auto-selects first UV(W) channel */
            vbo[i*stride+n++] = (scene->mMeshes[meshId]->mTextureCoords[0])[i].x;
            vbo[i*stride+n++] = (scene->mMeshes[meshId]->mTextureCoords[0])[i].y;
            vbo[i*stride+n++] = (scene->mMeshes[meshId]->mTextureCoords[0])[i].z;
            //std::cout << "derp: " << scene->mMeshes[meshId]->mTextureCoords[i]->z << std::endl;
            vbo[i*stride+n++] = 1.0f;
        }
    }
}
Esempio n. 3
0
QGeometry *ObjLoader::geometry() const
{
    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(QBuffer::VertexBuffer));
    buf->setData(bufferBytes);

    QGeometry *geometry = new QGeometry();
    QAttribute *positionAttribute = new QAttribute(buf, QAttribute::defaultPositionAttributeName(), QAttribute::Float, 3, count, 0, stride);
    geometry->addAttribute(positionAttribute);
    quint32 offset = sizeof(float) * 3;

    if (hasTextureCoordinates()) {
        QAttribute *texCoordAttribute = new QAttribute(buf, QAttribute::defaultTextureCoordinateAttributeName(),  QAttribute::Float, 2, count, offset, stride);
        geometry->addAttribute(texCoordAttribute);
        offset += sizeof(float) * 2;
    }

    if (hasNormals()) {
        QAttribute *normalAttribute = new QAttribute(buf, QAttribute::defaultNormalAttributeName(), QAttribute::Float, 3, count, offset, stride);
        geometry->addAttribute(normalAttribute);
        offset += sizeof(float) * 3;
    }

    if (hasTangents()) {
        QAttribute *tangentAttribute = new QAttribute(buf, QAttribute::defaultTangentAttributeName(),QAttribute::Float, 4, count, offset, stride);
        geometry->addAttribute(tangentAttribute);
        offset += sizeof(float) * 4;
    }

    QByteArray indexBytes;
    QAttribute::DataType 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(QBuffer::IndexBuffer));
    indexBuffer->setData(indexBytes);
    QAttribute *indexAttribute = new QAttribute(indexBuffer, ty, 1, m_indices.size());
    indexAttribute->setAttributeType(QAttribute::IndexAttribute);
    geometry->addAttribute(indexAttribute);

    return geometry;
}