Ignore:
Timestamp:
01/11/09 01:18:50 (16 years ago)
Author:
mattausch
Message:

working on pompeii loading. fixed bug in obj conversion

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter/ObjConverter2.cpp

    r3261 r3262  
    1212                                                const VertexArray &vertices,  
    1313                                                const VertexArray &normals,  
    14                                                 const vector<pair<float, float> > &texcoords, 
     14                                                const vector<Texcoord> &texcoords, 
    1515                                                VertexArray &faceVertices, 
    1616                                                VertexArray &faceNormals, 
     
    2828        { 
    2929                string s(pch); 
    30                 //s += "\n", 
    3130                triples.push_back(s); 
    3231                 
     
    3433        } 
    3534 
     35        // throw away last symbol (\n) 
     36        triples.back().resize(triples.back().size() - 1); 
     37 
    3638        vector<int> indices; 
    3739        vector<int> nIndices; 
    3840        vector<int> tIndices; 
    3941 
    40         char seps[] = " /\t\n"; 
     42        char seps[] = " "; 
     43        char seps2[] = "/"; 
    4144 
    4245        for (size_t i = 0; i < triples.size(); ++ i) 
    4346        { 
    44                 static int dummy = 0; 
    45  
     47                //cout << "triple " << i << " " << triples[i] << endl; 
    4648                size_t found; 
    47                 found = triples[i].find_first_of(seps); 
     49                found = triples[i].find_first_of(seps2); 
    4850                size_t prevfound = 0; 
    49                 // vertex, normal, texture indices 
     51 
     52                // extract vertex, normal, texture indices 
    5053                string str = triples[i].substr(prevfound, found); 
    5154 
    5255                int index = (int)strtol(str.c_str(), NULL, 10) - 1; 
    53  
    5456                int tIndex = index; 
    5557                int nIndex = index;      
    5658                 
    57                 prevfound = found; 
    58                 found = triples[i].find_first_of(seps, found + 1);   
     59                // try to extract texture and normal indices 
     60                prevfound = found + 1; 
     61                found = triples[i].find_first_of(seps2, prevfound);   
    5962 
    6063                if (found != string::npos) 
     
    6568                        if (idx > 0) tIndex = idx; 
    6669                } 
    67  
    68                 if ((found + 1) < triples[i].size()) 
     70                 
     71                if (found != string::npos) 
    6972                { 
    7073                        str = triples[i].substr(found + 1); 
     
    8083                        nIndices.push_back(nIndex); 
    8184                        tIndices.push_back(tIndex); 
    82                 } 
    83  
     85 
     86                        //cout << index << " " << tIndex << " " << nIndex << endl; 
     87                } 
     88 
     89                int idx[3]; 
    8490                // new triangle found 
    8591                if (indices.size() > 2) 
     
    8793                        // change orientation of faces? 
    8894#if 1 
    89                         int idx1 = 0; 
    90                         int idx2 = (int)indices.size() - 2; 
    91                         int idx3 = (int)indices.size() - 1; 
     95                        idx[0] = 0; 
     96                        idx[1] = (int)indices.size() - 2; 
     97                        idx[2] = (int)indices.size() - 1; 
    9298#else 
    93                         int idx3 = 0; 
    94                         int idx2 = (int)indices.size() - 2; 
    95                         int idx1 = (int)indices.size() - 1; 
     99                        idx[2] = 0; 
     100                        idx[1] = (int)indices.size() - 2; 
     101                        idx[0] = (int)indices.size() - 1; 
    96102#endif 
    97                         faceVertices.push_back(vertices[indices[idx1]]); 
    98                         faceVertices.push_back(vertices[indices[idx2]]); 
    99                         faceVertices.push_back(vertices[indices[idx3]]); 
     103                        for (int j = 0; j < 3; ++ j) 
     104                                faceVertices.push_back(vertices[indices[idx[j]]]); 
    100105 
    101106 
    102107                        if (!normals.empty()) 
    103108                        { 
    104                                 faceNormals.push_back(normals[nIndices[idx1]]); 
    105                                 faceNormals.push_back(normals[nIndices[idx2]]); 
    106                                 faceNormals.push_back(normals[nIndices[idx3]]); 
     109                                for (int j = 0; j < 3; ++ j) 
     110                                        faceNormals.push_back(normals[nIndices[idx[j]]]); 
    107111                        } 
    108112                        else 
    109113                        { 
    110114                                // no face normals? => create normals 
    111                                 const SimpleTri tri(vertices[indices[idx1]], 
    112                                                         vertices[indices[idx2]],  
    113                                                                         vertices[indices[idx3]]); 
     115                                const SimpleTri tri(vertices[indices[idx[0]]], 
     116                                                        vertices[indices[idx[1]]],  
     117                                                                        vertices[indices[idx[2]]]); 
     118 
    114119                                const SimpleVec n = tri.GetNormal(); 
    115120 
     
    121126                        if (!texcoords.empty()) 
    122127                        { 
    123                                 faceTexcoords.push_back(texcoords[tIndices[idx1]]); 
    124                                 faceTexcoords.push_back(texcoords[tIndices[idx2]]); 
    125                                 faceTexcoords.push_back(texcoords[tIndices[idx3]]); 
     128                                for (int j = 0; j < 3; ++ j) 
     129                                { 
     130                                        if (tIndices[idx[j]] >= (int)texcoords.size()) 
     131                                                cerr << "error: texcoord indices exceed array size " << texcoords.size() << " " << tIndices[idx[j]] << endl; 
     132                                        const int tidx = min((int)texcoords.size() - 1, tIndices[idx[j]]); 
     133 
     134                                        faceTexcoords.push_back(texcoords[tidx]); 
     135                                } 
    126136                        } 
    127137                } 
     
    219229        } 
    220230 
    221  
    222231        return true; 
    223232} 
     
    233242        for (it = filenames.begin(); it != it_end; ++ it) 
    234243        { 
    235                 cout << "\n==================\n loading file " << *it << endl; 
    236  
    237                 if (!ReadFile(*it)) 
    238                 { 
    239                         cerr << "could not read file " << *it << endl; 
    240                         return false; 
    241                 } 
    242         } 
     244                const string filename = *it; 
     245 
     246                cout << "\n==================\n loading file " << filename << endl; 
     247 
     248                if (!ReadFile(filename)) 
     249                { 
     250                        cerr << "could not read file ... skipping " << filename << endl; 
     251                        //return false; 
     252                } 
     253        } 
     254 
     255        cout << "\n*******************\n writing file " << outputFilename << endl; 
    243256 
    244257        if (!WriteFile(outputFilename)) 
     
    279292        while (fgets(str, len, file) != NULL) 
    280293        { 
     294                //cout << "line: " << line << endl; 
     295 
    281296                switch (str[0]) 
    282297                { 
     
    323338                                //-- indices in the current line 
    324339 
     340                                //cout << "f: " << vertices.size() << " n: " << normals.size() << " " << texcoords.size() << endl; 
     341 
    325342                                LoadIndices(str,  
    326343                                                vertices, normals, texcoords,  
Note: See TracChangeset for help on using the changeset viewer.