engine

game engine on C
git clone git://git.evenfri.pw/engine.git
Log | Files | Refs | README

texture.c (926B)


      1 #include "engine.h"
      2 #include "stb_image.h"
      3 
      4 uint texture_load(const char *path) {
      5   uint texture_id;
      6   glGenTextures(1, &texture_id);
      7   glBindTexture(GL_TEXTURE_2D, texture_id);
      8 
      9   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
     10   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     11   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     12   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     13 
     14   int w, h, channels;
     15   stbi_set_flip_vertically_on_load(true);
     16   unsigned char *data = stbi_load(path, &w, &h, &channels, 0);
     17   if (data) {
     18     GLenum format = (channels == 4) ? GL_RGBA : (channels == 3) ? 
     19       GL_RGB : GL_RED;
     20     glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, 
     21         data);
     22     glGenerateMipmap(GL_TEXTURE_2D);
     23   } else {
     24     printf("err: failed to load texture: %s\n", path);
     25   }
     26   stbi_image_free(data);
     27   return texture_id;
     28 }