Skip to content

Commit

Permalink
Make a guy surrenderring
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Apr 17, 2024
1 parent 33e4f6e commit cfd9b01
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
32 changes: 25 additions & 7 deletions example/shapes.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions example/spotlight.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
26 changes: 26 additions & 0 deletions example/utils.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit cfd9b01

Please sign in to comment.