polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

texture.c (1201B)


      1 
      2 #include "texture.h"
      3 #include "common.h"
      4 #include "gl.h"
      5 #include "stb_image.h"
      6 #include "util.h"
      7 
      8 
      9 #include "texture.h"
     10 #include "common.h"
     11 
     12 u32 create_cubemap(const char *faces[6]) {
     13     u32 tid;
     14     int width, height, n_channels;
     15     glGenTextures(1, &tid);
     16     glBindTexture(GL_TEXTURE_CUBE_MAP, tid);
     17 
     18     for (u32 i = 0; i < 6; i++) {
     19         const char *texture = faces[i];
     20         u8 *data = stbi_load(texture, &width, &height, &n_channels, 0);
     21         if (data) {
     22             glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width,
     23                          height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
     24             stbi_image_free(data);
     25         }
     26         else {
     27             printf("cubemap texture failed to load: %s\n", texture);
     28             assert(0);
     29         }
     30     }
     31 
     32     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     33     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     34     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
     35     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     36     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
     37 
     38     return tid;
     39 }
     40 
     41