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
-1
View File
@@ -1,7 +1,6 @@
------------ Tyra's v2.0 roadmap to publish on GitHub ------------ ------------ Tyra's v2.0 roadmap to publish on GitHub ------------
--- H4570 --- H4570
- [Loaders] ".tyrobj" format
- [Loaders] DFF loader as static Mesh loader - [Loaders] DFF loader as static Mesh loader
- [Game] Update intellisense from docker - [Game] Update intellisense from docker
- [Texture] Test cache hits with large amount of textures (dolphin sample?) - [Texture] Test cache hits with large amount of textures (dolphin sample?)
+99 -99
View File
@@ -126,8 +126,6 @@ void TyrobjLoader::loadFile(FILE* file, const std::string& path,
char* lineHeader = new char[128]; char* lineHeader = new char[128];
while (res != EOF) { while (res != EOF) {
res = fscanf(file, "%s", lineHeader);
if (strcmp(lineHeader, "v") == 0) { if (strcmp(lineHeader, "v") == 0) {
readVertices(&readInfo, file, frameIndex, inputData, outputData); readVertices(&readInfo, file, frameIndex, inputData, outputData);
} else if (strcmp(lineHeader, "vt") == 0) { } 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) { } else if (strcmp(lineHeader, "f") == 0) {
readFaces(&readInfo, file, frameIndex, inputData, outputData); readFaces(&readInfo, file, frameIndex, inputData, outputData);
} }
res = fscanf(file, "%s", lineHeader);
} }
delete[] lineHeader; delete[] lineHeader;
@@ -207,114 +207,114 @@ void TyrobjLoader::readMaterials(TyraobjReadInfo* info, FILE* file,
info->faceI = 0; info->faceI = 0;
} }
int test123 = 0;
void TyrobjLoader::readFaces(TyraobjReadInfo* info, FILE* file, void TyrobjLoader::readFaces(TyraobjReadInfo* info, FILE* file,
const u16& frameIndex, const u16& frameIndex,
const TyraobjData& inputData, const TyraobjData& inputData,
MeshBuilderData* outputData) { MeshBuilderData* outputData) {
// int* x = nullptr; int* x = new int[9];
// fpos_t start; fpos_t start;
// fgetpos(file, &start); fgetpos(file, &start);
// int matches = int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &x[0], &x[1],
// fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", x, x, x, x, x, x, x, x, &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &x[8]);
// x); fsetpos(file, &start);
// fsetpos(file, &start);
// int newerMatches = 0;
// // TODO: No support of vc! // TODO: No support of vc!
// u32 vertexIndex[3], coordIndex[3], normalIndex[3]; // ,colorIndex[3]; u32 vertexIndex[3], coordIndex[3], normalIndex[3]; // ,colorIndex[3];
// for (u16 i = 0; i < 3; i++) { for (u16 i = 0; i < 3; i++) {
// vertexIndex[i] = 0; vertexIndex[i] = 0;
// coordIndex[i] = 0; coordIndex[i] = 0;
// normalIndex[i] = 0; normalIndex[i] = 0;
// // colorIndex[i] = 0; // colorIndex[i] = 0;
// } }
// switch (matches) { int newerMatches = 0;
// /** 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;
// /** Loaded only two digits (V, VT) succesfuly. Not setting switch (matches) {
// VN. */ /** Vs, VTs and VNs all set */
// case 3: { case 9: {
// fscanf(file, "%d/%d/ %d/%d/ %d/%d/\n", &vertexIndex[0], &coordIndex[0], fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0],
// &vertexIndex[1], &coordIndex[1], &vertexIndex[2], &coordIndex[0], &normalIndex[0], &vertexIndex[1], &coordIndex[1],
// &coordIndex[2]); &normalIndex[1], &vertexIndex[2], &coordIndex[2], &normalIndex[2]);
// } break; } break;
// /** Only V set. Checking for existance of VT or VN. */ /** Loaded only two digits (V, VT) succesfuly. Not setting
// case 1: { VN. */
// /** Check for existance of V/// configuration.. */ case 3: {
// newerMatches = fscanf(file, "%d// %d// %d//\n", x, x, x); fscanf(file, "%d/%d/ %d/%d/ %d/%d/\n", &vertexIndex[0], &coordIndex[0],
// fsetpos(file, &start); &vertexIndex[1], &coordIndex[1], &vertexIndex[2], &coordIndex[2]);
// if (newerMatches == 3) { } break;
// /** 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;
// }
// }
// if (matches == 9 || matches == 2 || matches == 1) { /** Only V set. Checking for existance of VT or VN. */
// outputData->materials[info->materialsI]->vertexFaces[info->faceI] = case 1: {
// vertexIndex[0] - 1; /** Check for existance of V/// configuration.. */
// outputData->materials[info->materialsI]->vertexFaces[info->faceI + 1] = newerMatches = fscanf(file, "%d// %d// %d//\n", &x[0], &x[1], &x[2]);
// vertexIndex[1] - 1; fsetpos(file, &start);
// outputData->materials[info->materialsI]->vertexFaces[info->faceI + 2] = if (newerMatches == 3) {
// vertexIndex[2] - 1; /** 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) { // Set vertex indices
// outputData->materials[info->materialsI]->textureCoordFaces[info->faceI] = if (matches == 9 || matches == 2 || matches == 1) {
// coordIndex[0] - 1; outputData->materials[info->materialsI]->vertexFaces[info->faceI] =
// outputData->materials[info->materialsI] vertexIndex[0] - 1;
// ->textureCoordFaces[info->faceI + 1] = coordIndex[1] - 1; outputData->materials[info->materialsI]->vertexFaces[info->faceI + 1] =
// outputData->materials[info->materialsI] vertexIndex[1] - 1;
// ->textureCoordFaces[info->faceI + 2] = coordIndex[2] - 1; outputData->materials[info->materialsI]->vertexFaces[info->faceI + 2] =
// } vertexIndex[2] - 1;
}
// if (matches == 9) { // Set texture coord indices
// outputData->materials[info->materialsI]->normalFaces[info->faceI] = if (matches == 9 || matches == 2) {
// normalIndex[0] - 1; outputData->materials[info->materialsI]->textureCoordFaces[info->faceI] =
// outputData->materials[info->materialsI]->normalFaces[info->faceI + 1] = coordIndex[0] - 1;
// normalIndex[1] - 1; outputData->materials[info->materialsI]
// outputData->materials[info->materialsI]->normalFaces[info->faceI + 2] = ->textureCoordFaces[info->faceI + 1] = coordIndex[1] - 1;
// normalIndex[2] - 1; outputData->materials[info->materialsI]
// info->faceI += 3; ->textureCoordFaces[info->faceI + 2] = coordIndex[2] - 1;
// } else if (matches == 2) { }
// info->faceI += 2;
// } else if (matches == 1) { // Set normal indices
// if (newerMatches == 3) { if (matches == 9) {
// info->faceI += 1; outputData->materials[info->materialsI]->normalFaces[info->faceI] =
// } else if (newerMatches == 6) { normalIndex[0] - 1;
// outputData->materials[info->materialsI]->normalFaces[info->faceI] = outputData->materials[info->materialsI]->normalFaces[info->faceI + 1] =
// normalIndex[0] - 1; normalIndex[1] - 1;
// outputData->materials[info->materialsI]->normalFaces[info->faceI + 1] = outputData->materials[info->materialsI]->normalFaces[info->faceI + 2] =
// normalIndex[1] - 1; normalIndex[2] - 1;
// outputData->materials[info->materialsI]->normalFaces[info->faceI + 2] = } else if (matches == 1 && newerMatches == 6) {
// normalIndex[2] - 1; outputData->materials[info->materialsI]->normalFaces[info->faceI] =
// info->faceI += 2; 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 } // namespace Tyra
+4 -28
View File
@@ -130,6 +130,7 @@ void H4570::loop() {
} }
for (u8 i = 0; i < warriorsCount; i++) warriors[i]->animate(); for (u8 i = 0; i < warriorsCount; i++) warriors[i]->animate();
cube->animate();
engine->renderer.beginFrame(CameraInfo3D(&cameraPosition, &cameraLookAt)); engine->renderer.beginFrame(CameraInfo3D(&cameraPosition, &cameraLookAt));
{ {
@@ -148,7 +149,7 @@ void H4570::loop() {
engine->renderer.renderer3D.usePipeline(&dynpip); engine->renderer.renderer3D.usePipeline(&dynpip);
{ {
// dynpip.render(cube); dynpip.render(cube);
Threading::switchThread(); Threading::switchThread();
for (u8 i = 0; i < warriorsCount; i++) { for (u8 i = 0; i < warriorsCount; i++) {
dynpip.render(warriors[i], dynOptions); dynpip.render(warriors[i], dynOptions);
@@ -196,35 +197,10 @@ DynamicMesh* getWarrior(Renderer* renderer) {
DynamicMesh* getCube(Renderer* renderer) { DynamicMesh* getCube(Renderer* renderer) {
TyrobjLoader loader; TyrobjLoader loader;
auto* data = auto* data =
loader.load(FileUtils::fromCwd("untitled.tyrobj"), 2, .08F, false); loader.load(FileUtils::fromCwd("untitled.tyrobj"), 2, 3.0F, false);
TYRA_LOG("Frames count: ", data->framesCount);
TYRA_LOG("Materials count: ", data->materialsCount);
for (u32 i = 0; i < data->framesCount; i++) {
TYRA_LOG("Vertex count ", i, ": ", data->frames[i]->verticesCount);
for (u32 j = 0; j < data->frames[i]->verticesCount; j++) {
TYRA_LOG("Vertex ", j, ": ", data->frames[i]->vertices[j].getPrint());
}
TYRA_LOG("Normals count ", i, ": ", data->frames[i]->normalsCount);
for (u32 j = 0; j < data->frames[i]->normalsCount; j++) {
TYRA_LOG("Normal ", j, ": ", data->frames[i]->normals[j].getPrint());
}
}
for (u32 i = 0; i < data->materialsCount; i++) {
TYRA_LOG("Material ", i, ": ", data->materials[i]->name);
TYRA_LOG("Faces count: ", data->materials[i]->count);
for (u32 j = 0; j < data->materials[i]->count; j++) {
TYRA_LOG("Vertex face ", j, ": ", data->materials[i]->vertexFaces);
TYRA_LOG("Normal face ", j, ": ", data->materials[i]->normalFaces);
}
}
TYRA_BREAKPOINT();
auto* result = new DynamicMesh(*data); auto* result = new DynamicMesh(*data);
// result->translation.translateZ(-30.0F); result->translation.translateZ(-30.0F);
delete data; delete data;
result->playAnimation(0, result->getFramesCount() - 1); result->playAnimation(0, result->getFramesCount() - 1);