3d Cube Example

Post Reply
DrChip
Posts: 167
Joined: Wed Oct 22, 2014 3:26 pm
My devices: iPhone 4 to 6+,iPad mini to iPad air 2

3d Cube Example

Post by DrChip »

Code: Select all

rem Fast vector Routine (Code Snippet).
rem it's just a quick 3D example.
rem iPhone 6 Plus / iOS 8.4 b1 / sB 4.8

gosub initialize


loop:
refresh off
   fill color (40+(rd/5))/255,30/255,20/255
    fill tri 0,0, sw,sh, 0,sh
    fill tri 0,0, sw,sh, sw,0
    mm=mm+.05
    gosub rotate
    r=0 ! g=0 ! b=200
    f1=1 ! f2=2 ! f3=3 ! f4=4
    gosub draw
    r=0 ! g=200 ! b=0
    f1=5 ! f2=8 ! f3=7 ! f4=6
    gosub draw
    r=200 ! g=0 ! b=0
    f1=6 ! f2=2 ! f3=1 ! f4=5
    gosub draw
    r=0 ! g=100 ! b=100
    f1=8 ! f2=4 ! f3=3 ! f4=7
    gosub draw
    r=100 ! g=100 ! b=0
    f1=2 ! f2=6 ! f3=7 ! f4=3
    gosub draw
    r=100 ! g=100 ! b=100
    f1=8 ! f2=5 ! f3=1 ! f4=4
    gosub draw
draw color 1,1,1
for a=1 to polys
 fill circle tx(a),ty(a) size 5
 draw text str$(a) at tx(a),ty(a)
next a
refresh on
goto loop


draw:

'Draw A Face Of The Cube
  vx1= tx(f1)-tx(f2)
  vy1= ty(f1)-ty(f2)
  vx2= tx(f3)-tx(f2)
  vy2= ty(f3)-ty(f2)
  if  (vx1*vy2-vx2*vy1)<0 then
xc=(tx(f1)+tx(f2)+tx(f3)+tx(f4))/4
yc=(ty(f1)+ty(f2)+ty(f3)+ty(f4))/4
light=-(tz(f1)+tz(f2)+tz(f3)+tz(f4))*5
fill color (r+light)/255,(g+light)/255,(b+light)/255
fill tri tx(f1),ty(f1), tx(f2),ty(f2), tx(f3),ty(f3)
fill tri tx(f1),ty(f1), tx(f4),ty(f4), tx(f3),ty(f3)
end if
return


rotate:

'Rotate And Scale Each Point Store Result
 for a=1 to polys
  x1=x(a)
  y1=y(a)
  z1=z(a)

'X,Y,Z rotations
  xx=x1
  yy=y1*cs(xr)+z1*sn(xr)
  zz=z1*cs(xr)-y1*sn(xr)
  y1=yy
  x1=xx*cs(yr)-zz*sn(yr)
  z1=xx*sn(yr)+zz*cs(yr)
  zz=z1
  xx=x1*cs(zr)-y1*sn(zr)
  yy=x1*sn(zr)+y1*cs(zr)

'Apply Perspective
  xx=size*(xx/((zz/50)+1))+sw/2 'center X
  yy=size*(yy/((zz/50)+1))+sh/2 'center Y
  tx(a)=xx
  ty(a)=yy
  tz(a)=zz
 next a
xr=xr+3
yr=yr+2
zr=zr+5
if xr>720 then xr=xr-720
if yr>720 then yr=yr-720
if zr>720 then zr=zr-720
return


initialize:

graphics
sw=screen_width()
sh=screen_height()

pi=3.1415 ' pi
size=10 ' how big do you want it?
dw=1 '            Double buffering Variable
polys=8 '        The amount of polygons in the object
dim x(polys+1) '  Original X co-ordinate store
dim y(polys+1) '  Original Y co-ordinate store
dim z(polys+1) '  Original Z co-ordinate store
dim tx(polys+1) ' Transformed  X co-ordinate store
dim ty(polys+1) ' Transformed Y co-ordinate store
dim tz(polys+1) ' Transformed Z co-ordinate store

'Define Sine Tables
 dim cs(721)
 dim sn(721)
 for ang=0 to 720
  cs(ang)=cos(ang*(pi/360))
  sn(ang)=sin(ang*(pi/360))
 next ang

'Read in the object
for a=1 to polys
 read x(a),y(a),z(a)
next a

'The Object Description As Data
'The Data Below Describes A Cube.
data -10,10,10,10,10,10,10,-10,10,-10,-10,10
data -10,10,-10,10,10,-10,10,-10,-10,-10,-10,-10
return
Attachments
image.jpg
image.jpg (122.4 KiB) Viewed 1840 times
image.jpg
image.jpg (115.6 KiB) Viewed 1840 times
image.jpg
image.jpg (106.44 KiB) Viewed 1840 times
image.jpg
image.jpg (123.52 KiB) Viewed 1840 times
Last edited by DrChip on Mon May 18, 2015 2:50 pm, edited 2 times in total.

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: 3d Cube Example

Post by Dutchman »

You still have SmartBasic version 8.4? :D

DrChip
Posts: 167
Joined: Wed Oct 22, 2014 3:26 pm
My devices: iPhone 4 to 6+,iPad mini to iPad air 2

Re: 3d Cube Example

Post by DrChip »

lol! I fixed it. Thanks! :)

Post Reply