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

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