Here’s a prototype for you. I’m not convinced that I have the touch handing quite right, but it moves it roughly in the right direction. Of course, to do it properly one would put everything in a viewport
class.
function setup()
m = mesh()
ver = {
vec3(1,0,0),
vec3(0,1,0),
vec3(0,0,1),
vec3(-1,0,0),
vec3(0,1,0),
vec3(0,0,1),
vec3(1,0,0),
vec3(0,-1,0),
vec3(0,0,1),
vec3(-1,0,0),
vec3(0,-1,0),
vec3(0,0,1),
vec3(1,0,0),
vec3(0,1,0),
vec3(0,0,-1),
vec3(-1,0,0),
vec3(0,1,0),
vec3(0,0,-1),
vec3(1,0,0),
vec3(0,-1,0),
vec3(0,0,-1),
vec3(-1,0,0),
vec3(0,-1,0),
vec3(0,0,-1)
}
c = {}
vv = {}
for k,v in ipairs(ver) do
table.insert(c,color(128+v.x*127,128+v.y*127,128+v.z*127,255))
table.insert(vv,v)
end
m.vertices = vv
m.colors = c
parameter("azimuth",0,2*math.pi,math.pi)
parameter("zenith",0,math.pi,0)
parameter("size",.1,5,1)
dx,dy,dz = 0,0,0
end
function draw()
background(40, 40, 50)
clip(0,HEIGHT/2,WIDTH,HEIGHT)
camera(0,0,10,0,0,0,0,1,0)
drawObj(vec3(0,2,0))
clip(0,0,WIDTH,HEIGHT/2)
camera(0,10,10,0,0,0,1,0,0)
drawObj(vec3(-2,0,0))
end
function drawObj(o)
perspective(45,WIDTH/HEIGHT)
local ca = math.cos(azimuth)
local sa = math.sin(azimuth)
local cz = math.cos(zenith)
local sz = math.sin(zenith)
local mt =
matrix(
size,0,0,0,
0,size,0,0,
0,0,size,0,
0,0,0,1
)*
matrix(
ca,-sa,0,0,
sa,ca,0,0,
0,0,1,0,
0,0,0,1
) * matrix(
1,0,0,0,
0,cz,-sz,0,
0,sz,cz,0,
0,0,0,1
) *
GravityToMatrix() *
matrix(
1,0,0,0,
0,1,0,0,
0,0,1,0,
o.x+dx,o.y+dy,o.z+dz,1
)
local j,k,l
for i = 0,7 do
j = 2*(i%2)-1
k = 2*(math.floor(i/2)%2)-1
l = 2*(math.floor(i/4)%2)-1
modelMatrix(
matrix(
1,0,0,0,
0,1,0,0,
0,0,1,0,
j,k,l,1
) *
mt
)
m:draw()
end
end
function touched(touch)
if touch.y > HEIGHT/2 then
dx = dx + 2*touch.deltaX/WIDTH
dy = dy + 2*touch.deltaY/HEIGHT
else
dx = dx + 2*touch.deltaY/WIDTH
dy = dy - 2*touch.deltaX/HEIGHT
end
end
function GravityToMatrix()
local gxy, gy, gygxy, a, b, c, d
if Gravity.x == 0 and Gravity.y == 0 then
a,b,c,d = 1,0,0,0
else
gy = - Gravity.y
gxy = math.sqrt(math.pow(Gravity.x,2) + math.pow(Gravity.y,2))
gygxy = gy/gxy
a = math.sqrt(1 + gxy - gygxy - gy)/2
b = math.sqrt(1 - gxy - gygxy + gy)/2
c = math.sqrt(1 - gxy + gygxy - gy)/2
d = math.sqrt(1 + gxy + gygxy + gy)/2
if Gravity.y > 0 then
a = a
b = b
end
if Gravity.z < 0 then
b = - b
c = - c
end
if Gravity.x > 0 then
c = - c
d = - d
end
end
a,b,c,d = a,d,-c,-b
local aa = 2*a*a
local ab = 2*a*b
local ac = 2*a*c
local ad = 2*a*d
local bb = 2*b*b
local bc = 2*b*c
local bd = 2*b*d
local cc = 2*c*c
local cd = 2*c*d
return matrix(
1 - bb - cc,
ab - cd,
ac + bd,
0,
ab + cd,
1 - aa - cc,
bc - ad,
0,
ac - bd,
bc + ad,
1 - aa - bb,
0,
0,0,0,1
)
end