Exemple #1
0
void loadObj( const std::string& filename )
{
    ObjParser parser;

    if (! parser.parseFile( filename ) )
    {
        std::cerr
                << "Failed to load .obj file: " << parser.errorText() << std::endl
                << "FILE: " << parser.filename()   << ":"
                << parser.lineNumber() << std::endl
                << "LINE: " << parser.lineText()   << std::endl;
    }
    else
    {
        std::cout << parser.dump();
    }
}
ObjTriangleMesh::ObjTriangleMesh( const Transform *o2c, const Transform *c2o, int subdlevels, const std::string &objfile ): Shape(o2c, c2o) {
	std::vector<Point> points;
    std::vector<Vector> normals;
    std::vector<uv> uvs;
	std::vector<int> vIndex;
    std::vector<int> nIndex;
    std::vector<int> uvIndex;

    ObjParser parser = ObjParser(objfile, &points, &normals, &uvs, &vIndex, &nIndex, &uvIndex);
    bool success = parser.parseFile();
	assert(success);
		
	size_t numTriangles = vIndex.size() / 3;
	size_t numVertices = vIndex.size();
    LOG_INFO("Found " << numTriangles << " triangles and " << numVertices << " vertices.");

    Point *P = new Point[ numVertices ];
    Vector *N = new Vector[ numVertices ];
    assert(nIndex.size() == vIndex.size());
    uv *UV = NULL;

    if(uvs.size()){
        assert(uvIndex.size() == vIndex.size());
        UV = new uv[ numVertices ];
    }

    
    int *vertexIndex = new int[3 * numTriangles];
	for (size_t i=0; i < 3*numTriangles; i++) {
		// obj files starts vert indices at 1 instead of 0.
		vertexIndex[i] = vIndex[i] - 1;
        nIndex[i]--;
        if(UV)
            uvIndex[i]--;

		assert(vertexIndex[i] < numVertices);
	}
	

	for (size_t i = 0; i < numVertices; i++) {
		P[i] = points[vertexIndex[i]];

        N[i] = normals[nIndex[i]];
        if (N[i].x == 0. && N[i].y == 0. && N[i].z == 0.) {
            LOG_WARNING("Found empty normal. Setting to 0,1,0");
            N[i] = Vector(0,1,0);
        }
        
        if (UV){
            UV[i] = uvs[uvIndex[i]];
        }
        
        vertexIndex[i] = (int)i;
	}

    if (subdlevels){
        m_shape = shared_ptr<Shape>(new LoopSubdivMesh(o2c, c2o, (int)numTriangles, (int)numVertices, vertexIndex, P, UV, subdlevels));
    } else {
        m_shape = shared_ptr<Shape>(new TriangleMesh(o2c, c2o, (int)numTriangles, (int)numVertices, vertexIndex, P, N, UV));
    }
    
    delete[] vertexIndex;
	delete[] P;
	delete[] N;
    if(UV)
        delete[] UV;
}