Spherify objects using this new function, Spherify(dest_obj,src_obj,k#)
k# is the spherify amount:
Spherify(dest_obj,src_obj,0) will do no spherification.
Spherify(dest_obj,src_obj,1) will make each vertex equally distant from the center of the object (like a sphere)
Spherify(dest_obj,src_obj,0.5) will do a partial spherification.
Spherify(dest_obj,src_obj,-0.5) will unspherify the object, that is, the points closer to the center will get closer, the corners of the object will get spikier, the bumps will be exagerated, etc.
Hold shift to watch the transformation. Use up/down keys to change the k amount for the spherify function.
sync on
sync rate 0
cls
set display mode 1024,768,32
for x=1 to 100
for y=1 to 100
ink rgb(0,rnd(255),rnd(255)),0
dot x,y
next y
next x
get image 1,1,1,100,100
hide mouse
set camera range 0.01,100
autocam off
make matrix 1,100,100,40,40
position camera 50,2,50
color backdrop 0
timer0=timer()
global time#=0
ink rgb(0,255,0),0
dim oldkeystate(200)
dim keypress(200)
global dist#=5.0
wi=1
k#=0.2
do
print "[1] Box"
print "[2] Sphere"
print "[3] Cylinder"
print "Spacebar - subdivide the last object again"
print "Enter - toggle wireframe for the last object"
print "Shift - Spherify last object"
print "Spherify amount:",k#
set cursor 0,0
time#=(timer()-timer0)/1000.0
timer0=timer()
rotate camera camera angle x()+mousemovey(),camera angle y()+mousemovex(),0
move camera (keystate(17)-keystate(31))*5*time#
xr#=camera angle x()
xrotate camera 0
yrotate camera camera angle y()+90
move camera (keystate(32)-keystate(30))*5*time#
yrotate camera camera angle y()-90
xrotate camera xr#
if keypress(2)=1
o=freeobject()
make object box o,rnd(100)/100.0+1,rnd(100)/100.0+1,rnd(100)/100.0+1
o2=freeobject()
subdivide(o2,o)
delete object o
o=o2
rotate object o,rnd(20)-10,rnd(360),rnd(20)-10
move camera dist#:position object o,camera position x(),camera position y(),camera position z()
move camera -dist#
last_o=o
endif
if keypress(3)=1
o=freeobject()
make object sphere o,rnd(100)/100.0+1
o2=freeobject()
subdivide(o2,o)
delete object o
o=o2
rotate object o,rnd(20)-10,rnd(360),rnd(20)-10
move camera dist#:position object o,camera position x(),camera position y(),camera position z()
move camera -dist#
last_o=o
endif
if keypress(4)=1
o=freeobject()
make object cylinder o,rnd(100)/100.0+1
o2=freeobject()
subdivide(o2,o)
delete object o
o=o2
rotate object o,rnd(20)-10,rnd(360),rnd(20)-10
move camera dist#:position object o,camera position x(),camera position y(),camera position z()
move camera -dist#
last_o=o
endif
if spacekey()=1 and sp=0
if last_o>0
o2=freeobject()
xr#=object angle x(last_o)
yr#=object angle y(last_o)
zr#=object angle z(last_o)
rotate object last_o,0,0,0
subdivide(o2,last_o)
position object o2,object position x(last_o),object position y(last_o),object position z(last_o)
rotate object o2,xr#,yr#,zr#
delete object last_o
last_o=o2
endif
endif
if shiftkey()=1 and timer()>l_shift+100
l_shift=timer()
if last_o>0
o2=freeobject()
xr#=object angle x(last_o)
yr#=object angle y(last_o)
zr#=object angle z(last_o)
rotate object last_o,0,0,0
spherify(o2,last_o,k#)
position object o2,object position x(last_o),object position y(last_o),object position z(last_o)
rotate object o2,xr#,yr#,zr#
delete object last_o
last_o=o2
endif
endif
sp=spacekey()
if last_o>0
if wi=1
set object wireframe last_o,1
set object cull last_o,0
else
set object wireframe last_o,0
set object cull last_o,1
texture object last_o,1
endif
endif
if returnkey()=1 and rk=0
wi=1-wi
endif
k#=k#+(upkey()-downkey())*0.001
if k#>2 then k#=2
if k#<-2 then k#=-2
rk=returnkey()
for n=1 to 200
keypress(n)=keystate(n)-oldkeystate(n)
oldkeystate(n)=keystate(n)
next n
sync
loop
function freeobject()
o=1
while object exist(o)
inc o
endwhile
endfunction o
function subdivide(dest_obj,src_obj)
make mesh from object 1,src_obj
make memblock from mesh 1,1
delete mesh 1
fvf_format=memblock dword(1,0)
fvf_size=memblock dword(1,4)
vertex_amount=memblock dword(1,8)
triangle_amount=vertex_amount/3
make memblock 2,12+(triangle_amount*3)*4*fvf_size
write memblock dword 2,0,fvf_format
write memblock dword 2,4,fvf_size
write memblock dword 2,8,vertex_amount*4
t2=-1
for t=0 to triangle_amount-1
p1x#=memblock float(1,12+fvf_size*(t*3))
p1y#=memblock float(1,12+fvf_size*(t*3)+4)
p1z#=memblock float(1,12+fvf_size*(t*3)+8)
p2x#=memblock float(1,12+fvf_size*(t*3+1))
p2y#=memblock float(1,12+fvf_size*(t*3+1)+4)
p2z#=memblock float(1,12+fvf_size*(t*3+1)+8)
p3x#=memblock float(1,12+fvf_size*(t*3+2))
p3y#=memblock float(1,12+fvf_size*(t*3+2)+4)
p3z#=memblock float(1,12+fvf_size*(t*3+2)+8)
u1#=memblock float(1,12+fvf_size*(t*3)+24)
v1#=memblock float(1,12+fvf_size*(t*3)+28)
u2#=memblock float(1,12+fvf_size*(t*3+1)+24)
v2#=memblock float(1,12+fvf_size*(t*3+1)+28)
u3#=memblock float(1,12+fvf_size*(t*3+2)+24)
v3#=memblock float(1,12+fvf_size*(t*3+2)+28)
inc t2
write memblock float 2,12+fvf_size*(t2*3),p1x#
write memblock float 2,12+fvf_size*(t2*3)+4,p1y#
write memblock float 2,12+fvf_size*(t2*3)+8,p1z#
write memblock float 2,12+fvf_size*(t2*3)+24,u1#
write memblock float 2,12+fvf_size*(t2*3)+28,v1#
write memblock float 2,12+fvf_size*(t2*3+1),(p1x#+p2x#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+4,(p1y#+p2y#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+8,(p1z#+p2z#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+24,(u1#+u2#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+28,(v1#+v2#)/2
write memblock float 2,12+fvf_size*(t2*3+2),(p1x#+p3x#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+4,(p1y#+p3y#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+8,(p1z#+p3z#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+24,(u1#+u3#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+28,(v1#+v3#)/2
inc t2
write memblock float 2,12+fvf_size*(t2*3),(p1x#+p2x#)/2
write memblock float 2,12+fvf_size*(t2*3)+4,(p1y#+p2y#)/2
write memblock float 2,12+fvf_size*(t2*3)+8,(p1z#+p2z#)/2
write memblock float 2,12+fvf_size*(t2*3)+24,(u1#+u2#)/2
write memblock float 2,12+fvf_size*(t2*3)+28,(v1#+v2#)/2
write memblock float 2,12+fvf_size*(t2*3+1),p2x#
write memblock float 2,12+fvf_size*(t2*3+1)+4,p2y#
write memblock float 2,12+fvf_size*(t2*3+1)+8,p2z#
write memblock float 2,12+fvf_size*(t2*3+1)+24,u2#
write memblock float 2,12+fvf_size*(t2*3+1)+28,v2#
write memblock float 2,12+fvf_size*(t2*3+2),(p2x#+p3x#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+4,(p2y#+p3y#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+8,(p2z#+p3z#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+24,(u2#+u3#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+28,(v2#+v3#)/2
inc t2
write memblock float 2,12+fvf_size*(t2*3),(p1x#+p3x#)/2
write memblock float 2,12+fvf_size*(t2*3)+4,(p1y#+p3y#)/2
write memblock float 2,12+fvf_size*(t2*3)+8,(p1z#+p3z#)/2
write memblock float 2,12+fvf_size*(t2*3)+24,(u1#+u3#)/2
write memblock float 2,12+fvf_size*(t2*3)+28,(v1#+v3#)/2
write memblock float 2,12+fvf_size*(t2*3+1),(p2x#+p3x#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+4,(p2y#+p3y#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+8,(p2z#+p3z#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+24,(u2#+u3#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+28,(v2#+v3#)/2
write memblock float 2,12+fvf_size*(t2*3+2),p3x#
write memblock float 2,12+fvf_size*(t2*3+2)+4,p3y#
write memblock float 2,12+fvf_size*(t2*3+2)+8,p3z#
write memblock float 2,12+fvf_size*(t2*3+2)+24,u3#
write memblock float 2,12+fvf_size*(t2*3+2)+28,v3#
inc t2
write memblock float 2,12+fvf_size*(t2*3),(p1x#+p3x#)/2
write memblock float 2,12+fvf_size*(t2*3)+4,(p1y#+p3y#)/2
write memblock float 2,12+fvf_size*(t2*3)+8,(p1z#+p3z#)/2
write memblock float 2,12+fvf_size*(t2*3)+24,(u1#+u3#)/2
write memblock float 2,12+fvf_size*(t2*3)+28,(v1#+v3#)/2
write memblock float 2,12+fvf_size*(t2*3+1),(p1x#+p2x#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+4,(p1y#+p2y#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+8,(p1z#+p2z#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+24,(u1#+u2#)/2
write memblock float 2,12+fvf_size*(t2*3+1)+28,(v1#+v2#)/2
write memblock float 2,12+fvf_size*(t2*3+2),(p2x#+p3x#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+4,(p2y#+p3y#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+8,(p2z#+p3z#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+24,(u2#+u3#)/2
write memblock float 2,12+fvf_size*(t2*3+2)+28,(v2#+v3#)/2
next t
rem create the mesh
make mesh from memblock 1,2
delete memblock 1
delete memblock 2
rem create the object
make object dest_obj,1,0
delete mesh 1
set object normals dest_obj
endfunction
function spherify(dest_obj,src_obj,k#)
make mesh from object 1,src_obj
make memblock from mesh 1,1
delete mesh 1
fvf_format=memblock dword(1,0)
fvf_size=memblock dword(1,4)
vertex_amount=memblock dword(1,8)
make memblock 2,12+vertex_amount*fvf_size
write memblock dword 2,0,fvf_format
write memblock dword 2,4,fvf_size
write memblock dword 2,8,vertex_amount
total#=0
for v=0 to vertex_amount-1
px#=memblock float(1,12+fvf_size*v)
py#=memblock float(1,12+fvf_size*v+4)
pz#=memblock float(1,12+fvf_size*v+8)
total#=total#+sqrt(px#^2+py#^2+pz#^2)
next v
avg#=total#/vertex_amount
for v=0 to vertex_amount-1
px#=memblock float(1,12+fvf_size*v)
py#=memblock float(1,12+fvf_size*v+4)
pz#=memblock float(1,12+fvf_size*v+8)
d#=sqrt(px#^2+py#^2+pz#^2)
nx#=px#/d#
ny#=py#/d#
nz#=pz#/d#
d#=d#+k#*(avg#-d#)
x#=nx#*d#
y#=ny#*d#
z#=nz#*d#
write memblock float 2,12+fvf_size*v,x#
write memblock float 2,12+fvf_size*v+4,y#
write memblock float 2,12+fvf_size*v+8,z#
write memblock float 2,12+fvf_size*v+24,memblock float(1,12+fvf_size*v+24)
write memblock float 2,12+fvf_size*v+28,memblock float(1,12+fvf_size*v+28)
next v
rem create the mesh
make mesh from memblock 1,2
delete memblock 1
delete memblock 2
rem create the object
make object dest_obj,1,0
delete mesh 1
set object normals dest_obj
endfunction