Exemplo n.º 1
0
PolygonalDrawable * ObjIO::createPolygonalDrawable(
    const ObjObject & object
,   const ObjGroup & group)
{
    if(group.vis.empty())
        return NULL;

    // TODO: this should test if all consecutive offsets are equal 3.
    // The current expression could return false positives.
    if(group.vis.size() / 3 != group.foffs.size())
    {
        qCritical("Ignore polygon with num vertices != 3 (only triangles are supported).");
        return NULL;
    }

    const bool usesTexCoordIndices(!group.vtis.empty());
    const bool usesNormalIndices(!group.vnis.empty());

    PolygonalDrawable * drawable = new PolygonalDrawable();

    const GLuint size(static_cast<GLuint>(group.vis.size()));

    for(GLuint i = 0; i < size; ++i)
    {
        // add vertex and its new index based on obj index

		// TODO: make use of vertex reuse!

        drawable->indices().push_back(i);
        drawable->vertices().push_back(object.vs[group.vis[i]]);

        if(usesTexCoordIndices)
            drawable->texcs().push_back(object.vts[group.vtis[i]]);
        if(usesNormalIndices)
            drawable->normals().push_back(object.vns[group.vnis[i]]);
    }

    // TODO: support other modes here!
    drawable->setMode(GL_TRIANGLES);

    if(!usesNormalIndices)
        drawable->retrieveNormals();

    return drawable;
}
Exemplo n.º 2
0
PolygonalDrawable * AssimpLoader::parseMesh(const aiMesh & mesh) const
{
    auto geometry = std::make_shared<PolygonalGeometry>(m_registry);
    
    const bool usesNormalIndices(mesh.mNormals != NULL);
    
    for (unsigned int i = 0; i < mesh.mNumVertices; i++) {
        glm::vec3 vector(
                         mesh.mVertices[i].x, mesh.mVertices[i].y, mesh.mVertices[i].z
                         );
        geometry->setVertex(i, vector);
    }
    
    if (usesNormalIndices) {
        for (unsigned int i = 0; i < mesh.mNumVertices; i++) {
            glm::vec3 vector(
                             mesh.mNormals[i].x, mesh.mNormals[i].y, mesh.mNormals[i].z
                             );
            geometry->setNormal(i, vector);
        }
    }
    
    unsigned int currentIndex = 0;
    for (unsigned int i = 0; i < mesh.mNumFaces; i++) {
        if (mesh.mFaces[i].mNumIndices != 3)
            qCritical("Ignore polygon with num vertices != 3 (only triangles are supported).");
        else
            for (unsigned int j = 0; j < mesh.mFaces[i].mNumIndices; j++)
                geometry->setIndex(currentIndex++, mesh.mFaces[i].mIndices[j]);
    }
    
    if (!usesNormalIndices)
        geometry->retrieveNormals();
    
    PolygonalDrawable * drawable = new PolygonalDrawable(mesh.mName.C_Str());
    drawable->setGeometry(geometry);
    drawable->setMode(GL_TRIANGLES);
    return drawable;
}