0

I want to preload 3d obj files before I run InitWindow from Raylib by preloading with assimp. So I have the following code:

std::vector<Mesh> preloadedMeshes;    // Store preloaded meshes
std::vector<Model> terrainTiles;      // Models created from the preloaded meshes

// Function to load meshes with Assimp
Mesh LoadMeshWithAssimp(const std::string& filePath) {
    Assimp::Importer importer;
    const aiScene* scene = importer.ReadFile(filePath, aiProcess_Triangulate | aiProcess_FlipUVs);

    if (!scene || !scene->HasMeshes()) {
        throw std::runtime_error("Failed to load mesh: " + filePath);
    }

    aiMesh* ai_mesh = scene->mMeshes[0]; // Assume a single mesh for simplicity

    Mesh mesh = { 0 };
    mesh.vertexCount = ai_mesh->mNumVertices;
    mesh.triangleCount = ai_mesh->mNumFaces;

    // Allocate and copy vertex positions (CPU memory)
    mesh.vertices = (float*)MemAlloc(mesh.vertexCount * 3 * sizeof(float));
    for (unsigned int i = 0; i < mesh.vertexCount; i++) {
        mesh.vertices[i * 3 + 0] = ai_mesh->mVertices[i].x;
        mesh.vertices[i * 3 + 1] = ai_mesh->mVertices[i].y;
        mesh.vertices[i * 3 + 2] = ai_mesh->mVertices[i].z;
    }

    // Allocate and copy normals (CPU memory)
    if (ai_mesh->HasNormals()) {
        mesh.normals = (float*)MemAlloc(mesh.vertexCount * 3 * sizeof(float));
        for (unsigned int i = 0; i < mesh.vertexCount; i++) {
            mesh.normals[i * 3 + 0] = ai_mesh->mNormals[i].x;
            mesh.normals[i * 3 + 1] = ai_mesh->mNormals[i].y;
            mesh.normals[i * 3 + 2] = ai_mesh->mNormals[i].z;
        }
    }

    // Allocate and copy texture coordinates (CPU memory)
    if (ai_mesh->HasTextureCoords(0)) {
        mesh.texcoords = (float*)MemAlloc(mesh.vertexCount * 2 * sizeof(float));
        for (unsigned int i = 0; i < mesh.vertexCount; i++) {
            mesh.texcoords[i * 2 + 0] = ai_mesh->mTextureCoords[0][i].x;
            mesh.texcoords[i * 2 + 1] = ai_mesh->mTextureCoords[0][i].y;
        }
    }

    // Allocate and copy indices (CPU memory)
    mesh.indices = (unsigned short*)MemAlloc(mesh.triangleCount * 3 * sizeof(unsigned short));
    for (unsigned int i = 0; i < mesh.triangleCount; i++) {
        mesh.indices[i * 3 + 0] = (unsigned short)ai_mesh->mFaces[i].mIndices[0];
        mesh.indices[i * 3 + 1] = (unsigned short)ai_mesh->mFaces[i].mIndices[1];
        mesh.indices[i * 3 + 2] = (unsigned short)ai_mesh->mFaces[i].mIndices[2];
    }

    return mesh; // CPU-side Mesh, not yet uploaded
}



// Preload meshes before initializing OpenGL
void PreloadMeshes() {
    try {
        preloadedMeshes.push_back(LoadMeshWithAssimp("resources/models/Farabale_0.obj"));
        preloadedMeshes.push_back(LoadMeshWithAssimp("resources/models/Farabale_1.obj"));
        preloadedMeshes.push_back(LoadMeshWithAssimp("resources/models/Farabale_2.obj"));
        preloadedMeshes.push_back(LoadMeshWithAssimp("resources/models/Farabale_3.obj"));
    } catch (const std::exception& e) {
        TraceLog(LOG_ERROR, e.what());
    }
}

// Upload meshes to GPU and create models
void CreateModelsFromMeshes() {
    for (auto& mesh : preloadedMeshes) {
        UploadMesh(&mesh, true);  // Upload the mesh to GPU
        Model model = LoadModelFromMesh(mesh);
        terrainTiles.push_back(model);
    }
}

And if I do the following:

const int screenWidth = 1280;
const int screenHeight = 720;
Vector3 unityPosition = (Vector3){-189.29, 105.66, 68.22};
PreloadMeshes();
InitWindow(screenWidth, screenHeight, "Dear ImGui Raylib(OpenGL) example");
CreateModelsFromMeshes();

I can see very weird obj files loaded. Take a look:

enter image description here

But if I comment out and call loading of obj by the LoadModel function provided from raylib I see the scene perfectly fine:

PreloadMeshes();
InitWindow(screenWidth, screenHeight, "Dear ImGui Raylib(OpenGL) example");
//CreateModelsFromMeshes();

terrainTiles.push_back(LoadModel("resources/models/Farabale_0.obj"));
terrainTiles.push_back(LoadModel("resources/models/Farabale_1.obj"));
terrainTiles.push_back(LoadModel("resources/models/Farabale_2.obj"));
terrainTiles.push_back(LoadModel("resources/models/Farabale_3.obj"));

Produces this:

enter image description here

So as you can see no weird artifacts. Why is that? What am I doing wrong with Assimp ?

1
  • Did you verify that the obj file only contains a single mesh? You can use a debugger to query the output of LoadModel and look at the meshCount member.
    – Botje
    Commented Dec 18, 2024 at 8:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.