From a6a644340d0dad7fe5a6eba4429e246e850ba176 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 17 Dec 2021 14:27:51 -0600 Subject: [PATCH] added a testing plane --- run_tree/assets/shaders/basic_fragment.glsl | 3 +- run_tree/assets/shaders/basic_vertex.glsl | 3 +- src/main.onyx | 105 ++++++++++++++++---- 3 files changed, 85 insertions(+), 26 deletions(-) diff --git a/run_tree/assets/shaders/basic_fragment.glsl b/run_tree/assets/shaders/basic_fragment.glsl index ebfd107..ce631c3 100644 --- a/run_tree/assets/shaders/basic_fragment.glsl +++ b/run_tree/assets/shaders/basic_fragment.glsl @@ -4,10 +4,9 @@ precision mediump float; uniform sampler2D u_sampler; uniform float u_sampler_enabled; -in vec4 v_col; in vec2 v_tex; out vec4 fragColor; void main() { - fragColor = mix(v_col, texture(u_sampler, v_tex), u_sampler_enabled); + fragColor = mix(vec4(0.8, 0.8, 0.8, 1), texture(u_sampler, v_tex), u_sampler_enabled); } diff --git a/run_tree/assets/shaders/basic_vertex.glsl b/run_tree/assets/shaders/basic_vertex.glsl index d68b046..5efc2ec 100644 --- a/run_tree/assets/shaders/basic_vertex.glsl +++ b/run_tree/assets/shaders/basic_vertex.glsl @@ -5,13 +5,12 @@ layout(location = 1) in vec2 a_tex; layout(std140) uniform u_matrix_block { mat4 u_view; mat4 u_world; + mat4 u_model; }; -out vec4 v_col; out vec2 v_tex; void main() { gl_Position = u_view * u_world * vec4(a_pos, 1); - v_col = vec4(a_pos, 1); v_tex = a_tex; } diff --git a/src/main.onyx b/src/main.onyx index 8307b08..2e3b932 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -17,12 +17,6 @@ create_window :: () => { glfwSwapInterval(1); glfwSetWindowSizeCallback(window, "on_resize"); glfwSetKeyCallback(window, "on_key"); - - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); - - if glfwRawMouseMotionSupported() == GLFW_TRUE { - glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE); - } } #export "on_resize" (window: GLFWwindow_p, width, height: u32) { @@ -33,7 +27,27 @@ create_window :: () => { #export "on_key" (window: GLFWwindow_p, key, scancode, action, mod: u32) { if key == GLFW_KEY_ESCAPE && action == GLFW_PRESS { - glfwSetWindowShouldClose(window, true); + if cursor_grabbed do toggle_cursor_grabbed(); + else do glfwSetWindowShouldClose(window, true); + } +} + +cursor_grabbed := false; +toggle_cursor_grabbed :: () { + cursor_grabbed = !cursor_grabbed; + + if cursor_grabbed { + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + if glfwRawMouseMotionSupported() == GLFW_TRUE { + glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE); + } + } else { + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + + if glfwRawMouseMotionSupported() == GLFW_TRUE { + glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_FALSE); + } } } @@ -43,6 +57,12 @@ Mesh :: struct { indicies: [] u32; } +mesh_draw :: (use mesh: ^Mesh) { + glBindVertexArray(handle); + glDrawElements(GL_TRIANGLES, indicies.count, GL_UNSIGNED_INT, ~~0); + glBindVertexArray(-1); +} + Vertex :: struct { x, y, z: f32; } @@ -59,8 +79,8 @@ make_cube_mesh :: () -> Mesh { vertex_data[7] = .{1,0,1}; #persist index_data := u32.[ - 0, 1, 2, // back - 0, 2, 3, + 0, 2, 1, // back + 0, 3, 2, 3, 6, 2, // right side 3, 7, 6, @@ -103,13 +123,49 @@ make_cube_mesh :: () -> Mesh { return mesh; } +make_plane_mesh :: () -> Mesh { + vertex_data := memory.make_slice(Vertex, 4); + vertex_data[0] = .{-10, 0, 10}; + vertex_data[1] = .{-10, 0, -10}; + vertex_data[2] = .{ 10, 0, -10}; + vertex_data[3] = .{ 10, 0, 10}; + + #persist index_data := u32.[ 0, 1, 2, 0, 2, 3 ]; + + mesh: Mesh; + mesh.verticies = vertex_data; + mesh.indicies = index_data; + + glGenVertexArrays(1, ^mesh.handle); + glBindVertexArray(mesh.handle); + + vbo: GLuint; + glGenBuffers(1, ^vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof Vertex * vertex_data.count, vertex_data.data, GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * sizeof GLfloat, ~~0); + + ibo: GLuint; + glGenBuffers(1, ^ibo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof i32 * 6, ~~index_data, GL_STATIC_DRAW); + + return mesh; +} + cube_mesh: Mesh; +plane_mesh: Mesh; matrix_block_buffer: GLuint; setup_opengl :: () { glInit(glfwGetLoadProcAddress()); glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glFrontFace(GL_CW); + glCullFace(GL_BACK); compile_shader :: (source: str, type: GLenum) -> GLint { shader := glCreateShader(type); @@ -157,7 +213,7 @@ setup_opengl :: () { matrix: [32] GLfloat; glGenBuffers(1, ^matrix_block_buffer); glBindBuffer(GL_UNIFORM_BUFFER, matrix_block_buffer); - glBufferData(GL_UNIFORM_BUFFER, sizeof f32 * (16 + 16), ^matrix, GL_DYNAMIC_DRAW); + glBufferData(GL_UNIFORM_BUFFER, sizeof f32 * (16 + 16 + 16), ^matrix, GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, -1); glBindBufferBase(GL_UNIFORM_BUFFER, 0, matrix_block_buffer); @@ -166,6 +222,7 @@ setup_opengl :: () { glUniformBlockBinding(prog, matrix_block_index, 0); cube_mesh = make_cube_mesh(); + plane_mesh = make_plane_mesh(); __initialize(^camera); camera.position = .{5,5,5}; @@ -197,27 +254,33 @@ update_world_matrix :: () { } update :: (dt: f32) { - #persist t := 0.0f; - t += dt; - #persist last_mouse_x: f64; #persist last_mouse_y: f64; mx, my: f64; glfwGetCursorPos(window, ^mx, ^my); + defer { + last_mouse_x = mx; + last_mouse_y = my; + } + + if !cursor_grabbed { + if glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_TRUE { + toggle_cursor_grabbed(); + } + return; + } + camera.y_rot += ~~(last_mouse_x - mx) / 400.0f; 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); - last_mouse_x = mx; - last_mouse_y = my; - facing := camera_get_forward(^camera); if glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS { - camera.position += Vector3.norm(facing) * 3 * dt; + camera.position += facing * 3 * dt; } if glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS { - camera.position -= Vector3.norm(facing) * 3 * dt; + camera.position -= facing * 3 * dt; } if glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS { camera.position += Vector3.norm(Vector3.cross(facing, .{0,1,0})) * 3 * dt; @@ -233,10 +296,8 @@ draw :: () { glClearColor(.2, .2, .2, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glBindVertexArray(cube_mesh.handle); - // glDrawArrays(GL_TRIANGLES, 0, 18); - glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, ~~0); - glBindVertexArray(-1); + mesh_draw(^plane_mesh); + mesh_draw(^cube_mesh); glfwSwapBuffers(window); } -- 2.25.1