DECLARE SUB drawsphere3 (x%, y%, r%, clr%) DECLARE SUB drawsphere (x%, y%, r%, clr%, scrang!, vertang!) 'Here's a little QBASIC sub to draw spheres, randomly dithered for a 'random direction lighting effect. 'by Antoni Gual agual@eic.ictnet.es 5/2001 'using screen 12 for this demo, should work in screen 13 SCREEN 12 RANDOMIZE TIMER CONST pi = 3.141592 'a color range has 4 colors, so in screen 12 we can have 4 ball colors CONST clrng% = 4 'define color ranges, darkest color must have the lowest palette nr 'range of colors used by red balls PALETTE 0, &H5 PALETTE 1, &H10 PALETTE 2, &H20 PALETTE 3, &H30 'range of colors used by green balls PALETTE 4, &H500 PALETTE 5, &H1000 PALETTE 6, &H2000 PALETTE 7, &H3000 'range of colors used by blue balls PALETTE 8, &H50000 PALETTE 9, &H100000 PALETTE 10, &H200000 PALETTE 11, &H300000 'range of colors used by white balls PALETTE 12, &H50505 PALETTE 13, &H101010 PALETTE 14, &H202020 PALETTE 15, &H303030 'demo start CLS p$ = SPACE$(79) LOCATE 28, 1: PRINT p$; LOCATE 28, 1: PRINT "Press a key to draw some randomly lighted balls..Another key to stop"; a$ = INPUT$(1) DO x% = RND * 640 y% = RND * 480 r% = RND * 80 + 10 c% = INT(RND * clrng%) * clrng% 'we must pass the lowest PALETTE nr of range a = RND * pi * 2 b = RND * pi drawsphere x%, y%, r%, c%, a, b LOOP UNTIL LEN(INKEY$) LOCATE 28, 1: PRINT p$; LOCATE 28, 1: PRINT "If light source is at the top we can do it faster.. Press a key!"; a$ = INPUT$(1) DO x% = RND * 640 y% = RND * 480 r% = RND * 80 + 10 c% = INT(RND * clrng%) * clrng% drawsphere3 x%, y%, r%, c% LOOP UNTIL LEN(INKEY$) LOCATE 28, 1: PRINT p$; LOCATE 28, 1: PRINT "End of Balls demo, by Antoni Gual agual@eic.ictnet.es"; SUB drawsphere (x%, y%, r%, clr%, scrang, vertang) '3d shadowed sphere drawing using dithering and a range of color palette 'by Antoni Gual agual@eic.ictnet.es 5/2001 'inputs; 'x%, y% position on screen 'r% radius 'clr% base color (the darkest one in range, it must be the lower palette nr) 'scrang in radians direction of the light source 'vertangin radians angle of the light source to a line perpendicular to screen 'Also the global constant clrng% must be set to the width of a color range '---------------------------------------------------------------------------- clr1% = clr% + 1 r2% = r% * r% clrng1% = clrng% - 1 x1 = COS(scrang) * SIN(vertang) y1 = SIN(scrang) * SIN(vertang) z1 = COS(vertang) FOR i% = -r% TO r% i2% = i% * i% i1 = CSNG(i%) / r% i12 = i1 * i1 a = SQR(r2% - i2%) FOR j = -a TO a j1 = j / r% k1 = SQR(1.001 - (i12 + j1 * j1)) c = clrng1% * (x1 * i1 + j1 * y1 + k1 * z1) ccc% = clr1% + INT(c) + (RND > (c - INT(c))) 'comment out next line for backlight effect IF ccc% < clr% THEN ccc% = clr% PSET (x% + i%, y% + j), ccc% NEXT j, i% END SUB SUB drawsphere3 (x%, y%, r%, clr%) '3d shadowed sphere drawing using dithering and a range of color palette 'Special case: Light source is supposed to be in front of the screen 'by Antoni Gual agual@eic.ictnet.es 5/2001 'inputs; 'x%, y% position on screen 'r% radius 'clr% base color (the darkest one in range, it must be the lower palette nr) 'Also the global constant clrng% must be set to the width of a color range '---------------------------------------------------------------------------- clr1% = clrng% + clr% - 1 r2% = r% * r%: r3 = (clrng% - 1) / r% FOR i% = 0 TO r% i2% = i% * i% a% = CINT(SQR(r2% - i2%)) tmp% = x% + i%: tmp1% = x% - i% FOR j% = -a% TO a% c = r3 * SQR(j% * j% + i2%) cc% = clr1% - INT(c) + (RND < c - INT(c)) PSET (tmp%, y% + j%), cc% PSET (tmp1%, y% + j%), cc% NEXT NEXT END SUB