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

108 lines
3.4 KiB

  1. /*
  2. * Copyright (C) 2017-2019 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 "resource-loader.hpp"
  20. #include "image.hpp"
  21. #include <sstream>
  22. #include <emergent/emergent.hpp>
  23. using namespace Emergent;
  24. template <>
  25. Texture2D* ResourceLoader<Texture2D>::load(ResourceManager* resourceManager, std::istream* is)
  26. {
  27. Image* image = ResourceLoader<Image>::load(resourceManager, is);
  28. // Generate OpenGL texture ID
  29. GLuint textureID;
  30. glGenTextures(1, &textureID);
  31. glBindTexture(GL_TEXTURE_2D, textureID);
  32. // Set wrapping and filtering parameters
  33. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  34. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  35. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  36. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  37. // Select texture formats according to number of color channels in the image
  38. GLint internalFormat;
  39. GLenum format;
  40. GLenum type = (image->isHDR()) ? GL_FLOAT : GL_UNSIGNED_BYTE;
  41. if (image->getChannels() == 1)
  42. {
  43. // Grey
  44. internalFormat = (image->isHDR()) ? GL_R32F : GL_R8;
  45. format = GL_RED;
  46. GLint swizzleMask[] = {GL_RED, GL_RED, GL_RED, GL_ONE};
  47. glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
  48. }
  49. else if (image->getChannels() == 2)
  50. {
  51. // Grey, alpha
  52. internalFormat = (image->isHDR()) ? GL_RG32F : GL_RG8;
  53. format = GL_RG;
  54. GLint swizzleMask[] = {GL_RED, GL_RED, GL_RED, GL_GREEN};
  55. glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
  56. }
  57. else if (image->getChannels() == 3)
  58. {
  59. // Red, green, blue
  60. internalFormat = (image->isHDR()) ? GL_RGB32F : GL_RGB8;
  61. format = GL_RGB;
  62. GLint swizzleMask[] = {GL_RED, GL_GREEN, GL_BLUE, GL_ONE};
  63. glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
  64. }
  65. else if (image->getChannels() == 4)
  66. {
  67. // Red, green, blue, alpha
  68. internalFormat = (image->isHDR()) ? GL_RGBA32F : GL_RGBA8;
  69. format = GL_RGBA;
  70. }
  71. else
  72. {
  73. std::stringstream stream;
  74. stream << std::string("Texture cannot be created from an image with an unsupported number of color channels (") << image->getChannels() << std::string(").");
  75. delete image;
  76. glDeleteTextures(1, &textureID);
  77. throw std::runtime_error(stream.str().c_str());
  78. }
  79. // Upload image data to OpenGL
  80. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  81. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  82. glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, image->getWidth(), image->getHeight(), 0, format, type, image->getPixels());
  83. // Generate mipmaps
  84. glGenerateMipmap(GL_TEXTURE_2D);
  85. // Create Texture2D
  86. Texture2D* texture = new Texture2D();
  87. texture->setTextureID(textureID);
  88. texture->setWidth(image->getWidth());
  89. texture->setHeight(image->getHeight());
  90. // Free loaded image
  91. delete image;
  92. return texture;
  93. }