Sponza рэндэрлэх оролдлого 1, 3D Модел файлаа унших

одоогийн байдлаар ASSIMP санг ашиглаж sponza моделийг уншиж авч ачаалах цаашлаад рэндэрлэх зорилготой байна. Хэрэв ингэж чадвал график програмчлалын холбоотой янз бүрийн туршилт хийх боломж бүрдэнэ.

#include <string>
#include <iostream>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>

Эхний удаад файлыг ASSIMP аар уншуулж мэш болон материалуудыг нь уншиж авна.
Энэ уншуулах үйлдлийг Importer классаар гүйцэтгүүлдэг.
    Assimp::Importer importer;
уншигч классаар объект үүсгэсэн бол файлын зам болон уншиж авах горимуудыг нь тохируулж гишүүн функцүүдийг нь дуудна. Үүний хариуд aiScene төрөлтэй объект буцна. Энэ объект дотор тухайн моделийн талаархи бүх мэдээлэл бий.
    std::string filePath = "sponza.obj";
    const aiScene *scene = importer.ReadFile(
            filePath.c_str(),
            aiProcess_OptimizeGraph |
            aiProcess_OptimizeMeshes |
            aiProcess_ImproveCacheLocality |
            aiProcess_SplitLargeMeshes |
            aiProcess_Triangulate |
            aiProcess_JoinIdenticalVertices |
            aiProcess_SortByPType
            );
    if (!scene) {
        std::cout << "error in loading file : " << importer.GetErrorString() << std::endl;
        return 1;
    }
    std::cout << "number of meshes " << scene->mNumMeshes << std::endl;
    std::cout << "number of materials " << scene->mNumMaterials << std::endl;

эхний удаад материал болон текстур зургийн мэдээллийг нь гаргаж ирж харая
for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
        processMaterial(scene->mMaterials[i], i);
}

processMaterial функцийн эх бие дотор орж ирсэн материалын өнгө болон текстурын нэр зам гэх зэргийн мэдээллийг авч хэвлэж харуулна.
void processMaterial(aiMaterial* material, int index) {
    aiString materialName;

    // Get the name for managing purpose
    if (material->Get(AI_MATKEY_NAME, materialName) != AI_SUCCESS) {
        std::cout << "ERROR: while getting name of material" << std::endl;
        return;
    }

    aiString diffuseTexturePath, normalMapTexturePath, specularTexturePath;
    aiColor3D diffuseColor, ambientColor, specularColor, emissiveColor;
    float opacity, shininess;

    // Get diffuse color
    if (material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuseColor) != AI_SUCCESS) {
        std::cout << "ERROR: Could not get diffuse color for Material " << materialName.data << std::endl;
        return;
    }

    // Get ambient color
    if (material->Get(AI_MATKEY_COLOR_AMBIENT, ambientColor) != AI_SUCCESS) {
        std::cout << "ERROR: Could not get ambient color for Material " << materialName.data << std::endl;
        return;
    }

    // Get diffuse color
    if (material->Get(AI_MATKEY_COLOR_SPECULAR, specularColor) != AI_SUCCESS) {
        std::cout << "ERROR: Could not get specular color for Material " << materialName.data << std::endl;
        return;
    }

    // Get opacity
    if (material->Get(AI_MATKEY_OPACITY, opacity) != AI_SUCCESS) {
        std::cout << "ERROR: Could not get opacity for Material " << materialName.data << std::endl;
        return;
    }

    // Get emissive color
    if (material->Get(AI_MATKEY_COLOR_EMISSIVE, emissiveColor) != AI_SUCCESS)
        emissiveColor = aiColor3D(0);

    // Get shininess
    if (material->Get(AI_MATKEY_SHININESS, shininess) != AI_SUCCESS) {
        std::cout << "ERROR: Could not get Shininess for Material " << materialName.data << std::endl;
        return;
    }


    // material color parameters
    std::cout << "diffuse color [" << diffuseColor.r << "," << diffuseColor.g << "," << diffuseColor.b << "] opacity=" << opacity << std::endl;
    std::cout << "ambient color [" << ambientColor.r << "," << ambientColor.g << "," << ambientColor.b << "]" << std::endl;
    std::cout << "specular color [" << specularColor.r << "," << specularColor.g << "," << specularColor.b << "]" << std::endl;
    std::cout << "emissive color [" << emissiveColor.r << "," << emissiveColor.g << "," << emissiveColor.b << "]" << std::endl;

    // Load diffuse texture if there is one
    if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0) {
        if (material->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), diffuseTexturePath) != AI_SUCCESS) {
            std::cout << "ERROR: Could not get Diffuse Texture for Material " << materialName.data << std::endl;
            return;
        }
        std::cout << "diffuse texture path " << diffuseTexturePath.data << std::endl;
    }

    // Load height map texture if there is one
    if (material->GetTextureCount(aiTextureType_HEIGHT) > 0) {
        if (material->Get(AI_MATKEY_TEXTURE(aiTextureType_HEIGHT, 0), normalMapTexturePath) != AI_SUCCESS) {
            std::cout << "ERROR: Could not get Height Map Texture for Material " << materialName.data << std::endl;
            return;
        }
        std::cout << "height map texture path " << normalMapTexturePath.data << std::endl;
    }

    // Load mask texture if there is one
    if (material->GetTextureCount(aiTextureType_SPECULAR) > 0) {
        if (material->Get(AI_MATKEY_TEXTURE(aiTextureType_SPECULAR, 0), specularTexturePath) != AI_SUCCESS) {
            std::cout << "ERROR: Could not get Mask Texture for Material " << materialName.data << std::endl;
            return;
        }
        std::cout << "specular texture path " << specularTexturePath.data << std::endl;
    }
}

одоогоор ямар нэгэн алдаагүй хэвийн ажиллаж байна. Гаралтын өгөгдлүүд нь
number of meshes 34
number of materials 27
diffuse color [0.6,0.6,0.6] opacity=1
ambient color [0,0,0]
specular color [0,0,0]
emissive color [0,0,0]
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_thorn_diff.tga
height map texture path textures\sponza_thorn_ddn.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\vase_round.tga
height map texture path textures\vase_round_ddn.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\vase_plant.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\background.tga
height map texture path textures\background_ddn.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\gi_flag.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\spnza_bricks_a_diff.tga
height map texture path textures\spnza_bricks_a_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_arch_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_ceiling_a_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_column_a_diff.tga
height map texture path textures\sponza_column_a_ddn.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_floor_a_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_column_c_diff.tga
height map texture path textures\sponza_column_c_ddn.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_details_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_column_b_diff.tga
height map texture path textures\sponza_column_b_ddn.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_flagpole_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_fabric_green_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_fabric_blue_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_fabric_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_curtain_blue_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_curtain_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_curtain_green_diff.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\chain_texture.tga
height map texture path textures\chain_texture_ddn.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\vase_hanging.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\vase_dif.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\lion.tga
height map texture path textures\lion_ddn.tga
diffuse color [0.588,0.588,0.588] opacity=1
ambient color [0.588,0.588,0.588]
specular color [0,0,0]
emissive color [0,0,0]
diffuse texture path textures\sponza_roof_diff.tga

RUN SUCCESSFUL (total time: 2s)

Програмыг компайлдаж холбох горим нь тун энгийн. Ямар нэгэн OpenGL-тэй холбоотой функц хэрэглээгүй зөвхөн ASSIMP санг хэрэглэж байгаа учраас
-lassimp.dll -lgdi32 -luser32 -lkernel32

Аан тийм lib холбосон ч гэсэн libassimp.dll файлыг үүссэн exe файлтай нэг хавтсан дотор эсвэл system фолдер дотор хуулж өгч байж ажиллуулах ёстой. Мөн Assimp санг mingw компилятортой хамт хэрэглэх тохиолдолд lib болон dll файлуудыг нь татахаар тавиагүй байдаг. Вэб хуудас дээрхи файлууд нь бүгд VisualC++ ийн хөрвүүлэгчидэд зориулагдсан байгаа тиймээс mingw-тэй хамтарч ажиллахгүй. Ийм болохоор Assimp санг github хуудаснаас нь татаж аваад CMake хэрэгслийг ашиглаж make файлаа гаргаж аваад mingw32-make командаар хөрвүүлж өөрийн гэсэн assimp lib dll файлуудаа гаргаж авна.
mingw-ээр хөрвүүлэхэд IFC модултай холбоотой бөөн алдаа асуудал гарсан, тиймээс IFC-г нь макро ашиглаж идэвхигүй болгож байгаад хөрвүүлэхэд амжилттай болж байна лээ. Mingw хэрэглэгчидэд асуудал гаргахгүйн үүднээс өөрийн хөрвүүлсэн 32 битийн хувилбарыг татахаар тавьлаа.

DOWNLOAD Assimp-Mingw32

Popular posts from this blog

Apache Spark + Cassandra. Hello World