mirror of
https://github.com/IllusionMan1212/C2048.git
synced 2026-10-24 02:12:31 +00:00
46 lines
1.0 KiB
C
46 lines
1.0 KiB
C
#include "glad/gl.h"
|
|||
|
|
#include "stb_image.h"
|
||
|
|
|
||
|
|
#include "core.h"
|
||
|
|
#include "texture.h"
|
||
|
|
|
||
|
|
TextureId load_texture(const char *path) {
|
||
|
|
int width, height, nrChannels;
|
||
|
|
unsigned char *data = stbi_load(path, &width, &height, &nrChannels, STBI_rgb_alpha);
|
||
|
|
GLuint texture_id;
|
||
|
|
|
||
|
|
u32 format = GL_RGBA;
|
||
|
|
|
||
|
|
switch (nrChannels) {
|
||
|
|
case 1:
|
||
|
|
format = GL_RED;
|
||
|
|
break;
|
||
|
|
case 2:
|
||
|
|
format = GL_RG;
|
||
|
|
break;
|
||
|
|
case 3:
|
||
|
|
format = GL_RGB;
|
||
|
|
break;
|
||
|
|
case 4:
|
||
|
|
format = GL_RGBA;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
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_MIPMAP_LINEAR);
|
||
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||
|
|
|
||
|
|
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||
|
|
|
||
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||
|
|
|
||
|
|
stbi_image_free(data);
|
||
|
|
|
||
|
|
return texture_id;
|
||
|
|
}
|