Author Topic: Stencils  (Read 2493 times)

0 Members and 1 Guest are viewing this topic.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Stencils
« on: October 19, 2006 »
This is just a lame stencil routine with bugs in the rasteriser :)

Code: [Select]
'Stencil Vector Routine.
'~~~~~~~~~~~~~~~~~~~~~~~
'I thought that it wouldn't be right to shout my mouth off
'about stencil vectors without having coded them myself
'though, so here they are.
'
'I set a competition on the forums for the first person
'to do a stencil vector. At the time of writing (01/10/02)
'2 people have posted thier code. Fryer and Praabellum.
'
'I like both entries, Fryer was first though so he won.
'Other people who expressed an interest in coding this
'routine were: Jim Shaw , Xalthorn and Jinx. I think that
'Jim is writing a routine to do this, I don't know if
'Xalthorn is going to have a try.
'
'Jinx went one better and coded a proper texture mapped
'3D cube.
'
'Hiya's to : Fryer, Parabellum, Xalthorn, Jim, Jinx and
'all the rest of you ;o) Sorry if I forgot you.
'
'Coded By Shockwave C) 2002.
'#########################################################

'Res=1 (best quality but slowest) bigger numbers for res
'will increase speed at the expense of image quality.


res=7

'
gosub initialise
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
repeat
    setdrawbuf dw
    dw=1-dw
    setdispbuf dw
    clear window
    gosub rotate
    gosub construct
until (and(peek("port1"),16384)<>0)
exit

'---------------------------------------------------------
'                     Draw The Object;
'---------------------------------------------------------
label construct
for a=1 to faces
gosub draw
next a
return



label 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 Centre;
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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

label cft
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'       Triangle Rasteriser (C) Shockwave 2002.
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'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
 fi
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

nn=min(lx,rx)
sz=abs(lx-rx)/res
for mmm=1 to sz
   r=mod(nn,25)
   g=mod(cy,25)
   b=mod(cy+nn,25)
   setrgb 1,r*a,g*a,b*a
   fill rect nn,cy-res to nn+res,cy+res
   nn=nn+res
next mmm

   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) step res

nn=min(lx,rx)
sz=abs(lx-rx)/res
for mmm=1 to sz
   r=mod(nn,25)
   g=mod(cy,25)
   b=mod(cy+nn,25)
   setrgb 1,r*a,g*a,b*a
      fill rect nn,cy-res to nn+res,cy+res
  nn=nn+res
next mmm
    lx=lx+la
    rx=rx+ra
 next cy

return

'---------------------------------------------------------
'              Draw A Face Of The Object;
'---------------------------------------------------------

label 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
  gosub customfill
fi
return


label 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/40)+1
  xx=size*(xx/dv)+320
  yy=size*(yy/dv)+256
  tx(a)=xx
  ty(a)=yy
  tz(a)=zz
 next a
xr=xr+7
yr=yr+6
zr=zr+5
if xr>720 xr=xr-720
if yr>720 yr=yr-720
if zr>720 zr=zr-720
return
label initialise
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
' This Sub-Routine Initialises The Program.
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'######################
'## Open Gfx Screen! ##
'######################
open window 640,512
'---------------------------------------------------------
'            Define the necessary variables;
'---------------------------------------------------------
size=12: rem                       how big do you want it?
dw=1 : Rem                       Double buffering Variable
points=8 : Rem          The amount of points in the object
faces=6 : Rem            The Amount of faces in the object
dim cfx(5),cfy(5)
dim ctx(3),cty(3)
dim x(points): Rem            Original X co-ordinate store
dim y(points): Rem            Original Y co-ordinate store
dim z(points): Rem            Original Z co-ordinate store
dim tx(points): Rem       Transformed  X co-ordinate store
dim ty(points): Rem        Transformed Y co-ordinate store
dim tz(points): Rem        Transformed Z co-ordinate store
dim f1(faces):rem                   Connections definition
dim f2(faces):rem                   Connections definition
dim f3(faces):rem                   Connections definition
dim f4(faces):rem                   Connections definition
'---------------------------------------------------------
'   Define Sine Tables for faster matrix calculations;
'---------------------------------------------------------

 dim cs(720)
 dim sn(720)
 for ang=0 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)
next a
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'   The Object Description As Data!
'   The Data Below Describes A Cube.
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

'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
data 5,8,7,6
data 6,2,1,5
data 8,4,3,7
data 2,6,7,3
data 8,5,1,4

return
Shockwave ^ Codigos
Challenge Trophies Won: