From: Brendan Hansen Date: Sun, 19 Dec 2021 00:17:47 +0000 (-0600) Subject: testing lots of cubes X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9555b4a71c3f2ddb909a4c28b4fe0d206cf22ed9;p=voxel-shooter.git testing lots of cubes --- diff --git a/run_tree/assets/shaders/world_vertex.glsl b/run_tree/assets/shaders/world_vertex.glsl index d93598a..d267002 100644 --- a/run_tree/assets/shaders/world_vertex.glsl +++ b/run_tree/assets/shaders/world_vertex.glsl @@ -19,5 +19,5 @@ void main() { float((a_data & 0x00F00U) >> 8U) / 15.0 ) * (float((a_data & 0x0F000U) >> 12U) / 15.0); - v_color = vec4(block_color, 1); + v_color = vec4(block_color, 0.6); } diff --git a/src/main.onyx b/src/main.onyx index bde1562..2595d0c 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -10,11 +10,11 @@ camera: Camera; create_window :: () => { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); window = glfwCreateWindow(800, 600, #cstr "Voxel Shooter"); glfwMakeContextCurrent(window); - glfwSwapInterval(1); + glfwSwapInterval(0); glfwSetWindowSizeCallback(window, "on_resize"); glfwSetKeyCallback(window, "on_key"); } @@ -94,6 +94,8 @@ setup_opengl :: () { glEnable(GL_CULL_FACE); glFrontFace(GL_CW); glCullFace(GL_BACK); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); compile_shader :: (source: str, type: GLenum) -> GLint { shader := glCreateShader(type); @@ -149,10 +151,10 @@ setup_opengl :: () { matrix_block_index := glGetUniformBlockIndex(prog, #cstr "u_matrix_block"); glUniformBlockBinding(prog, matrix_block_index, 0); - for 100 { - x := random.between(0, 10); - y := random.between(0, 10); - z := random.between(0, 10); + for 1000 { + x := random.between(0, 31); + y := random.between(0, 31); + z := random.between(0, 31); cubes << mesh_make_cube(~~x, ~~y, ~~z); } @@ -207,18 +209,23 @@ update :: (dt: f32) { camera.x_rot += ~~(my - last_mouse_y) / 400.0f; camera.x_rot = math.clamp(camera.x_rot, -math.PI / 2 + 0.1, math.PI / 2 - 0.1); + speed := 3 * dt; + if glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS { + speed = 8 * dt; + } + facing := camera_get_forward(^camera); if glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS { - camera.position += facing * 3 * dt; + camera.position += facing * speed; } if glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS { - camera.position -= facing * 3 * dt; + camera.position -= facing * speed; } if glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS { - camera.position += Vector3.norm(Vector3.cross(facing, .{0,1,0})) * 3 * dt; + camera.position += Vector3.norm(Vector3.cross(facing, .{0,1,0})) * speed; } if glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS { - camera.position -= Vector3.norm(Vector3.cross(facing, .{0,1,0})) * 3 * dt; + camera.position -= Vector3.norm(Vector3.cross(facing, .{0,1,0})) * speed; } update_world_matrix(); @@ -228,9 +235,19 @@ draw :: () { glClearColor(.2, .2, .2, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // mesh_draw(^plane_mesh); - //mesh_draw(^cube_mesh); - for^ cubes do mesh_draw(it); + #persist forward : Vector3; + forward = camera_get_forward(^camera); + array.sort(cubes, (x, y) => { + v := Vector3.dot(forward, *(cast(^Vector3) ^y.verticies[0]) - camera.position) - Vector3.dot(forward, *(cast(^Vector3) ^x.verticies[0]) - camera.position); + if v < 0 do return -1; + if v > 0 do return 1; + return 0; + }); + + for^ cubes { + if Vector3.dot(forward, *(cast(^Vector3) ^it.verticies[0]) - camera.position) < 0 do continue; + mesh_draw(it); + } glfwSwapBuffers(window); } diff --git a/src/mesh.onyx b/src/mesh.onyx index c38141b..d79e1c7 100644 --- a/src/mesh.onyx +++ b/src/mesh.onyx @@ -10,13 +10,6 @@ Vertex :: struct { // 4-bits (4..7) - green color // 4-bits (8..11) - blue color // 4-bits (12..15) - intensity - // 12-bits (16..27) - light per face (2 bits per face) - // 16..17 - top - // 18..19 - bottom - // 20..21 - front - // 22..23 - left - // 24..25 - back - // 26..27 - right data: u32; }