Subversion Repositories gelsvn

Rev

Rev 667 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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