Example #1
0
Mesh::Mesh(Point3 *vertices_, Vector3 *vertexNormals_,
           int nI_, int nJ_, bool wrapI_, bool wrapJ_)
    : nI(nI_), nJ(nJ_), wrapI(wrapI_), wrapJ(wrapJ_)
{
    nVertices = nI * nJ;
    vertices = new Point3[nVertices];
    vertexNormals = new Vector3[nVertices];
    for (int i = 0; i < nVertices; i++) {
        vertices[i] = vertices_[i];
        vertexNormals[i] = vertexNormals_[i];
    }

    // This enforces our requirement for distinct mesh points and thus
    // prevents later trouble.
    assert(pointsAreDistinct());

    createVertexIndices();
    createFaceNormals();

    allocateBuffers();
    //
    // Since we're handling transforms in the vertex shader, we only
    // need to download the buffers once, here in the constructor,
    // rather than in Mesh::render().
    //
    updateBuffers();
}
Example #2
0
bool OBJLoader::load(std::istream& istream)
{
    createGroup(bRenderer::DEFAULT_GROUP_NAME());
    obj::obj_parser obj_parser(_flags);
    
    std::string ifilename;
    obj_parser.info_callback(boost::bind(&OBJLoader::info_callback, this, ifilename, _1, _2));
    obj_parser.warning_callback(boost::bind(&OBJLoader::warning_callback, this, ifilename, _1, _2));
    obj_parser.error_callback(boost::bind(&OBJLoader::error_callback, this, ifilename, _1, _2));
    
    obj_parser.geometric_vertex_callback(boost::bind(&OBJLoader::geometric_vertex_callback, this, _1, _2, _3));
    obj_parser.texture_vertex_callback(boost::bind(&OBJLoader::texture_vertex_callback, this, _1, _2));
    obj_parser.vertex_normal_callback(boost::bind(&OBJLoader::vertex_normal_callback, this, _1, _2, _3));
    obj_parser.face_callbacks(
                              boost::bind(&OBJLoader::triangular_face_geometric_vertices_callback, this, _1, _2, _3),
                              boost::bind(&OBJLoader::triangular_face_geometric_vertices_texture_vertices_callback, this, _1, _2, _3),
                              boost::bind(&OBJLoader::triangular_face_geometric_vertices_vertex_normals_callback, this, _1, _2, _3),
                              boost::bind(&OBJLoader::triangular_face_geometric_vertices_texture_vertices_vertex_normals_callback, this, _1, _2, _3),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_begin_callback, this, _1, _2, _3),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_vertex_callback, this, _1),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_end_callback, this),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_texture_vertices_begin_callback, this, _1, _2, _3),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_texture_vertices_vertex_callback, this, _1),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_texture_vertices_end_callback, this),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_vertex_normals_begin_callback, this, _1, _2, _3),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_vertex_normals_vertex_callback, this, _1),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_vertex_normals_end_callback, this),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_texture_vertices_vertex_normals_begin_callback, this, _1, _2, _3),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_texture_vertices_vertex_normals_vertex_callback, this, _1),
                              boost::bind(&OBJLoader::polygonal_face_geometric_vertices_texture_vertices_vertex_normals_end_callback, this)
                              );
    obj_parser.group_name_callback(boost::bind(&OBJLoader::group_name_callback, this, _1));
    obj_parser.object_name_callback(boost::bind(&OBJLoader::object_name_callback, this, _1));
    obj_parser.material_library_callback(boost::bind(&OBJLoader::material_library_callback, this, _1));
    obj_parser.material_name_callback(boost::bind(&OBJLoader::material_name_callback, this, _1));
    obj_parser.comment_callback(boost::bind(&OBJLoader::comment_callback, this, _1));
    
    bool ret = obj_parser.parse(istream);
    
    // if there is not only vertex- but also face data, update normals accordingly
    if (_faces.size() > 0)
    {
        createFaceNormals();
        createVertexNormals();
        
        for (auto i = _groups.begin(); i != _groups.end(); ++i)
        {
            for (auto j = i->second->vboIndices.begin(); j != i->second->vboIndices.end(); ++j)
            {
                auto idx = i->second->indices[*j].vertexIndex;
                
                vmml::Vector3f cNormal = _vertices[idx].normal;
                Vector3 &normal = i->second->vboVertices[*j].normal;
                normal.x = cNormal.x();
                normal.y = cNormal.y();
                normal.z = cNormal.z();
                
                vmml::Vector3f cTangent = _vertices[idx].tangent;
                Vector3 &tangent = i->second->vboVertices[*j].tangent;
                tangent.x = cTangent.x();
                tangent.y = cTangent.y();
                tangent.z = cTangent.z();
                
                vmml::Vector3f cBitangent = _vertices[idx].bitangent;
                Vector3 &bitangent = i->second->vboVertices[*j].bitangent;
                bitangent.x = cBitangent.x();
                bitangent.y = cBitangent.y();
                bitangent.z = cBitangent.z();
            }
        }
    }
    
    // delete empty groups
    auto i = _groups.begin();
    while (i != _groups.end())
    {
        if (i->second->vboIndices.size() == 0 || i->second->vboVertices.size() == 0)
            _groups.erase(i++);
        else
            ++i;
    }
    
    return ret;
}