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
| std::stringstream ssFileContent((char*)fileContent); std::string temp; std::vector<FloatData> positions, texcoords, normals;
std::vector<VertexDefine> vertexes;
std::vector<int> indexes;
char szOneLine[256];
while (!ssFileContent.eof()) { memset(szOneLine, 0, 256); ssFileContent.getline(szOneLine, 256); if (strlen(szOneLine) <= 0) { continue; } if (szOneLine[0] == 'v') { std::stringstream ssOneLine(szOneLine);
if (szOneLine[1] == 't') { FloatData floatData; ssOneLine >> temp; ssOneLine >> floatData.v[0]; ssOneLine >> floatData.v[1];
texcoords.push_back(floatData);
printf("texcoord : %f , %f\n", floatData.v[0], floatData.v[1]); } else if (szOneLine[1] == 'n') { FloatData floatData; ssOneLine >> temp; ssOneLine >> floatData.v[0]; ssOneLine >> floatData.v[1]; ssOneLine >> floatData.v[2];
normals.push_back(floatData);
printf("normal : %f , %f , %f\n", floatData.v[0], floatData.v[1], floatData.v[2]); } else { FloatData floatData; ssOneLine >> temp; ssOneLine >> floatData.v[0]; ssOneLine >> floatData.v[1]; ssOneLine >> floatData.v[2];
positions.push_back(floatData);
printf("position : %f , %f, %f \n", floatData.v[0], floatData.v[1], floatData.v[2]); } } else if (szOneLine[0] == 'f') { std::stringstream ssOneLine(szOneLine); ssOneLine >> temp; std::string vertexStr; for (int i = 0; i < 3; i++) { ssOneLine >> vertexStr; size_t pos1 = vertexStr.find_first_of('/'); std::string posIndexStr = vertexStr.substr(0, pos1);
size_t pos2 = vertexStr.find_first_of('/', pos1 + 1); std::string texcoordIndexStr = vertexStr.substr(pos1 + 1, pos2 - (pos1 + 1)); std::string normalIndexStr = vertexStr.substr(pos2 + 1, vertexStr.length() - (pos2 + 1));
VertexDefine vd; vd.posIndex = atoi(posIndexStr.c_str()); vd.texcoordIndex = atoi(texcoordIndexStr.c_str()); vd.nomalIndex = atoi(normalIndexStr.c_str());
int nCurrentVertexIndex = -1; int nCurrentVertexCount = (int)vertexes.size();
for (int j = 0; j < nCurrentVertexCount; j++) { if (vertexes[j].posIndex == vd.posIndex && vertexes[j].texcoordIndex == vd.texcoordIndex && vertexes[j].nomalIndex == vd.nomalIndex) { nCurrentVertexIndex = j; break; } }
if (nCurrentVertexIndex == -1) { nCurrentVertexIndex = (int)vertexes.size(); vertexes.push_back(vd); }
indexes.push_back(nCurrentVertexIndex); }
} }
mIndexCount = (int)indexes.size(); mIndexes = new unsigned short[mIndexCount];
for (int i = 0; i < mIndexCount; i++) { mIndexes[i] = indexes[i]; }
int vertexCount = (int)vertexes.size(); mVertexData = new VertexData[vertexCount];
for (int i = 0; i < vertexCount; i++) { memcpy(mVertexData[i].positions, positions[vertexes[i].posIndex - 1].v, sizeof(float) * 3); memcpy(mVertexData[i].texcoords, texcoords[vertexes[i].texcoordIndex - 1].v, sizeof(float) * 2); memcpy(mVertexData[i].normals, normals[vertexes[i].nomalIndex - 1].v, sizeof(float) * 3);
}
|