Advertisement
Guest User

normals calculations

a guest
Oct 11th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. MeshParams_vec_sp buildExtractedMesh(TerrainThreadRequest_sp threadReq) {
  2.     int materialCount = threadReq->terrainMaterials->Num();
  3.     MeshParams_vec_sp meshParams_vec_sp = MakeShareable(new std::vector<MeshParams>(materialCount));
  4.     threadReq->location_cm = getLocFromRegionLoc(threadReq->regionWidth, threadReq->regionLoc);
  5.     int chunkCounter = 0;
  6.     PolyVox::Mesh<PolyVox::Vertex<MDP88>, unsigned int> DecodedMesh;
  7.     if (threadReq->smooth) {
  8.         //auto DecodedMesh = decodeMesh(threadReq->marchingExtractedMesh);
  9.         DecodedMesh = decodeMesh(threadReq->marchingExtractedMesh);
  10.     }
  11.     else {
  12.         DecodedMesh = decodeMesh(threadReq->cubicExtractedMesh);
  13.     }
  14.    
  15.     for (int Material = 0; Material < materialCount; Material++)
  16.     {
  17.         MeshParams* mp = &meshParams_vec_sp->data()[Material];
  18.         mp->material_index = Material;
  19.         mp->location = threadReq->location_cm;
  20.         mp->colliderEnabled = true;
  21.         // Loop over all of the triangle vertex indices
  22.         for (uint32 i = 0; i < DecodedMesh.getNoOfIndices() - 2; i += 3)
  23.         {
  24.             // We need to add the vertices of each triangle in reverse or the mesh will be upside down
  25.             auto Index = DecodedMesh.getIndex(i + 2);
  26.             auto Vertex2 = DecodedMesh.getVertex(Index);
  27.             auto TriangleMaterial = Vertex2.data.getMaterial();
  28.  
  29.             // Before we continue, we need to be sure that the triangle is the right material; we don't want to use verticies from other materials
  30.             if (TriangleMaterial == (Material + 1))
  31.             {
  32.                 // If it is of the same material, then we need to add the correct indices now
  33.                 mp->Indices.Add(mp->Vertices.Add(FPolyVoxVector(Vertex2.position) * 100.f));
  34.  
  35.                 Index = DecodedMesh.getIndex(i + 1);
  36.                 auto Vertex1 = DecodedMesh.getVertex(Index);
  37.                 mp->Indices.Add(mp->Vertices.Add(FPolyVoxVector(Vertex1.position) * 100.f));
  38.  
  39.                 Index = DecodedMesh.getIndex(i);
  40.                 auto Vertex0 = DecodedMesh.getVertex(Index);
  41.                 mp->Indices.Add(mp->Vertices.Add(FPolyVoxVector(Vertex0.position) * 100.f));
  42.  
  43.                 // Calculate the tangents of our triangle
  44.                 const FVector Edge01 = FPolyVoxVector(Vertex1.position - Vertex0.position);
  45.                 const FVector Edge02 = FPolyVoxVector(Vertex2.position - Vertex0.position);
  46.  
  47.                 const FVector TangentX = Edge01.GetSafeNormal();
  48.                 FVector TangentZ = (Edge01 ^ Edge02).GetSafeNormal();
  49.  
  50.                 for (int32 i = 0; i < 3; i++)
  51.                 {
  52.                     mp->Tangents.Add(FRuntimeMeshTangent(TangentX, false));
  53.                     mp->Normals.Add(TangentZ);
  54.                 }
  55.             }
  56.         }
  57.     }
  58.    
  59.    
  60.     return meshParams_vec_sp;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement