Skip to content

Faces normal reverse after triangulation ? #319

@wongshek

Description

@wongshek

Describe the issue
Import a polygon .obj file with triangulation (earcut option on), then export to .off, but some face normal reverse.

To Reproduce

/*********************Read Mesh*******************/
std::string inputfile = meshName;
tinyobj::ObjReaderConfig reader_config;
reader_config.triangulate = true;
reader_config.triangulation_method = "earcut";
reader_config.vertex_color = false;
reader_config.mtl_search_path = "./";

tinyobj::ObjReader reader;

if (!reader.ParseFromFile(inputfile, reader_config)) {
	if (!reader.Error().empty()) {
		std::cerr << "TinyObjReader: " << reader.Error();
	}
	exit(1);
}

auto& attrib = reader.GetAttrib();
auto& shapes = reader.GetShapes();
auto& materials = reader.GetMaterials();

// Loop over vertices
for (size_t i = 0; i < attrib.vertices.size(); i += 3)
{
	Point3d XYZ;
	XYZ.x = attrib.vertices[i + 0];
	XYZ.y = attrib.vertices[i + 1];
	XYZ.z = attrib.vertices[i + 2];
	vertexdPosition.emplace_back(XYZ);
}

// Loop over shapes
for (size_t s = 0; s < shapes.size(); s++)
{
	// Loop over faces(triangle)
	for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++)
	{
		// Access to vertex
		int id_x = shapes[s].mesh.indices[3 * f + 0].vertex_index;
		int id_y = shapes[s].mesh.indices[3 * f + 1].vertex_index;
		int id_z = shapes[s].mesh.indices[3 * f + 2].vertex_index;

		Point3i index;
		index.x = id_x;
		index.y = id_y;
		index.z = id_z;

		triangleIndex.emplace_back(index);
	}
}

/*********************Write Mesh*******************/
std::ofstream outFile("mesh.off");
outFile << "OFF" << std::endl;
outFile << vertexdPosition.size() << " "
		<< triangleIndex.size() << " " << 0 << std::endl;
for (const auto& pos : vertexdPosition)
{
	outFile << pos.x << " " << pos.y << " " << pos.z << std::endl;
}
outFile << std::endl;
for (const auto& idx : triangleIndex)
{
	outFile << 3 << " " << idx.x << " " << idx.y << " " << idx.z << std::endl;
}
outFile.close();
  • Result:
    image

Expected behavior
Face without reverse.

Environment

  • TinyObjLoader version 2.0.0
  • OS: [windows 10]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions