testing lots of cubes
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 19 Dec 2021 00:17:47 +0000 (18:17 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 19 Dec 2021 00:17:47 +0000 (18:17 -0600)
run_tree/assets/shaders/world_vertex.glsl
src/main.onyx
src/mesh.onyx

index d93598aa9e1ea034eb67d38788087c41bd6200c5..d267002ec32da9d1cdc2fca717a1149a55967c53 100644 (file)
@@ -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);
 }
index bde1562a39c70c8bd26a04bbe86691c92757857b..2595d0c59d64a8d62ef1f5a534395b347c5ba2c2 100644 (file)
@@ -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);
 }
index c38141b60e4a95dc86671616c651bf8c424ff1a5..d79e1c7eee506cde2a57a9b896e35cecf2b83ec9 100644 (file)
@@ -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;
 }