Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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