Guest User

loop_vert_read

a guest
Dec 28th, 2023
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. std::unordered_map<Vertex,u32> individVerts{};
  2.  
  3. u32 verts_count {0};
  4.  
  5. for(const auto& shape : shapes){
  6. for(const auto& ind : shape.mesh.indices){
  7. Vertex vert{};
  8.  
  9. vert.pos = {
  10. attr.vertices[3 * ind.vertex_index + 0],
  11. attr.vertices[3 * ind.vertex_index + 1],
  12. attr.vertices[3 * ind.vertex_index + 2],
  13. };
  14.  
  15. vert.uvco = {
  16. attr.texcoords[2 * ind.texcoord_index + 0],
  17. 1.f - attr.texcoords[2 * ind.texcoord_index + 1]
  18. };
  19.  
  20. vert.norm = {
  21. attr.normals[3 * ind.normal_index + 0],
  22. attr.normals[3 * ind.normal_index + 1],
  23. attr.normals[3 * ind.normal_index + 2],
  24. };
  25.  
  26. if(individVerts.count(vert) == 0){
  27. individVerts[vert] = static_cast<u32>(obju->verts.size());
  28. obju->verts . push_back(vert);
  29. }
  30.  
  31. obju->inds.push_back(individVerts[vert]);
  32. verts_count++;
  33.  
  34. size_t size = shape.mesh.indices.size();
  35.  
  36. if(verts_count % 3 == 0){
  37. u64 ind_1 = obju->inds[verts_count - 3];
  38. u64 ind_2 = obju->inds[verts_count - 2];
  39. u64 ind_3 = obju->inds[verts_count - 1];
  40.  
  41. Vertex vert_1 = obju->verts[ind_1];
  42. Vertex vert_2 = obju->verts[ind_2];
  43. Vertex vert_3 = obju->verts[ind_3];
  44.  
  45. glm::vec3 E_1 = vert_2.pos - vert_1.pos;
  46. glm::vec3 E_2 = vert_3.pos - vert_1.pos;
  47.  
  48. glm::vec2 dUV_1 = vert_2.uvco - vert_1.uvco;
  49. glm::vec2 dUV_2 = vert_3.uvco - vert_1.uvco;
  50.  
  51. f32 f = 1.0f / (dUV_1.x * dUV_2.y - dUV_2.x * dUV_1.y);
  52.  
  53. glm::vec3 tan;
  54.  
  55. tan.x = f * (dUV_2.y * E_1.x - dUV_1.y * E_2.x);
  56. tan.y = f * (dUV_2.y * E_1.y - dUV_1.y * E_2.y);
  57. tan.z = f * (dUV_2.y * E_1.z - dUV_1.y * E_2.z);
  58.  
  59. glm::vec3 btan;
  60.  
  61. btan.x = f * (-dUV_2.x * E_1.x + dUV_1.x * E_2.x);
  62. btan.y = f * (-dUV_2.x * E_1.y + dUV_1.x * E_2.y);
  63. btan.z = f * (-dUV_2.x * E_1.z + dUV_1.x * E_2.z);
  64.  
  65. obju->verts[ind_1].tan = tan;
  66. obju->verts[ind_2].tan = tan;
  67. obju->verts[ind_3].tan = tan;
  68.  
  69. obju->verts[ind_1].btan = btan;
  70. obju->verts[ind_2].btan = btan;
  71. obju->verts[ind_3].btan = btan;
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment