Texture.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "Texture.h"
  9. #include <SOIL/SOIL.h>
  10. #include <GL/glew.h>
  11. #include <SDL/SDL.h>
  12. Texture::Texture()
  13. :mTextureID(0)
  14. ,mWidth(0)
  15. ,mHeight(0)
  16. {
  17. }
  18. Texture::~Texture()
  19. {
  20. }
  21. bool Texture::Load(const std::string& fileName)
  22. {
  23. int channels = 0;
  24. unsigned char* image = SOIL_load_image(fileName.c_str(),
  25. &mWidth, &mHeight, &channels, SOIL_LOAD_AUTO);
  26. if (image == nullptr)
  27. {
  28. SDL_Log("SOIL failed to load image %s: %s", fileName.c_str(), SOIL_last_result());
  29. return false;
  30. }
  31. int format = GL_RGB;
  32. if (channels == 4)
  33. {
  34. format = GL_RGBA;
  35. }
  36. glGenTextures(1, &mTextureID);
  37. glBindTexture(GL_TEXTURE_2D, mTextureID);
  38. glTexImage2D(GL_TEXTURE_2D, 0, format, mWidth, mHeight, 0, format,
  39. GL_UNSIGNED_BYTE, image);
  40. SOIL_free_image_data(image);
  41. // Generate mipmaps for texture
  42. glGenerateMipmap(GL_TEXTURE_2D);
  43. // Enable linear filtering
  44. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  45. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  46. // Enable anisotropic filtering, if supported
  47. if (GLEW_EXT_texture_filter_anisotropic)
  48. {
  49. // Get the maximum anisotropy value
  50. GLfloat largest;
  51. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &largest);
  52. // Enable it
  53. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, largest);
  54. }
  55. return true;
  56. }
  57. void Texture::Unload()
  58. {
  59. glDeleteTextures(1, &mTextureID);
  60. }
  61. void Texture::CreateFromSurface(SDL_Surface* surface)
  62. {
  63. mWidth = surface->w;
  64. mHeight = surface->h;
  65. // Generate a GL texture
  66. glGenTextures(1, &mTextureID);
  67. glBindTexture(GL_TEXTURE_2D, mTextureID);
  68. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_BGRA,
  69. GL_UNSIGNED_BYTE, surface->pixels);
  70. // Use linear filtering
  71. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  72. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  73. }
  74. void Texture::CreateForRendering(int width, int height, unsigned int format)
  75. {
  76. mWidth = width;
  77. mHeight = height;
  78. // Create the texture id
  79. glGenTextures(1, &mTextureID);
  80. glBindTexture(GL_TEXTURE_2D, mTextureID);
  81. // Set the image width/height with null initial data
  82. glTexImage2D(GL_TEXTURE_2D, 0, format, mWidth, mHeight, 0, GL_RGB,
  83. GL_FLOAT, nullptr);
  84. // For a texture we'll render to, just use nearest neighbor
  85. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  86. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  87. }
  88. void Texture::SetActive(int index)
  89. {
  90. glActiveTexture(GL_TEXTURE0 + index);
  91. glBindTexture(GL_TEXTURE_2D, mTextureID);
  92. }