0 Members and 1 Guest are viewing this topic.
'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 constructuntil (and(peek("port1"),16384)<>0)exit'---------------------------------------------------------' Draw The Object;'---------------------------------------------------------label constructfor a=1 to facesgosub drawnext areturnlabel 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))/2yc=(cfy(1)+cfy(3))/2cfx(5)=xccfy(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 cftctx(1)=cfx(2) : ctx(2)=cfx(3) : ctx(3)=cfx(5)cty(1)=cfy(2) : cty(2)=cfy(3) : cty(3)=cfy(5)gosub cftctx(1)=cfx(3) : ctx(2)=cfx(4) : ctx(3)=cfx(5)cty(1)=cfy(3) : cty(2)=cfy(4) : cty(3)=cfy(5)gosub cftctx(1)=cfx(4) : ctx(2)=cfx(1) : ctx(3)=cfx(5)cty(1)=cfy(4) : cty(2)=cfy(1) : cty(3)=cfy(5)gosub cftreturnlabel cft'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%' Triangle Rasteriser (C) Shockwave 2002.'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'Sort Triangle Y;for b=1 to 2for 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 finext mnext 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 resnn=min(lx,rx)sz=abs(lx-rx)/resfor 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+resnext 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 resnn=min(lx,rx)sz=abs(lx-rx)/resfor 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+resnext mmm lx=lx+la rx=rx+ra next cyreturn'---------------------------------------------------------' 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 customfillfireturnlabel 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 axr=xr+7yr=yr+6zr=zr+5if xr>720 xr=xr-720if yr>720 yr=yr-720if zr>720 zr=zr-720returnlabel 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 Variablepoints=8 : Rem The amount of points in the objectfaces=6 : Rem The Amount of faces in the objectdim cfx(5),cfy(5)dim ctx(3),cty(3)dim x(points): Rem Original X co-ordinate storedim y(points): Rem Original Y co-ordinate storedim z(points): Rem Original Z co-ordinate storedim tx(points): Rem Transformed X co-ordinate storedim ty(points): Rem Transformed Y co-ordinate storedim tz(points): Rem Transformed Z co-ordinate storedim f1(faces):rem Connections definitiondim f2(faces):rem Connections definitiondim f3(faces):rem Connections definitiondim 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 facesread 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,10data -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,4data 5,8,7,6data 6,2,1,5data 8,4,3,7data 2,6,7,3data 8,5,1,4return