34 std::ifstream fileInput(fileName);
35 if (!fileInput || !fileInput.is_open()) {
39 std::string tmpLine, token;
40 int totalVertices = 0, totalNormals = 0,
41 totalTexC = 0, totalFaces = 0;
43 while (std::getline(fileInput, tmpLine))
45 if (tmpLine.empty()) {
49 std::stringstream ss(tmpLine);
51 if (token ==
"v") { totalVertices++; }
52 else if (token ==
"vn") { totalNormals++; }
53 else if (token ==
"vt") { totalTexC++; }
54 else if (token ==
"f") { totalFaces++; }
57 fileInput.seekg(0, fileInput.beg);
59 float* verts =
new float[totalVertices*3];
60 float* norms =
new float[totalNormals*3];
61 float* texC =
new float[totalTexC*2];
62 int* faces =
new int[totalFaces*9];
63 int vIndex = 0, nIndex = 0, tIndex = 0, fIndex = 0, index = 0;
65 while (std::getline(fileInput, tmpLine))
67 if (tmpLine.empty()) {
71 std::stringstream ss(tmpLine);
75 verts[vIndex++] = (float)std::atof(token.c_str());
78 verts[vIndex++] = (float)std::atof(token.c_str());
81 verts[vIndex++] = (float)std::atof(token.c_str());
84 else if (token ==
"vn") {
86 norms[nIndex++] = (float)std::atof(token.c_str());
89 norms[nIndex++] = (float)std::atof(token.c_str());
92 norms[nIndex++] = (float)std::atof(token.c_str());
95 else if (token ==
"vt") {
97 texC[tIndex++] = (float)std::atof(token.c_str());
100 texC[tIndex++] = (float)std::atof(token.c_str());
103 else if (token ==
"f") {
104 for (
int i=0; i<3; i++) {
106 int len = (int)token.size();
107 for (
int s=0; s<len+1; s++) {
109 if (token[s] !=
'/' && s < len) {
110 buff[index++] = token[s];
113 faces[fIndex++] = (int)atoi(buff);
128 vIndex = 0, nIndex = 0, tIndex = 0, fIndex = 0, index = 0;
130 model->
vertices =
new float[totalFaces * 3 * 3];
131 if (totalNormals) { model->
normals =
new float[totalFaces * 3 * 3]; }
132 if (totalTexC) { model->
texCoords =
new float[totalFaces * 3 * 2]; }
134 for (
int f=0; f<totalFaces*9; f+=3) {
135 model->
vertices[vIndex+0] = verts[(faces[f+0]-1)*3 + 0];
136 model->
vertices[vIndex+1] = verts[(faces[f+0]-1)*3 + 1];
137 model->
vertices[vIndex+2] = verts[(faces[f+0]-1)*3 + 2];
141 model->
texCoords[tIndex+0] = texC[(faces[f+1]-1)*2 + 0];
142 model->
texCoords[tIndex+1] = texC[(faces[f+1]-1)*2 + 1];
147 model->
normals[nIndex + 0] = norms[(faces[f+2]-1)*3 + 0];
148 model->
normals[nIndex + 1] = norms[(faces[f+2]-1)*3 + 1];
149 model->
normals[nIndex + 2] = norms[(faces[f+2]-1)*3 + 2];
bool SetVertices(PrimitiveType type, int totalVertices, int stride, char *vertices)
Definition ModelData.cpp:53