Код: Выделить всё
std::vector preloadedMeshes; // Store preloaded meshes
std::vector 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);
}
}
Код: Выделить всё
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();

Но если я закомментирую и вызову загрузку объекта с помощью функции LoadModel, предоставленной из raylib, я увижу сцену совершенно нормально:
Код: Выделить всё
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"));
Чтобы вы не увидели никаких странных артефактов. Почему это? Что я делаю не так с Assimp?
Подробнее здесь: https://stackoverflow.com/questions/792 ... ith-assimp
Мобильная версия