void update_chunk(Chunk *chunk) { Map *map = &chunk->map; if (chunk->faces) { glDeleteBuffers(1, &chunk->position_buffer); glDeleteBuffers(1, &chunk->normal_buffer); glDeleteBuffers(1, &chunk->uv_buffer); } int faces = 0; MAP_FOR_EACH(map, e) { if (e->w <= 0) { continue; } int f1, f2, f3, f4, f5, f6; exposed_faces(map, e->x, e->y, e->z, &f1, &f2, &f3, &f4, &f5, &f6); int total = f1 + f2 + f3 + f4 + f5 + f6; if (is_plant(e->w)) { total = total ? 4 : 0; } faces += total; } END_MAP_FOR_EACH; GLfloat *position_data = malloc(sizeof(GLfloat) * faces * 18); GLfloat *normal_data = malloc(sizeof(GLfloat) * faces * 18); GLfloat *uv_data = malloc(sizeof(GLfloat) * faces * 12); int position_offset = 0; int uv_offset = 0; MAP_FOR_EACH(map, e) { if (e->w <= 0) { continue; } int f1, f2, f3, f4, f5, f6; exposed_faces(map, e->x, e->y, e->z, &f1, &f2, &f3, &f4, &f5, &f6); int total = f1 + f2 + f3 + f4 + f5 + f6; if (is_plant(e->w)) { total = total ? 4 : 0; } if (total == 0) { continue; } if (is_plant(e->w)) { float rotation = simplex3(e->x, e->y, e->z, 4, 0.5, 2) * 360; make_plant( position_data + position_offset, normal_data + position_offset, uv_data + uv_offset, e->x, e->y, e->z, 0.5, e->w, rotation); } else { make_cube( position_data + position_offset, normal_data + position_offset, uv_data + uv_offset, f1, f2, f3, f4, f5, f6, e->x, e->y, e->z, 0.5, e->w); } position_offset += total * 18; uv_offset += total * 12; } END_MAP_FOR_EACH; GLuint position_buffer = make_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 18, position_data ); GLuint normal_buffer = make_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 18, normal_data ); GLuint uv_buffer = make_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 12, uv_data ); free(position_data); free(normal_data); free(uv_data); chunk->faces = faces; chunk->position_buffer = position_buffer; chunk->normal_buffer = normal_buffer; chunk->uv_buffer = uv_buffer; }
void gen_chunk_buffers(Chunk *chunk) { Map *map = &chunk->map; int faces = 0; MAP_FOR_EACH(map, e) { if (e->w <= 0) { continue; } int f1, f2, f3, f4, f5, f6; exposed_faces(map, e->x, e->y, e->z, &f1, &f2, &f3, &f4, &f5, &f6); int total = f1 + f2 + f3 + f4 + f5 + f6; if (is_plant(e->w)) { total = total ? 4 : 0; } faces += total; } END_MAP_FOR_EACH; GLfloat *position_data, *normal_data, *uv_data; malloc_buffers(3, faces, &position_data, &normal_data, &uv_data); int position_offset = 0; int uv_offset = 0; MAP_FOR_EACH(map, e) { if (e->w <= 0) { continue; } int f1, f2, f3, f4, f5, f6; exposed_faces(map, e->x, e->y, e->z, &f1, &f2, &f3, &f4, &f5, &f6); int total = f1 + f2 + f3 + f4 + f5 + f6; if (is_plant(e->w)) { total = total ? 4 : 0; } if (total == 0) { continue; } if (is_plant(e->w)) { float rotation = simplex3(e->x, e->y, e->z, 4, 0.5, 2) * 360; make_plant( position_data + position_offset, normal_data + position_offset, uv_data + uv_offset, e->x, e->y, e->z, 0.5, e->w, rotation); } else { make_cube( position_data + position_offset, normal_data + position_offset, uv_data + uv_offset, f1, f2, f3, f4, f5, f6, e->x, e->y, e->z, 0.5, e->w); } position_offset += total * 18; uv_offset += total * 12; } END_MAP_FOR_EACH; gen_buffers( 3, faces, position_data, normal_data, uv_data, &chunk->position_buffer, &chunk->normal_buffer, &chunk->uv_buffer); chunk->faces = faces; chunk->dirty = 0; }