Example #1
0
static poly
read_polygon(ifstream& fin) {
  u32 verticesCount = 0;
  fin >> verticesCount;
  poly res;
  for (size_t i = 0; i < verticesCount; i++) {
    auto v = read_vertex(fin);
    res.push_back(v);
  }
  return res;
}
    /*template<typename G>*/
    void loader/*<G>*/::process_line( const std::string& str )
    {
        char cmd='i';
        std::stringstream ss (str);
        ss >> cmd;

        if (cmd == 'v')
        {
            read_vertex(ss);
        }
        else if (cmd == 'a')
        {
            read_edge (ss);
        }
        else if (cmd == 'r')
        {
            read_reach (ss);
        }
    }
Example #3
0
    Vertex_info * getNext()
        {
            if (finalized_vertices.size () > 0)
            {
                dias::vertex_id v_id = finalized_vertices.front ();
                finalized_vertices.pop ();
                dias::vertex_db::iterator v_ptr = vertices.find (v_id);
                assert (v_ptr != vertices.end ());
                Vertex_info * result = new Vertex_info;
                result->vid = v_id;
                result->vi = v_ptr->second;
                vertices.erase (v_ptr);
                return result;
            }

            if (!f.eof ())
            {
                while (finalized_vertices.size () == 0 && !f.eof ())
                {
                    std::string line = "";

                    while (line == "")
                    {
                        if (f.eof ())
                            break;
                        std::getline (f, line);
                    }
                    if (f.eof ())
                        break;

                    boost::char_delimiters_separator<char> sep (false, "", 0);
                    boost::tokenizer<> tokens (line, sep);
                    boost::tokenizer<>::iterator p = tokens.begin ();

                    std::string type = *p++;

                    if (type == "#")
                        continue;

                    if (type == "v")
                        read_vertex (p, tokens.end ());
                    else if (type == "c")
                    {
                        dias::tetrahedra t
                            = read_tetrahedra (p, tokens.end ());
                        add_neighbours (t);
                    }
                    else
                        assert (false);
                }

                if (finalized_vertices.size () > 0)
                    return getNext ();
            }

            assert (vertices.size () > 0);
            assert (finalized_vertices.size () == 0);
            dias::vertex_db::const_iterator p = vertices.begin ();
            finalized_vertices.push (p->first);
            return getNext ();
        }
Example #4
0
Model_error_code OBJ_load (const char* filename, char clockwise, char left_handed, Model* model)
{
    vec3* norms     = 0;
    vec3* verts     = 0;
    texCoord* texcs = 0;
    triangle* tris  = 0;
    
    FILE* file = fopen (filename, "r");
    if (file == NULL) return Model_File_Not_Found;
    
    vec3 vert;
    vec3 normal;
    texCoord texc;
    
    vector vertices;
    vector normals;
    vector texCoords;
    vector triangles;
    
    int result = vector_new (&vertices,  sizeof(vec3),     0)
               | vector_new (&normals,   sizeof(vec3),     0)
               | vector_new (&texCoords, sizeof(texCoord), 0)
               | vector_new (&triangles, sizeof(triangle), 0);
    
    if (result != 0)
    {
        safe_free (vertices.data);
        safe_free (normals.data);
        safe_free (texCoords.data);
        safe_free (triangles.data);
        return Model_Memory_Allocation_Error;
    }
    
    while (!feof(file))
    {
        switch (fgetc(file))
        {
        case 'v':
            switch (fgetc(file))
            {
            case 't':
                read_tex_coord (file, &texc);
                vector_append (&texCoords, &texc);
                break;
                
            case 'n':
                read_normal (file, &normal);
                vector_append (&normals, &normal);
                break;
                
            default:
                read_vertex (file, &vert);
                vector_append (&vertices, &vert);
                break;
            }
            break;
            
        case 'f':
            // Initialise the normals vector if it is empty.
            if (vector_size(&normals) == 0)
            {
                vec3 zero;
                zero.x = 0.0f; zero.y = 0.0f; zero.z = 0.0f;
                vector_new (&normals, sizeof(vec3), vector_size(&vertices));
                vector_initialise (&normals, &zero);
            }
            read_face (file, clockwise, &vertices, &normals, &triangles);
            break;
        
        case '#':
        {
            int x = 17;
            while (x != '\n' && x != EOF) x = fgetc(file);
            break;
        }
            
        default: break;
        }
    }
    
    fclose (file);
    
    unsigned numVertices = vector_size (&vertices);
    
    // Normalise normals.
    unsigned i;
    for (i = 0; i < numVertices; ++i)
    {
        normalise (vector_ith (&normals, i));
    }
    
    model->vertices      = (vec3*) vertices.data;
    model->normals       = (vec3*) normals.data;
    model->texCoords     = (texCoord*) texCoords.data;
    model->triangles     = (triangle*) triangles.data;
    model->skins         = 0;
    model->animations    = 0;
    model->numFrames     = 1;
    model->numVertices   = numVertices;
    model->numTriangles  = vector_size (&triangles);
    model->numTexCoords  = vector_size (&texCoords);
    model->numSkins      = 0;
    model->numAnimations = 0;
    
    return Model_Success;
}