Main sections
X_SETCELSHADES
X_SETCELSHADES shades#[]
Sets the steps of brightness which are to be used for cel shading. shades#[] is an array of values from 0.0 (black) to 1.0 (bright) that indicate the brightness which is to be applied for each reflection angle.
The function should be called at the beginning of the program only once as all textures need to be recreated when they are next rendered. As a result of the texture recreation you will experience a small delay for every call to the function.
Basic usage:
DIMDATA sh[], 0.1, 0.3, 1.0
X_SETCELSHADES sh[]
WHILE TRUE
Render()
WEND

A sample that shows the result achieved:
// --------------------------------- //
// Project: ToonShading
CreateTorus(1, 5, 10, 22, 22, 2, 2)
// Image data
LOADSPRITE "image.bmp", 0
DIMDATA cheap[], .5, 1
DIMDATA nice[], .3, .6, .6, .6, .6, .6, .6, .7, 1, 1, 1, 1, 1, 1, 1, 1
WHILE TRUE
// main loop
DRAWRECT 0,0,640,480,RGB(0,0,200)
phi=phi+GETTIMER()/30
VIEWPORT 0,0,320,480
// optional - set cel shading steps
X_SETCELSHADES cheap[]
Render(phi)
VIEWPORT 320,0,320,480
X_SETCELSHADES nice[]
Render(phi)
dtime = GETTIMER()
fps = ((1000.0/dtime)+fps)/2
delay=delay+dtime
IF delay>1000 // 1/2 sec
delay=0
showfps=fps
ENDIF
X_MAKE2D
PRINT "FPS: "+FORMAT$(0,1,showfps), 100, 0 // + dtime, 0,0
SHOWSCREEN
WEND
// ------------------------------------------------------------- //
// Renders a Donut
// ------------------------------------------------------------- //
FUNCTION Render: phi
X_MAKE3D 1, 250, 45
X_CAMERA 0, 10, 50, 0,0,0
// Licht Nr. -2 is Cel-Shading Postition / Light no -2 is for cel shading
X_SPOT_LT -2, 0, 0,0,100, 0,0,0,90
X_CULLMODE 1 // niemals 0 verwenden bei toons / Never use 0 with toon shading
X_SETTEXTURE 0, -1 // 0=Tex
X_ROTATION 80, 1,0,0
X_ROTATION phi, 0, 1, 0.1
X_DRAWOBJ 1, 0
ENDFUNCTION
// ------------------------------------------------------------- //
// -=# CREATETORUS #=-
// Donut Objekt machen / Make a donut object
//
// By Samuel R. Buss
// http://math.ucsd.edu/~sbuss/MathCG
// ------------------------------------------------------------- //
FUNCTION CreateTorus: num, MinorRadius, MajorRadius, NumWraps, NumPerWrap, TextureWrapVert, TextureWrapHoriz
LOCAL i, di, j, wrapFrac, wrapFracTex, phi, thetaFrac, thetaFracTex, theta
LOCAL x, y, z, r
X_AUTONORMALS 2 // smooth
X_OBJSTART num
FOR di=0 TO NumWraps-1
FOR j=0 TO NumPerWrap
FOR i=di+1 TO di STEP -1
wrapFrac = MOD(j, NumPerWrap)/NumPerWrap
wrapFracTex = j/NumPerWrap
phi = 360*wrapFrac
thetaFrac = (MOD(i, NumWraps)+wrapFracTex)/NumWraps
thetaFracTex = (i+wrapFracTex)/NumWraps
theta = 360*thetaFrac
r = MajorRadius + MinorRadius*COS(phi)
x = SIN(theta)*r
z = COS(theta)*r
y = MinorRadius*SIN(phi)
X_OBJADDVERTEX x,y,z, thetaFracTex*TextureWrapVert, 1-wrapFracTex*TextureWrapHoriz, _
RGB(0xff,0xff,0xff)
NEXT
NEXT
X_OBJNEWGROUP
NEXT
X_OBJEND
ENDFUNCTION // y