- Timestamp:
- 01/11/09 01:18:50 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter/ObjConverter2.cpp
r3261 r3262 12 12 const VertexArray &vertices, 13 13 const VertexArray &normals, 14 const vector< pair<float, float>> &texcoords,14 const vector<Texcoord> &texcoords, 15 15 VertexArray &faceVertices, 16 16 VertexArray &faceNormals, … … 28 28 { 29 29 string s(pch); 30 //s += "\n",31 30 triples.push_back(s); 32 31 … … 34 33 } 35 34 35 // throw away last symbol (\n) 36 triples.back().resize(triples.back().size() - 1); 37 36 38 vector<int> indices; 37 39 vector<int> nIndices; 38 40 vector<int> tIndices; 39 41 40 char seps[] = " /\t\n"; 42 char seps[] = " "; 43 char seps2[] = "/"; 41 44 42 45 for (size_t i = 0; i < triples.size(); ++ i) 43 46 { 44 static int dummy = 0; 45 47 //cout << "triple " << i << " " << triples[i] << endl; 46 48 size_t found; 47 found = triples[i].find_first_of(seps );49 found = triples[i].find_first_of(seps2); 48 50 size_t prevfound = 0; 49 // vertex, normal, texture indices 51 52 // extract vertex, normal, texture indices 50 53 string str = triples[i].substr(prevfound, found); 51 54 52 55 int index = (int)strtol(str.c_str(), NULL, 10) - 1; 53 54 56 int tIndex = index; 55 57 int nIndex = index; 56 58 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); 59 62 60 63 if (found != string::npos) … … 65 68 if (idx > 0) tIndex = idx; 66 69 } 67 68 if ( (found + 1) < triples[i].size())70 71 if (found != string::npos) 69 72 { 70 73 str = triples[i].substr(found + 1); … … 80 83 nIndices.push_back(nIndex); 81 84 tIndices.push_back(tIndex); 82 } 83 85 86 //cout << index << " " << tIndex << " " << nIndex << endl; 87 } 88 89 int idx[3]; 84 90 // new triangle found 85 91 if (indices.size() > 2) … … 87 93 // change orientation of faces? 88 94 #if 1 89 i nt idx1= 0;90 i nt idx2= (int)indices.size() - 2;91 i nt idx3= (int)indices.size() - 1;95 idx[0] = 0; 96 idx[1] = (int)indices.size() - 2; 97 idx[2] = (int)indices.size() - 1; 92 98 #else 93 i nt idx3= 0;94 i nt idx2= (int)indices.size() - 2;95 i nt idx1= (int)indices.size() - 1;99 idx[2] = 0; 100 idx[1] = (int)indices.size() - 2; 101 idx[0] = (int)indices.size() - 1; 96 102 #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]]]); 100 105 101 106 102 107 if (!normals.empty()) 103 108 { 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]]]); 107 111 } 108 112 else 109 113 { 110 114 // 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 114 119 const SimpleVec n = tri.GetNormal(); 115 120 … … 121 126 if (!texcoords.empty()) 122 127 { 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 } 126 136 } 127 137 } … … 219 229 } 220 230 221 222 231 return true; 223 232 } … … 233 242 for (it = filenames.begin(); it != it_end; ++ it) 234 243 { 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; 243 256 244 257 if (!WriteFile(outputFilename)) … … 279 292 while (fgets(str, len, file) != NULL) 280 293 { 294 //cout << "line: " << line << endl; 295 281 296 switch (str[0]) 282 297 { … … 323 338 //-- indices in the current line 324 339 340 //cout << "f: " << vertices.size() << " n: " << normals.size() << " " << texcoords.size() << endl; 341 325 342 LoadIndices(str, 326 343 vertices, normals, texcoords,
Note: See TracChangeset
for help on using the changeset viewer.