-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureUnit.cpp
More file actions
35 lines (26 loc) · 981 Bytes
/
Copy pathTextureUnit.cpp
File metadata and controls
35 lines (26 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "TextureUnit.hpp"
#include <SDL2/SDL_image.h>
#include <iostream>
TTexture::TTexture() : textureID(0) {}
TTexture::~TTexture() {
if (textureID != 0) glDeleteTextures(1, &textureID);
}
bool TTexture::LoadFrom(const std::string& filename) {
SDL_Surface* surface = IMG_Load(filename.c_str());
if (!surface) {
std::cerr << "Failed to load texture: " << filename << std::endl;
return false;
}
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
GLenum format = GL_RGB;
if (surface->format->BytesPerPixel == 4) format = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, format, surface->w, surface->h, 0, format, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SDL_FreeSurface(surface);
return true;
}
void TTexture::bind() {
glBindTexture(GL_TEXTURE_2D, textureID);
}