diff --git a/include/test.h b/include/test.h index cdf6a2e..65a708a 100644 --- a/include/test.h +++ b/include/test.h @@ -7,7 +7,8 @@ extern "C" { void load(const char * source_path); void draw(); void update(float lx, float ly, float rx, float ry, float tl, float tr, - int up, int down, int left, int right); + int up, int down, int left, int right, + int a, int b, int x, int y); #ifdef __cplusplus } diff --git a/main.lua b/main.lua index c2ce636..d6fa7e3 100644 --- a/main.lua +++ b/main.lua @@ -9,7 +9,8 @@ void load(const char * source_path); void update_window(int width, int height); void draw(); void update(float lx, float ly, float rx, float ry, float tl, float tr, - int up, int down, int left, int right); + int up, int down, int left, int right, + int a, int b, int x, int y); ]] local source_path = love.filesystem.getSource() test = ffi.load(source_path .. "/test.so") @@ -29,7 +30,11 @@ local update = function(dt) local down = joystick:isGamepadDown("dpdown") local left = joystick:isGamepadDown("dpleft") local right = joystick:isGamepadDown("dpright") - test.update(lx, ly, rx, ry, tl, tr, up, down, left, right) + local a = joystick:isGamepadDown("a") + local b = joystick:isGamepadDown("b") + local x = joystick:isGamepadDown("x") + local y = joystick:isGamepadDown("y") + test.update(lx, ly, rx, ry, tl, tr, up, down, left, right, a, b, x, y) end end diff --git a/minecraft/block_id_to_texture_id.data b/minecraft/block_id_to_texture_id.data new file mode 100644 index 0000000..0b688ae Binary files /dev/null and b/minecraft/block_id_to_texture_id.data differ diff --git a/minecraft/gen/blocks.py b/minecraft/gen/blocks.py index f50d99d..ec847e9 100644 --- a/minecraft/gen/blocks.py +++ b/minecraft/gen/blocks.py @@ -1,15 +1,13 @@ import struct +import data +import sys -unk = 185 +unk = 253 -lookup = { - k: v for k, v, _ in mapping -} - -with open("block_id_to_texture_id.data", "wb") as f: +with open(sys.argv[1], "wb") as f: for i in range(256): - value = lookup.get(i, unk) + if i in data.tiles_by_id: + value = data.tiles_by_id[i].texture + else: + value = unk f.write(struct.pack("> (1 - (block_index % 2)) * 4) & 0xf + xyz = mcregion.xyz_from_block_index(block_index) center_position = vec3.add(xyz, (chunk_x * 16, 0, chunk_z * 16)) @@ -74,7 +77,7 @@ def block_neighbors(level_table, chunk_x, chunk_z, block_index): normal_indices = list(find_non_neighbors()) if block_id in non_solid_blocks or normal_indices: - yield center_position, block_id, normal_indices + yield center_position, block_id, block_data, normal_indices def devoxelize_region(level_table, level_table_keys): for chunk_x, chunk_z in level_table_keys: @@ -107,15 +110,15 @@ def build_block_configuration_table(): indices.extend(vertex_buffer.faces_by_normal[vertex_buffer.normals[j]]) yield indices -def pack_instance_data(position, block_id): +def pack_instance_data(position, block_id, block_data): packed = struct.pack(" 1.5f) view_state.pitch = 1.5f; - if (view_state.pitch < -1.5f) view_state.pitch = -1.5f; + if (view_state.pitch > 1.57f) view_state.pitch = 1.57f; + if (view_state.pitch < -1.57f) view_state.pitch = -1.57f; XMMATRIX mrn = XMMatrixRotationAxis(normal, view_state.pitch); view_state.direction = XMVector3Transform(view_state.forward, mrn); @@ -438,6 +522,12 @@ void update(float lx, float ly, float rx, float ry, float tl, float tr, if (new_fov > 0.00001f) { view_state.fov = new_fov; } + lighting.quadratic += 0.01 * a + -0.01 * b; + if (lighting.quadratic < 0.0f) + lighting.quadratic = 0.0f; + lighting.linear += 0.01 * x + -0.01 * y; + if (lighting.linear < 0.0f) + lighting.linear = 0.0f; } static inline int popcount(int x) @@ -454,6 +544,20 @@ void labeled_value(char * const buf, char const * const label, char const * cons buf[label_length + len] = 0; } +inline static float draw_vector(font::font const& ter_best, char * const buf, float y, char const * const label, XMVECTOR vec) +{ + labeled_value(buf, label, ".x: %.2f", XMVectorGetX(vec)); + font::draw_string(ter_best, buf, 10, y); + y += ter_best.desc->glyph_height; + labeled_value(buf, label, ".y: %.2f", XMVectorGetY(vec)); + font::draw_string(ter_best, buf, 10, y); + y += ter_best.desc->glyph_height; + labeled_value(buf, label, ".z: %.2f", XMVectorGetZ(vec)); + font::draw_string(ter_best, buf, 10, y); + y += ter_best.desc->glyph_height; + return y; +} + void draw_hud() { char buf[512]; @@ -469,11 +573,22 @@ void draw_hud() font::draw_string(ter_best, buf, 10, y); y += ter_best.desc->glyph_height; - labeled_value(buf, "pitch: ", "%.9f", view_state.pitch); + labeled_value(buf, "font_height: ", "%d", ter_best.desc->glyph_height); font::draw_string(ter_best, buf, 10, y); y += ter_best.desc->glyph_height; - labeled_value(buf, "font_height: ", "%d", ter_best.desc->glyph_height); + labeled_value(buf, "lighting.quadratic: ", "%.2f", lighting.quadratic); + font::draw_string(ter_best, buf, 10, y); + y += ter_best.desc->glyph_height; + + labeled_value(buf, "lighting.linear: ", "%.2f", lighting.linear); + font::draw_string(ter_best, buf, 10, y); + y += ter_best.desc->glyph_height; + + y = draw_vector(ter_best, buf, y, "eye", view_state.eye); + y = draw_vector(ter_best, buf, y, "forward", view_state.forward); + + labeled_value(buf, "pitch: ", "%.9f", view_state.pitch); font::draw_string(ter_best, buf, 10, y); y += ter_best.desc->glyph_height; } @@ -505,7 +620,7 @@ void draw_minecraft() glUniformMatrix4fv(test_location.uniform.transform, 1, false, (float *)&transform); glUniform1i(test_location.uniform.terrain_sampler, 0); - //glBindBufferBase(GL_UNIFORM_BUFFER, location.binding.light_block, light_uniform_buffer); + glBindBufferBase(GL_UNIFORM_BUFFER, test_location.binding.texture_id, texture_id_uniform_buffer); //glEnable(GL_CULL_FACE); //glCullFace(GL_FRONT); @@ -565,6 +680,45 @@ void draw_quad() glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, (void *)0); } +static inline bool near_zero(float a) +{ + return (fabsf(a) < 0.00001f); +} + +void draw_lighting() +{ + glUseProgram(lighting_program); + glDepthFunc(GL_ALWAYS); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, geometry_buffer_pnc.target[0]); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, geometry_buffer_pnc.target[1]); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, geometry_buffer_pnc.target[2]); + + glUniform1i(lighting_location.uniform.position_sampler, 0); + glUniform1i(lighting_location.uniform.normal_sampler, 1); + glUniform1i(lighting_location.uniform.color_sampler, 2); + + float quadratic = near_zero(lighting.quadratic) ? 0.0 : 1.0f / lighting.quadratic; + float linear = near_zero(lighting.linear) ? 0.0 : 1.0f / lighting.linear; + glUniform1f(lighting_location.uniform.quadratic, quadratic); + glUniform1f(lighting_location.uniform.linear, linear); + + + XMFLOAT3 eye; + XMStoreFloat3(&eye, view_state.eye); + glUniform3fv(lighting_location.uniform.eye, 1, (float*)&eye); + + glBindBufferBase(GL_UNIFORM_BUFFER, lighting_location.binding.lights, light_uniform_buffer); + + glBindVertexArray(empty_vertex_array_object); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_index_buffer); + + glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, (void *)0); +} + void draw() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); @@ -578,6 +732,7 @@ void draw() glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - draw_quad(); - //draw_hud(); + draw_lighting(); + //draw_quad(); + draw_hud(); }