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) {
#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);
+ }
}
}
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;
}
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,
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);
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);
glUniformBlockBinding(prog, matrix_block_index, 0);
cube_mesh = make_cube_mesh();
+ plane_mesh = make_plane_mesh();
__initialize(^camera);
camera.position = .{5,5,5};
}
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;
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);
}