polyadvent

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

texture.c (1348B)


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