rendering a colored cube
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 17 Dec 2021 02:17:27 +0000 (20:17 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 17 Dec 2021 02:17:27 +0000 (20:17 -0600)
run_tree/assets/shaders/basic_fragment.glsl
run_tree/assets/shaders/basic_vertex.glsl
run_tree/lib/onyx_glfw3.dll
run_tree/lib/onyx_opengles.dll
src/build.onyx
src/main.onyx

index f81d31230041fa66cd43e2748a2fd93dc5150396..ebfd107e0e79268d313ee541223209bcd75aaed8 100644 (file)
@@ -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);
 }
index e8d9f2fdf388591b620f57cd10991d036594ec6f..d68b0464064c36521dea2e6b1a88158fb301c799 100644 (file)
@@ -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;
 }
index 73650fa112adf7c2d067fca429c4b1d70c7ddf23..ad2c4d481aeed961ce33af69b1a336c4b56f4552 100644 (file)
Binary files a/run_tree/lib/onyx_glfw3.dll and b/run_tree/lib/onyx_glfw3.dll differ
index a168d513fa146cfdf0d7152a905687bf90756b56..ac4d04be55224b641458f8d4ecf1fa2d530ec47f 100644 (file)
Binary files a/run_tree/lib/onyx_opengles.dll and b/run_tree/lib/onyx_opengles.dll differ
index 4e10af622717316fabc70a606c06c41ebeeed47d..31d4a030b50b52ab385f591cacabdce1f65a46f3 100644 (file)
@@ -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
index 5b609e0d776683192cf2f25d02e1d30c67044649..c9bcf3fe7c7c5952fe2a932435d6f831e842144c 100644 (file)
@@ -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);