Raster Cube

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

Raster Cube

Post by DrChip »

Code: Select all

rem raster cube
rem iPhone 6 Plus / 8.3 b3
rem Enjoy...
'Example Of Fast Cube Rasterisation.
'THIS ROUTINE RENDERS EACH FACE WITH A CUSTOM FILL ROUTINE
'Res=1 (best quality but slowest) bigger numbers for res
'will increase speed at the expense of image quality.

res=10


gosub initialize

loop:
    refresh off
    graphics clear 0,0,0
    fill color 1,1,1
    fill rect 0,0 to sw,20
    gosub rotate
    gosub construct
    draw text "Raster Cube" at 0,0
    refresh on
goto loop


'Draw The Object
construct:
for a=1 to faces
gosub draw
next a
return

customfill:
'Pass four points of rectangle to renderer
cfx(1)=tx(f1(a))
cfx(2)=tx(f2(a))
cfx(3)=tx(f3(a))
cfx(4)=tx(f4(a))
cfy(1)=ty(f1(a))
cfy(2)=ty(f2(a))
cfy(3)=ty(f3(a))
cfy(4)=ty(f4(a))

'Calculate Approximate Center
xc=(cfx(1)+cfx(3))/2
yc=(cfy(1)+cfy(3))/2
cfx(5)=xc
cfy(5)=yc

'Pass Points into render engine variables
ctx(1)=cfx(1) ! ctx(2)=cfx(2) ! ctx(3)=cfx(5)
cty(1)=cfy(1) ! cty(2)=cfy(2) ! cty(3)=cfy(5)
gosub cft
ctx(1)=cfx(2) ! ctx(2)=cfx(3) ! ctx(3)=cfx(5)
cty(1)=cfy(2) ! cty(2)=cfy(3) ! cty(3)=cfy(5)
gosub cft
ctx(1)=cfx(3) ! ctx(2)=cfx(4) ! ctx(3)=cfx(5)
cty(1)=cfy(3) ! cty(2)=cfy(4) ! cty(3)=cfy(5)
gosub cft
ctx(1)=cfx(4) ! ctx(2)=cfx(1) ! ctx(3)=cfx(5)
cty(1)=cfy(4) ! cty(2)=cfy(1) ! cty(3)=cfy(5)
gosub cft
return

cft:
'Triangle Rasteriser
'Sort Triangle Y
for b=1 to 2
for m=1 to 2
 if cty(m)>cty(m+1) then
  tmx=ctx(m)
  tmy=cty(m)
  ctx(m)=ctx(m+1)
  cty(m)=cty(m+1)
  ctx(m+1)=tmx
  cty(m+1)=tmy
 end if
next m
next b

 lx=ctx(1)
 rx=ctx(1)
 la=(ctx(1)-ctx(2))/(cty(1)-cty(2))*res
 ra=(ctx(1)-ctx(3))/(cty(1)-cty(3))*res
 for cy=cty(1) to cty(2) step res
   fill rect lx,cy-res to rx,cy+res
   lx=lx+la
   rx=rx+ra
 next cy

 lx=ctx(2)
 la=(ctx(2)-ctx(3))/(cty(2)-cty(3))*res
 for cy=cty(2) to cty(3)-res step res
    fill rect lx,cy-res to rx,cy+res
    lx=lx+la
    rx=rx+ra
 next cy

return


'Draw A Face Of The Object
draw:
  vx1= tx(f1(a))-tx(f2(a))
  vy1= ty(f1(a))-ty(f2(a))
  vx2= tx(f3(a))-tx(f2(a))
  vy2= ty(f3(a))-ty(f2(a))
  n=  vx1*vy2-vx2*vy1
 if n<0 then
 n=-(n/500)
 fill color (cr(a)+n)/255,(cg(a)+n)/255,(cb(a)+n)/255
 draw color (cr(a)+n)/255,(cg(a)+n)/255,(cb(a)+n)/255
  gosub customfill

' fill triangle tx(f1(a)),ty(f1(a)) to tx(f2(a)),ty(f2(a)) to tx(f3(a)),ty(f3(a))
' fill triangle tx(f1(a)),ty(f1(a)) to tx(f4(a)),ty(f4(a)) to tx(f3(a)),ty(f3(a))
 if cls(a)=1 then
   draw color 0,150/255,150/255
   draw line tx(f1(a)),ty(f1(a)) to tx(f2(a)),ty(f2(a))
   draw line tx(f2(a)),ty(f2(a)) to tx(f3(a)),ty(f3(a))
   draw line tx(f3(a)),ty(f3(a)) to tx(f4(a)),ty(f4(a))
   draw line tx(f4(a)),ty(f4(a)) to tx(f1(a)),ty(f1(a))
 end if
end if
return


rotate:
'Rotate And Scale Each Point Store Result 
 for a=1 to points
  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
dv=(zz/30)+1
  xx=size*(xx/dv)+sw/2
  yy=size*(yy/dv)+sh/2
  tx(a)=xx
  ty(a)=yy
  tz(a)=zz
 next a
xr=xr+4
yr=yr+3
zr=zr+1
if xr>720 then xr=xr-720
if yr>720 then yr=yr-720
if zr>720 then zr=zr-720
return

initialize:
'Open Gfx Screen
graphics
sw=screen_width()
sh=screen_height() 


'Define the necessary variables
pi=3.1415
size=8 ' how big do you want it?
points=8 ' The amount of points in the object
faces=6 ' The Amount of faces in the object
dim cfx(6),cfy(6)
dim ctx(4),cty(4)
dim x(points+1) 'Original X co-ordinate store
dim y(points+1) 'Original Y co-ordinate store
dim z(points+1) 'Original Z co-ordinate store
dim tx(points+1) 'Transformed  X co-ordinate store
dim ty(points+1) 'Transformed Y co-ordinate store
dim tz(points+1) 'Transformed Z co-ordinate store
dim f1(faces+1) 'Connections definition
dim f2(faces+1) 'Connections definition
dim f3(faces+1) 'Connections definition
dim f4(faces+1) 'Connections definition
dim cr(faces+1) 'Red Component
dim cg(faces+1) 'Green Component
dim cb(faces+1) 'Blue Component
dim cls(faces+1) 'Cell Shade Face?

'Define Sine Tables for faster matrix calculations;

 dim cs(721)
 dim sn(721)
 for ang=1 to 720
  cs(ang)=cos(ang*(pi/360))
  sn(ang)=sin(ang*(pi/360))
 next ang

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

'Read In Connections and face parameters;
for a=1 to faces
read f1(a)
read f2(a)
read f3(a)
read f4(a)
read cr(a),cg(a),cb(a),cls(a)
next a

'The Object Description As Data
'Points definition
'Below are the points of the object defined as x,y,z

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

'Connection definition;
'Below are the faces of the object defined as vertice
'numbers, specified in clockwise order. These are followed
'by r,g,b values for the face and finally cell shaded
'parameter (0)=off (1)=on.

data 1,2,3,4,30,0,0,0
data 5,8,7,6,30,0,0,0
data 6,2,1,5,0,30,0,0
data 8,4,3,7,0,30,0,0
data 2,6,7,3,0,0,30,0
data 8,5,1,4,0,0,30,0

return
Attachments
image.jpg
image.jpg (96.82 KiB) Viewed 2964 times
image.jpg
image.jpg (94 KiB) Viewed 2964 times
image.jpg
image.jpg (94.76 KiB) Viewed 2964 times

Post Reply