diff options
| author | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 23:02:32 +0100 |
|---|---|---|
| committer | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 23:02:32 +0100 |
| commit | 6b8af9cf83851c075c6c9514b1deaa931c2b19a4 (patch) | |
| tree | 428986b49c32e21d3f7a3c2dfa41858ae0153209 /08-august/src/texture.c | |
Initial commit
Diffstat (limited to '08-august/src/texture.c')
| -rw-r--r-- | 08-august/src/texture.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/08-august/src/texture.c b/08-august/src/texture.c new file mode 100644 index 0000000..d1b70d9 --- /dev/null +++ b/08-august/src/texture.c @@ -0,0 +1,71 @@ +#include "texture.h" +#include "util/util.h" + +#include <SDL2/SDL.h> +#include <SDL2/SDL_image.h> + +GLuint Texture_Create2D( const char *path ) +{ + GLuint ID; + SDL_Surface *data = IMG_Load(path); + if(data == NULL) + Util_FatalError("Texture %s could not be found!\n", path); + + glGenTextures(1, &ID); + glBindTexture(GL_TEXTURE_2D, ID); + + SDL_LockSurface(data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data->w, data->h, GL_FALSE, GL_RGBA, GL_UNSIGNED_BYTE, data->pixels); + SDL_UnlockSurface(data); + + glGenerateMipmap(GL_TEXTURE_2D); + 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_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -2.4); + + SDL_FreeSurface(data); + glBindTexture(GL_TEXTURE_2D, 0); + + return ID; +} + +GLuint Texture_CreateCubeMap( const char *paths[6] ) +{ + GLuint ID; + glGenTextures(1, &ID); + glBindTexture(GL_TEXTURE_CUBE_MAP, ID); + + SDL_Surface *data; + + int i; + for(i = 0; i < 6; i++) + { + data = IMG_Load(paths[i]); + + if(data == NULL) + Util_FatalError("Texture %s could not be found!\n", paths[i]); + + SDL_LockSurface(data); + + /* All the textures sides are linearly stored so we just add "i" */ + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, + data->w, data->h, GL_FALSE, GL_RGBA, GL_UNSIGNED_BYTE, data->pixels); + + SDL_UnlockSurface(data); + + SDL_FreeSurface(data); + } + + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + return ID; +} + +void Texture_Destroy(GLuint ID) +{ + glDeleteTextures(1, &ID); +} |
