vk_mesh.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "vk_mesh.h"
  2. #include <tiny_obj_loader.h>
  3. #include <iostream>
  4. using namespace Coral3D;
  5. VertexInputDescription Vertex::get_vert_desc()
  6. {
  7. VertexInputDescription desc;
  8. // We only have one vertex buffer binding, with a per-vertex rate
  9. VkVertexInputBindingDescription mainBinding{};
  10. mainBinding.binding = 0;
  11. mainBinding.stride = sizeof(Vertex);
  12. mainBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
  13. desc.bindings.emplace_back(mainBinding);
  14. // Position will be stored at Location 0
  15. VkVertexInputAttributeDescription positionAttribute{};
  16. positionAttribute.binding = 0;
  17. positionAttribute.location = 0;
  18. positionAttribute.format = VK_FORMAT_R32G32B32_SFLOAT;
  19. positionAttribute.offset = offsetof(Vertex, position);
  20. // Normal will be stored at Location 1
  21. VkVertexInputAttributeDescription normalAttribute{};
  22. normalAttribute.binding = 0;
  23. normalAttribute.location = 1;
  24. normalAttribute.format = VK_FORMAT_R32G32B32_SFLOAT;
  25. normalAttribute.offset = offsetof(Vertex, normal);
  26. // Color will be stored at Location 2
  27. VkVertexInputAttributeDescription colorAttribute{};
  28. colorAttribute.binding = 0;
  29. colorAttribute.location = 2;
  30. colorAttribute.format = VK_FORMAT_R32G32B32_SFLOAT;
  31. colorAttribute.offset = offsetof(Vertex, color);
  32. // UV will be stored at Location 3
  33. VkVertexInputAttributeDescription uvAttribute{};
  34. uvAttribute.binding = 0;
  35. uvAttribute.location = 3;
  36. uvAttribute.format = VK_FORMAT_R32G32_SFLOAT;
  37. uvAttribute.offset = offsetof(Vertex, uv);
  38. desc.attributes.emplace_back(positionAttribute);
  39. desc.attributes.emplace_back(normalAttribute);
  40. desc.attributes.emplace_back(colorAttribute);
  41. desc.attributes.emplace_back(uvAttribute);
  42. return desc;
  43. }
  44. bool Mesh::load_from_obj(const char* filename)
  45. {
  46. // attrib will contain the vertex arrays of the file
  47. tinyobj::attrib_t attrib;
  48. // shapes contains the info for each separate object in the file
  49. std::vector<tinyobj::shape_t> shapes;
  50. // materials contains the information about the material of each shape, not yet used.
  51. std::vector<tinyobj::material_t> materials;
  52. // error and warning output from the load function
  53. std::string warn;
  54. std::string err;
  55. // load the OBJ file
  56. tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename);
  57. //make sure to output the warnings to the console, in case there are issues with the file
  58. if (!warn.empty())
  59. {
  60. std::cout << "WARNING! Mesh::load_from_obj() >> " << warn << std::endl;
  61. }
  62. //if we have any error, print it to the console, and break the mesh loading.
  63. //This happens if the file can't be found or is malformed
  64. if (!err.empty())
  65. {
  66. std::cerr << err << std::endl;
  67. return false;
  68. }
  69. // Loop over shapes
  70. for (size_t s = 0; s < shapes.size(); s++)
  71. {
  72. // Loop over faces(polygon)
  73. size_t index_offset = 0;
  74. for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++)
  75. {
  76. //hardcode loading to triangles
  77. int fv = 3;
  78. // Loop over vertices in the face.
  79. for (size_t v = 0; v < fv; v++)
  80. {
  81. // access to vertex
  82. tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
  83. //vertex position
  84. tinyobj::real_t vx = attrib.vertices[3 * idx.vertex_index + 0];
  85. tinyobj::real_t vy = attrib.vertices[3 * idx.vertex_index + 1];
  86. tinyobj::real_t vz = attrib.vertices[3 * idx.vertex_index + 2];
  87. //vertex normal
  88. tinyobj::real_t nx = attrib.normals[3 * idx.normal_index + 0];
  89. tinyobj::real_t ny = attrib.normals[3 * idx.normal_index + 1];
  90. tinyobj::real_t nz = attrib.normals[3 * idx.normal_index + 2];
  91. //copy it into our vertex
  92. Vertex new_vert;
  93. new_vert.position.x = vx;
  94. new_vert.position.y = vy;
  95. new_vert.position.z = vz;
  96. new_vert.normal.x = nx;
  97. new_vert.normal.y = ny;
  98. new_vert.normal.z = nz;
  99. //we are setting the vertex color as the vertex normal. This is just for display purposes
  100. new_vert.color = new_vert.normal;
  101. tinyobj::real_t ux{attrib.texcoords[2 * idx.texcoord_index + 0]};
  102. tinyobj::real_t uy{attrib.texcoords[2 * idx.texcoord_index + 1]};
  103. new_vert.uv = { ux, 1 - uy };
  104. vertices.push_back(new_vert);
  105. }
  106. index_offset += fv;
  107. }
  108. }
  109. return true;
  110. }