Merge pull request #51 from benikabocha/fix-obj-minus-index

Fix loading of OBJ file with minus index.
This commit is contained in:
Ondrej Stava 2017-01-27 08:22:06 -08:00 committed by GitHub
commit 8c0911dcb0

View File

@ -309,14 +309,23 @@ bool ObjDecoder::ParseFace(bool *error) {
if (indices[0] > 0) { if (indices[0] > 0) {
out_point_cloud_->attribute(pos_att_id_) out_point_cloud_->attribute(pos_att_id_)
->SetPointMapEntry(vert_id, AttributeValueIndex(indices[0] - 1)); ->SetPointMapEntry(vert_id, AttributeValueIndex(indices[0] - 1));
} else if (indices[0] < 0) {
out_point_cloud_->attribute(pos_att_id_)
->SetPointMapEntry(vert_id, AttributeValueIndex(num_positions_ + indices[0]));
} }
if (indices[1] > 0) { if (indices[1] > 0) {
out_point_cloud_->attribute(tex_att_id_) out_point_cloud_->attribute(tex_att_id_)
->SetPointMapEntry(vert_id, AttributeValueIndex(indices[1] - 1)); ->SetPointMapEntry(vert_id, AttributeValueIndex(indices[1] - 1));
} else if (indices[1] < 0) {
out_point_cloud_->attribute(tex_att_id_)
->SetPointMapEntry(vert_id, AttributeValueIndex(num_tex_coords_ + indices[1]));
} }
if (indices[2] > 0) { if (indices[2] > 0) {
out_point_cloud_->attribute(norm_att_id_) out_point_cloud_->attribute(norm_att_id_)
->SetPointMapEntry(vert_id, AttributeValueIndex(indices[2] - 1)); ->SetPointMapEntry(vert_id, AttributeValueIndex(indices[2] - 1));
} else if (indices[2] < 0) {
out_point_cloud_->attribute(norm_att_id_)
->SetPointMapEntry(vert_id, AttributeValueIndex(num_normals_ + indices[2]));
} }
if (material_att_id_ >= 0) { if (material_att_id_ >= 0) {
out_point_cloud_->attribute(material_att_id_) out_point_cloud_->attribute(material_att_id_)
@ -393,7 +402,7 @@ bool ObjDecoder::ParseVertexIndices(std::array<int32_t, 3> *out_indices) {
// 4. POS_INDEX//NORMAL_INDEX // 4. POS_INDEX//NORMAL_INDEX
parser::SkipWhitespace(buffer()); parser::SkipWhitespace(buffer());
if (!parser::ParseSignedInt(buffer(), &(*out_indices)[0]) || if (!parser::ParseSignedInt(buffer(), &(*out_indices)[0]) ||
(*out_indices)[0] < 1) (*out_indices)[0] == 0)
return false; // Position index must be present and valid. return false; // Position index must be present and valid.
(*out_indices)[1] = (*out_indices)[2] = 0; (*out_indices)[1] = (*out_indices)[2] = 0;
char ch; char ch;
@ -408,7 +417,7 @@ bool ObjDecoder::ParseVertexIndices(std::array<int32_t, 3> *out_indices) {
if (ch != '/') { if (ch != '/') {
// Must be texture coord index. // Must be texture coord index.
if (!parser::ParseSignedInt(buffer(), &(*out_indices)[1]) || if (!parser::ParseSignedInt(buffer(), &(*out_indices)[1]) ||
(*out_indices)[1] < 1) (*out_indices)[1] == 0)
return false; // Texture index must be present and valid. return false; // Texture index must be present and valid.
} }
if (!buffer()->Peek(&ch)) if (!buffer()->Peek(&ch))
@ -417,7 +426,7 @@ bool ObjDecoder::ParseVertexIndices(std::array<int32_t, 3> *out_indices) {
buffer()->Advance(1); buffer()->Advance(1);
// Read normal index. // Read normal index.
if (!parser::ParseSignedInt(buffer(), &(*out_indices)[2]) || if (!parser::ParseSignedInt(buffer(), &(*out_indices)[2]) ||
(*out_indices)[2] < 1) (*out_indices)[2] == 0)
return false; // Normal index must be present and valid. return false; // Normal index must be present and valid.
} }
return true; return true;