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

72 lines
2.2 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 "resources/image.hpp"
  21. #include "gl/pixel-type.hpp"
  22. #include "gl/pixel-format.hpp"
  23. #include "gl/texture-2d.hpp"
  24. #include <sstream>
  25. template <>
  26. gl::texture_2d* resource_loader<gl::texture_2d>::load(resource_manager* resource_manager, PHYSFS_File* file)
  27. {
  28. // Load image
  29. ::image* image = resource_loader<::image>::load(resource_manager, file);
  30. // Determine pixel type
  31. gl::pixel_type type = (image->is_hdr()) ? gl::pixel_type::float_32 : gl::pixel_type::uint_8;
  32. // Determine pixel format
  33. gl::pixel_format format;
  34. if (image->get_channels() == 1)
  35. {
  36. format = gl::pixel_format::r;
  37. }
  38. else if (image->get_channels() == 2)
  39. {
  40. format = gl::pixel_format::rg;
  41. }
  42. else if (image->get_channels() == 3)
  43. {
  44. format = gl::pixel_format::rgb;
  45. }
  46. else if (image->get_channels() == 4)
  47. {
  48. format = gl::pixel_format::rgba;
  49. }
  50. else
  51. {
  52. std::stringstream stream;
  53. stream << std::string("Texture cannot be created from an image with an unsupported number of color channels (") << image->get_channels() << std::string(").");
  54. delete image;
  55. throw std::runtime_error(stream.str().c_str());
  56. }
  57. // Assume linear color space
  58. gl::color_space color_space = gl::color_space::linear;
  59. // Create texture
  60. gl::texture_2d* texture = new gl::texture_2d(image->get_width(), image->get_height(), type, format, color_space, image->get_pixels());
  61. // Free loaded image
  62. delete image;
  63. return texture;
  64. }