1 #define STB_IMAGE_IMPLEMENTATION
2 #include <stb/stb_image.h>
4 #include <dragontype/list.h>
5 #include "client/client_config.h"
6 #include "client/texture.h"
11 __attribute((constructor(101))) static void textures_init()
13 textures = list_create(&list_compare_string);
16 static void list_delete_texture(unused void *key, void *value, unused void *arg)
18 texture_delete(value);
21 __attribute((destructor)) static void textures_deinit()
23 list_clear_func(&textures, &list_delete_texture, NULL);
26 Texture *texture_create(unsigned char *data, int width, int height, GLenum format, bool mipmap)
28 Texture *texture = malloc(sizeof(Texture));
29 texture->width = width;
30 texture->height = height;
32 glGenTextures(1, &texture->id);
34 glBindTexture(GL_TEXTURE_2D, texture->id);
36 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (mipmap && client_config.mipmap) ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST);
37 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
38 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
39 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
41 glTexImage2D(GL_TEXTURE_2D, 0, format, texture->width, texture->height, 0, format, GL_UNSIGNED_BYTE, data);
42 glGenerateMipmap(GL_TEXTURE_2D);
44 glBindTexture(GL_TEXTURE_2D, 0);
49 GLuint texture_create_cubemap(char *path)
52 glGenTextures(1, &id);
54 glBindTexture(GL_TEXTURE_CUBE_MAP, id);
56 const char *directions[6] = {
65 for (int i = 0; i < 6; i++) {
66 char filename[strlen(path) + 1 + strlen(directions[i]) + 1 + 3 + 1];
67 sprintf(filename, "%s/%s.png", path, directions[i]);
69 int width, height, channels;
70 unsigned char *data = stbi_load(filename, &width, &height, &channels, 0);
72 fprintf(stderr, "Failed to load texture %s\n", filename);
76 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
77 stbi_image_free(data);
80 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
81 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
82 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
83 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
84 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
89 void texture_delete(Texture *texture)
91 glDeleteTextures(1, &texture->id);
95 Texture *texture_load(char *path, bool mipmap)
97 int width, height, channels;
99 unsigned char *data = stbi_load(path, &width, &height, &channels, 0);
101 fprintf(stderr, "Failed to load texture %s\n", path);
105 Texture *texture = texture_create(data, width, height, GL_RGBA, mipmap);
107 stbi_image_free(data);
109 list_put(&textures, texture, NULL);