obj loader

This commit is contained in:
h4570
2022-07-26 20:27:01 +02:00
parent 7a9b294945
commit 97e44507ff
3 changed files with 103 additions and 128 deletions
+99 -99
View File
@@ -126,8 +126,6 @@ void TyrobjLoader::loadFile(FILE* file, const std::string& path,
char* lineHeader = new char[128];
while (res != EOF) {
res = fscanf(file, "%s", lineHeader);
if (strcmp(lineHeader, "v") == 0) {
readVertices(&readInfo, file, frameIndex, inputData, outputData);
} else if (strcmp(lineHeader, "vt") == 0) {
@@ -141,6 +139,8 @@ void TyrobjLoader::loadFile(FILE* file, const std::string& path,
} else if (strcmp(lineHeader, "f") == 0) {
readFaces(&readInfo, file, frameIndex, inputData, outputData);
}
res = fscanf(file, "%s", lineHeader);
}
delete[] lineHeader;
@@ -207,114 +207,114 @@ void TyrobjLoader::readMaterials(TyraobjReadInfo* info, FILE* file,
info->faceI = 0;
}
int test123 = 0;
void TyrobjLoader::readFaces(TyraobjReadInfo* info, FILE* file,
const u16& frameIndex,
const TyraobjData& inputData,
MeshBuilderData* outputData) {
// int* x = nullptr;
// fpos_t start;
// fgetpos(file, &start);
// int matches =
// fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", x, x, x, x, x, x, x, x,
// x);
// fsetpos(file, &start);
// int newerMatches = 0;
int* x = new int[9];
fpos_t start;
fgetpos(file, &start);
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &x[0], &x[1],
&x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &x[8]);
fsetpos(file, &start);
// // TODO: No support of vc!
// u32 vertexIndex[3], coordIndex[3], normalIndex[3]; // ,colorIndex[3];
// for (u16 i = 0; i < 3; i++) {
// vertexIndex[i] = 0;
// coordIndex[i] = 0;
// normalIndex[i] = 0;
// // colorIndex[i] = 0;
// }
// TODO: No support of vc!
u32 vertexIndex[3], coordIndex[3], normalIndex[3]; // ,colorIndex[3];
for (u16 i = 0; i < 3; i++) {
vertexIndex[i] = 0;
coordIndex[i] = 0;
normalIndex[i] = 0;
// colorIndex[i] = 0;
}
// switch (matches) {
// /** Vs, VTs and VNs all set */
// case 9: {
// fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0],
// &coordIndex[0], &normalIndex[0], &vertexIndex[1],
// &coordIndex[1], &normalIndex[1], &vertexIndex[2],
// &coordIndex[2], &normalIndex[2]);
// } break;
int newerMatches = 0;
// /** Loaded only two digits (V, VT) succesfuly. Not setting
// VN. */
// case 3: {
// fscanf(file, "%d/%d/ %d/%d/ %d/%d/\n", &vertexIndex[0], &coordIndex[0],
// &vertexIndex[1], &coordIndex[1], &vertexIndex[2],
// &coordIndex[2]);
// } break;
switch (matches) {
/** Vs, VTs and VNs all set */
case 9: {
fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0],
&coordIndex[0], &normalIndex[0], &vertexIndex[1], &coordIndex[1],
&normalIndex[1], &vertexIndex[2], &coordIndex[2], &normalIndex[2]);
} break;
// /** Only V set. Checking for existance of VT or VN. */
// case 1: {
// /** Check for existance of V/// configuration.. */
// newerMatches = fscanf(file, "%d// %d// %d//\n", x, x, x);
// fsetpos(file, &start);
// if (newerMatches == 3) {
// /** Configuration confirmed. */
// fscanf(file, "%d// %d// %d//\n", &vertexIndex[0], &vertexIndex[1],
// &vertexIndex[2]);
// } else {
// /** Failed, checking configuration V//VN */
// newerMatches = fscanf(file, "%d//%d %d//%d %d//%d", x, x, x, x, x,
// x); fsetpos(file, &start); TYRA_ASSERT(newerMatches == 6, "Unknown
// .obj face for .obj file!");
// /** Configuration confirmed. */
// newerMatches = fscanf(file, "%d//%d %d//%d %d//%d", &vertexIndex[0],
// &normalIndex[0], &vertexIndex[1],
// &normalIndex[1], &vertexIndex[2],
// &normalIndex[2]);
// }
// break;
// } break;
// default: {
// TYRA_TRAP("Unknown faces format in .obj file!");
// break;
// }
// }
/** Loaded only two digits (V, VT) succesfuly. Not setting
VN. */
case 3: {
fscanf(file, "%d/%d/ %d/%d/ %d/%d/\n", &vertexIndex[0], &coordIndex[0],
&vertexIndex[1], &coordIndex[1], &vertexIndex[2], &coordIndex[2]);
} break;
// if (matches == 9 || matches == 2 || matches == 1) {
// outputData->materials[info->materialsI]->vertexFaces[info->faceI] =
// vertexIndex[0] - 1;
// outputData->materials[info->materialsI]->vertexFaces[info->faceI + 1] =
// vertexIndex[1] - 1;
// outputData->materials[info->materialsI]->vertexFaces[info->faceI + 2] =
// vertexIndex[2] - 1;
// }
/** Only V set. Checking for existance of VT or VN. */
case 1: {
/** Check for existance of V/// configuration.. */
newerMatches = fscanf(file, "%d// %d// %d//\n", &x[0], &x[1], &x[2]);
fsetpos(file, &start);
if (newerMatches == 3) {
/** Configuration confirmed. */
fscanf(file, "%d// %d// %d//\n", &vertexIndex[0], &vertexIndex[1],
&vertexIndex[2]);
} else {
/** Failed, checking configuration V//VN */
newerMatches = fscanf(file, "%d//%d %d//%d %d//%d", &x[0], &x[1], &x[2],
&x[3], &x[4], &x[5]);
fsetpos(file, &start);
TYRA_ASSERT(newerMatches == 6, "Unknown .obj face for .obj file!");
/** Configuration confirmed. */
newerMatches = fscanf(file, "%d//%d %d//%d %d//%d", &vertexIndex[0],
&normalIndex[0], &vertexIndex[1], &normalIndex[1],
&vertexIndex[2], &normalIndex[2]);
}
break;
} break;
default: {
TYRA_TRAP("Unknown faces format in .obj file!");
break;
}
}
// if (matches == 9 || matches == 2) {
// outputData->materials[info->materialsI]->textureCoordFaces[info->faceI] =
// coordIndex[0] - 1;
// outputData->materials[info->materialsI]
// ->textureCoordFaces[info->faceI + 1] = coordIndex[1] - 1;
// outputData->materials[info->materialsI]
// ->textureCoordFaces[info->faceI + 2] = coordIndex[2] - 1;
// }
// Set vertex indices
if (matches == 9 || matches == 2 || matches == 1) {
outputData->materials[info->materialsI]->vertexFaces[info->faceI] =
vertexIndex[0] - 1;
outputData->materials[info->materialsI]->vertexFaces[info->faceI + 1] =
vertexIndex[1] - 1;
outputData->materials[info->materialsI]->vertexFaces[info->faceI + 2] =
vertexIndex[2] - 1;
}
// if (matches == 9) {
// outputData->materials[info->materialsI]->normalFaces[info->faceI] =
// normalIndex[0] - 1;
// outputData->materials[info->materialsI]->normalFaces[info->faceI + 1] =
// normalIndex[1] - 1;
// outputData->materials[info->materialsI]->normalFaces[info->faceI + 2] =
// normalIndex[2] - 1;
// info->faceI += 3;
// } else if (matches == 2) {
// info->faceI += 2;
// } else if (matches == 1) {
// if (newerMatches == 3) {
// info->faceI += 1;
// } else if (newerMatches == 6) {
// outputData->materials[info->materialsI]->normalFaces[info->faceI] =
// normalIndex[0] - 1;
// outputData->materials[info->materialsI]->normalFaces[info->faceI + 1] =
// normalIndex[1] - 1;
// outputData->materials[info->materialsI]->normalFaces[info->faceI + 2] =
// normalIndex[2] - 1;
// info->faceI += 2;
// }
// }
// Set texture coord indices
if (matches == 9 || matches == 2) {
outputData->materials[info->materialsI]->textureCoordFaces[info->faceI] =
coordIndex[0] - 1;
outputData->materials[info->materialsI]
->textureCoordFaces[info->faceI + 1] = coordIndex[1] - 1;
outputData->materials[info->materialsI]
->textureCoordFaces[info->faceI + 2] = coordIndex[2] - 1;
}
// Set normal indices
if (matches == 9) {
outputData->materials[info->materialsI]->normalFaces[info->faceI] =
normalIndex[0] - 1;
outputData->materials[info->materialsI]->normalFaces[info->faceI + 1] =
normalIndex[1] - 1;
outputData->materials[info->materialsI]->normalFaces[info->faceI + 2] =
normalIndex[2] - 1;
} else if (matches == 1 && newerMatches == 6) {
outputData->materials[info->materialsI]->normalFaces[info->faceI] =
normalIndex[0] - 1;
outputData->materials[info->materialsI]->normalFaces[info->faceI + 1] =
normalIndex[1] - 1;
outputData->materials[info->materialsI]->normalFaces[info->faceI + 2] =
normalIndex[2] - 1;
}
// Push index
info->faceI += 3;
delete[] x;
}
} // namespace Tyra