Guest User

Model.h

a guest
Feb 12th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <stdio.h>
  4. #include <string>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <iostream>
  8. #include <map>
  9. #include <vector>
  10.  
  11. #include <GL/glew.h>
  12. #include <glm/glm.hpp>
  13. #include <glm/gtc/matrix_transform.hpp>
  14. #include <SOIL/SOIL.h>
  15. #include <assimp/Importer.hpp>
  16. #include <assimp/scene.h>
  17. #include <assimp/postprocess.h>
  18. #include "TextureLoader.hpp"
  19. #include "Mesh.h"
  20.  
  21. using namespace std;
  22.  
  23. class Model
  24. {
  25. public:
  26.     /*  Functions   */
  27.     Model( GLchar *path )
  28.     {
  29.         this->loadModel( path );
  30.     }
  31.    
  32.     void Draw( Shader shader )
  33.     {
  34.         for ( GLuint i = 0; i < this->meshes.size( ); i++ )
  35.         {
  36.             this->meshes[i].Draw( shader );
  37.         }
  38.  
  39.     }
  40.    
  41. private:
  42.     /*  Model Data  */
  43.     vector<Mesh> meshes;
  44.     string directory;
  45.     vector<Texture> textures_loaded;   
  46.    
  47.     /*  Functions   */
  48.     void loadModel( string path )
  49.     {
  50.         Assimp::Importer importer;
  51.         const aiScene *scene = importer.ReadFile( path, aiProcess_Triangulate | aiProcess_FlipUVs );
  52.        
  53.         if( !scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode ) // if is Not Zero
  54.         {
  55.             cout << "Model could not be loaded!\n " << importer.GetErrorString( ) << endl;
  56.             return;
  57.         }
  58.         this->directory = path.substr( 0, path.find_last_of( '/' ) );
  59.        
  60.         this->processNode( scene->mRootNode, scene );
  61.     }
  62.    
  63.     void processNode( aiNode* node, const aiScene* scene )
  64.     {        
  65.         for ( GLuint i = 0; i < node->mNumMeshes; i++ )
  66.         {
  67.             aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
  68.            
  69.             this->meshes.push_back( this->processMesh( mesh, scene ) );
  70.         }
  71.        
  72.         for ( GLuint i = 0; i < node->mNumChildren; i++ )
  73.         {
  74.             this->processNode( node->mChildren[i], scene );
  75.         }
  76.     }
  77.    
  78.     Mesh processMesh( aiMesh *mesh, const aiScene *scene )
  79.     {
  80.         vector<Vertex> vertices;
  81.         vector<GLuint> indices;
  82.         vector<Texture> textures;
  83.        
  84.         for ( GLuint i = 0; i < mesh->mNumVertices; i++ )
  85.         {
  86.             Vertex vertex;
  87.             // Positions
  88.             vertex.Position.x = mesh->mVertices[i].x;
  89.             vertex.Position.y = mesh->mVertices[i].y;
  90.             vertex.Position.z = mesh->mVertices[i].z;
  91.            
  92.             // Normals
  93.             vertex.Normal.x = mesh->mNormals[i].x;
  94.             vertex.Normal.y = mesh->mNormals[i].y;
  95.             vertex.Normal.z = mesh->mNormals[i].z;
  96.            
  97.             // Texture Coordinates
  98.             if( mesh->mTextureCoords[0] )
  99.             {
  100.                 vertex.TexCoords.x = mesh->mTextureCoords[0][i].x;
  101.                 vertex.TexCoords.y = mesh->mTextureCoords[0][i].y;
  102.             }
  103.             else
  104.             {
  105.                 vertex.TexCoords = glm::vec2( 0.0f, 0.0f );
  106.             }
  107.            
  108.             vertices.push_back( vertex );
  109.         }
  110.        
  111.         for ( GLuint i = 0; i < mesh->mNumFaces; i++ )
  112.         {
  113.             aiFace face = mesh->mFaces[i];
  114.  
  115.             for ( GLuint j = 0; j < face.mNumIndices; j++ )
  116.             {
  117.                 indices.push_back( face.mIndices[j] );
  118.             }
  119.         }
  120.        
  121.         // Process materials
  122.         if( mesh->mMaterialIndex >= 0 )
  123.         {
  124.             aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
  125.  
  126.            
  127.             // 1. Diffuse maps
  128.             vector<Texture> diffuseMaps = this->loadMaterialTextures( material, aiTextureType_DIFFUSE, "texture_diffuse" );
  129.             textures.insert( textures.end( ), diffuseMaps.begin( ), diffuseMaps.end( ) );
  130.            
  131.             // 2. Specular maps
  132.             vector<Texture> specularMaps = this->loadMaterialTextures( material, aiTextureType_SPECULAR, "texture_specular" );
  133.             textures.insert( textures.end( ), specularMaps.begin( ), specularMaps.end( ) );
  134.         }
  135.        
  136.  
  137.         return Mesh( vertices, indices, textures );
  138.     }
  139.    
  140.     vector<Texture> loadMaterialTextures( aiMaterial *mat, aiTextureType type, string typeName )
  141.     {
  142.         vector<Texture> textures;
  143.        
  144.         for ( GLuint i = 0; i < mat->GetTextureCount( type ); i++ )
  145.         {
  146.             aiString str;
  147.             mat->GetTexture( type, i, &str );
  148.            
  149.  
  150.             GLboolean skip = false;
  151.            
  152.             for ( GLuint j = 0; j < textures_loaded.size( ); j++ )
  153.             {
  154.                 if( textures_loaded[j].path == str )
  155.                 {
  156.                     textures.push_back( textures_loaded[j] );
  157.                     skip = true;
  158.                    
  159.                     break;
  160.                 }
  161.             }
  162.            
  163.             if( !skip )
  164.             {
  165.                 Texture texture;
  166.                 texture.id = TextureLoader::loadTextureFromFile( str.C_Str( ), this->directory );
  167.                 texture.type = typeName;
  168.                 texture.path = str;
  169.                 textures.push_back( texture );
  170.                
  171.                 this->textures_loaded.push_back( texture );
  172.             }
  173.         }
  174.        
  175.         return textures;
  176.     }
  177. };
Add Comment
Please, Sign In to add comment