! end current facegroup and append to mesh */
| 554 | |
| 555 | /*! end current facegroup and append to mesh */ |
| 556 | void OBJLoader::flushTriGroup() |
| 557 | { |
| 558 | if (curGroup.empty()) return; |
| 559 | |
| 560 | if (subdivMode) |
| 561 | { |
| 562 | Ref<SceneGraph::SubdivMeshNode> mesh = new SceneGraph::SubdivMeshNode(curMaterial,BBox1f(0,1),1); |
| 563 | mesh->normals.resize(1); |
| 564 | group->add(mesh.cast<SceneGraph::Node>()); |
| 565 | |
| 566 | for (size_t i=0; i<v.size(); i++) mesh->positions[0].push_back(v[i]); |
| 567 | for (size_t i=0; i<vn.size(); i++) mesh->normals[0].push_back(vn[i]); |
| 568 | for (size_t i=0; i<vt.size(); i++) mesh->texcoords.push_back(vt[i]); |
| 569 | |
| 570 | for (size_t i=0; i<ec.size(); ++i) { |
| 571 | assert(((size_t)ec[i].a < v.size()) && ((size_t)ec[i].b < v.size())); |
| 572 | mesh->edge_creases.push_back(Vec2i(ec[i].a, ec[i].b)); |
| 573 | mesh->edge_crease_weights.push_back(ec[i].w); |
| 574 | } |
| 575 | |
| 576 | for (size_t j=0; j<curGroup.size(); j++) |
| 577 | { |
| 578 | const std::vector<Vertex>& face = curGroup[j]; |
| 579 | mesh->verticesPerFace.push_back(int(face.size())); |
| 580 | for (size_t i=0; i<face.size(); i++) |
| 581 | mesh->position_indices.push_back(face[i].v); |
| 582 | } |
| 583 | if (mesh->normals[0].size() == 0) |
| 584 | mesh->normals.clear(); |
| 585 | mesh->verify(); |
| 586 | } |
| 587 | else |
| 588 | { |
| 589 | Ref<SceneGraph::TriangleMeshNode> mesh = new SceneGraph::TriangleMeshNode(curMaterial,BBox1f(0,1),1); |
| 590 | mesh->normals.resize(1); |
| 591 | group->add(mesh.cast<SceneGraph::Node>()); |
| 592 | // merge three indices into one |
| 593 | std::map<Vertex, uint32_t> vertexMap; |
| 594 | for (size_t j=0; j<curGroup.size(); j++) |
| 595 | { |
| 596 | /* iterate over all faces */ |
| 597 | const std::vector<Vertex>& face = curGroup[j]; |
| 598 | |
| 599 | /* triangulate the face with a triangle fan */ |
| 600 | Vertex i0 = face[0], i1 = Vertex(-1), i2 = face[1]; |
| 601 | for (size_t k=2; k < face.size(); k++) |
| 602 | { |
| 603 | i1 = i2; i2 = face[k]; |
| 604 | uint32_t v0,v1,v2; |
| 605 | v0 = getVertex(vertexMap, mesh, i0); |
| 606 | v1 = getVertex(vertexMap, mesh, i1); |
| 607 | v2 = getVertex(vertexMap, mesh, i2); |
| 608 | assert(v0 < mesh->numVertices()); |
| 609 | assert(v1 < mesh->numVertices()); |
| 610 | assert(v2 < mesh->numVertices()); |
| 611 | mesh->triangles.push_back(SceneGraph::TriangleMeshNode::Triangle(v0,v1,v2)); |
| 612 | } |
| 613 | } |