Skip to content

Commit

Permalink
save obj with UVs
Browse files Browse the repository at this point in the history
  • Loading branch information
mlivesu committed Mar 7, 2024
1 parent 7657074 commit caf8e6c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
38 changes: 38 additions & 0 deletions include/cinolib/io/write_OBJ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,42 @@ void write_OBJ(const char *filename,
fclose(f_mtl);
}

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CINO_INLINE
void write_OBJ(const char * filename,
const std::vector<double> & xyz,
const std::vector<double> & uv,
const std::vector<uint> & tri)
{
setlocale(LC_NUMERIC, "en_US.UTF-8"); // makes sure "." is the decimal separator

FILE *fp = fopen(filename, "w");

if(!fp)
{
std::cerr << "ERROR : " << __FILE__ << ", line " << __LINE__ << " : save_OBJ() : couldn't open input file " << filename << std::endl;
exit(-1);
}

for(size_t i=0; i<xyz.size(); i+=3)
{
// http://stackoverflow.com/questions/16839658/printf-width-specifier-to-maintain-precision-of-floating-point-value
//
fprintf(fp, "v %.17g %.17g %.17g\n", xyz[i], xyz[i+1], xyz[i+2]);
}

for(size_t i=0; i<uv.size(); i+=2)
{
fprintf(fp, "vt %.17g %.17g\n", uv[i], uv[i+1]);
}

for(size_t i=0; i<tri.size(); i+=3)
{
fprintf(fp, "f %d %d %d\n", tri[i] + 1, tri[i+1] + 1, tri[i+2] + 1);
}

fclose(fp);
}

}
8 changes: 8 additions & 0 deletions include/cinolib/io/write_OBJ.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ void write_OBJ(const char * filename,
const std::vector<std::vector<uint>> & poly,
const std::vector<int> & labels);

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CINO_INLINE
void write_OBJ(const char * filename,
const std::vector<double> & xyz,
const std::vector<double> & uv,
const std::vector<uint> & tri);

}

#ifndef CINO_STATIC_LIB
Expand Down

0 comments on commit caf8e6c

Please sign in to comment.