C++/Raylib — невозможно загрузить сетку с помощью AssimpC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 C++/Raylib — невозможно загрузить сетку с помощью Assimp

Сообщение Anonymous »

Я хочу предварительно загрузить 3D-файлы obj перед запуском InitWindow из Raylib, предварительно загрузив их с помощью assimp. Итак, у меня есть следующий код:

Код: Выделить всё

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();
Я вижу загруженные очень странные obj-файлы. Взгляните:
Изображение

Но если я закомментирую и вызову загрузку объекта с помощью функции 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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»