diff options
| author | Aaditya Dhruv <[email protected]> | 2026-01-30 19:12:03 -0600 |
|---|---|---|
| committer | Aaditya Dhruv <[email protected]> | 2026-01-30 19:12:03 -0600 |
| commit | 1a721b98caf7559f4a18baa8d3b92269e8f1f6ce (patch) | |
| tree | 32be8966361f8f8ae74c6c0af226e39fe9feb375 /src/block.c | |
| parent | 4992ce0098cac8caef6c9315816b688d96259bce (diff) | |
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
Diffstat (limited to 'src/block.c')
| -rw-r--r-- | src/block.c | 116 |
1 files changed, 73 insertions, 43 deletions
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 <math.h> @@ -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; |
