OBJWriter: avoid precision loss when writing coordinates and normals.

This commit is contained in:
Martin Heistermann
2024-05-06 09:23:51 +02:00
parent 961acb9e3d
commit a305f82bee

View File

@@ -216,8 +216,6 @@ _OBJWriter_::
write(std::ostream& _out, BaseExporter& _be, const Options& _writeOptions, std::streamsize _precision) const write(std::ostream& _out, BaseExporter& _be, const Options& _writeOptions, std::streamsize _precision) const
{ {
unsigned int idx; unsigned int idx;
Vec3f v, n;
Vec2f t;
VertexHandle vh; VertexHandle vh;
std::vector<VertexHandle> vhandles; std::vector<VertexHandle> vhandles;
bool useMatrial = false; bool useMatrial = false;
@@ -296,7 +294,7 @@ write(std::ostream& _out, BaseExporter& _be, const Options& _writeOptions, std::
for (size_t i=0, nV=_be.n_vertices(); i<nV; ++i) for (size_t i=0, nV=_be.n_vertices(); i<nV; ++i)
{ {
vh = VertexHandle(static_cast<int>(i)); vh = VertexHandle(static_cast<int>(i));
t = _be.texcoord(vh); Vec2f t = _be.texcoord(vh);
texMap[t] = static_cast<int>(i); texMap[t] = static_cast<int>(i);
} }
} }
@@ -313,18 +311,27 @@ write(std::ostream& _out, BaseExporter& _be, const Options& _writeOptions, std::
} }
} }
// vertex data (point, normals, texcoords) const bool normal_double = _be.is_normal_double();
const bool point_double = _be.is_point_double();
for (size_t i=0, nV=_be.n_vertices(); i<nV; ++i) for (size_t i=0, nV=_be.n_vertices(); i<nV; ++i)
{ {
vh = VertexHandle(int(i)); vh = VertexHandle(int(i));
v = _be.point(vh); if (point_double) {
n = _be.normal(vh); auto v = _be.pointd(vh);
t = _be.texcoord(vh); _out << "v " << v[0] <<" "<< v[1] <<" "<< v[2] << '\n';
} else {
_out << "v " << v[0] <<" "<< v[1] <<" "<< v[2] << '\n'; auto v = _be.point(vh);
_out << "v " << v[0] <<" "<< v[1] <<" "<< v[2] << '\n';
if (_writeOptions.check(Options::VertexNormal)) }
_out << "vn " << n[0] <<" "<< n[1] <<" "<< n[2] << '\n'; if (_writeOptions.check(Options::VertexNormal)) {
if (normal_double) {
auto n = _be.normald(vh);
_out << "vn " << n[0] <<" "<< n[1] <<" "<< n[2] << '\n';
} else {
auto n = _be.normal(vh);
_out << "vn " << n[0] <<" "<< n[1] <<" "<< n[2] << '\n';
}
}
} }
size_t lastMat = std::numeric_limits<std::size_t>::max(); size_t lastMat = std::numeric_limits<std::size_t>::max();