File: ObjLoader.cpp

package info (click to toggle)
vimix 0.8.4%2Bgit20250220%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 88,204 kB
  • sloc: cpp: 92,163; ansic: 34,427; makefile: 373; objc: 97; xml: 77; sh: 45
file content (444 lines) | stat: -rw-r--r-- 15,301 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// 
// ObjLoader.cpp -- modified for glm
//  modified from https://github.com/mortennobel/OpenGL_3_2_Utils
//
/*!
 * OpenGL 3.2 Utils - Extension to the Angel library (from the book Interactive Computer Graphics 6th ed
 * https://github.com/mortennobel/OpenGL_3_2_Utils
 *
 * New BSD License
 *
 * Copyright (c) 2011, Morten Nobel-Joergensen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
 * disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "ObjLoader.h"

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/string_cast.hpp>
using namespace glm;

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>

using namespace std;

const int DEBUG = 1;

void printDebug(vector<vec3> &positions, vector<int> &indices);
void printDebug(Material *m);

vec3 toVec3(istringstream &iss){
    float x,y,z;
    iss >> x;
    iss >> y;
    iss >> z;
    return vec3(x,y,z);
}

vec2 toVec2(istringstream &iss){
    float x,y;
    iss >> x;
    iss >> y;
    return vec2(x,y);
}

struct TriangleIndex{
    // this is a vertex, represented as the indices into their
    // respective arrays of positions, normals, and texture coords
    int position;
    int normal;
    int uv;

    bool replace(std::string& str, const std::string& from, const std::string& to) {
        size_t start_pos = str.find(from);
        if(start_pos == std::string::npos)
            return false;
        str.replace(start_pos, from.length(), to);
        return true;
    }

    TriangleIndex(string p):position(-1),normal(-1),uv(-1) {
        // position/uv/normal
        replace(p, "//","/-1/");
        stringstream ss(p);
        char buffer[50];

        ss.getline(buffer,50, '/');
        position = atoi(buffer);
        if (ss.good()){
            ss.getline(buffer,50, '/');
            uv = atoi(buffer);
        }
        if (ss.good()){
            ss.getline(buffer,50, '/');
            normal = atoi(buffer);
        }
    }

    // needed to use TriangleIndex as key in map
    bool operator <(const TriangleIndex& Rhs) const {
        return (position < Rhs.position) ||
                (position == Rhs.position && normal < Rhs.normal) ||
                (position == Rhs.position && normal == Rhs.normal && uv < Rhs.uv);
    }
};

struct TriangleString{
    TriangleIndex v0;
    TriangleIndex v1;
    TriangleIndex v2;

    TriangleString(string v0, string v1, string v2):v0(v0),v1(v1),v2(v2){
    }

    TriangleIndex get(int index){
        if (index == 0) {
            return v0;
        } else if (index == 1) {
            return v1;
        }
        return v2;
    }
};



std::string
makeMtlFilename ( std::string mtlfile, std::string objfile )
{
  // pull off the path to the directory from the objfile
  size_t pos = objfile.rfind ('/');
  // tack the directory onto the materal "mtllib" filename
  if ( pos == std::string::npos ) {
    return mtlfile;
  }  else {
    std::string path;
    path = objfile.substr(0,pos);
    return path+"/"+mtlfile;
  }
}


bool loadMaterialLibrary ( string mtlText, map<string,Material*> &outMaterials)
{
    Material *mat = nullptr;
    stringstream ifs(mtlText);
    char buffer[512];
    while (ifs.good()) {
        ifs.getline(buffer,512);
        string line(buffer);
        istringstream iss(line);
        string token;
        vec3 color;
        iss >> token;
        if (token.compare("newmtl") ==0) {
            // create a new material and store in map
            mat = new Material;
            iss >> token;
            outMaterials[token] = mat;
        } else if (token.compare("Ka") == 0 && mat) {
            mat->ambient = toVec3 ( iss );
        } else if (token.compare("Kd") == 0 && mat) {
            mat->diffuse = toVec3 ( iss );
        } else if (token.compare("Ks") == 0 && mat) {
            mat->specular = toVec3 ( iss );
        } else if (token.compare("Ns") == 0 && mat) {
            iss >> mat->shininess;
        } else if (token.compare("map_Kd") == 0 && mat) {
            iss >> mat->diffuseTexture;
        } else if (token.compare("map_Disp") == 0 && mat) {
            iss >> mat->bumpTexture;
        }
    }

    return (mat != nullptr);
}

bool loadObject(string objText,
                vector<vec3> &outPositions,
                vector<vec3> &outNormal,
                vector<vec2> &outUv,
                vector<unsigned int> &outIndices,
                std::string &outMtlfilename
                )
{
    vector<vec3> positions;
    vector<vec3> normals;
    vector<vec2> uvs;
    vector<TriangleString> triangles;

    stringstream ifs(objText);
    char buffer[512];
    while (ifs.good()){
        ifs.getline(buffer,512);

        string line(buffer);
        istringstream iss(line);
        string token;
        iss >> token;
        if (token.compare("o")==0){
            // does not support multiple objects
        } else if (token.compare("g")==0){
        } else if (token.compare("mtllib")==0){
            // read the name of .mtl file
            iss >> outMtlfilename;
        } else if (token.compare("usemtl")==0){
            // does not support multiple materials
        } else if (token.compare("v")==0){
            positions.push_back( toVec3(iss));
        } else if (token.compare("vn")==0){
            normals.push_back( toVec3(iss));
        } else if (token.compare("vt")==0){
            uvs.push_back( toVec2(iss));
        } else if (token.compare("f")==0){
            vector<string> polygon;
            do {
                string index;
                iss >> index;
                if (index.length() > 0) {
                    polygon.push_back(index);
                }
            } while (iss);

            // triangulate polygon (assumes convex )
            TriangleString triangle(polygon[0], polygon[1], polygon[2]);
            triangles.push_back(triangle);
            for (int i=3;i<polygon.size();i++){
                TriangleString triangle2(polygon[i-1], polygon[i], polygon[0]);
                triangles.push_back(triangle2);
            }
        }
    }

    map<TriangleIndex,int> cache;
    for (int i=0;i<triangles.size();i++){
        TriangleString triangleString = triangles[i];
        for (int j=0;j<3;j++){
            TriangleIndex index = triangleString.get(j);
            map<TriangleIndex,int>::iterator cachedIndex = cache.find(index);
            if (cachedIndex != cache.end()) {
                outIndices.push_back(cachedIndex->second);
            } else {
                int vertexIndex = outPositions.size();
                outPositions.push_back(positions[index.position-1]);
                if (index.normal != -1){
                    outNormal.push_back(normals[index.normal-1]);
                }
                if (index.uv != -1) {
                    outUv.push_back(uvs[index.uv-1]);
                }
                outIndices.push_back(vertexIndex);
                cache[index] = vertexIndex;
            }
        }
    }

    return (outPositions.size()>0);
}


struct Group {
    vector<TriangleString> triangles;
    Material *mat;
};

//
// loadObjectGroups loads a .obj file containing materials and textures,
// creating a ModelNode that draws it.
bool loadObjectGroups ( string filename,
                        vector<vec3> &outPositions,
                        vector<vec3> &outNormal,
                        vector<vec2> &outUv,
                        vector< vector<unsigned int> > &outIndices, // per group
                        vector< Material* >&outMaterials // per group
                       )
{
    map<string,Material*> materials;
    vector<vec3> positions;
    vector<vec3> normals;
    vector<vec2> uvs;

    map<string,Group*> groups;
    string currentGroupName("");
    static int groupnum = 1;

    string path = std::string( filename );
    ifstream ifs ( path.c_str() , ifstream::in );
    char buffer[512];
    while (ifs.good()){
        ifs.getline(buffer,512);

        string line(buffer);
        istringstream iss(line);
        string token;
        iss >> token;
        if (token.compare("o")==0){
            // does not support multiple objects
        } else if (token.compare("g")==0){
            // for a new group of faces (eg with a diff material)
            // create a group
            iss >> currentGroupName;
            groups[currentGroupName] = new Group;
        } else if (token.compare("mtllib")==0){
            // read the .mtl file and create the Materials
            string mtlfile;
            iss >> mtlfile;
//            loadMaterialLibrary( mtlfile.c_str(),
//                                  filename,
//                                  materials);
        } else if (token.compare("usemtl")==0){
            // create group if none exists, or if this is a "usemtl" line without a preceding "g" line
            if ( currentGroupName=="" || groups[currentGroupName]->triangles.size() ) {
                string name;
                ostringstream oss(name);
                //	static int groupnum = 1;
                oss << "dummy" << groupnum++;
                currentGroupName=oss.str();
                groups[currentGroupName] = new Group;
            }
            iss >> token;
            if (token=="usemtl") {
                if (DEBUG)
                    std::cout << "null token" << std::endl;
                token = "dummy1";
            }
            if (DEBUG) {
                std::cout << "group is " << currentGroupName << std::endl;
                std::cout << "usemtl " << token << std::endl;
                std::cout << "materials[token] == " << materials[token] << std::endl;
            }
            if ( materials[token] == 0 ) {
                materials[token] = new Material;
            }
            printDebug ( materials[token] );
            groups[currentGroupName]->mat = materials[token];
            outMaterials.push_back((materials[token]));

        } else if (token.compare("v")==0){
            positions.push_back( toVec3(iss));
        } else if (token.compare("vn")==0){
            normals.push_back( toVec3(iss));
        } else if (token.compare("vt")==0){
            uvs.push_back( toVec2(iss));
        } else if (token.compare("f")==0){
            // assign face to current group
            vector<string> polygon;
            do {
                string index;
                iss >> index;
                if (index.length() > 0) {
                    polygon.push_back(index);
                }
            } while (iss);

            // oops no usemtl or mtllib or group seen
            //      if ( currentGroupName=="" || groups[currentGroupName]->triangles.size()==0 ) {
            if ( currentGroupName=="" ) {
                string name;
                ostringstream oss(name);
                //	static int groupnum = 1;
                oss << "dummy" << groupnum++;
                currentGroupName=oss.str();
                groups[currentGroupName] = new Group;
                outMaterials.push_back( groups[currentGroupName]->mat = new Material );
            }


            // triangulate polygon
            if ( polygon.size() >= 3 ) {
                TriangleString triangle(polygon[0], polygon[1], polygon[2]);
                groups[currentGroupName]->triangles.push_back(triangle);
                for (int i=3;i<polygon.size();i++){
                    TriangleString triangle2(polygon[i-1], polygon[i], polygon[0]);
                    groups[currentGroupName]->triangles.push_back(triangle2);
                }
            }
        }
    }
    ifs.close();

    // XXX
    // repeat this for each group.
    // return vectors must be made vectors of vectors (or maybe just the indices?)
    // for each group
    //  deref and pack the vertex positions, normal, and uv,
    //  pack the element array for each group separately
    map<TriangleIndex,int> cache;
    for ( map<string,Group*>::iterator g = groups.begin(); g != groups.end();g++ ) {
        // each group gets its own element array
        vector<unsigned int> groupIndices;
        for (int i=0;i<g->second->triangles.size();i++){
            TriangleString triangleString = g->second->triangles[i];
            for (int j=0;j<3;j++){
                TriangleIndex index = triangleString.get(j);
                map<TriangleIndex,int>::iterator cachedIndex = cache.find(index);
                if (cachedIndex != cache.end()) {
                    groupIndices.push_back(cachedIndex->second);
                } else {
                    int vertexIndex = outPositions.size();
                    outPositions.push_back(positions[index.position-1]);
                    if (index.normal != -1){
                        outNormal.push_back(normals[index.normal-1]);
                    }
                    if (index.uv != -1) {
                        outUv.push_back(uvs[index.uv-1]);
                    }
                    groupIndices.push_back(vertexIndex);
                    cache[index] = vertexIndex;
                }
            }
        }
        // pack the indices for this group
        outIndices.push_back(groupIndices);
    }
    //cout <<"Indices "<< outIndices.size() << endl;
    //cout <<"Positions "<< outPositions.size() << endl;
    //printDebug(outPositions, outIndices);
    return true;
}


void printDebug(vector<vec3> &positions, vector<int> &indices){
    if (!DEBUG)
        return;
    for (int i=0;i<indices.size();i++){
        //		cout << positions[indices[i]] <<" ";
        cout << positions[indices[i]].x <<",";
        cout << positions[indices[i]].y <<",";
        cout << positions[indices[i]].z <<" ";
        if ((i+1)%3==0){
            cout << endl;
        }
    }
}

void printDebug(Material *m){
    if (!DEBUG) return;
    cout << "ambient " << glm::to_string(m->ambient) << endl;
    cout << "diffuse " << glm::to_string(m->diffuse) << endl;
    cout << "specular " << glm::to_string(m->specular) << endl;
    cout << "shininess " << m->shininess << endl;
    cout << "diffuse texture " << m->diffuseTexture << endl;
    cout << "bump texture " << m->bumpTexture << endl;
}