#include #include #include #include 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; } uint32_t load_texture(const char* path) { stbi_set_flip_vertically_on_load(1); 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; unsigned char* pixels = stbi_load(path, &width, &height, &channels, 4); 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; } void draw_texture(uint32_t id, vec3 pos, vec4 color) { batch_draw_texture(texture_quad_batch, id, pos, color); } 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 } }; void batch_draw_texture(RenderBatch* batch, uint32_t texture, vec3 pos, vec4 color) { TextureQuadBatchData* batch_data = batch->data; uint32_t vertex_add = 6; if(batch_needs_flush(batch, vertex_add) || batch_data->texture_index > max_textures) 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; vertex->Position = vec3Add(quad_vertex_positions[i], pos); vertex->Tint = color; vertex->TexCoord = texture_quad_texcoords[i]; vertex->TexID = tex_id; batch->vertex_ptr += batch->vertex_size; batch->vertex_count++; } } void texture_quad_batch_flush_callback(RenderBatch* batch) { 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); texture_quad_batch->data = calloc(sizeof(TextureQuadBatchData), 1); texture_quad_batch->flush_callback = &texture_quad_batch_flush_callback; 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; }