Advertisement
Guest User

Untitled

a guest
Aug 4th, 2020
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include "texture.h"
  2. #include "debug.h"
  3.  
  4. #define STB_IMAGE_IMPLEMENTATION
  5. #include "stb_image.h"
  6.  
  7. Texture::Texture(std::string imagePath) :
  8.     m_id(0), m_wrapS(GL_REPEAT), m_wrapT(GL_REPEAT), m_filterMin(GL_LINEAR_MIPMAP_LINEAR), m_filterMax(GL_LINEAR)
  9. {
  10.     unsigned char* imageData = stbi_load(imagePath.c_str(), (int*)&m_width, (int*)&m_height, (int*)&m_channelCount, 0);
  11.  
  12.     if (imageData)
  13.     {
  14.         createTexture((std::byte*)imageData);
  15.         stbi_image_free(imageData);
  16.     }
  17. }
  18.  
  19. Texture::Texture(std::byte * imageData, size_t width, size_t height, size_t channelsCount) :
  20.     m_id(0), m_wrapS(GL_REPEAT), m_wrapT(GL_REPEAT), m_filterMin(GL_LINEAR_MIPMAP_LINEAR), m_filterMax(GL_LINEAR), m_width(width), m_height(height), m_channelCount(channelsCount)
  21. {
  22.     createTexture(imageData);
  23. }
  24.  
  25. Texture::~Texture()
  26. {
  27.     glDeleteTextures(1, &m_id);
  28. }
  29.  
  30. GLuint Texture::getId() const
  31. {
  32.     return m_id;
  33. }
  34.  
  35. size_t Texture::getWidth() const
  36. {
  37.     return m_width;
  38. }
  39.  
  40. size_t Texture::getHeight() const
  41. {
  42.     return m_height;
  43. }
  44.  
  45. void Texture::bind(GLenum activeTexture)
  46. {
  47.     glActiveTexture(activeTexture);
  48.     glBindTexture(GL_TEXTURE_2D, m_id);
  49. }
  50.  
  51. void Texture::createTexture(std::byte * imageData)
  52. {
  53.     // Texture construction shall not interfere with the current texture unit configuration
  54.     // Retrieve current texture in texture unit 0 and restore it later.
  55.     GLint currentTextureId = 0;
  56.     glActiveTexture(GL_TEXTURE0);
  57.     glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTextureId);
  58.  
  59.  
  60.     glGenTextures(1, &m_id);
  61.     glBindTexture(GL_TEXTURE_2D, m_id);
  62.  
  63.     GLenum format = GL_RGB;
  64.  
  65.     switch (m_channelCount)
  66.     {
  67.     case 3:
  68.         format = GL_RGB;
  69.         break;
  70.     case 4:
  71.         format = GL_RGBA;
  72.         break;
  73.     default:
  74.         ASSERT(1 == 2);
  75.         break;
  76.     }
  77.  
  78.     glTexImage2D(GL_TEXTURE_2D, 0, format, m_width, m_height, 0, format, GL_UNSIGNED_BYTE, imageData);
  79.     glGenerateMipmap(GL_TEXTURE_2D);
  80.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_wrapS);
  81.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_wrapT);
  82.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, m_filterMin);
  83.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_filterMax);
  84.  
  85.  
  86.     glBindTexture(GL_TEXTURE_2D, currentTextureId); // Restore previous config of texture unit 0
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement