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) {
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;
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();
}
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);
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);