diff options
author | sanine-a <sanine.not@pm.me> | 2020-10-19 05:35:06 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-10-19 05:35:06 -0500 |
commit | 41fa908dc15b522e53946a716f4f6c00520bd46f (patch) | |
tree | bc8176462000b2c77c00d3227037c1acbb13e0ee /src/texture | |
parent | 2d046ffd16d8ff3a06f92ca438ca6b2d6842ce6a (diff) |
add honey libraries back and reorganize directories
Diffstat (limited to 'src/texture')
-rw-r--r-- | src/texture/texture.c | 45 | ||||
-rw-r--r-- | src/texture/texture.h | 42 |
2 files changed, 87 insertions, 0 deletions
diff --git a/src/texture/texture.c b/src/texture/texture.c new file mode 100644 index 0000000..591ee11 --- /dev/null +++ b/src/texture/texture.c @@ -0,0 +1,45 @@ +#include "texture.h" + +enum honey_texture_result honey_texture_new(honey_texture* texture, + char* texture_path, + bool alpha_channel) { + unsigned int texture_id; + glGenTextures(1, &texture_id); + glBindTexture(GL_TEXTURE_2D, texture_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_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + int width, height, channels; + unsigned char* image_data = stbi_load(texture_path, &width, &height, &channels, 0); + if (image_data == NULL) { + fprintf(stderr, "ERROR: failed to load '%s'\n", texture_path); + return TEXTURE_FAILED; + } + + if (alpha_channel) { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data); + } + else { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data); + } + + glGenerateMipmap(GL_TEXTURE_2D); + stbi_image_free(image_data); + + (*texture).texture_id = texture_id; + (*texture).width = width; + (*texture).height = height; + (*texture).channels = channels; + + return TEXTURE_OK; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_texture_use(honey_texture texture, int texture_unit) { + glActiveTexture(GL_TEXTURE0 + texture_unit); + glBindTexture(GL_TEXTURE_2D, texture.texture_id); +} diff --git a/src/texture/texture.h b/src/texture/texture.h new file mode 100644 index 0000000..037d3d0 --- /dev/null +++ b/src/texture/texture.h @@ -0,0 +1,42 @@ +#ifndef HONEY_TEXTURE_H +#define HONEY_TEXTURE_H + +/** @file texture.h + * + *@brief Defines the honey_texture struct and associated functions. +*/ + +#include "../common.h" + +enum honey_texture_result { + TEXTURE_OK, + TEXTURE_FAILED, + N_TEXTURE_RESULTS }; + +typedef struct { + unsigned int texture_id; + int width; + int height; + int channels; +} honey_texture; + +/** @brief Load a texture from disk. + * + * @param[out] texture Pointer to the destination texture + * @param[in] texture_path Path to the location of the texture + * @param[in] alpha_channel Set to true if the target image contains an alpha channel + * + * @return Success or failure type + */ +enum honey_texture_result honey_texture_new(honey_texture* texture, + char* texture_path, + bool alpha_channel); + +/** @brief Load a texture into a texture unit. + * + * @param[in] texture The texture to use + * @param[in] texture_unit The texture unit to put the texture in + */ +void honey_texture_use(honey_texture texture, int texture_unit); + +#endif |