• Home
  • About
    • Muru Chen photo

      Muru Chen

      Game programmer, Game designer, photographer & Interactive media artist

    • Learn More
    • Email
    • LinkedIn
    • Github
  • Games
  • OpenGL
  • Interactive Arts

Load Model

Reading time ~1 minute

image

Use Assimp library

Mesh

I use a mesh class to store vertices, intices and texture coords.

Set up

    class Mesh {
       public:
           vector<Vertex> vertices;
           vector<unsigned int> indices;
           vector<Texture> textures;
           Mesh(vector<Vertex> vertices, vector<unsigned int> indices, vector<Texture> textures);
           void Draw(Shader shader);
       private:
           unsigned int VAO, VBO, EBO;
           void setupMesh();
};  

Render

We get correspond uniform value name from 0 to n per texture type. like “material.texture_diffuse0”, “material.texture_specular1”. Give the value to active texture unit and bind the texture.

    class Mesh {
       public:
           vector<Vertex> vertices;
           vector<unsigned int> indices;
           vector<Texture> textures;
           Mesh(vector<Vertex> vertices, vector<unsigned int> indices, vector<Texture> textures);
           void Draw(Shader shader);
       private:
           unsigned int VAO, VBO, EBO;
           void setupMesh();
    };  

Model

Use Assimp to load model and get assimp scene and assimp mesh.

We need to get data from assimp mesh to set up the mesh that we defined.

Also we need to be careful that sometimes the model does not have texture images. Instead, it has a default texture that is from the modeling software

    unsigned int LoadTextureFromAssImp(const aiTexture* aiTex)
    {
	    if (aiTex == nullptr)
	    {
	    	return 0;
	    }
	    unsigned int textureID;
	    glGenTextures(1, &textureID);
	    glBindTexture(GL_TEXTURE_2D, textureID);

	    int width, height, nrComponents;
	    unsigned char* data = nullptr;
	    if (aiTex->mHeight == 0)
	    	data = stbi_load_from_memory(reinterpret_cast<unsigned char*>(aiTex->pcData), aiTex->mWidth, &width, &height, &nrComponents, 0);
	    else
	    	data = stbi_load_from_memory(reinterpret_cast<unsigned char*>(aiTex->pcData), aiTex->mWidth * aiTex->mHeight, &width, &height, &nrComponents, 0);
        ....
    }
Download codes


OpenGLC++ Share Tweet +1