Added texture loading. A texture must be at path assets/block_atlas.png

This commit is contained in:
Eric-Paul Ickhorn 2023-10-11 15:01:54 +02:00
parent 511ec18e6f
commit 6c57999c8f
12 changed files with 8274 additions and 50 deletions

View File

@ -2,11 +2,11 @@
mkdir -p dependencies/build/ mkdir -p dependencies/build/
gcc -o dependencies/build/glad.o \ gcc -g -o dependencies/build/glad.o \
-c dependencies/sources/glad/glad.c \ -c dependencies/sources/glad/glad.c \
-I dependencies/include/ -I dependencies/include/
gcc -o techneck.elf \ gcc -g -o techneck.elf \
code/source-c/*.c dependencies/build/glad.o \ code/source-c/*.c dependencies/build/glad.o \
-I dependencies/include -lGL -lSDL2 -lm -I dependencies/include -lGL -lSDL2 -lm

63
code/source-c/assets.c Normal file
View File

@ -0,0 +1,63 @@
#include "assets.h"
#include "state.h"
#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
tc_asset_storage_s tc_init_asset_storage(char *asset_folder_path)
{
tc_asset_storage_s assets;
assets.images_capacity = 256;
assets.num_images = 0;
assets.images = calloc(sizeof(tc_image_s *), assets.images_capacity);
return assets;
}
void tc_upload_image(tc_image_s *image)
{
glGenTextures(1, &image->gl_identifier);
glBindTexture(GL_TEXTURE_2D, image->gl_identifier);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
image->width, image->height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
image->pixels
);
}
tc_image_s * tc_load_image_from_disk(char *path)
{
tc_image_s *image = malloc(sizeof(tc_image_s));
image->attributes_capacity = 8;
image->attributes = calloc(sizeof(tc_asset_attribute_s), image->attributes_capacity);
image->num_attributes = 0;
image->pixels = stbi_load(path, &image->width, &image->height, NULL, 4);
image->len_name = 0;
image->name = NULL;
if(tc_game_state_g.asset_storage.num_images >= tc_game_state_g.asset_storage.images_capacity)
{
tc_game_state_g.asset_storage.images_capacity *= 2;
tc_game_state_g.asset_storage.images =
realloc(tc_game_state_g.asset_storage.images,
sizeof(tc_image_s *) * tc_game_state_g.asset_storage.images_capacity
);
}
tc_game_state_g.asset_storage.images[tc_game_state_g.asset_storage.num_images] = image;
return image;
}

56
code/source-c/assets.h Normal file
View File

@ -0,0 +1,56 @@
#ifndef TC_ASSETS_H
#define TC_ASSETS_H
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
typedef struct tc_asset_attribute
{
char *issuer;
char *name;
union
{
void *pointer;
int64_t integer;
char *text;
} data;
} tc_asset_attribute_s;
typedef struct tc_image
{
uint32_t gl_identifier;
uint32_t len_name;
char *name;
uint32_t attributes_capacity;
uint32_t num_attributes;
tc_asset_attribute_s *attributes;
uint32_t width;
uint32_t height;
void *pixels;
} tc_image_s;
typedef struct tc_asset_storage
{
uint32_t images_capacity;
uint32_t num_images;
tc_image_s **images;
// todo: meshes
} tc_asset_storage_s;
tc_asset_storage_s tc_init_asset_storage (char *asset_folder_path);
tc_image_s * tc_load_image_from_disk (char *path);
void tc_upload_image (tc_image_s *image);
#endif

View File

@ -32,17 +32,23 @@ void tc_init_renderer(techneck_s *techneck)
} }
void tc_load_textures()
{
tc_game_state_g.block_texture_atlas = tc_load_image_from_disk("assets/block_atlas.png");
tc_upload_image(tc_game_state_g.block_texture_atlas);
}
techneck_s tc_init() techneck_s tc_init()
{ {
techneck_s techneck; techneck_s techneck;
tc_init_renderer(&techneck); tc_init_renderer(&techneck);
techneck.main_world = tc_init_worlds(); techneck.main_world = tc_init_worlds();
techneck.asset_storage = tc_init_asset_storage("assets/");
techneck.block_registry = tc_init_blocks(); techneck.block_registry = tc_init_blocks();
return techneck; return techneck;
} }
void tc_cleanup() void tc_cleanup()
{ {
SDL_GL_DeleteContext(tc_game_state_g.renderer.gl_context); SDL_GL_DeleteContext(tc_game_state_g.renderer.gl_context);

View File

@ -74,10 +74,11 @@ tc_block_s block;
void render_block(tc_block_s block) void render_block(tc_block_s block)
{ {
mat4x4 model_matrix; mat4x4 model_matrix;
mat4x4_translate(model_matrix, block.position.x, block.position.y, block.position.z); mat4x4_identity(model_matrix);
// mat4x4_rotate_X(view_matrix, model_matrix, block.rotation.x); mat4x4_rotate_X(view_matrix, model_matrix, block.rotation.x);
// mat4x4_rotate_Y(view_matrix, model_matrix, block.rotation.y); mat4x4_rotate_Y(view_matrix, model_matrix, block.rotation.y);
// mat4x4_rotate_Z(view_matrix, model_matrix, block.rotation.z); mat4x4_rotate_Z(view_matrix, model_matrix, block.rotation.z);
mat4x4_translate_in_place(model_matrix, block.position.x, block.position.y, block.position.z);
int model_matrix_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix"); int model_matrix_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix");
glUniformMatrix4fv(model_matrix_uniform_location, 1, GL_FALSE, &model_matrix[0][0]); glUniformMatrix4fv(model_matrix_uniform_location, 1, GL_FALSE, &model_matrix[0][0]);
@ -94,6 +95,9 @@ tc_block_s tc_new_block_at_3f(float x, float y, float z)
block.position.x = x; block.position.x = x;
block.position.y = y; block.position.y = y;
block.position.z = z; block.position.z = z;
block.rotation.x = 0.0f;
block.rotation.y = 0.0f;
block.rotation.z = 0.0f;
glGenBuffers(1, &block.drawing.vbo); glGenBuffers(1, &block.drawing.vbo);
glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo); glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 108, &triangle_vertices[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 108, &triangle_vertices[0], GL_STATIC_DRAW);
@ -299,6 +303,8 @@ int main(int argc, char **argv)
tc_game_state_g = tc_init(); tc_game_state_g = tc_init();
tc_new_chunk(&tc_game_state_g.main_world, 1.0f, 1.0f, 1.0f); tc_new_chunk(&tc_game_state_g.main_world, 1.0f, 1.0f, 1.0f);
tc_load_textures();
block = tc_new_block_at_3f(0.0, 0.0, 0.0); block = tc_new_block_at_3f(0.0, 0.0, 0.0);
int64_t frame_index = 1; int64_t frame_index = 1;

View File

@ -59,6 +59,64 @@ float tc_block_vertices[108] =
-0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f 0.5f, -0.5f, -0.5f
}; };
float test_uvs[72] =
{
// Front
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
// Back
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
// Left
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
// Right
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
// Top
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
1.0f, 0.0f,
// Back
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f
};
uint32_t tc_count_chunk_vertices(tc_chunk_s *chunk) uint32_t tc_count_chunk_vertices(tc_chunk_s *chunk)
{ {
uint32_t num_vertices = 0; uint32_t num_vertices = 0;
@ -74,7 +132,7 @@ uint32_t tc_count_chunk_vertices(tc_chunk_s *chunk)
z_in_chunk = 0; z_in_chunk = 0;
while(z_in_chunk < 32) while(z_in_chunk < 32)
{ {
if(chunk->blocks[x_in_chunk][y_in_chunk][z_in_chunk].type_identifier > 0) if(chunk->blocks[x_in_chunk][y_in_chunk][z_in_chunk] > 0)
{ {
num_vertices += 36; num_vertices += 36;
} }
@ -90,10 +148,14 @@ uint32_t tc_count_chunk_vertices(tc_chunk_s *chunk)
void tc_meshize_chunk(tc_chunk_s *chunk) void tc_meshize_chunk(tc_chunk_s *chunk)
{ {
chunk->num_vertices = tc_count_chunk_vertices(chunk); chunk->num_vertices = tc_count_chunk_vertices(chunk);
chunk->vertex_positions = malloc(sizeof(float) * 3 * chunk->num_vertices); chunk->vertex_positions = malloc(sizeof(float) * (3) * chunk->num_vertices);
chunk->vertex_uvs = malloc(sizeof(float) * (2) * chunk->num_vertices);
uint32_t vertex_index = 0; uint32_t vertex_index = 0;
float block_positions[36*3]; float block_positions[36*3];
float block_uvs[36*2];
memcpy(block_uvs, test_uvs, sizeof(float) * 36 * 2);
uint32_t x_in_chunk = 0; uint32_t x_in_chunk = 0;
uint32_t y_in_chunk = 0; uint32_t y_in_chunk = 0;
@ -106,7 +168,7 @@ void tc_meshize_chunk(tc_chunk_s *chunk)
z_in_chunk = 0; z_in_chunk = 0;
while(z_in_chunk < 32) while(z_in_chunk < 32)
{ {
if(chunk->blocks[x_in_chunk][y_in_chunk][z_in_chunk].type_identifier > 0) if(chunk->blocks[x_in_chunk][y_in_chunk][z_in_chunk] > 0)
{ {
memcpy( memcpy(
block_positions, block_positions,
@ -134,6 +196,12 @@ void tc_meshize_chunk(tc_chunk_s *chunk)
block_positions, block_positions,
sizeof(float) * 36 * 3 sizeof(float) * 36 * 3
); );
memcpy(
&chunk->vertex_uvs[vertex_index*2],
block_uvs,
sizeof(float) * 36 * 2
);
vertex_index += 36; vertex_index += 36;
} }
++z_in_chunk; ++z_in_chunk;

View File

@ -6,6 +6,7 @@
#include <stdint.h> #include <stdint.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "assets.h"
#include "shaders.h" #include "shaders.h"
#include "blocks.h" #include "blocks.h"
#include "world.h" #include "world.h"
@ -32,9 +33,12 @@ typedef struct tc_renderer
typedef struct typedef struct
{ {
tc_renderer_s renderer; tc_renderer_s renderer;
tc_asset_storage_s asset_storage;
tc_block_registry_s block_registry; tc_block_registry_s block_registry;
tc_world_s main_world; tc_world_s main_world;
tc_image_s *block_texture_atlas;
} techneck_s; } techneck_s;
extern techneck_s tc_game_state_g; extern techneck_s tc_game_state_g;

View File

@ -9,7 +9,8 @@ tc_worldgen_s tc_default_terrain_generator_g;
void tc_draw_chunk(tc_chunk_s *chunk) void tc_draw_chunk(tc_chunk_s *chunk)
{ {
mat4x4 model_matrix; mat4x4 model_matrix;
mat4x4_translate(model_matrix, chunk->position.x*32, chunk->position.y*32, -chunk->position.z*32); mat4x4_identity(model_matrix);
mat4x4_translate_in_place(model_matrix, chunk->position.x*32, chunk->position.y*32, -chunk->position.z*32);
int model_matrix_uniform_location = int model_matrix_uniform_location =
glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix"); glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix");
@ -20,6 +21,20 @@ void tc_draw_chunk(tc_chunk_s *chunk)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
2 * sizeof(float),
(void *) (sizeof(float) * 3 * chunk->num_vertices)
);
glEnableVertexAttribArray(1);
glActiveTexture(GL_TEXTURE0);
glUniform1i(
glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "block_atlas"),
0
);
glBindTexture(GL_TEXTURE_2D, tc_game_state_g.block_texture_atlas->gl_identifier);
glDrawArrays(GL_TRIANGLES, 0, chunk->num_vertices); glDrawArrays(GL_TRIANGLES, 0, chunk->num_vertices);
glBindVertexArray(0); glBindVertexArray(0);
@ -51,7 +66,7 @@ void tc_set_block_in_chunk(
uint32_t x, uint32_t y, uint32_t z, uint32_t x, uint32_t y, uint32_t z,
tc_block_s block tc_block_s block
) { ) {
chunk->blocks[x][y][z] = block; chunk->blocks[x][y][z] = block.type_identifier;
} }
@ -93,7 +108,17 @@ tc_chunk_s * tc_new_chunk(tc_world_s *world, float x, float y, float z)
glGenBuffers(1, &chunk->vertex_data); glGenBuffers(1, &chunk->vertex_data);
glBindBuffer(GL_ARRAY_BUFFER, chunk->vertex_data); glBindBuffer(GL_ARRAY_BUFFER, chunk->vertex_data);
glBufferData(GL_ARRAY_BUFFER, chunk->num_vertices * 3 * sizeof(float), chunk->vertex_positions, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, chunk->num_vertices * 5 * sizeof(float), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0,
chunk->num_vertices * 3 * sizeof(float),
chunk->vertex_positions
);
glBufferSubData(GL_ARRAY_BUFFER,
chunk->num_vertices * 3 * sizeof(float),
chunk->num_vertices * 2 * sizeof(float),
chunk->vertex_uvs
);
glBindVertexArray(0); glBindVertexArray(0);
tc_add_chunk(world, chunk); tc_add_chunk(world, chunk);

View File

@ -24,6 +24,7 @@ struct tc_block
{ {
uint32_t type_identifier; uint32_t type_identifier;
tc_vec3_s position; tc_vec3_s position;
tc_vec3_s rotation;
tc_object_s drawing; tc_object_s drawing;
}; };
@ -31,10 +32,11 @@ struct tc_block
struct tc_chunk struct tc_chunk
{ {
tc_vec3_s position; tc_vec3_s position;
tc_block_s blocks[32][32][32]; uint32_t blocks[32][32][32];
uint32_t num_vertices; uint32_t num_vertices;
float *vertex_positions; float *vertex_positions;
float *vertex_uvs;
uint32_t vao; uint32_t vao;
uint32_t vertex_data; uint32_t vertex_data;
@ -62,6 +64,7 @@ void tc_set_block_in_chunk(
tc_block_s block tc_block_s block
); );
uint32_t tc_count_chunk_vertices (tc_chunk_s *chunk);
void tc_meshize_chunk (tc_chunk_s *chunk); void tc_meshize_chunk (tc_chunk_s *chunk);
bool tc_generate_default_terrain_chunk (tc_worldgen_s *gen, tc_chunk_s *chunk); bool tc_generate_default_terrain_chunk (tc_worldgen_s *gen, tc_chunk_s *chunk);

7987
dependencies/include/stb_image.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,12 @@
#version 420 core #version 420 core
in vec2 uv_coords;
out vec4 color; out vec4 color;
uniform sampler2D block_atlas;
void main() { void main() {
color = vec4(1.0, 0.0, 0.0, 1.0); color = texture(block_atlas, uv_coords); // vec4(uv_coords, 0.0f, 1.0); // vec4(1.0, 0.0, 0.0, 1.0);
} }

View File

@ -1,6 +1,9 @@
#version 420 core #version 420 core
layout(location=0) in vec3 pos; layout(location=0) in vec3 pos;
layout(location=1) in vec2 uv;
out vec2 uv_coords;
uniform mat4 projection_matrix; uniform mat4 projection_matrix;
uniform mat4 view_matrix; uniform mat4 view_matrix;
@ -9,5 +12,6 @@ uniform mat4 model_matrix;
void main() { void main() {
gl_Position = projection_matrix * view_matrix * model_matrix * vec4(pos, 1.0); gl_Position = projection_matrix * view_matrix * model_matrix * vec4(pos, 1.0);
uv_coords = uv;
} }