From 97e44507ffc74bc9eab455f0afa246d71cfe50b9 Mon Sep 17 00:00:00 2001 From: h4570 Date: Tue, 26 Jul 2022 20:27:01 +0200 Subject: [PATCH] obj loader --- ROADMAP.txt | 1 - .../src/loaders/3d/tyrobj/tyrobj_loader.cpp | 198 +++++++++--------- samples/h4570/src/h4570.cpp | 32 +-- 3 files changed, 103 insertions(+), 128 deletions(-) diff --git a/ROADMAP.txt b/ROADMAP.txt index 21bd5ce..dbb127d 100644 --- a/ROADMAP.txt +++ b/ROADMAP.txt @@ -1,7 +1,6 @@ ------------ Tyra's v2.0 roadmap to publish on GitHub ------------ --- H4570 -- [Loaders] ".tyrobj" format - [Loaders] DFF loader as static Mesh loader - [Game] Update intellisense from docker - [Texture] Test cache hits with large amount of textures (dolphin sample?) diff --git a/engine/src/loaders/3d/tyrobj/tyrobj_loader.cpp b/engine/src/loaders/3d/tyrobj/tyrobj_loader.cpp index f7c6b72..608403d 100644 --- a/engine/src/loaders/3d/tyrobj/tyrobj_loader.cpp +++ b/engine/src/loaders/3d/tyrobj/tyrobj_loader.cpp @@ -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 diff --git a/samples/h4570/src/h4570.cpp b/samples/h4570/src/h4570.cpp index 4907718..6905a91 100644 --- a/samples/h4570/src/h4570.cpp +++ b/samples/h4570/src/h4570.cpp @@ -130,6 +130,7 @@ void H4570::loop() { } for (u8 i = 0; i < warriorsCount; i++) warriors[i]->animate(); + cube->animate(); engine->renderer.beginFrame(CameraInfo3D(&cameraPosition, &cameraLookAt)); { @@ -148,7 +149,7 @@ void H4570::loop() { engine->renderer.renderer3D.usePipeline(&dynpip); { - // dynpip.render(cube); + dynpip.render(cube); Threading::switchThread(); for (u8 i = 0; i < warriorsCount; i++) { dynpip.render(warriors[i], dynOptions); @@ -196,35 +197,10 @@ DynamicMesh* getWarrior(Renderer* renderer) { DynamicMesh* getCube(Renderer* renderer) { TyrobjLoader loader; 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); - // result->translation.translateZ(-30.0F); + result->translation.translateZ(-30.0F); delete data; result->playAnimation(0, result->getFramesCount() - 1);