Skip to content

Instantly share code, notes, and snippets.

@jkrumbiegel
Created June 26, 2020 09:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jkrumbiegel/1a8b4aeaf50518fb1db07d37bdcccbb0 to your computer and use it in GitHub Desktop.
Save jkrumbiegel/1a8b4aeaf50518fb1db07d37bdcccbb0 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