Advertisement
atm959

Texture.h

Aug 5th, 2018
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #ifndef TEXTURE_H
  2. #define TEXTURE_H
  3.  
  4. #include <iostream>
  5.  
  6. #include <glad\glad.h>
  7. #include "stb_image.h"
  8.  
  9. class Texture {
  10. public:
  11.     unsigned int ID;
  12.    
  13.     Texture(const char* path) {
  14.         glGenTextures(1, &ID);
  15.         glBindTexture(GL_TEXTURE_2D, ID);
  16.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  17.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  18.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  19.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  20.  
  21.         int width, height, nChannels;
  22.         stbi_set_flip_vertically_on_load(true);
  23.         unsigned char* data = stbi_load(path, &width, &height, &nChannels, STBI_rgb_alpha);
  24.  
  25.         if (!data) {
  26.             std::cout << "Failed to load texture from path \"" << path << "\", substituting." << std::endl;
  27.             data = stbi_load("res/imgNotFound.png", &width, &height, &nChannels, STBI_rgb_alpha);
  28.         } else {
  29.             std::cout << "Loaded image from \"" << path << "\" into texture of ID " << ID << "." << std::endl;
  30.         }
  31.  
  32.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  33.         glGenerateMipmap(GL_TEXTURE_2D);
  34.  
  35.         stbi_image_free(data);
  36.     };
  37.  
  38.     void bindToTexUnit(int texUnit) {
  39.         glActiveTexture(GL_TEXTURE0 + texUnit);
  40.         glBindTexture(GL_TEXTURE_2D, ID);
  41.     };
  42.  
  43.     void destroy() {
  44.         std::cout << "Deleted texture with ID " << ID << "." << std::endl;
  45.         glDeleteTextures(1, &ID);
  46.     };
  47. };
  48.  
  49. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement