I have found a Vectorball routine and i have it rewritten for BlitzMax.
But something is wrong!
The sort routine does not work correctly!
Need help!
Here is the Code
Strict
Graphics 800,600,32,hardsync
HideMouse
Incbin "media/bubblew2.png"
Global bob = LoadImage("incbin::media/bubblew2.png")
Global maxpoints
Global screenxcenter:Float
Global screenycenter:Float
Global distance:Float
maxpoints=125
screenxcenter=400
screenycenter=300
distance=30
Global points [maxpoints , 3]
Global tpoints [maxpoints , 3]
Global opoints [maxpoints]
Global i
Global j
Global z
Global y
Global x
Global ang1:Float
Global ang2:Float
Global ang3:Float
Global sx:Float
Global cx:Float
Global sy:Float
Global cy:Float
Global sz:Float
Global cz:Float
Global xy:Float
Global xz:Float
Global yz:Float
Global yx:Float
Global zx:Float
Global zy:Float
Global temp
i=0
For z = -2 To 2
For y = -2 To 2
For x = -2 To 2
points [i,0] = x
points [i,1] = y
points [i,2] = z
opoints [i] =i
i:+1
Print i
Next
Next
Next
HideMouse
maxpoints=i
While Not KeyHit(KEY_ESCAPE)
Cls
rotate3dpoints (ang1,ang2,ang3)
draw3dbobs()
sort3dbobs()
ang1=ang1+1
ang2=ang2+.21
ang3=ang3+.3
distance=30+Sin(ang1)*22
Flip
End While
ShowMouse
End
Function draw3dbobs()
SetColor 255,0,0
For i=0 To maxpoints-1
DrawImage bob,tpoints[i,0],tpoints[opoints[i],1]
'Plot tpoints[opoints[i],0],tpoints[opoints[i],1]
Next
End Function
Function sort3dbobs()
For i=0 To maxpoints
For j=0 To maxpoints-2
If tpoints[opoints[j],1] < tpoints[opoints[j+1],2] Then
temp=opoints[j+1]
opoints[j+1]=opoints[j]
opoints[j]=temp
EndIf
Next
Next
End Function
Function rotate3dpoints(xrot,yrot,zrot)
sx = Sin(xrot)
cx = Cos(xrot)
sy = Sin(yrot)
cy = Cos(yrot)
sz = Sin(zrot)
cz = Cos(zrot)
For i=0 To maxpoints-1
x=points[i,0]
y=points[i,1]
z=points[i,2]
'rotation x
xy=cx*y-sx*z
xz=sx*y+cx*z
'rotation y
yz=cy*xz-sy*x
yx=sy*xz+cy*x
'rotation z
zx=cz*yx-sz*xy
zy=sz*yx+cz*xy
'perspective correction
tpoints [i,0]=zx/(distance+yz)*512+screenxcenter
tpoints [i,1]=zy/(distance+yz)*512+screenycenter
tpoints [i,2]=yz
Next
End Function
But something is wrong!
The sort routine does not work correctly!
Need help!
Here is the Code
Strict
Graphics 800,600,32,hardsync
HideMouse
Incbin "media/bubblew2.png"
Global bob = LoadImage("incbin::media/bubblew2.png")
Global maxpoints
Global screenxcenter:Float
Global screenycenter:Float
Global distance:Float
maxpoints=125
screenxcenter=400
screenycenter=300
distance=30
Global points [maxpoints , 3]
Global tpoints [maxpoints , 3]
Global opoints [maxpoints]
Global i
Global j
Global z
Global y
Global x
Global ang1:Float
Global ang2:Float
Global ang3:Float
Global sx:Float
Global cx:Float
Global sy:Float
Global cy:Float
Global sz:Float
Global cz:Float
Global xy:Float
Global xz:Float
Global yz:Float
Global yx:Float
Global zx:Float
Global zy:Float
Global temp
i=0
For z = -2 To 2
For y = -2 To 2
For x = -2 To 2
points [i,0] = x
points [i,1] = y
points [i,2] = z
opoints [i] =i
i:+1
Print i
Next
Next
Next
HideMouse
maxpoints=i
While Not KeyHit(KEY_ESCAPE)
Cls
rotate3dpoints (ang1,ang2,ang3)
draw3dbobs()
sort3dbobs()
ang1=ang1+1
ang2=ang2+.21
ang3=ang3+.3
distance=30+Sin(ang1)*22
Flip
End While
ShowMouse
End
Function draw3dbobs()
SetColor 255,0,0
For i=0 To maxpoints-1
DrawImage bob,tpoints[i,0],tpoints[opoints[i],1]
'Plot tpoints[opoints[i],0],tpoints[opoints[i],1]
Next
End Function
Function sort3dbobs()
For i=0 To maxpoints
For j=0 To maxpoints-2
If tpoints[opoints[j],1] < tpoints[opoints[j+1],2] Then
temp=opoints[j+1]
opoints[j+1]=opoints[j]
opoints[j]=temp
EndIf
Next
Next
End Function
Function rotate3dpoints(xrot,yrot,zrot)
sx = Sin(xrot)
cx = Cos(xrot)
sy = Sin(yrot)
cy = Cos(yrot)
sz = Sin(zrot)
cz = Cos(zrot)
For i=0 To maxpoints-1
x=points[i,0]
y=points[i,1]
z=points[i,2]
'rotation x
xy=cx*y-sx*z
xz=sx*y+cx*z
'rotation y
yz=cy*xz-sy*x
yx=sy*xz+cy*x
'rotation z
zx=cz*yx-sz*xy
zy=sz*yx+cz*xy
'perspective correction
tpoints [i,0]=zx/(distance+yz)*512+screenxcenter
tpoints [i,1]=zy/(distance+yz)*512+screenycenter
tpoints [i,2]=yz
Next
End Function