From 1a721b98caf7559f4a18baa8d3b92269e8f1f6ce Mon Sep 17 00:00:00 2001 From: Aaditya Dhruv Date: Fri, 30 Jan 2026 19:12:03 -0600 Subject: Add basic block textures - Remove the code that sent colors through uniform variables, instead send texture data - Each vertex now has a texture coordinate - struct texture is a easy way to represent textures, can be extended later - Shaders updated to use textures --- src/CMakeLists.txt | 1 + src/block.c | 116 +++++++++++++++++++++++++++++++++-------------------- src/block.h | 4 +- src/chunk.c | 4 +- src/chunk.h | 4 +- src/engine.c | 9 ++++- src/engine.h | 1 + src/shader.c | 10 +++++ src/texture.c | 26 ++++++++++++ src/texture.h | 9 +++++ src/util.c | 9 +++++ src/util.h | 2 + 12 files changed, 147 insertions(+), 48 deletions(-) create mode 100644 src/texture.c create mode 100644 src/texture.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9b9cf03..0a7978b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,4 +10,5 @@ add_executable(junkcraft world.c input.c camera.c + texture.c ) diff --git a/src/block.c b/src/block.c index 56db3f4..14ffa08 100644 --- a/src/block.c +++ b/src/block.c @@ -8,6 +8,7 @@ #include "glad/glad.h" #include "block.h" #include "shader.h" +#include "texture.h" #include "util.h" #include "block.h" #include @@ -21,6 +22,7 @@ void block_update(struct block* blk); int block_init(vec3 pos, struct block* blk) { // Store buffer data into struct // Initialize vbo and ebo for block + memcpy(blk->coords, pos, sizeof(vec3)); block_load_gpu(blk); block_update(blk); @@ -32,58 +34,82 @@ void block_load_gpu(struct block* blk) { // Local world coordinates float vertices[] = { 1.0f, 1.0f, 0.0f, // top-right - 0.0f, 0.0f, 1.0f, // Front + 0.0f, 0.0f, 1.0f, // Front normal + 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, // top-left - 0.0f, 0.0f, 1.0f, // Front + 0.0f, 0.0f, 1.0f, // Front normal + 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, // bottom-left - 0.0f, 0.0f, 1.0f, // Front + 0.0f, 0.0f, 1.0f, // Front normal + 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-right - 0.0f, 0.0f, 1.0f, // Front + 0.0f, 0.0f, 1.0f, // Front normal + 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, // top-right (back plane) - 0.0f, 0.0f, -1.0f, // Back + 0.0f, 0.0f, -1.0f, // Back normal + 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, // top-left (back plane) - 0.0f, 0.0f, -1.0f, // Back + 0.0f, 0.0f, -1.0f, // Back normal + 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, // bottom-left (back plane) - 0.0f, 0.0f, -1.0f, // Back + 0.0f, 0.0f, -1.0f, // Back normal + 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, // bottom-right (back plane) - 0.0f, 0.0f, -1.0f, // Back + 0.0f, 0.0f, -1.0f, // Back normal + 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, // top-right (back plane) - 1.0f, 0.0f, 0.0f, // Right + 1.0f, 0.0f, 0.0f, // Right normal + 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // top-right - 1.0f, 0.0f, 0.0f, // Right + 1.0f, 0.0f, 0.0f, // Right normal + 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, // bottom-right - 1.0f, 0.0f, 0.0f, // Right + 1.0f, 0.0f, 0.0f, // Right normal + 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, // bottom-right (back plane) - 1.0f, 0.0f, 0.0f, // Right + 1.0f, 0.0f, 0.0f, // Right normal + 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left - -1.0f, 0.0f, 0.0f, // Left + -1.0f, 0.0f, 0.0f, // Left normal + 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, // top-left (back plane) - -1.0f, 0.0f, 0.0f, // Left + -1.0f, 0.0f, 0.0f, // Left normal + 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, // bottom-left (back plane) - -1.0f, 0.0f, 0.0f, // Left + -1.0f, 0.0f, 0.0f, // Left normal + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-left - -1.0f, 0.0f, 0.0f, // Left + -1.0f, 0.0f, 0.0f, // Left normal + 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, // top-right (back plane) - 0.0f, 1.0f, 0.0f, // Top + 0.0f, 1.0f, 0.0f, // Top normal + 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, // top-left (back plane) - 0.0f, 1.0f, 0.0f, // Top + 0.0f, 1.0f, 0.0f, // Top normal + 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // top-left - 0.0f, 1.0f, 0.0f, // Top + 0.0f, 1.0f, 0.0f, // Top normal + 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, // top-right - 0.0f, 1.0f, 0.0f, // Top + 0.0f, 1.0f, 0.0f, // Top normal + 1.0f, 0.0f, 1.0f, 0.0f, -1.0f, // bottom-right (back plane) - 0.0f, -1.0f, 0.0f, // Bottom + 0.0f, -1.0f, 0.0f, // Bottom normal + 1.0f, 1.0f, 0.0f, 0.0f, -1.0f, // bottom-left (back plane) - 0.0f, -1.0f, 0.0f, // Bottom + 0.0f, -1.0f, 0.0f, // Bottom normal + 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, // bottom-left - 0.0f, -1.0f, 0.0f, // Bottom + 0.0f, -1.0f, 0.0f, // Bottom normal + 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-right - 0.0f, -1.0f, 0.0f, // Bottom + 0.0f, -1.0f, 0.0f, // Bottom normal + 1.0f, 0.0f, }; // int vertex_order[] = { // 1, 2, 3, 3, 0, 1, // Front @@ -111,14 +137,17 @@ void block_load_gpu(struct block* blk) { blk->_vertex_count = ARRAY_SIZE(vertex_order); glGenVertexArrays(1, &blk->_vao); glBindVertexArray(blk->_vao); - // Enable 2 attribs - position and normals + // Enable 3 attribs - position normals texture glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); // set vao_buffer to pos buffer obj glBindBuffer(GL_ARRAY_BUFFER, blk->_vbo); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), 0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0); // set vao_buffer to normals buffer obj - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (GLvoid*)(3*sizeof(float))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(3*sizeof(float))); + // set vao_buffer to texture buffer obj + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (GLvoid*)(6*sizeof(float))); // Set EBO to the vertex_order glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, blk->_ebo); //NOTE: This is important, otherwise with multiple block_init calls, it @@ -145,24 +174,25 @@ void block_update(struct block* blk) { } // Register block vbos and ebos to context -int block_draw(struct block* blk, struct shader* shader) { +int block_draw(struct block* blk, struct shader* shader, struct texture* texture) { glBindVertexArray(blk->_vao); set_uniform_mat4("model", shader, blk->model); - GLuint loc = glGetUniformLocation(shader->program, "face_colors"); - if (loc == -1) { - fprintf(stderr, "Invalid var %s for get_uniform_mat4: Does not exist\n", "face_colors"); - exit(1); - return -1; - } - float colors[] = { - 0.761f, 0.424f, 0.0f, - 0.761f, 0.424f, 0.0f, - 0.761f, 0.424f, 0.0f, - 0.761f, 0.424f, 0.0f, - 0.404f, 0.776f, 0.027f, - 0.761f, 0.424f, 0.0f, - }; - glUniform3fv(loc, 6, (void*)colors); + // GLuint loc = glGetUniformLocation(shader->program, "face_colors"); + // if (loc == -1) { + // fprintf(stderr, "Invalid var %s for get_uniform_mat4: Does not exist\n", "face_colors"); + // exit(1); + // return -1; + // } + // float colors[] = { + // 0.761f, 0.424f, 0.0f, + // 0.761f, 0.424f, 0.0f, + // 0.761f, 0.424f, 0.0f, + // 0.761f, 0.424f, 0.0f, + // 0.404f, 0.776f, 0.027f, + // 0.761f, 0.424f, 0.0f, + // }; + // glUniform3fv(loc, 6, (void*)colors); + // texture_draw(texture); glDrawElements(GL_TRIANGLES, blk->_vertex_count, GL_UNSIGNED_INT, 0); glBindVertexArray(0); return 0; diff --git a/src/block.h b/src/block.h index f55f784..ad81c47 100644 --- a/src/block.h +++ b/src/block.h @@ -2,12 +2,14 @@ #include "cglm/types.h" #include "glad/glad.h" #include "shader.h" +#include "texture.h" struct block { vec3 coords; GLuint _vao; GLuint _vbo; GLuint _ebo; + GLuint _tbo; int _vertex_count; mat4 model; float angle; @@ -21,7 +23,7 @@ struct block { * */ int block_init(vec3 pos, struct block* blk); -int block_draw(struct block* blk, struct shader* shader); +int block_draw(struct block* blk, struct shader* shader, struct texture* texture); void block_debug(struct block* blk); void block_update(struct block* blk); diff --git a/src/chunk.c b/src/chunk.c index 4918d22..3a7b334 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -147,7 +147,7 @@ void chunk_load(struct chunk *chunk, int coord[2]) { } } -void chunk_draw(struct chunk* chunk, struct shader* shader) { +void chunk_draw(struct chunk* chunk, struct shader* shader, struct texture* texture) { int counter = 0; for (int i = 0; i < CHUNK_WIDTH; i++) { for (int j = 0; j < CHUNK_LENGTH; j++) { @@ -156,7 +156,7 @@ void chunk_draw(struct chunk* chunk, struct shader* shader) { if (blk == NULL) { continue; } - block_draw(blk, shader); + block_draw(blk, shader, texture); counter += 1; } } diff --git a/src/chunk.h b/src/chunk.h index 0122cf9..d49ba80 100644 --- a/src/chunk.h +++ b/src/chunk.h @@ -1,5 +1,6 @@ #pragma once #include "block.h" +#include "texture.h" #include "world.h" #include #define CHUNK_WIDTH 16 @@ -49,5 +50,6 @@ void chunk_unload(struct chunk* chunk); * Technically this wraps block_draw, so block_draw is the one doing all the work * @param chunk Chunk to draw * @param shader Shader to pass to block_draw + * @param texture Textures that block_draw will use */ -void chunk_draw(struct chunk* chunk, struct shader* shader); +void chunk_draw(struct chunk* chunk, struct shader* shader, struct texture* texture); diff --git a/src/engine.c b/src/engine.c index ec1d9d3..5b6cf05 100644 --- a/src/engine.c +++ b/src/engine.c @@ -4,6 +4,7 @@ #include "cglm/io.h" #include "chunk.h" #include "input.h" +#include "texture.h" #include "window.h" #include "world.h" #include @@ -35,6 +36,12 @@ int engine_init(struct engine *engine) { }; engine->shader = shader; + + // Load Textures + struct texture* texture = { 0 }; + texture_init(&engine->texture); + texture_load(engine->texture, "textures/grass.jpg"); + // Setup Objects to draw // memset(engine->loaded_chunks, 0, (1 + CHUNK_DISTANCE * 2) * (1 + CHUNK_DISTANCE * 2)); @@ -131,7 +138,7 @@ void engine_start(struct engine* engine) { world_get_chunk(engine->world, chunk_coord, &chunk); // Load chunk chunk_load(chunk, chunk_coord); - chunk_draw(chunk, engine->shader); + chunk_draw(chunk, engine->shader, engine->texture); } } SDL_RenderPresent(engine->window->renderer); diff --git a/src/engine.h b/src/engine.h index 92689dd..3248d40 100644 --- a/src/engine.h +++ b/src/engine.h @@ -15,6 +15,7 @@ struct engine { struct window* window; struct shader* shader; struct camera* camera; + struct texture* texture; int game_loop; int curr_chunk[2]; struct world* world; diff --git a/src/shader.c b/src/shader.c index 9c36791..c32b12f 100644 --- a/src/shader.c +++ b/src/shader.c @@ -82,3 +82,13 @@ int set_uniform_mat4(char* var, struct shader* shader, mat4 matrix) { glUniformMatrix4fv(loc, 1, GL_FALSE, (void*)matrix); return 0; } +int set_uniform_sampler2d(char* var, struct shader* shader, mat4 matrix) { + GLuint loc = glGetUniformLocation(shader->program, var); + if (loc == -1) { + fprintf(stderr, "Invalid var %s for get_uniform_mat4: Does not exist\n", var); + exit(1); + return -1; + } + glUniformMatrix4fv(loc, 1, GL_FALSE, (void*)matrix); + return 0; +} diff --git a/src/texture.c b/src/texture.c new file mode 100644 index 0000000..8f2a2a7 --- /dev/null +++ b/src/texture.c @@ -0,0 +1,26 @@ +#include "texture.h" +#include "cglm/io.h" +#include "util.h" +#define STB_IMAGE_IMPLEMENTATION +#include "stb/stb_image.h" +#include +#include +void texture_init(struct texture** texture) { + *texture = malloc(sizeof(struct texture)); + memset(*texture, 0, sizeof(struct texture)); +} + +void texture_load(struct texture* texture, char* path) { + int width, height, nr_channels; + unsigned char *data = stbi_load(path, &width, &height, &nr_channels, 0); + vec2 size = { width, height }; + glm_vec2_print(size, stderr); + create_texture(&texture->_tbo, data, size); + stbi_image_free(data); + fprintf(stderr, "Loaded texture\n"); +} + +void texture_draw(struct texture* texture) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture->_tbo); +} diff --git a/src/texture.h b/src/texture.h new file mode 100644 index 0000000..721356b --- /dev/null +++ b/src/texture.h @@ -0,0 +1,9 @@ +#pragma once +#include "glad/glad.h" +struct texture { + GLuint _tbo; +}; + +void texture_init(struct texture** texture); +void texture_load(struct texture* texture, char* path); +void texture_draw(struct texture* texture); diff --git a/src/util.c b/src/util.c index 0a5d983..b296b56 100644 --- a/src/util.c +++ b/src/util.c @@ -22,3 +22,12 @@ void create_ebo(GLuint *ebo, void* buf, int size) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } +void create_texture(GLuint* tbo, void* buf, vec2 size) { + glGenTextures(1, tbo); + glBindTexture(GL_TEXTURE_2D, *tbo); + float borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f }; + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size[0], size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, buf); + glGenerateMipmap(GL_TEXTURE_2D); + // glBindTexture(GL_TEXTURE_2D, 0); +} diff --git a/src/util.h b/src/util.h index 81c9733..721b1a3 100644 --- a/src/util.h +++ b/src/util.h @@ -1,4 +1,6 @@ #pragma once #include "glad/glad.h" +#include "cglm/types.h" void create_vbo(GLuint *vbo, void* buf, int size); void create_ebo(GLuint *ebo, void* buf, int size); +void create_texture(GLuint* tbo, void* buf, vec2 size); -- cgit