diff --git a/example/shapes.odin b/example/shapes.odin index 128106f..596a58d 100644 --- a/example/shapes.odin +++ b/example/shapes.odin @@ -237,16 +237,34 @@ JOINT_VERTICES :: 3 * JOINT_TRIANGLES get_joint :: proc(from, to: Vec, w: f32) -> [JOINT_VERTICES]Vec { - mid: Vec = from*(1.0/3.0) + to*(2.0/3.0) + mid := from*(1.0/3.0) + to*(2.0/3.0) from, to := from, to - if from.y < to.y || from.x > to.x || from.z < to.z { - from, to = to, from - } - normal: Vec = normalize(to - from) - normal_x := vec3_transform(normal, mat4_rotate_x(PI/2) * mat4_rotate_y(PI/2)) - normal_z := vec3_transform(normal, mat4_rotate_z(PI/2) * mat4_rotate_y(PI/2)) + normal := normalize(to - from) + + mat_x: Mat4 + mat_z: Mat4 + + if normal.x > 0.5 || normal.x < -0.5 { + mat_x = mat4_rotate_y(PI/2) + mat_z = mat4_rotate_z(PI/2) + } else { + mat_x = mat4_rotate_x(PI/2) + if normal.z > 0.5 || normal.z < -0.5 { + mat_z = mat4_rotate_y(PI/2) + } else { + mat_z = mat4_rotate_z(PI/2) + if normal.y > 0.5 || normal.y < -0.5 { + mat_z = mat4_rotate_z(PI/2) + } else { + mat_z = mat4_rotate_x(PI/2) + } + } + } + + normal_x := vec3_transform(normal, mat_x) + normal_z := vec3_transform(normal, mat_z) move_x: Vec = normal_x * w move_z: Vec = normal_z * w diff --git a/example/spotlight.odin b/example/spotlight.odin index 092be6e..60df8c2 100644 --- a/example/spotlight.odin +++ b/example/spotlight.odin @@ -13,13 +13,16 @@ GUY_WIDTH :: 70 PLANE_WIDTH :: 2000 GUY_JOINT_POSITIONS :: [?]struct {from: Vec, to: Vec} { - {{0, 0, 0}, {0, 100, 0}}, - {{0, 100, 0}, {0, 200, 0}}, - {{0, 100, 0}, {100, 100, 0}}, - {{0, 100, 0}, {100, 100, 0}}, - {{0, 100, 0}, {-100, 100, 0}}, - {{0, 100, 0}, {0, 100, 100}}, - {{0, 100, 0}, {0, 100, -100}}, + {{ 0, 40, 0}, { 0, 120, 20}}, + {{ 0, 120, 20}, { 0, 130, 30}}, + {{ 0, 40, 0}, { 30, 5, 40}}, + {{ 0, 40, 0}, {-30, 5, 40}}, + {{ 30, 5, 40}, { 20, 0, -40}}, + {{-30, 5, 40}, {-20, 0, -40}}, + {{ 0, 120, 20}, { 35, 140, 25}}, + {{ 0, 120, 20}, {-35, 140, 25}}, + {{ 35, 140, 25}, { 30, 190, 25}}, + {{-35, 140, 25}, {-30, 190, 25}}, } GUY_JOINTS :: len(GUY_JOINT_POSITIONS) @@ -142,7 +145,7 @@ spotlight_frame :: proc(delta: f32) { camera_mat: Mat4 = 1 camera_mat *= mat4_rotate_y(camera_angle) camera_mat *= mat4_translate(camera_pos) - camera_mat *= mat4_look_at(camera_pos, {0, GUY_HEIGHT, 0}, {0, 1, 0}) + camera_mat *= mat4_look_at(camera_pos, {0, 50, 0}, {0, 1, 0}) camera_mat = glm.inverse_mat4(camera_mat) view_mat := glm.mat4PerspectiveInfinite( diff --git a/example/utils.odin b/example/utils.odin index 84da700..e90bcfa 100644 --- a/example/utils.odin +++ b/example/utils.odin @@ -157,6 +157,32 @@ mat4_look_at :: proc "contextless" (eye, target, up: Vec3) -> Mat4 { } } +// Rotates a vector around an axis +@(require_results) +vec3_rotate_by_axis_angle :: proc "contextless" (v, axis: Vec, angle: f32) -> Vec { + axis, angle := axis, angle + + axis = normalize(axis) + + angle *= 0.5 + a := sin(angle) + b := axis.x*a + c := axis.y*a + d := axis.z*a + a = cos(angle) + w := Vec{b, c, d} + + wv := cross(w, v) + wwv := cross(w, wv) + + a *= 2 + wv *= a + + wwv *= 2 + + return v + wv + wwv +} + vec3_transform :: proc "contextless" (v: Vec, m: Mat4) -> Vec { w := m[0][3] * v.x + m[1][3] * v.y + m[2][3] * v.z + m[3][3] // assume v[3] is 1