From: Brendan Hansen Date: Fri, 17 Dec 2021 02:17:27 +0000 (-0600) Subject: rendering a colored cube X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=614ce1f77ed7784f7625453c5a8712e0dae21e42;p=voxel-shooter.git rendering a colored cube --- diff --git a/run_tree/assets/shaders/basic_fragment.glsl b/run_tree/assets/shaders/basic_fragment.glsl index f81d312..ebfd107 100644 --- a/run_tree/assets/shaders/basic_fragment.glsl +++ b/run_tree/assets/shaders/basic_fragment.glsl @@ -4,9 +4,10 @@ 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(vec4(1, 1, 1, 1), texture(u_sampler, v_tex), u_sampler_enabled); + fragColor = mix(v_col, 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 e8d9f2f..d68b046 100644 --- a/run_tree/assets/shaders/basic_vertex.glsl +++ b/run_tree/assets/shaders/basic_vertex.glsl @@ -7,9 +7,11 @@ layout(std140) uniform u_matrix_block { mat4 u_world; }; +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/run_tree/lib/onyx_glfw3.dll b/run_tree/lib/onyx_glfw3.dll index 73650fa..ad2c4d4 100644 Binary files a/run_tree/lib/onyx_glfw3.dll and b/run_tree/lib/onyx_glfw3.dll differ diff --git a/run_tree/lib/onyx_opengles.dll b/run_tree/lib/onyx_opengles.dll index a168d51..ac4d04b 100644 Binary files a/run_tree/lib/onyx_opengles.dll and b/run_tree/lib/onyx_opengles.dll differ diff --git a/src/build.onyx b/src/build.onyx index 4e10af6..31d4a03 100644 --- a/src/build.onyx +++ b/src/build.onyx @@ -1,14 +1,18 @@ - -#load "config" -#load "main" - #load "core/std" #local runtime :: package runtime #load_path runtime.vars.ONYX_MODULE_DIR - +#load_path "./../src" +#load_path "./../lib" #library_path "./lib" + +// Primary source files +#load "config" +#load "main" + +// Onyx library code +#load "stb_truetype" + +// Onyx modules #load "glfw3/module" #load "opengles/module" - -#load "./../lib/stb_truetype" \ No newline at end of file diff --git a/src/main.onyx b/src/main.onyx index 5b609e0..c9bcf3f 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -12,7 +12,9 @@ create_window :: () => { window = glfwCreateWindow(800, 600, #cstr "Voxel Shooter"); glfwMakeContextCurrent(window); + glfwSwapInterval(1); glfwSetWindowSizeCallback(window, "on_resize"); + glfwSetKeyCallback(window, "on_key"); } #export "on_resize" (window: GLFWwindow_p, width, height: u32) { @@ -20,45 +22,86 @@ create_window :: () => { update_view_matrix(); } +#export "on_key" (window: GLFWwindow_p, key, scancode, action, mod: u32) { + if key == GLFW_KEY_ESCAPE && action == GLFW_PRESS { + glfwSetWindowShouldClose(window, true); + } +} + Mesh :: struct { handle: GLuint; - verticies: [] f32; - indicies: [] u16; + verticies: [] Vertex; + indicies: [] u32; } -cube_verticies :: (x, y, z: f32) -> [] GLfloat { - vertex_data := GLfloat.[ - x-0.5, y-0.5, z-0.5, - x+0.5, y-0.5, z-0.5, - x+0.5, y+0.5, z-0.5, - x-0.5, y-0.5, z-0.5, - x+0.5, y+0.5, z-0.5, - x-0.5, y+0.5, z-0.5, - - x-0.5, y-0.5, z-0.5, - x-0.5, y+0.5, z-0.5, - x-0.5, y+0.5, z+0.5, - x-0.5, y-0.5, z-0.5, - x-0.5, y+0.5, z+0.5, - x-0.5, y-0.5, z+0.5, - - x-0.5, y-0.5, z-0.5, - x-0.5, y+0.5, z+0.5, - x+0.5, y+0.5, z+0.5, - x-0.5, y-0.5, z-0.5, - x+0.5, y+0.5, z+0.5, - x+0.5, y-0.5, z-0.5, +Vertex :: struct { + x, y, z: f32; +} + +make_cube_mesh :: () -> Mesh { + vertex_data := memory.make_slice(Vertex, 8); + vertex_data[0] = .{0,0,0}; + vertex_data[1] = .{0,1,0}; + vertex_data[2] = .{1,1,0}; + vertex_data[3] = .{1,0,0}; + vertex_data[4] = .{0,0,1}; + vertex_data[5] = .{0,1,1}; + vertex_data[6] = .{1,1,1}; + vertex_data[7] = .{1,0,1}; + + #persist index_data := u32.[ + 0, 1, 2, // back + 0, 2, 3, + + 3, 6, 2, // right side + 3, 7, 6, + + 5, 0, 1, // left side + 5, 4, 0, + + 4, 5, 6, // front + 4, 6, 7, + + 5, 1, 2, // top + 5, 2, 6, + + 0, 4, 7, // bottom + 0, 7, 3, ]; - return memory.copy_slice(vertex_data); + 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 * 36, ~~index_data, GL_STATIC_DRAW); + + glBindVertexArray(-1); + + return mesh; } -vao: GLuint; +cube_mesh: Mesh; matrix_block_buffer: GLuint; setup_opengl :: () { glInit(glfwGetLoadProcAddress()); + glEnable(GL_DEPTH_TEST); + compile_shader :: (source: str, type: GLenum) -> GLint { shader := glCreateShader(type); source_data := source.data; @@ -113,20 +156,8 @@ setup_opengl :: () { matrix_block_index := glGetUniformBlockIndex(prog, #cstr "u_matrix_block"); glUniformBlockBinding(prog, matrix_block_index, 0); - glGenVertexArrays(1, ^vao); - glBindVertexArray(vao); + cube_mesh = make_cube_mesh(); - vbo: GLuint; - glGenBuffers(1, ^vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - - vertex_data := cube_verticies(0, 0, -4); - glBufferData(GL_ARRAY_BUFFER, sizeof GLfloat * vertex_data.count, vertex_data.data, GL_STATIC_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * sizeof GLfloat, ~~0); - - glBindVertexArray(-1); update_view_matrix(); update_world_matrix(); } @@ -162,10 +193,10 @@ update_world_matrix :: () { c := math.cos(t); s := math.sin(t); matrix := GLfloat.[ - c, 0, s, 0, - 0, 1, 0, 0, - -s, 0, c, 0, - 0, 0, 0, 1, + 1, 0, 0, 0, + 0, c, s, 0, + 0, -s, c, 0, + 0, 0, -3, 1, ]; glBindBuffer(GL_UNIFORM_BUFFER, matrix_block_buffer); @@ -179,10 +210,11 @@ update :: (dt: f32) { draw :: () { glClearColor(.2, .2, .2, 1); - glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glBindVertexArray(vao); - glDrawArrays(GL_TRIANGLES, 0, 18); + glBindVertexArray(cube_mesh.handle); + // glDrawArrays(GL_TRIANGLES, 0, 18); + glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, ~~0); glBindVertexArray(-1); glfwSwapBuffers(window);