OK, cool.
And I find no frustrum clipping sort of annoying, don't know about others though.
And I find no frustrum clipping sort of annoying, don't know about others though.
-- Rotate 3d object in stereo
-- (C) nsg, 2013
function mat_mul(m1,m2)
-- resulting matrix has as many rows as m1 and as many columns as m2
local res={}
for r=1,#m1 do
local row={}
assert(#m1[r]==#m2)
-- print("mul r=",r)
for c=1,#m2[1] do
local s=0
for k=1,#m2 do
s=s+m1[r][k]*m2[k][c]
end
table.insert(row,s)
end
table.insert(res,row)
end
return res
end
function mat_transpose(mat)
local res={}
for i=1,#mat[1] do
table.insert(res,{})
end
for r=1,#mat do
for c=1,#mat[r] do
table.insert(res[c],mat[r][c])
end
end
return res
end
function mat_make_rotation(axis,angle)
local mat={}
for r=1,3 do
table.insert(mat,{})
for c=1,3 do
local v=0
if r==axis and c== axis then v=1
else v=0
end
table.insert(mat[r],v)
end
end
local c=math.cos(angle)
local s=math.sin(angle)
mat[(axis+0)%3+1][(axis+0)%3+1]=c
mat[(axis+0)%3+1][(axis+1)%3+1]=s
mat[(axis+1)%3+1][(axis+0)%3+1]=-s
mat[(axis+1)%3+1][(axis+1)%3+1]=c
return mat
end
function make_cube(side)
if nil==side then
side=1
end
local function coord(i,mask)
return (0==bit32.band(i,mask) and side or -side)
end
local vertex={}
local edge={}
for i=0,7 do
table.insert(vertex,{coord(i,1),coord(i,2),coord(i,4)})
local bit=1
for b=1,3 do
local next=bit32.bxor(i,bit)
if next<i then table.insert(edge,{i+1,next+1}) end
bit=bit*2
end
end
return vertex,edge
end
function mat_dump(m,name)
if nil==name then name="?" end
print(string.format("matrix %s with %d rows",name,#m))
for r=1,#m do
io.write(string.format("%3d:",r))
for c=1,#m[r] do
io.write(string.format(" %3d",m[r][c]))
end
io.write("\n")
end
end
function dumpargs(...)
print(...)
end
function nothing() end
if nil==zmg then
zmg={
makeColor=function() return 999 end,
drawLine=dumpargs,
keyMenu=function()
io.write("key=");
local k=io.read();
return tonumber(k)
end,
drawRectFill=dumpargs,
fastCopy=dumpargs,
}
else
mat_dump=nothing
end
m,e=make_cube(10)
mat_dump(m,"vertexes")
mat_dump(e,"edges")
SCREENX,SCREENY=384,216
BLACK=zmg.makeColor(0,0,0)
xrot=5.585
yrot=0.436
SCL=265
AWAY=57
OX=SCREENX/2
OY=SCREENY/2
function screenfrom3d(v)
local z=SCL/(v[3]+AWAY)
return OX+v[1]*z, OY+v[2]*z
end
function V_drawbody(v,e)
for i=1,#e do
local x1,y1=screenfrom3d(v[e[i][1]])
local x2,y2=screenfrom3d(v[e[i][2]])
zmg.drawLine(x1,y1,x2,y2,clr)
end
end
function V_display()
local rot=mat_transpose(mat_mul(mat_make_rotation(1,xrot),mat_make_rotation(2,yrot)))
mat_dump(rot,"rot")
mat_dump(m,"m")
local v=mat_mul(m,rot)
mat_dump(v,"translated vertexes")
clr=zmg.makeColor(255,255,255)
zmg.drawRectFill(0,0,SCREENX,SCREENY,BLACK)
local vt=mat_mul(v,EYE1)
OX=(flip and 3 or 1)*SCREENX/4
V_drawbody(vt,e)
vt=mat_mul(v,EYE2)
OX=(flip and 1 or 3)*SCREENX/4
V_drawbody(vt,e)
zmg.fastCopy()
end
PI2=math.pi*2
dx=5*PI2/360
dy=dx
flip=false
function makeeyes(eyebase)
EYEBASE=eyebase
EYE1=mat_make_rotation(2,eyebase)
EYE2=mat_make_rotation(2,-eyebase)
end
makeeyes(4*PI2/360)
K_EXIT= 47
K_LEFT= 38
K_DOWN= 37
K_UP= 28
K_RIGHT= 27
K_SIN= 46
K_COS= 36
K_DEL= 44 -- [DEL]
K_MUL= 43 -- [*]
K_PLUS= 42 -- [+]
K_NEG= 41 -- [(-)]
K_DIV= 33 -- [/]
K_MINUS= 32 -- [-]
K_EXE= 31 -- [EXE]
function playstereo()
quit=false
repeat
V_display()
local k=zmg.keyMenu()
if k==K_EXIT then quit=true
elseif k==K_LEFT then yrot=yrot+dy
elseif k==K_RIGHT then yrot=yrot-dy
elseif k==K_UP then xrot=xrot-dx
elseif k==K_DOWN then xrot=xrot+dx
elseif k==K_PLUS then AWAY=AWAY-1
elseif k==K_MINUS then AWAY=AWAY+1
elseif k==K_MUL then SCL=SCL*1.1
elseif k==K_DIV then SCL=SCL/1.1
elseif k==K_SIN then makeeyes(EYEBASE-0.001)
elseif k==K_COS then makeeyes(EYEBASE+0.001)
elseif k==K_EXE then flip=not flip
end
xrot=xrot % PI2
yrot=yrot % PI2
until quit
print("yrot=",yrot)
print("xrot=",xrot)
print("AWAY=",AWAY)
print("SCL=",SCL)
print("EYEBASE=",EYEBASE)
print("flip=",flip)
end
A=playstereo -- to easy typing in prizm
playstereo()
Advertisement