Subversion Repositories gelsvn

Rev

Rev 652 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
7
#include <fstream>
149 jab 8
#include "obj_load.h"
595 jab 9
#include "Manifold.h"
10
 
149 jab 11
using namespace std;
12
using namespace CGLA;
13
 
150 jab 14
namespace HMesh
149 jab 15
{
595 jab 16
    using std::string;
17
 
18
    bool obj_load(const string& filename, Manifold& m)
19
    {
652 janba 20
        ifstream obj_file(filename.data());
595 jab 21
 
652 janba 22
        if(obj_file)
595 jab 23
        {
652 janba 24
            string buf;
595 jab 25
            vector<Vec3f> vertices;
26
            vector<int> faces;
27
            vector<int> indices;
652 janba 28
 
29
            while(obj_file >> buf)
595 jab 30
            {
652 janba 31
                switch(buf[0])
595 jab 32
                {
652 janba 33
                    case 'v': // v, vn, vt
595 jab 34
                    {
652 janba 35
                        if(buf == "v")
36
                        {
37
                            Vec3f v_geo;
38
                            obj_file >> v_geo;
39
                            vertices.push_back(v_geo);
40
                        }
41
                        else if(buf == "vn")
42
                        {
43
                            Vec3f v_norm;
44
                            obj_file >> v_norm;
45
                        }
46
                        else if(buf == "vt")
47
                        {
48
                            Vec3f v_tex;
49
                            obj_file >> v_tex[0] >> v_tex[1];
50
                            v_tex[2]=1;
51
                        }
595 jab 52
                    }
652 janba 53
                        break;
54
                    case 'f':
55
                    {
56
                        char line[1024];
57
 
58
                        vector<int> v_indices;
59
                        vector<int> t_indices;
60
                        vector<int> n_indices;
61
 
62
                        obj_file.getline(line, 1022);
63
                        char* pch = strtok(line, " \t");
64
                        int ctr = 0;
65
                        while(pch != 0)
66
                        {
67
                            int v,t,n;
68
                            if(sscanf(pch, "%d/%d/%d", &v, &t, &n)==3)
69
                            {
70
                                indices.push_back(v>0?v-1:vertices.size()+v);
71
                                ++ctr;
72
                            }
73
                            else if(sscanf(pch, "%d//%d", &v, &n)==2)
74
                            {
75
                                indices.push_back(v>0?v-1:vertices.size()+v);
76
                                ++ctr;
77
                            }
78
                            else if(sscanf(pch, "%d/%d", &v, &t)==2)
79
                            {
80
                                indices.push_back(v>0?v-1:vertices.size()+v);
81
                                ++ctr;
82
                            }
83
                            else if(sscanf(pch, "%d", &v)==1)
84
                            {
85
                                indices.push_back(v>0?v-1:vertices.size()+v);
86
                                ++ctr;
87
                            }
88
                            pch = strtok(0, " \t");
89
                        }
595 jab 90
                        faces.push_back(ctr);
652 janba 91
                    }
92
                        break;
93
                    case 'm':
94
                        obj_file >> buf;
95
                        break;
96
                    case 'u':
97
                        obj_file >> buf;
98
                        break;
99
                    case '#':
100
                    default:
101
                        obj_file.ignore(1024, '\n');
102
                        break;
595 jab 103
                }
104
            }
662 janba 105
            cout << "Loaded " << vertices.size() << " vertices and " << faces.size() << " faces"<< endl;
595 jab 106
            m.clear();
107
            m.build(vertices.size(),
108
                    reinterpret_cast<float*>(&vertices[0]),
109
                    faces.size(),
110
                    &faces[0],
111
                    &indices[0]);
112
 
113
            return true;
114
        }
115
        return false;
116
 
117
    }
118
}