💿🐜 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.

91 lines
2.5 KiB

  1. /*
  2. * Copyright (C) 2021 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 "geom/mesh.hpp"
  21. #include "geom/mesh-functions.hpp"
  22. #include "utility/fundamental-types.hpp"
  23. #include <sstream>
  24. #include <stdexcept>
  25. #include <physfs.h>
  26. template <>
  27. geom::mesh* resource_loader<geom::mesh>::load(resource_manager* resource_manager, PHYSFS_File* file)
  28. {
  29. std::string line;
  30. std::vector<float3> vertices;
  31. std::vector<std::array<std::uint_fast32_t, 3>> triangles;
  32. while (!PHYSFS_eof(file))
  33. {
  34. // Read line
  35. physfs_getline(file, line);
  36. // Tokenize line
  37. std::vector<std::string> tokens;
  38. std::string token;
  39. std::istringstream linestream(line);
  40. while (linestream >> token)
  41. tokens.push_back(token);
  42. // Skip empty lines and comments
  43. if (tokens.empty() || tokens[0][0] == '#')
  44. continue;
  45. if (tokens[0] == "v")
  46. {
  47. if (tokens.size() != 4)
  48. {
  49. std::stringstream stream;
  50. stream << "resource_loader<mesh>::load(): Invalid line \"" << line << "\"" << std::endl;
  51. throw std::runtime_error(stream.str());
  52. }
  53. float3 vertex;
  54. std::stringstream(tokens[1]) >> vertex[0];
  55. std::stringstream(tokens[2]) >> vertex[1];
  56. std::stringstream(tokens[3]) >> vertex[2];
  57. vertices.push_back(vertex);
  58. }
  59. else if (tokens[0] == "f")
  60. {
  61. if (tokens.size() != 4)
  62. {
  63. std::stringstream stream;
  64. stream << "resource_loader<mesh>::load(): Invalid line \"" << line << "\"" << std::endl;
  65. throw std::runtime_error(stream.str());
  66. }
  67. std::uint_fast32_t a, b, c;
  68. std::stringstream(tokens[1]) >> a;
  69. std::stringstream(tokens[2]) >> b;
  70. std::stringstream(tokens[3]) >> c;
  71. triangles.push_back({a - 1, b - 1, c - 1});
  72. }
  73. }
  74. geom::mesh* mesh = new geom::mesh();
  75. geom::create_triangle_mesh(*mesh, vertices, triangles);
  76. return mesh;
  77. }