无法使用assimp加载3D模型

时间:2017-11-28 03:50:54

标签: c++ opengl 3d sfml assimp

我的生活就像装载3D模型一样。因此我尝试使用Assimp。

可悲的是,我无法从中得到任何结果。 我能够使网格类工作,并通过手动填充数据,我能够制作一个立方体。但我的模特课似乎不起作用。

Assimp没有返回任何类型的错误,所以我想在Assimp的心中我没有错(我可能做过)

根据调试器,数据已成功填写(注意:" mod"是模型的名称)

enter image description here

除此之外,代码的灵感来自this,最大的区别在于纹理加载(我使用SWML库加载纹理)和网格加载中的一点点,逻辑保持不变

这里是代码

model.h:

#ifndef MODEL_H
#define MODEL_H

#include "Mesh.h"
#include "ressourceManager/TextureManager.h"

#include <iostream>
#include <string>


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


using namespace std;

class Model
{
    public:
        Model(const string &path);
        void Draw(Shader &shader);

    private:
        vector<Mesh> meshes;

        string directory;

        void loadModel(const string &path);
        void processNode(aiNode *node, const aiScene *scene);
        Mesh processMesh(aiMesh *mesh, const aiScene *scene);
        vector<Texture> loadMaterialTextures(aiMaterial *mat, aiTextureType type, string typeName);

};

#endif // MODEL_H

model.cpp:

#include "Model.h"


Model::Model(const string &path)
{

    loadModel(path);
}

void Model::Draw(Shader &shader)
{
    for(Mesh mesh: meshes)
        mesh.draw(shader);

}

void Model::loadModel(const string &path)
{
    Assimp::Importer import;
    const aiScene *scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);

    if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode){
        cout << "ERROR::ASSIMP::" << import.GetErrorString() << endl;
        return;
    }

    directory = path.substr(0, path.find_last_of('/'));
    processNode(scene->mRootNode, scene);

}

void Model::processNode(aiNode* node, const aiScene* scene)
{
    for(unsigned int i = 0; i < node->mNumMeshes; i++){
        aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
        meshes.push_back(processMesh(mesh, scene));
    }

    for(unsigned int i = 0; i < node->mNumChildren; i++)
        processNode(node->mChildren[i], scene);

}

Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene)
{

    vector<Vertex> vertices;
    vector<GLuint> indices;
    vector<Texture> textures;

    for(unsigned int i = 0; i < mesh->mNumVertices; i++){
        Vertex vertex;

        glm::vec3 vector;

        vector.x = mesh->mVertices[i].x;
        vector.y = mesh->mVertices[i].y;
        vector.z = mesh->mVertices[i].z;
        vertex.pos = vector;

        vector.x = mesh->mNormals[i].x;
        vector.y = mesh->mNormals[i].y;
        vector.z = mesh->mNormals[i].z;
        vertex.normal = vector;

        if(mesh->mTextureCoords[0]){
            glm::vec2 vec;
            vec.x = mesh->mTextureCoords[0][i].x;
            vec.y = mesh->mTextureCoords[0][i].y;
            vertex.texCoord = vec;

        }else
            vertex.texCoord = glm::vec2(0.0f, 0.0f);


        vertices.push_back(vertex);
    }

    for ( GLuint i = 0; i < mesh->mNumFaces; i++ ){
        aiFace face = mesh->mFaces[i];
        for ( GLuint j = 0; j < face.mNumIndices; j++ )
            indices.push_back( face.mIndices[j] );
    }

    if(mesh->mMaterialIndex >= 0){
        aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];

        vector<Texture> diffuseMaps = this->loadMaterialTextures( material, aiTextureType_DIFFUSE, "texture_diffuse" );
        textures.insert( textures.end( ), diffuseMaps.begin( ), diffuseMaps.end( ) );

        // 2. Specular maps
        vector<Texture> specularMaps = this->loadMaterialTextures( material, aiTextureType_SPECULAR, "texture_specular" );
        textures.insert( textures.end( ), specularMaps.begin( ), specularMaps.end( ) );
   }

    return Mesh(vertices, indices, textures);
}

vector<Texture> Model::loadMaterialTextures(aiMaterial* mat, aiTextureType type, string typeName)
{

    vector<Texture> textures;

    for(unsigned int i = 0; i < mat->GetTextureCount(type); i++){

        aiString str;
        mat->GetTexture( type, i, &str );

        Texture texture;
        texture.ID = *RessourceManager::TextureManager::get(directory + "/" + str.C_Str());
        texture.type = typeName;
        texture.path = str;
        textures.push_back(texture);
    }
    return textures;
}

textureManager.h(我用来加载纹理的东西):

#ifndef TEXTUREMANAGER_H
#define TEXTUREMANAGER_H

#include <unordered_map>
#include <memory>
#include <GL/glew.h>

namespace RessourceManager{
    class TextureManager
        {
            public:
                TextureManager();
                static std::shared_ptr<GLuint> get(const std::string& name);
                static void removeUnused();
            private:
                static std::unordered_map<std::string, std::shared_ptr<GLuint>> p_textureIDs;
        };
    }

    #endif // TEXTUREMANAGER_H

textureManager.cpp

#include "TextureManager.h"
#include "SFML/Graphics.hpp"
#include <iostream>
namespace RessourceManager{
    TextureManager::TextureManager()
    {

    }

    std::shared_ptr<GLuint> TextureManager::get(const std::string& name)
    {
        const auto i = p_textureIDs.find(name);
        if(i != p_textureIDs.end())
            return i->second;
        else{
            std::shared_ptr<GLuint> p_textureID = std::make_shared<GLuint>();

            glGenTextures(1, p_textureID.get());


            sf::Image image;
            image.loadFromFile(name);

            glBindTexture(GL_TEXTURE_2D, *p_textureID.get());
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getSize().x, image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
            glGenerateMipmap(GL_TEXTURE_2D);

            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

            glBindTexture( GL_TEXTURE_2D, 0 );

            p_textureIDs.insert({name, p_textureID});
            std::cout << "new texture created" << std::endl;
            return p_textureID;
        }
    }

    void TextureManager::removeUnused()
    {
        for(auto i = p_textureIDs.begin(); i != p_textureIDs.end();)
            if(i->second.unique())
                i = p_textureIDs.erase(i);
            else
                ++i;
    }


    std::unordered_map<std::string, std::shared_ptr<GLuint>> TextureManager::p_textureIDs;

1 个答案:

答案 0 :(得分:0)

数据已成功加载。

您还需要以正确的方式将视图设置到模型上。也许你已经这样做但我无法在你的例子中检测到代码。

对于放置位置(0 | 0 | 0)的简单立方体,您可以查看此原点。您可以使用glm来计算正确的矩阵。您可以找到示例here

要检查的一点:您的顶点和片段也正确设置了设置?