💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
7.8 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "resources/resource-loader.hpp"
  20. #include "resources/resource-manager.hpp"
  21. #include "renderer/model.hpp"
  22. #include "renderer/vertex-attributes.hpp"
  23. #include "rasterizer/vertex-attribute-type.hpp"
  24. #include "rasterizer/drawing-mode.hpp"
  25. #include <sstream>
  26. #include <stdexcept>
  27. #include <limits>
  28. #include <physfs.h>
  29. #include <vmq/vmq.hpp>
  30. using namespace vmq::types;
  31. struct material_group
  32. {
  33. std::string name;
  34. material* material;
  35. std::size_t start_index;
  36. std::size_t index_count;
  37. };
  38. static const float3 barycentric_coords[3] =
  39. {
  40. float3{1, 0, 0},
  41. float3{0, 1, 0},
  42. float3{0, 0, 1}
  43. };
  44. template <>
  45. model* resource_loader<model>::load(resource_manager* resource_manager, PHYSFS_File* file)
  46. {
  47. std::string line;
  48. std::vector<float3> positions;
  49. std::vector<float2> uvs;
  50. std::vector<float3> normals;
  51. std::vector<std::vector<std::size_t>> faces;
  52. std::vector<material_group> material_groups;
  53. material_group* current_material_group = nullptr;
  54. aabb<float> bounds =
  55. {
  56. {std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()},
  57. {-std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity()}
  58. };
  59. while (!PHYSFS_eof(file))
  60. {
  61. // Read line
  62. physfs_getline(file, line);
  63. // Tokenize line
  64. std::vector<std::string> tokens;
  65. std::string token;
  66. std::istringstream linestream(line);
  67. while (linestream >> token)
  68. tokens.push_back(token);
  69. // Skip empty lines and comments
  70. if (tokens.empty() || tokens[0][0] == '#')
  71. continue;
  72. if (tokens[0] == "v")
  73. {
  74. if (tokens.size() != 4)
  75. {
  76. std::stringstream stream;
  77. stream << "resource_loader<mesh>::load(): Invalid line \"" << line << "\"" << std::endl;
  78. throw std::runtime_error(stream.str());
  79. }
  80. float3 position;
  81. std::stringstream(tokens[1]) >> position[0];
  82. std::stringstream(tokens[2]) >> position[1];
  83. std::stringstream(tokens[3]) >> position[2];
  84. positions.push_back(position);
  85. // Add position to bounds
  86. for (int i = 0; i < 3; ++i)
  87. {
  88. bounds.min_point[i] = std::min<float>(bounds.min_point[i], position[i]);
  89. bounds.max_point[i] = std::max<float>(bounds.max_point[i], position[i]);
  90. }
  91. }
  92. else if (tokens[0] == "vt")
  93. {
  94. if (tokens.size() != 3)
  95. {
  96. std::stringstream stream;
  97. stream << "resource_loader<mesh>::load(): Invalid line \"" << line << "\"" << std::endl;
  98. throw std::runtime_error(stream.str());
  99. }
  100. float2 uv;
  101. std::stringstream(tokens[1]) >> uv[0];
  102. std::stringstream(tokens[2]) >> uv[1];
  103. uvs.push_back(uv);
  104. }
  105. else if (tokens[0] == "vn")
  106. {
  107. if (tokens.size() != 4)
  108. {
  109. std::stringstream stream;
  110. stream << "resource_loader<mesh>::load(): Invalid line \"" << line << "\"" << std::endl;
  111. throw std::runtime_error(stream.str());
  112. }
  113. float3 normal;
  114. std::stringstream(tokens[1]) >> normal[0];
  115. std::stringstream(tokens[2]) >> normal[1];
  116. std::stringstream(tokens[3]) >> normal[2];
  117. normals.push_back(normal);
  118. }
  119. else if (tokens[0] == "f")
  120. {
  121. if (tokens.size() != 4)
  122. {
  123. std::stringstream stream;
  124. stream << "resource_loader<mesh>::load(): Invalid line \"" << line << "\"" << std::endl;
  125. throw std::runtime_error(stream.str());
  126. }
  127. std::vector<std::size_t> face;
  128. for (std::size_t i = 0; i < 3; ++i)
  129. {
  130. std::stringstream ss(tokens[i + 1]);
  131. while (ss.good())
  132. {
  133. std::string substring;
  134. std::getline(ss, substring, '/');
  135. if (!substring.empty())
  136. {
  137. std::size_t index = std::stoul(substring) - 1;
  138. face.push_back(index);
  139. }
  140. }
  141. }
  142. faces.push_back(face);
  143. }
  144. else if (tokens[0] == "usemtl")
  145. {
  146. if (tokens.size() != 2)
  147. {
  148. std::stringstream stream;
  149. stream << "resource_loader<mesh>::load(): Invalid line \"" << line << "\"" << std::endl;
  150. throw std::runtime_error(stream.str());
  151. }
  152. if (current_material_group)
  153. {
  154. current_material_group->index_count = faces.size() * 3 - current_material_group->start_index;
  155. }
  156. material_groups.push_back(material_group());
  157. current_material_group = &material_groups.back();
  158. current_material_group->name = tokens[1];
  159. current_material_group->start_index = faces.size() * 3;
  160. current_material_group->index_count = 0;
  161. }
  162. }
  163. if (current_material_group)
  164. {
  165. current_material_group->index_count = faces.size() * 3 - current_material_group->start_index;
  166. }
  167. // Load material group materials
  168. for (material_group& material_group: material_groups)
  169. {
  170. material_group.material = resource_manager->load<material>(material_group.name + ".mtl");
  171. }
  172. bool has_uvs = (!uvs.empty());
  173. bool has_normals = (!normals.empty());
  174. bool has_barycentric = true;
  175. std::size_t vertex_size = 3;
  176. if (has_uvs)
  177. vertex_size += 2;
  178. if (has_normals)
  179. vertex_size += 3;
  180. if (has_barycentric)
  181. vertex_size += 3;
  182. std::size_t vertex_stride = sizeof(float) * vertex_size;
  183. // Generate vertex buffer
  184. float* vertex_data = new float[vertex_size * faces.size() * 3];
  185. float* v = &vertex_data[0];
  186. for (std::size_t i = 0; i < faces.size(); ++i)
  187. {
  188. const std::vector<std::size_t>& face = faces[i];
  189. std::size_t k = 0;
  190. for (std::size_t j = 0; j < 3; ++j)
  191. {
  192. const float3& position = positions[face[k++]];
  193. *(v++) = position[0];
  194. *(v++) = position[1];
  195. *(v++) = position[2];
  196. if (has_uvs)
  197. {
  198. const float2& uv = uvs[face[k++]];
  199. *(v++) = uv[0];
  200. *(v++) = uv[1];
  201. }
  202. if (has_normals)
  203. {
  204. const float3& normal = normals[face[k++]];
  205. *(v++) = normal[0];
  206. *(v++) = normal[1];
  207. *(v++) = normal[2];
  208. }
  209. if (has_barycentric)
  210. {
  211. *(v++) = barycentric_coords[j][0];
  212. *(v++) = barycentric_coords[j][1];
  213. *(v++) = barycentric_coords[j][2];
  214. }
  215. }
  216. }
  217. // Allocate a model
  218. model* model = new ::model();
  219. model->set_bounds(bounds);
  220. vertex_buffer* vbo = model->get_vertex_buffer();
  221. vertex_array* vao = model->get_vertex_array();
  222. vbo->resize(sizeof(float) * vertex_size * faces.size() * 3, vertex_data);
  223. std::size_t offset = 0;
  224. vao->bind_attribute(VERTEX_POSITION_LOCATION, *vbo, 3, vertex_attribute_type::float_32, vertex_stride, 0);
  225. offset += 3;
  226. if (has_uvs)
  227. {
  228. vao->bind_attribute(VERTEX_TEXCOORD_LOCATION, *vbo, 2, vertex_attribute_type::float_32, vertex_stride, sizeof(float) * offset);
  229. offset += 2;
  230. }
  231. if (has_normals)
  232. {
  233. vao->bind_attribute(VERTEX_NORMAL_LOCATION, *vbo, 3, vertex_attribute_type::float_32, vertex_stride, sizeof(float) * offset);
  234. offset += 3;
  235. }
  236. if (has_barycentric)
  237. {
  238. vao->bind_attribute(VERTEX_BARYCENTRIC_LOCATION, *vbo, 3, vertex_attribute_type::float_32, vertex_stride, sizeof(float) * offset);
  239. offset += 3;
  240. }
  241. // Add model groups for each material
  242. for (const material_group& material_group: material_groups)
  243. {
  244. model_group* model_group = model->add_group(material_group.name);
  245. model_group->set_material(material_group.material);
  246. model_group->set_drawing_mode(drawing_mode::triangles);
  247. model_group->set_start_index(material_group.start_index);
  248. model_group->set_index_count(material_group.index_count);
  249. }
  250. // Deallocate vertex data
  251. delete[] vertex_data;
  252. return model;
  253. }