Skip to content

Instantly share code, notes, and snippets.

@terasakisatoshi
Forked from jkrumbiegel/dancing_cubes.jl
Created June 26, 2020 12:46
Show Gist options
  • Save terasakisatoshi/10a33cada3fee900d6fb277eabbebb3d to your computer and use it in GitHub Desktop.
Save terasakisatoshi/10a33cada3fee900d6fb277eabbebb3d to your computer and use it in GitHub Desktop.
Dancing cubes
using AbstractPlotting
using AbstractPlotting.MakieLayout
using GLMakie; GLMakie.activate!()
GLMakie.GLFW.WindowHint(GLMakie.GLFW.FLOATING, 1)
using LinearAlgebra
##
scene = Scene(center = false, raw = true, resolution = (800, 800))
azimuth = Node(0.0)
elevation = Node(0.0)
targetlimits = Node(FRect3D((-100, -100, -100), (100, 100, 100)))
limits = lift(identity, targetlimits)
view = lift(azimuth, elevation, limits) do a, e, lims
center = lims.origin .+ 0.5 .* widths(lims)
dist = maximum(widths(lims)) # should be enough to be outside?
eyepos = center .+ dist .* LinearAlgebra.normalize(Vec3f0(sin(e) * cos(a), sin(e) * sin(a), cos(e)))
eyeright = LinearAlgebra.cross(Vec(0.0, 0, 1), LinearAlgebra.normalize(eyepos .- center))
# TODO: fix eyeup so there is no flip at the elevation zero point
eyeup = LinearAlgebra.cross(eyeright, LinearAlgebra.normalize(eyepos .- center))
AbstractPlotting.lookat(eyepos, center, -eyeup)
end
projection = lift(view) do v
lims = limits[]
ox, oy, oz = lims.origin
wx, wy, wz = lims.widths
corners = [
ox oy oz 0
ox oy oz + wz 0
ox oy + wy oz 0
ox oy + wy oz + wz 0
ox + wx oy oz 0
ox + wx oy oz + wz 0
ox + wx oy + wy oz 0
ox + wx oy + wy oz + wz 0
]
viewed_corners = (v * corners')'
boxmin = minimum(viewed_corners, dims = 1)
boxmax = maximum(viewed_corners, dims = 1)
dist = boxmax .- boxmin
minis = boxmin .+ 0.5 .* dist
maxis = boxmax .- 0.5 .* dist
boxmin
boxmax
AbstractPlotting.orthographicprojection(
boxmin[1], boxmax[1],
boxmin[2], boxmax[2],
boxmin[3] - 10000, boxmax[3] + 10000) # * flipmatrix
end
projectionview = lift(projection, view) do p, v
p * v
end
disconnect!(scene.camera)
on(projectionview) do pv
camera(scene).view[] = view[]
camera(scene).projection[] = projection[]
camera(scene).projectionview[] = pv
end
#########################################################
cubes = map(1:10) do i
ori = Point3f0(0, 0, 11i)
ws = Point3f0(10, 10, 10)
cube = Node(FRect3D(ori, ws))
end
for cube in cubes
mesh!(cube, color = :tomato, shading = false)
wireframe!(cube, color = :black)
end
elevation[] = deg2rad(45)
azimuth[] = deg2rad(45)
record(scene, "cubes.gif", 0:1/30:5) do t
for i in 1:10
cubes[i][] = FRect3D((0, 3sin(i + 4t), 11i), (10, 10, 10))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment