Exemple #1
0
int main(int argc, char *argv[])
{
	std::string texture_filename("granite.dds");
	std::string object_filename("car.object");
	if (argc > 2) texture_filename = argv[2];
	if (argc > 1) object_filename = argv[1];
	if (!app.Init()) exit(8);
	if (!app.InitShaders()) exit(9);
	if (!app.LoadTexture(texture_filename.c_str())) exit(10);
	if (!app.LoadObject(object_filename.c_str())) exit(11);
	app.RunRender();
	return 0;
}
Exemple #2
0
/**
 * @brief Writes an obj file of the given Mesh, including texture coordinates,
 * and an mtl file containing a reference to the isomap.
 *
 * The obj will contain texture coordinates for the mesh, and the
 * mtl file will link to a file named <filename>.isomap.png.
 * Note that the texture (isomap) has to be saved separately.
 *
 * @param[in] mesh The mesh to save as obj.
 * @param[in] filename Output filename.
 */
inline void write_textured_obj(Mesh mesh, std::string filename)
{
    assert((mesh.vertices.size() == mesh.colors.size() || mesh.colors.empty()) && !mesh.texcoords.empty());

    std::ofstream obj_file(filename);

    boost::filesystem::path mtl_filename(filename);
    mtl_filename.replace_extension(".mtl");

    obj_file << "mtllib " << mtl_filename.filename().string() << std::endl; // first line of the obj file

    // same as in write_obj():
    if (mesh.colors.empty()) {
        for (std::size_t i = 0; i < mesh.vertices.size(); ++i) {
            obj_file << "v " << mesh.vertices[i][0] << " " << mesh.vertices[i][1] << " " << mesh.vertices[i][2] << " " << std::endl;
        }
    }
    else {
        for (std::size_t i = 0; i < mesh.vertices.size(); ++i) {
            obj_file << "v " << mesh.vertices[i][0] << " " << mesh.vertices[i][1] << " " << mesh.vertices[i][2] << " " << mesh.colors[i][0] << " " << mesh.colors[i][1] << " " << mesh.colors[i][2] << " " << std::endl;
        }
    }
    // end

    for (std::size_t i = 0; i < mesh.texcoords.size(); ++i) {
        obj_file << "vt " << mesh.texcoords[i][0] << " " << 1.0f - mesh.texcoords[i][1] << std::endl;
        // We invert y because Meshlab's uv origin (0, 0) is on the bottom-left
    }

    obj_file << "usemtl FaceTexture" << std::endl; // the name of our texture (material) will be 'FaceTexture'

    for (auto&& v : mesh.tvi) {
        // This assumes mesh.texcoords.size() == mesh.vertices.size(). The texture indices could theoretically be different (for example in the cube-mapped 3D scan)
        // Add one because obj starts counting triangle indices at 1
        obj_file << "f " << v[0] + 1 << "/" << v[0] + 1 << " " << v[1] + 1 << "/" << v[1] + 1 << " " << v[2] + 1 << "/" << v[2] + 1 << std::endl;
    }

    std::ofstream mtl_file(mtl_filename.string());
    boost::filesystem::path texture_filename(filename);
    texture_filename.replace_extension(".isomap.png");

    mtl_file << "newmtl FaceTexture" << std::endl;
    mtl_file << "map_Kd " << texture_filename.filename().string() << std::endl;

    return;
};