Advertisement
Guest User

Untitled

a guest
Mar 29th, 2018
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.48 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "BlastFracture.h"
  3. #include "Fracture.h"
  4.  
  5. using namespace Nv::Blast;
  6.  
  7. void loggingCallback(int type, const char* msg, const char* file, int line)
  8. {
  9.     (void)type;
  10.  
  11.     std::cout << msg << " FILE:" << file << " Line: " << line << "\n";
  12. }
  13.  
  14.  
  15. static Nv::Blast::Mesh* pMesh = NULL;
  16. static FractureSettings settings;
  17. static Nv::Blast::FractureTool fTool;
  18. static std::vector<std::vector<Nv::Blast::Triangle> > chunkMeshes;
  19. std::vector<physx::PxVec3> pos;
  20. std::vector<physx::PxVec3> norm;
  21. std::vector<physx::PxVec2> tex;
  22. std::vector<int> indexs;
  23.  
  24. extern "C" {
  25.  
  26.     DllExport void SetMesh(int vertexCount, void* sourceVertices, void* sourceNormals, void* sourceUVs,void* sourceIndices, int indicesCount)
  27.     {
  28.         pMesh = new Nv::Blast::Mesh((physx::PxVec3*)sourceVertices, (physx::PxVec3*)sourceNormals, (physx::PxVec2*)sourceUVs, vertexCount, (uint32_t*)sourceIndices, indicesCount);
  29.        
  30.         printf("SetMesh: isValid:%i\n", pMesh->isValid());
  31.         printf("V:%i    F:%i   E:%i\n", pMesh->getVerticesCount(), pMesh->getFacetCount(), pMesh->getEdgesCount());
  32.  
  33.         settings.cellsCount = 5;
  34.         settings.clusterCount = 5;
  35.         settings.clusterRadius = 1;
  36.         settings.offsetVariation = 0;
  37.         settings.slicingX = 0;
  38.         settings.slicingY = 0;
  39.         settings.slicingZ = 0;
  40.     }
  41.  
  42.     DllExport void Fracture(int count)
  43.     {
  44.         fTool.setSourceMesh(pMesh);
  45.  
  46.         SimpleRandomGenerator rnd;
  47.         rnd.seed((int32_t)time(nullptr)); // Keep the same seed to have reproducible results.
  48.  
  49.         std::cout << "Fracturing...\n" << std::endl;
  50.         VoronoiSitesGenerator stGenerator(pMesh, &rnd);
  51.  
  52.         stGenerator.uniformlyGenerateSitesInMesh(count);
  53.         fTool.voronoiFracturing(0, stGenerator.getVoronoiSites(), false);
  54.    
  55.         std::cout << "Creating geometry" << std::endl;
  56.         fTool.finalizeFracturing();
  57.  
  58.         chunkMeshes.resize(fTool.getChunkList().size());
  59.         //isSupport.resize(fTool.getChunkList().size());
  60.         for (uint32_t i = 0; i < fTool.getChunkList().size(); ++i)
  61.         {
  62.             fTool.getBaseMesh(i, chunkMeshes[i]);
  63.             //isSupport[i] = fTool.getChunkList()[i].isLeaf;
  64.         }
  65.  
  66.     }
  67.  
  68.     DllExport int Chunks()
  69.     {
  70.         //return chunkMeshes.size();
  71.         return fTool.getChunkList().size();
  72.     }
  73.  
  74.     DllExport int GetChunkCount(int idx)
  75.     {
  76.         if (idx >= chunkMeshes.size()) return NULL;
  77.         return chunkMeshes[idx].size();
  78.     }
  79.  
  80.     DllExport void* GetChunkVertex(int idx)
  81.     {
  82.         if (idx >= chunkMeshes.size()) return NULL;
  83.         pos.clear();
  84.         std::vector<Triangle>& chunk = chunkMeshes[idx];
  85.         for (uint32_t i = 0; i < chunk.size(); ++i)
  86.         {
  87.             pos.push_back(chunk[i].a.p);
  88.             pos.push_back(chunk[i].b.p);
  89.             pos.push_back(chunk[i].c.p);
  90.         }
  91.  
  92.         return &pos[0];
  93.     }
  94.  
  95.     DllExport void* GetChunkIndex(int idx)
  96.     {
  97.         if (idx >= chunkMeshes.size()) return NULL;
  98.         indexs.clear();
  99.         int index = 0;
  100.         std::vector<Triangle>& chunk = chunkMeshes[idx];
  101.         for (uint32_t i = 0; i < chunk.size(); ++i)
  102.         {
  103.             indexs.push_back(index++);
  104.             indexs.push_back(index++);
  105.             indexs.push_back(index++);
  106.         }
  107.         return &indexs[0];
  108.     }
  109.     DllExport void* GetChunkNormal(int idx)
  110.     {
  111.         if (idx >= chunkMeshes.size()) return NULL;
  112.         norm.clear();
  113.         int index = 0;
  114.         std::vector<Triangle>& chunk = chunkMeshes[idx];
  115.         for (uint32_t i = 0; i < chunk.size(); ++i)
  116.         {
  117.             norm.push_back(chunk[i].a.n);
  118.             norm.push_back(chunk[i].b.n);
  119.             norm.push_back(chunk[i].c.n);
  120.         }
  121.         return &norm[0];
  122.     }
  123.     DllExport void* GetChunkUV(int idx)
  124.     {
  125.         if (idx >= chunkMeshes.size()) return NULL;
  126.         tex.clear();
  127.         int index = 0;
  128.         std::vector<Triangle>& chunk = chunkMeshes[idx];
  129.         for (uint32_t i = 0; i < chunk.size(); ++i)
  130.         {
  131.             tex.push_back(chunk[i].a.uv[0]);
  132.             tex.push_back(chunk[i].b.uv[0]);
  133.             tex.push_back(chunk[i].c.uv[0]);
  134.         }
  135.         return &tex[0];
  136.     }
  137.  
  138.     DllExport int GetChunk(int idx,void* V,void* N,void* U,void* I)
  139.     {
  140.         printf("Triangles: %i\n", chunkMeshes[idx].size());
  141.  
  142.         pos.clear();
  143.         norm.clear();
  144.         tex.clear();
  145.         indexs.clear();
  146.  
  147.  
  148.         int index = 0;
  149.         std::vector<Triangle>& chunk = chunkMeshes[idx];
  150.         for (uint32_t i = 0; i < chunk.size(); ++i)
  151.         {
  152.             pos.push_back(chunk[i].a.p);
  153.             pos.push_back(chunk[i].b.p);
  154.             pos.push_back(chunk[i].c.p);
  155.  
  156.             norm.push_back(chunk[i].a.n);
  157.             norm.push_back(chunk[i].b.n);
  158.             norm.push_back(chunk[i].c.n);
  159.  
  160.             tex.push_back(chunk[i].a.uv[0]);
  161.             tex.push_back(chunk[i].b.uv[0]);
  162.             tex.push_back(chunk[i].c.uv[0]);
  163.  
  164.             indexs.push_back(index++);
  165.             indexs.push_back(index++);
  166.             indexs.push_back(index++);
  167.         }
  168.  
  169.         V = &pos[0];
  170.         N = &norm[0];
  171.         U = &tex[0];
  172.         I = &indexs[0];
  173.  
  174.         return chunk.size();
  175.     }
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement