Exemple #1
0
bool Mesh::load(const std::string& filename) {
    std::shared_ptr<ObjMesh> mesh = nullptr;

    if ( !LoadObjMesh(filename, mesh) ) return false;

    if ( mesh->faces.size() > 0 ) {
        if ( mesh->faces[0].type != TRIANGLE ) {
            std::cerr << "[Mesh:load] Error: Only triangle-face Obj files supported." << std::endl;
            return false;
        }
    }

    this->name = mesh->name;
    std::vector<Vector3f> normals;
    std::vector<Vector4f> tangents;
    std::vector<unsigned int> indices;
    std::vector<unsigned int> textureIndices;
    std::vector<unsigned int> normalIndices;

    //--------------------------------------------------------------------------
    // Copy the face indices from the *.obj file to this mesh.
    //--------------------------------------------------------------------------
    indices.resize(mesh->faces.size() * TRIANGLE_EDGE_COUNT);
    normalIndices.resize(mesh->faces.size() * TRIANGLE_EDGE_COUNT);
    textureIndices.resize(mesh->faces.size() * TRIANGLE_EDGE_COUNT);
    unsigned int index = 0;
    for ( unsigned int i = 0; i < mesh->faces.size(); i++ ) {
        indices[index] = static_cast<unsigned int>(mesh->faces[i].vertexIndices[A]);
        indices[index + 1] = static_cast<unsigned int>(mesh->faces[i].vertexIndices[B]);
        indices[index + 2] = static_cast<unsigned int>(mesh->faces[i].vertexIndices[C]);

        normalIndices[index] = static_cast<unsigned int>(mesh->faces[i].normalIndices[A]);
        normalIndices[index + 1] = static_cast<unsigned int>(mesh->faces[i].normalIndices[B]);
        normalIndices[index + 2] = static_cast<unsigned int>(mesh->faces[i].normalIndices[C]);

        textureIndices[index] = static_cast<unsigned int>(mesh->faces[i].textureIndices[A]);
        textureIndices[index + 1] = static_cast<unsigned int>(mesh->faces[i].textureIndices[B]);
        textureIndices[index + 2] = static_cast<unsigned int>(mesh->faces[i].textureIndices[C]);

        index += TRIANGLE_EDGE_COUNT;
    }

    //--------------------------------------------------------------------------
    // Calcualte the vertex normals, tangents, and face normals.
    //--------------------------------------------------------------------------
    //CalculateNormals(indices, mesh->vertices, normals);
    normals.resize(mesh->normals.size());
    for ( unsigned int i = 0; i < mesh->normals.size(); i++ ) {
        normals[i] = mesh->normals[i];
    }
    Decompress(indices, normalIndices, textureIndices, mesh->vertices, normals, mesh->textureCoordinates, tangents, this->vertices, this->faces);
    CalculateTangents(this->vertices, this->faces);
    this->constructOnGPU();
    return true;
}
Exemple #2
0
void GuiFileDialog::ShowPreview()
{
  // TODO Look up preview type associated with extension
  if (GetFileExt(lastPath) == "obj")
  {
    // TODO Load mesh - don't use ResourceManager
    ObjMesh* mesh = LoadObjMesh(lastPath);
    if (mesh)
    {
      GuiObjView* gov = dynamic_cast<GuiObjView*>(GetElementByName("fd-obj-view"));
      Assert(gov);
      gov->SetObjMesh(mesh);
    }
  }
}