Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef TEXTURE_H
- #define TEXTURE_H
- #include <iostream>
- #include <glad\glad.h>
- #include "stb_image.h"
- class Texture {
- public:
- unsigned int ID;
- Texture(const char* path) {
- glGenTextures(1, &ID);
- glBindTexture(GL_TEXTURE_2D, ID);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- int width, height, nChannels;
- stbi_set_flip_vertically_on_load(true);
- unsigned char* data = stbi_load(path, &width, &height, &nChannels, STBI_rgb_alpha);
- if (!data) {
- std::cout << "Failed to load texture from path \"" << path << "\", substituting." << std::endl;
- data = stbi_load("res/imgNotFound.png", &width, &height, &nChannels, STBI_rgb_alpha);
- } else {
- std::cout << "Loaded image from \"" << path << "\" into texture of ID " << ID << "." << std::endl;
- }
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
- glGenerateMipmap(GL_TEXTURE_2D);
- stbi_image_free(data);
- };
- void bindToTexUnit(int texUnit) {
- glActiveTexture(GL_TEXTURE0 + texUnit);
- glBindTexture(GL_TEXTURE_2D, ID);
- };
- void destroy() {
- std::cout << "Deleted texture with ID " << ID << "." << std::endl;
- glDeleteTextures(1, &ID);
- };
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement