int ConsistentOrientationBuilder::_findNextTriangle() { int result = -1 ; /* * find an oriented triangle (reached) and not visited */ for ( size_t i = 0; i < numTriangles(); i++ ) { if ( ! _oriented[i] || _visited[i] ) { continue ; } result = i ; break ; } //triangle found if ( result != -1 ) { return result ; } /* * here, a new connected part begins */ for ( size_t i = 0; i < numTriangles(); i++ ) { if ( ! _oriented[i] ) { _oriented[i] = true ; return i ; } } BOOST_ASSERT( result == -1 ); return result ; }
void renderScene(void) { int i; glClear(GL_COLOR_BUFFER_BIT); for(i=0;i<numTriangles(shape);i++) { glBegin(GL_TRIANGLES); glVertex3f(shape->vTable[shape->tTable[i*3]], shape->vTable[shape->tTable[i*3]+1], shape->vTable[shape->tTable[i*3]+2]); glVertex3f(shape->vTable[shape->tTable[i*3+1]], shape->vTable[shape->tTable[i*3+1]+1], shape->vTable[shape->tTable[i*3+1]+2]); glVertex3f(shape->vTable[shape->tTable[i*3+2]], shape->vTable[shape->tTable[i*3+2]+1], shape->vTable[shape->tTable[i*3+2]+2]); glEnd(); } glBegin(GL_TRIANGLES); glVertex3f(2.0,4.0,0.0); glVertex3f(2.1,4.1,0.1); glVertex3f(2.1,4.0,0.0); glEnd(); glFlush(); }
void BvhTriangleMesh::formTriangleAabbs() { void * cvs = verticesOnDevice(); void * tri = triangleIndicesOnDevice(); void * dst = bvh()->leafAabbs(); bvhCalculateLeafAabbsTriangle((Aabb *)dst, (float3 *)cvs, (uint3 *)tri, numTriangles()); }
void BvhTriangleSystem::initOnDevice() { std::cout<<"\n triangle system init on device"; setNumPrimitives(numTriangles()); CudaMassSystem::initOnDevice(); CudaLinearBvh::initOnDevice(); }
void ConsistentOrientationBuilder::_computeNeighbors() { _neighbors.clear() ; _neighbors.resize( numTriangles() ); for ( size_t i = 0; i < _triangles.size(); i++ ) { const std::vector< edge_descriptor > & triangle = _triangles[i] ; for ( size_t j = 0; j < triangle.size(); j++ ) { vertex_descriptor source = _graph.source( triangle[j] ) ; vertex_descriptor target = _graph.target( triangle[j] ) ; //get neighbor edges std::vector< directed_edge_descriptor > neighborEdges = _graph.edges( source, target ); //use marker to fill neighborGraph for ( size_t k = 0; k < neighborEdges.size(); k++ ) { size_t idOtherTriangle = ( size_t )_graph[ neighborEdges[k].first ].face; if ( idOtherTriangle == i ) { continue ; } _neighbors[i].insert( idOtherTriangle ); } } } }
void ConstraintDelaunayTriangulation::getTriangles( TriangulatedSurface& triangulatedSurface, bool filterExteriorParts ) const { triangulatedSurface.reserve( triangulatedSurface.numTriangles() + numTriangles() ); for ( Finite_faces_iterator it = finite_faces_begin(); it != finite_faces_end(); ++it ) { if ( filterExteriorParts && ( it->info().nestingLevel % 2 == 0 ) ) { continue ; } const Coordinate& a = it->vertex( 0 )->info().original ; const Coordinate& b = it->vertex( 1 )->info().original ; const Coordinate& c = it->vertex( 2 )->info().original ; // check that vertex has an original vertex if ( a.isEmpty() || b.isEmpty() || c.isEmpty() ) { BOOST_THROW_EXCEPTION( Exception( ( boost::format( "Can't convert Triangulation to TriangulatedSurface (constraint intersection found)" ) ).str() ) ) ; } triangulatedSurface.addTriangle( new Triangle( Point( a ), Point( b ), Point( c ) ) ); } }
void BvhTriangleMesh::initOnDevice() { m_verticesOnDevice = new CUDABuffer; m_verticesOnDevice->create(m_vertices->bufferSize()); m_triangleIndicesOnDevice = new CUDABuffer; m_triangleIndicesOnDevice->create(m_triangleIndices->bufferSize()); m_triangleIndicesOnDevice->hostToDevice(m_triangleIndices->data(), m_triangleIndices->bufferSize()); bvh()->setNumLeafNodes(numTriangles()); CollisionObject::initOnDevice(); }
TriangulatedSurface ConsistentOrientationBuilder::buildTriangulatedSurface() { _makeOrientationConsistent() ; TriangulatedSurface triangulatedSurface ; for ( size_t i = 0; i < numTriangles(); i++ ) { triangulatedSurface.addTriangle( triangleN( i ) ); } return triangulatedSurface ; }
void Mesh::DrawMesh(triangleshashtype &triSet, GLuint VBO) { //assert ((int)mesh_tri_verts.size() == numTriangles()*3); glBindBuffer(GL_ARRAY_BUFFER, VBO); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(VBOPosNormal), BUFFER_OFFSET(0)); glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, sizeof(VBOPosNormal), BUFFER_OFFSET(12)); glDrawArrays(GL_TRIANGLES,0,numTriangles(triSet)*3); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); }
void Mesh::DrawMesh() { assert ((int)mesh_tri_verts.size() == numTriangles()*3); glBindBuffer(GL_ARRAY_BUFFER, mesh_tri_verts_VBO); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(VBOPosNormalColorTexture), BUFFER_OFFSET(0)); glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, sizeof(VBOPosNormalColorTexture), BUFFER_OFFSET(12)); glEnableClientState(GL_COLOR_ARRAY); glColorPointer(3, GL_FLOAT, sizeof(VBOPosNormalColorTexture), BUFFER_OFFSET(24)); // Texture glEnable(GL_TEXTURE_2D); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer( 2, GL_FLOAT, sizeof(VBOPosNormalColorTexture), BUFFER_OFFSET(36)); glDrawArrays(GL_TRIANGLES,0,numTriangles()*3); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisable(GL_TEXTURE_2D); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); }
void Mesh::SetupMesh() { ComputeGouraudNormals(); for (triangleshashtype::iterator iter = triangles.begin(); iter != triangles.end(); iter++) { Triangle *t = iter->second; Vec3f a = (*t)[0]->getPos(); Vec3f b = (*t)[1]->getPos(); Vec3f c = (*t)[2]->getPos(); Vec3f na = (*t)[0]->getGouraudNormal(); Vec3f nb = (*t)[1]->getGouraudNormal(); Vec3f nc = (*t)[2]->getGouraudNormal(); Vec3f color = mesh_color; mesh_tri_verts.push_back(VBOPosNormalColorTexture(a,na,color,a.x()*1,a.z()*-1)); mesh_tri_verts.push_back(VBOPosNormalColorTexture(b,nb,color,b.x()*1,b.z()*-1)); mesh_tri_verts.push_back(VBOPosNormalColorTexture(c,nc,color,c.x()*1,c.z()*-1)); } glBindBuffer(GL_ARRAY_BUFFER,mesh_tri_verts_VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(VBOPosNormalColorTexture) * numTriangles() * 3, &mesh_tri_verts[0], GL_STATIC_DRAW); }
void Mesh::SetupMesh(triangleshashtype &triSet, GLuint VBO, std::vector<VBOPosNormal> &VBO_verts_vector) { for (auto iter = triSet.begin(); iter != triSet.end(); iter++) { Triangle *t = iter->second; Vec3f a = (*t)[0]->getPos(); Vec3f b = (*t)[1]->getPos(); Vec3f c = (*t)[2]->getPos(); Vec3f na = ComputeNormal(a,b,c); Vec3f nb = na; Vec3f nc = na; if (args->gouraud_normals) { na = (*t)[0]->getGouraudNormal(); nb = (*t)[1]->getGouraudNormal(); nc = (*t)[2]->getGouraudNormal(); } VBO_verts_vector.push_back(VBOPosNormal(a,na)); VBO_verts_vector.push_back(VBOPosNormal(b,nb)); VBO_verts_vector.push_back(VBOPosNormal(c,nc)); } glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(VBOPosNormal) * numTriangles(triSet) * 3, &VBO_verts_vector[0], GL_STATIC_DRAW); }
//---------------------------------------------------------------------------- std::vector<TriMesh*> Castle::LoadMeshPNT1Multi (const std::string& name) { // Get the vertex format. VertexFormat* vformat = VertexFormat::Create(3, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_NORMAL, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); int vstride = vformat->GetStride(); // Get the positions. std::string filename = Environment::GetPathR(name); std::ifstream inFile(filename.c_str()); int numPositions; Float3* positions; GetFloat3(inFile, numPositions, positions); // Get the normals. int numNormals; Float3* normals; GetFloat3(inFile, numNormals, normals); // Get the texture coordinates. int numTCoords; Float2* tcoords; GetFloat2(inFile, numTCoords, tcoords); // Get the vertices and indices. int numMeshes; inFile >> numMeshes; std::vector<int> numTriangles(numMeshes); int numTotalTriangles = 0; int m; for (m = 0; m < numMeshes; ++m) { inFile >> numTriangles[m]; numTotalTriangles += numTriangles[m]; } std::vector<std::vector<int> >indices(numMeshes); VertexPNT1* vertices = new1<VertexPNT1>(3*numTotalTriangles); std::vector<VertexPNT1> PNT1Array; std::map<VertexPNT1,int> PNT1Map; for (m = 0; m < numMeshes; ++m) { for (int t = 0; t < numTriangles[m]; ++t) { for (int j = 0, k = 3*t; j < 3; ++j, ++k) { VertexPNT1& vertex = vertices[k]; inFile >> vertex.PIndex; inFile >> vertex.NIndex; inFile >> vertex.TIndex; std::map<VertexPNT1,int>::iterator miter = PNT1Map.find(vertex); int index; if (miter != PNT1Map.end()) { // Second or later time the vertex is encountered. index = miter->second; } else { // First time the vertex is encountered. index = (int)PNT1Array.size(); PNT1Map.insert(std::make_pair(vertex, index)); PNT1Array.push_back(vertex); } indices[m].push_back(index); } } } inFile.close(); // Build the meshes. int numVertices = (int)PNT1Array.size(); VertexBuffer* vbuffer = new0 VertexBuffer(numVertices, vstride); VertexBufferAccessor vba(vformat, vbuffer); for (int i = 0; i < numVertices; ++i) { VertexPNT1& vertex = PNT1Array[i]; vba.Position<Float3>(i) = positions[vertex.PIndex]; vba.Normal<Float3>(i) = normals[vertex.NIndex]; vba.TCoord<Float2>(0, i) = tcoords[vertex.TIndex]; } std::vector<TriMesh*> meshes(numMeshes); for (m = 0; m < numMeshes; ++m) { int numIndices = (int)indices[m].size(); IndexBuffer* ibuffer = new0 IndexBuffer(numIndices, sizeof(int)); memcpy(ibuffer->GetData(), &indices[m][0], numIndices*sizeof(int)); meshes[m] = new0 TriMesh(vformat, vbuffer, ibuffer); } delete1(vertices); delete1(tcoords); delete1(normals); delete1(positions); return meshes; }
const unsigned ATriangleMesh::numComponents() const { return numTriangles(); }
const unsigned BvhTriangleMesh::numTriangleFaceVertices() const { return numTriangles() * 3; }
int main(int argc, char** argv) { Shape inShape = NULL; Shape outShape= NULL; int i,c; int doVTable=0; int *oTable, *mTable; float *vTable; int counter; float tester; if(argc != 3) { fprintf(stderr,"Usage:\n\t%s inFile outFile.\n",argv[0]); return 1; } inShape = readShapeFromFile(argv[1]); if(inShape == NULL) return 1; /* printf("Let's see what's in the inShape!\n");*/ #ifdef DEBUG printf("There are %d vertices.\n",inShape->numVertices); #endif /*printf("The vTable contains:\n");*/ for(c=0;c<inShape->numVertices*3;c+=3) { /* printf("%d => %f %f %f\n",c,inShape->vTable[c], inShape->vTable[c+1], inShape->vTable[c+2]);*/ } //printf("The tTable contains %d triangles\n\n",numTriangles(inShape)); // for(c=0;c<numTriangles(inShape)*3;c+=3) // { // printf("%d => %d %d %d\n", c,inShape->tTable[c], // inShape->tTable[c+1], // inShape->tTable[c+2]); // } oTable = malloc(sizeof(int)*numTriangles(inShape)*3); /*oTable = malloc(sizeof(int)*99999);*/ /* for(c=0;c<inShape->numVertices*3;c++)*/ for(c=0;c<numTriangles(inShape)*3;c++) { oTable[c] = -1; } /* for(c=0;c<numTriangles(inShape)*3;c++) for(i=c+1;i<inShape->numVertices*3;i++)*/ // printf("There are %d iterations of c and i\n",numTriangles(inShape)*3); for(c=0;c<numTriangles(inShape)*3;c++) for(i=c+1;i<numTriangles(inShape)*3;i++) { // if(c<10) // printf("Trying to match %d and %d..V(N(c))=%d, V(P(i))=%d, V(P(c)=%d,v(N(i))=%d\n",c,i,V(inShape,N(c)),V(inShape,P(i)),V(inShape,P(c)),V(inShape,N(i))); if((V(inShape,N(c)) == V(inShape,P(i))) &&(V(inShape,P(c)) == V(inShape,N(i)))) { // printf("Match found!\n"); oTable[c] = i; oTable[i] = c; } } printf("This is the oTable:\n"); for(c=0;c<numTriangles(inShape)*3;c++) { printf("%d => %d\n",c,oTable[c]); } for(c=0;c<numTriangles(inShape)*3;c++) if(oTable[c] == -1) { printf("MISALIGNMENT in corner %d!!!\n",c); printf("Cannot find a corner (opposite than at %d) with N(c)=%d and P(c)=%d\n", V(inShape,c),V(inShape,P(c)),V(inShape,N(c))); return 1; } /*mTable = malloc(sizeof(int)*inShape->numVertices*3);*/ mTable = malloc(sizeof(int)*numTriangles(inShape)*3); /* for(c=0;c<inShape->numVertices*3;c++)*/ for(c=0;c<numTriangles(inShape)*3;c++) mTable[c] = -1; /*outShape = createShape(4*inShape->numVertices-6);*/ outShape = createShape(4*inShape->numVertices,4*numTriangles(inShape)); printf("outShape has %d vertices\n",outShape->numVertices); printf("outShape has %d Triangles\n",numTriangles(outShape)); if(outShape == NULL) return 5; for(doVTable = 0;doVTable < inShape->numTables; doVTable++) { outShape->vTable = outShape->vTables[doVTable]; inShape->vTable = inShape->vTables[doVTable]; for(c=0;c<inShape->numVertices*3;c++) /* for(c=0;c<numTriangles(inShape)*3;c++)*/ { outShape->vTable[c] = inShape->vTable[c]; } } counter = inShape->numVertices - 1; printf("Counter starts at: %d\n",counter); /*for(c=0;c<inShape->numVertices*3;c++)*/ for(c=0;c<numTriangles(inShape)*3;c++) { if(mTable[c] == -1) { counter++; for(doVTable=0;doVTable<inShape->numTables;doVTable++) { outShape->vTable = outShape->vTables[doVTable]; inShape->vTable = inShape->vTables[doVTable]; vTable = inShape->vTable; outShape->vTable[counter*3]=0; outShape->vTable[counter*3]= (vTable[3*V(inShape,N(c))]+ vTable[3*V(inShape,P(c))])/2; outShape->vTable[counter*3]+= ( ( vTable[3*V(inShape,c)] + vTable[3*V(inShape,oTable[c])] )/2 - ( vTable[3*L(inShape,c,oTable)] + vTable[3*R(inShape,c,oTable)] +vTable[3*L(inShape,oTable[c],oTable)] + vTable[3*R(inShape,oTable[c],oTable)] )/4 )/4; outShape->vTable[counter*3+1]= (vTable[3*(V(inShape,N(c)))+1]+ vTable[3*(V(inShape,P(c)))+1])/2 + ( ( vTable[3*(V(inShape,c))+1] + vTable[3*(V(inShape,oTable[c]))+1] )/2 - ( vTable[3*(L(inShape,c,oTable))+1] + vTable[3*(R(inShape,c,oTable))+1] +vTable[3*(L(inShape,oTable[c],oTable))+1] + vTable[3*(R(inShape,oTable[c],oTable))+1] )/4 )/4; outShape->vTable[counter*3+2]= (vTable[3*(V(inShape,N(c)))+2]+ vTable[3*(V(inShape,P(c)))+2])/2 + ( ( vTable[3*(V(inShape,c))+2] + vTable[3*(V(inShape,oTable[c]))+2] )/2 - ( vTable[3*(L(inShape,c,oTable))+2] + vTable[3*(R(inShape,c,oTable))+2] +vTable[3*(L(inShape,oTable[c],oTable))+2] + vTable[3*(R(inShape,oTable[c],oTable))+2] )/4 )/4; } /* outShape->vTable[counter*3] = (vTable[3*V(inShape,N(c))] + vTable[3*V(inShape,P(c))])/2; outShape->vTable[counter*3+1] = (vTable[3*(V(inShape,N(c)))+1] + vTable[3*(V(inShape,P(c)))+1])/2; outShape->vTable[counter*3+2] = (vTable[3*(V(inShape,N(c)))+2] + vTable[3*(V(inShape,P(c)))+2])/2; */ printf("counter is %d\n",counter); printf("----\n"); mTable[c] = counter; mTable[oTable[c]] = counter; } } outShape->numVertices = counter+1; printf("outShape has %d vertices\n",outShape->numVertices); /* printf("mTable:\n"); for(c=0;c<inShape->numVertices*3;c++) printf("\t%d => %d\n",c,mTable[c]);*/ /*printf("new vTable:\n");*/ /* for(c=0;c<outShape->numVertices;c++) printf("\t%d => %f %f %f\n",c,outShape->vTable[c*3], outShape->vTable[c*3+1], outShape->vTable[c*3+2]);*/ /* printf("Counter finishes at: %d\n",counter);*/ printf("there are %d triangles\n",numTriangles(inShape)); for(c=0;c<numTriangles(inShape)*3;c+=3) { printf("Triangle %d %d %d becomes: \n",V(inShape,c),V(inShape,c+1), V(inShape,c+2)); outShape->tTable[c*4] = V(inShape,c); outShape->tTable[c*4+1]=mTable[c+2]; outShape->tTable[c*4+2]=mTable[c+1]; /* printf("\tTriangle %d %d %d\n", outShape->tTable[c*4], outShape->tTable[c*4+1], outShape->tTable[c*4+2]);*/ outShape->tTable[c*4+3]=mTable[c+1]; outShape->tTable[c*4+4]=mTable[c]; outShape->tTable[c*4+5]=V(inShape,c+2); /* printf("\tTriangle %d %d %d\n", outShape->tTable[c*4+3], outShape->tTable[c*4+4], outShape->tTable[c*4+5]);*/ outShape->tTable[c*4+6]=mTable[c+2]; outShape->tTable[c*4+7]=V(inShape,c+1); outShape->tTable[c*4+8]=mTable[c]; /* printf("\tTriangle %d %d %d\n", outShape->tTable[c*4+6], outShape->tTable[c*4+7], outShape->tTable[c*4+8]);*/ outShape->tTable[c*4+9]=mTable[c]; outShape->tTable[c*4+10]=mTable[c+1]; outShape->tTable[c*4+11]=mTable[c+2]; /* printf("\tTriangle %d %d %d\n", outShape->tTable[c*4+9], outShape->tTable[c*4+10], outShape->tTable[c*4+11]);*/ } printf("new tTable:\n"); for(c=0;c<numTriangles(outShape);c++) { printf("%d => %d %d %d\n",c,outShape->tTable[c*3], outShape->tTable[c*3+1], outShape->tTable[c*3+2]); } writeShapeToFile(outShape,argv[2]); printf("Hey there!!!!!\n\n\n");fflush(stdout); destroyShape(inShape); destroyShape(outShape); printf("Hey there!!!!!\n\n\n");fflush(stdout); return 0; }
void BvhTriangleSystem::formTetrahedronAabbs() { void * cvs = deviceX(); void * vsrc = deviceVa(); void * idx = deviceTretradhedronIndices(); void * dst = leafAabbs(); trianglesys::formTetrahedronAabbs((Aabb *)dst, (float3 *)cvs, (float3 *)vsrc, 1.f/60.f, (uint4 *)idx, numTriangles()); CudaBase::CheckCudaError("triangle system form aabb"); }
void ConsistentOrientationBuilder::_makeOrientationConsistent() { if ( _triangles.empty() ) { return ; } /* * mark all triangles as not oriented and not visited */ _visited.resize( numTriangles() ) ; _oriented.resize( numTriangles() ) ; for ( size_t i = 0; i < numTriangles(); i++ ) { _visited[i] = false ; _oriented[i] = false ; } _computeNeighbors(); // mark first one as oriented (reference) int currentTriangle = -1 ; while ( ( currentTriangle = _findNextTriangle() ) != -1 ) { //mark triangle as visited _visited[ currentTriangle ] = true ; //orient neighbors const std::set< size_t > & neighbors = _neighbors[ currentTriangle ] ; for ( std::set< size_t >::const_iterator it = neighbors.begin(); it != neighbors.end(); ++it ) { bool hasOppositeEdge, hasParallelEdge ; graph::algorithm::studyOrientation( _graph, _triangles[ currentTriangle ], _triangles[ ( *it ) ], hasOppositeEdge, hasParallelEdge ); // orientation is consistent if ( ! hasParallelEdge ) { _oriented[ *it ] = true ; continue ; } // orientation can't be consistent if ( hasOppositeEdge && hasParallelEdge ) { BOOST_THROW_EXCEPTION( Exception( "can't build consistent orientation from triangle set" ) ); } // orientation has already been fixed (moebius) if ( hasParallelEdge && _oriented[ *it ] ) { BOOST_THROW_EXCEPTION( Exception( "can't build consistent orientation from triangle set, inconsistent orientation for triangle" ) ); } //here, neighbor triangle should be reversed _graph.reverse( _triangles[ ( *it ) ] ); _oriented[ *it ] = true ; } } }
const unsigned TriangleSystem::numElements() const { return numTriangles(); }
std::vector<std::shared_ptr<Visual>> CastleWindow::LoadMeshPNT1Multi(std::string const& name) { // Get the positions, normals, and texture coordinates. std::string filename = mEnvironment.GetPath(name); std::ifstream inFile(filename); std::vector<Vector3<float>> positions, normals; std::vector<Vector2<float>> tcoords; GetTuple3(inFile, positions); GetTuple3(inFile, normals); GetTuple2(inFile, tcoords); // Get the vertices and indices. unsigned int numMeshes; inFile >> numMeshes; std::vector<unsigned int> numTriangles(numMeshes); unsigned int numTotalTriangles = 0; for (unsigned int m = 0; m < numMeshes; ++m) { inFile >> numTriangles[m]; numTotalTriangles += numTriangles[m]; } std::vector<std::vector<unsigned int>> indices(numMeshes); std::vector<LookupPNT1> lookups(3 * numTotalTriangles); std::vector<LookupPNT1> PNT1Array; std::map<LookupPNT1, unsigned int> PNT1Map; for (unsigned int m = 0; m < numMeshes; ++m) { for (unsigned int t = 0; t < numTriangles[m]; ++t) { for (unsigned int j = 0, k = 3 * t; j < 3; ++j, ++k) { LookupPNT1& lookup = lookups[k]; inFile >> lookup.PIndex; inFile >> lookup.NIndex; inFile >> lookup.TIndex; auto iter = PNT1Map.find(lookup); unsigned int index; if (iter != PNT1Map.end()) { // Second or later time the vertex is encountered. index = iter->second; } else { // First time the vertex is encountered. index = static_cast<unsigned int>(PNT1Array.size()); PNT1Map.insert(std::make_pair(lookup, index)); PNT1Array.push_back(lookup); } indices[m].push_back(index); } } } inFile.close(); // Build the meshes. unsigned int numVertices = static_cast<unsigned int>(PNT1Array.size()); std::shared_ptr<VertexBuffer> vbuffer = std::make_shared<VertexBuffer>(mPNT1Format, numVertices); VertexPNT1* vertex = vbuffer->Get<VertexPNT1>(); for (unsigned int i = 0; i < numVertices; ++i) { LookupPNT1& lookup = PNT1Array[i]; vertex[i].position = positions[lookup.PIndex]; vertex[i].normal = normals[lookup.NIndex]; vertex[i].tcoord = tcoords[lookup.TIndex]; } std::vector<std::shared_ptr<Visual>> meshes(numMeshes); for (unsigned int m = 0; m < numMeshes; ++m) { unsigned int numIndices = static_cast<unsigned int>(indices[m].size()); std::shared_ptr<IndexBuffer> ibuffer = std::make_shared<IndexBuffer>(IP_TRIMESH, numIndices, sizeof(unsigned int)); memcpy(ibuffer->GetData(), &indices[m][0], numIndices * sizeof(unsigned int)); meshes[m] = std::make_shared<Visual>(vbuffer, ibuffer); } return meshes; }