Advertisement
CapsAdmin

Untitled

May 30th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1.  
  2. #include "IIndexedMesh.h"
  3. #include <assimp/Importer.hpp>      // C++ importer interface
  4. #include <assimp/scene.h>           // Output data structure
  5. #include <assimp/postprocess.h>     // Post processing flags
  6.  
  7. #pragma comment(lib, "assimp.lib")
  8.  
  9. void CmdAssimpTest(IConsoleCmdArgs *pArgs)
  10. {
  11.     auto pFile = pArgs->GetArg(1);
  12.  
  13.     // Create an instance of the Importer class
  14.     Assimp::Importer importer;
  15.     // And have it read the given file with some example postprocessing
  16.     // Usually - if speed is not the most important aspect for you - you'll
  17.     // propably to request more postprocessing than we do in this example.
  18.     const aiScene* scene = importer.ReadFile( pFile,
  19.         aiProcess_CalcTangentSpace       |
  20.         aiProcess_Triangulate            |
  21.         //aiProcess_JoinIdenticalVertices  |
  22.         aiProcess_GenNormals             |
  23.         aiProcess_SortByPType);
  24.  
  25.     // If the import failed, report it
  26.     if( !scene)
  27.     {
  28.         CryLogAlways( importer.GetErrorString());
  29.         return;
  30.     }
  31.     // Now we can access the file's contents.
  32.     if (scene->HasMeshes())
  33.     {
  34.         for (int i = 0; i < scene->mNumMeshes; ++i)
  35.         {
  36.             auto assimp_mesh = scene->mMeshes[i];
  37.          
  38.             auto obj = gEnv->p3DEngine->CreateStatObj();
  39.             obj->AddRef();
  40.             obj->Refresh(FRO_GEOMETRY);
  41.  
  42.             auto imesh = obj->GetIndexedMesh(true);
  43.    
  44.             CMesh mesh;
  45.                 mesh.SetVertexCount( assimp_mesh->mNumVertices );
  46.                 mesh.SetFacesCount( assimp_mesh->mNumFaces );
  47.                 //mesh.SetIndexCount( assimp_mesh->mFaces->mNumIndices );
  48.  
  49.                 auto vertices = mesh.GetStreamPtr<Vec3>(CMesh::POSITIONS);
  50.                 auto normals = mesh.GetStreamPtr<Vec3>(CMesh::NORMALS);
  51.                 //auto indices = mesh.GetStreamPtr<uint16>(CMesh::INDICES);
  52.  
  53.                 for (int i = 0; i < assimp_mesh->mNumVertices; ++i)
  54.                 {
  55.                     vertices[i] = Vec3(assimp_mesh->mVertices[i].x, assimp_mesh->mVertices[i].y, assimp_mesh->mVertices[i].z);
  56.                 }
  57.  
  58.                 for (int i = 0; i < assimp_mesh->mNumVertices; ++i)
  59.                 {
  60.                     normals[i] = Vec3(assimp_mesh->mNormals[i].x, assimp_mesh->mNormals[i].y, assimp_mesh->mNormals[i].z);
  61.                 }
  62.  
  63.                
  64.                 /*for (int i = 0; i < assimp_mesh->mFaces->mNumIndices; ++i)
  65.                 {
  66.                     indices[i] = assimp_mesh->mFaces->mIndices[i];
  67.                 }*/
  68.  
  69.                 SMeshSubset sub;
  70.                     sub.nFirstIndexId = 0;
  71.                     sub.nFirstVertId = 0;
  72.                     //sub.nNumIndices = assimp_mesh->mFaces->mNumIndices;
  73.                     sub.nNumVerts = assimp_mesh->mNumVertices;
  74.  
  75.                 mesh.m_subsets.push_back( sub );
  76.                 mesh.m_bbox = obj->GetAABB();
  77.      
  78.                 const char *error;
  79.                 if (!mesh.Validate(&error))
  80.                 {
  81.                     CryLogAlways("mesh validation error: %s", error);
  82.                 }
  83.                
  84.             imesh->SetMesh(mesh);
  85.             imesh->Invalidate();
  86.  
  87.             auto rmesh = obj->GetRenderMesh();
  88.             rmesh->SetMesh(mesh);
  89.  
  90.             SEntitySpawnParams params;
  91.                 params.pClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass("BasicEntity");
  92.                 params.vPosition = Vec3(10,10,1);
  93.             auto ent = gEnv->pEntitySystem->SpawnEntity(params);
  94.             ent->Activate (true);
  95.  
  96.             {
  97.                 SEntityPhysicalizeParams params;
  98.                     params.type = PE_RIGID;
  99.                     params.nSlot = -1;
  100.                     params.mass = 50;
  101.                     params.density = 1.0f;
  102.                 ent->Physicalize(params);
  103.             }
  104.    
  105.             int slotCount = ent->GetSlotCount();
  106.             int slotId = ent->SetStatObj(obj, -1, false);
  107.             int slotCount2 = ent->GetSlotCount();
  108.           }
  109.       }
  110.   // We're done. Everything will be cleaned up by the importer destructor
  111.   return;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement