#include "world.h" #include "state.h" #include #include tc_worldgen_s tc_default_terrain_generator_g; void tc_draw_chunk(tc_chunk_s *chunk) { mat4x4 model_matrix; 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 = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix"); glUniformMatrix4fv(model_matrix_uniform_location, 1, GL_FALSE, &model_matrix[0][0]); // glBindVertexArray(chunk->vao); glBindBuffer(GL_ARRAY_BUFFER, chunk->vertex_data); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 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); glBindVertexArray(0); } void tc_init_world_generators() { tc_default_terrain_generator_g.name = "Default Terrain Generator"; tc_default_terrain_generator_g.fn_generate_chunk = &tc_generate_default_terrain_chunk; } tc_world_s tc_init_worlds() { tc_init_world_generators(); tc_world_s world; world.chunks_capacity = 256; world.num_chunks = 0; world.chunks = calloc(sizeof(tc_chunk_s *), world.chunks_capacity); world.worldgen = &tc_default_terrain_generator_g; return world; } void tc_set_block_in_chunk( tc_chunk_s *chunk, uint32_t x, uint32_t y, uint32_t z, tc_block_s block ) { chunk->blocks[x][y][z] = block.type_identifier; } void tc_draw_world(tc_world_s *world) { uint32_t chunk_index = 0; while(chunk_index < world->num_chunks) { tc_draw_chunk(world->chunks[chunk_index]); ++chunk_index; } } void tc_add_chunk(tc_world_s *world, tc_chunk_s *chunk) { if(world->num_chunks >= world->chunks_capacity) { world->chunks_capacity *= 2; world->chunks = realloc(world->chunks, sizeof(tc_chunk_s *) * world->chunks_capacity); } world->chunks[world->num_chunks] = chunk; ++world->num_chunks; } // Creates & Generates a chunk and provides a pointer to it tc_chunk_s * tc_new_chunk(tc_world_s *world, float x, float y, float z) { tc_chunk_s *chunk = malloc(sizeof(tc_chunk_s)); chunk->position.x = x; chunk->position.y = y; chunk->position.z = z; world->worldgen->fn_generate_chunk(world->worldgen, chunk); puts("Meshizing"); fflush(stdout); tc_meshize_chunk(chunk); puts("Meshized"); fflush(stdout); glGenVertexArrays(1, &chunk->vao); glBindVertexArray(chunk->vao); glGenBuffers(1, &chunk->vertex_data); glBindBuffer(GL_ARRAY_BUFFER, chunk->vertex_data); 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); tc_add_chunk(world, chunk); return chunk; }