Texture.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. mFileName = fileName;
  24. int channels = 0;
  25. unsigned char* image = SOIL_load_image(fileName.c_str(),
  26. &mWidth, &mHeight, &channels, SOIL_LOAD_AUTO);
  27. if (image == nullptr)
  28. {
  29. SDL_Log("SOIL failed to load image %s: %s", fileName.c_str(), SOIL_last_result());
  30. return false;
  31. }
  32. int format = GL_RGB;
  33. if (channels == 4)
  34. {
  35. format = GL_RGBA;
  36. }
  37. glGenTextures(1, &mTextureID);
  38. glBindTexture(GL_TEXTURE_2D, mTextureID);
  39. glTexImage2D(GL_TEXTURE_2D, 0, format, mWidth, mHeight, 0, format,
  40. GL_UNSIGNED_BYTE, image);
  41. SOIL_free_image_data(image);
  42. // Generate mipmaps for texture
  43. glGenerateMipmap(GL_TEXTURE_2D);
  44. // Enable linear filtering
  45. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  46. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  47. // Enable anisotropic filtering, if supported
  48. if (GLEW_EXT_texture_filter_anisotropic)
  49. {
  50. // Get the maximum anisotropy value
  51. GLfloat largest;
  52. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &largest);
  53. // Enable it
  54. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, largest);
  55. }
  56. return true;
  57. }
  58. void Texture::Unload()
  59. {
  60. glDeleteTextures(1, &mTextureID);
  61. }
  62. void Texture::CreateFromSurface(SDL_Surface* surface)
  63. {
  64. mWidth = surface->w;
  65. mHeight = surface->h;
  66. // Generate a GL texture
  67. glGenTextures(1, &mTextureID);
  68. glBindTexture(GL_TEXTURE_2D, mTextureID);
  69. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_BGRA,
  70. GL_UNSIGNED_BYTE, surface->pixels);
  71. // Use linear filtering
  72. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  73. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  74. }
  75. void Texture::CreateForRendering(int width, int height, unsigned int format)
  76. {
  77. mWidth = width;
  78. mHeight = height;
  79. // Create the texture id
  80. glGenTextures(1, &mTextureID);
  81. glBindTexture(GL_TEXTURE_2D, mTextureID);
  82. // Set the image width/height with null initial data
  83. glTexImage2D(GL_TEXTURE_2D, 0, format, mWidth, mHeight, 0, GL_RGB,
  84. GL_FLOAT, nullptr);
  85. // For a texture we'll render to, just use nearest neighbor
  86. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  87. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  88. }
  89. void Texture::SetActive(int index)
  90. {
  91. glActiveTexture(GL_TEXTURE0 + index);
  92. glBindTexture(GL_TEXTURE_2D, mTextureID);
  93. }