gearlib/src/textures.c

120 lines
3.8 KiB
C
Raw Normal View History

2024-04-30 23:02:19 +12:00
#include <textures.h>
#include <stb_image.h>
#include <opengl.h>
#include <stdio.h>
2024-05-03 11:23:24 +12:00
#include <assert.h>
2024-04-30 23:02:19 +12:00
2024-05-03 11:18:50 +12:00
RenderBatch* texture_quad_batch = NULL;
int max_textures;
void setup_textures() {
texture_quad_batch = create_texture_quad_batch();
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_textures);
if(max_textures > 32) max_textures = 32;
}
2024-04-30 23:02:19 +12:00
uint32_t load_texture(const char* path) {
uint32_t id;
glCreateTextures(GL_TEXTURE_2D, 1, &id);
glTextureParameteri(id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTextureParameteri(id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, channels;
2024-05-03 11:18:50 +12:00
unsigned char* pixels = stbi_load(path, &width, &height, &channels, 4);
2024-04-30 23:02:19 +12:00
if(pixels) {
glTextureStorage2D(id, 1, GL_RGBA8, width, height);
glTextureSubImage2D(id, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
printf("[Texture %d, %s] Loaded successfully (%dx%d, %d channels)\n", id, path, width, height, channels);
} else {
printf("[Texture %d, %s] Failed to load image data: %s\n", id, path, stbi_failure_reason());
exit(-1);
}
return id;
}
2024-05-03 11:18:50 +12:00
vec2 texture_quad_texcoords[] = {
{ 0.0f, 1.0f },
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 0.0f, 1.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f }
};
2024-05-03 22:52:53 +12:00
void draw_texture(Texture id, vec2 pos, vec2 size, vec4 tint) {
mat4 transform = mat4Multiply(
mat4Scale(size.x, size.y, 1.0f),
mat4Translate(pos.x, pos.y, 0.0f));
draw_texture_trans(id, transform, tint);
}
void draw_texture_trans(Texture id, mat4 transform, vec4 tint) {
assert(texture_quad_batch != NULL && "texture_quad_batch is null, was setup_textures() called?");
batch_draw_texture(texture_quad_batch, id, transform, tint);
}
void batch_draw_texture(RenderBatch* batch, Texture texture, mat4 transform, vec4 color) {
2024-05-03 11:18:50 +12:00
TextureQuadBatchData* batch_data = batch->data;
2024-05-03 22:52:53 +12:00
uint32_t vertex_add = 6;
2024-05-03 11:18:50 +12:00
2024-05-03 22:52:53 +12:00
if(batch_needs_flush(batch, vertex_add) || batch_data->texture_index >= max_textures)
2024-05-03 11:18:50 +12:00
flush_batch(batch);
uint32_t tex_id = batch_data->texture_index++;
glBindTextureUnit(tex_id, texture);
for(int i = 0; i < vertex_add; i++) {
TextureQuadVertex* vertex = batch->vertex_ptr;
2024-05-03 22:52:53 +12:00
vertex->Position = vec3Transform(quad_vertex_positions[i], transform);
2024-05-03 11:18:50 +12:00
vertex->Tint = color;
vertex->TexCoord = texture_quad_texcoords[i];
vertex->TexID = tex_id;
batch->vertex_ptr += batch->vertex_size;
batch->vertex_count++;
}
}
2024-05-03 22:52:53 +12:00
void texture_flush_callback(RenderBatch* batch) {
2024-05-03 11:18:50 +12:00
TextureQuadBatchData* data = batch->data;
data->texture_index = 0;
}
RenderBatch* create_texture_quad_batch() {
RenderBatch* texture_quad_batch = create_batch(sizeof(TextureQuadVertex), MAX_VERTICES);
texture_quad_batch->shader = load_shader_program(
compile_shader("assets/texture.vert", GL_VERTEX_SHADER),
compile_shader("assets/texture.frag", GL_FRAGMENT_SHADER), 0);
2024-05-03 22:52:53 +12:00
2024-05-03 11:18:50 +12:00
texture_quad_batch->data = calloc(sizeof(TextureQuadBatchData), 1);
2024-05-03 22:52:53 +12:00
texture_quad_batch->flush_callback = &texture_flush_callback;
2024-05-03 11:18:50 +12:00
batch_add_attrib(texture_quad_batch, (VertexAttrib){
.type = GL_FLOAT, .size = sizeof(float), .count = 3
}); // pos
batch_add_attrib(texture_quad_batch, (VertexAttrib){
.type = GL_FLOAT, .size = sizeof(float), .count = 4
}); // color
batch_add_attrib(texture_quad_batch, (VertexAttrib){
.type = GL_FLOAT, .size = sizeof(float), .count = 2
}); // texcoord
batch_add_attrib(texture_quad_batch, (VertexAttrib){
.type = GL_FLOAT, .size = sizeof(float), .count = 1
}); // texid
batch_bind_attribs(texture_quad_batch);
return texture_quad_batch;
}