Light Source and Shading
Submitted by ilee on Mon, 2006-03-20 18:54. :: Category
HOW TO USE IT
Press setup and go The sphere is created and the lightsource is placed in its initial position
You can use the up, down, left and right keys to change the position and distance of the light source from the object.
THINGS TO EXPLORE Note how the brightness and shading changes based on the position and distance from the object. Try moving the light source closer and further and changing is location.
Turtle procedures
; there are no turtle procedures
Observer procedures
globals [xmax ymax]
globals [lx ly lz]
globals [len lx2 ly2 lz2]
patches-own [xx yy zz lum shine new-color]
;go runs two procedures prepare and render
to go
prepare
render
end
;setup creates a turtle and positions it
;it also creates the first rendering of the "3d" shape
to setup
ct
crt 1
ask-turtle 0
[setx 0 - screen-half-width
sety screen-half-height
seth 135]
setxmax screen-half-width / 3
setymax screen-half-height / 3
prepare
render
end
;performs some lighting calculations
to prepare
setlx (xcor-of 0) / xmax
setly (ycor-of 0) / ymax
setlz 4
setlen sqrt lx ^ 2 + ly ^ 2 + lz ^ 2
setlx2 lx / len
setly2 ly / len
setlz2 lz / len
ask-patches [
setxx xcor / xmax
setyy ycor / ymax
setzz sqrt 1 - xx ^ 2 - yy ^ 2]
end
;actually has the patches display the proper coloring based on lighting
to render
ask-patches [
ifelse zz >= 0
[setlum xx * lx2 + yy * ly2 + zz * lz2
setshine 2 * zz * lum - lz2
if shine < 0 [setshine 0]
setnew-color lum * 5 + (shine ^ 6) * 3
if new-color < 0 or new-color > 9 [setnew-color 0]
setnew-color new-color + (pc - pc mod 10)
setpc new-color]
[setlum 0 setpc 0]
]
end
5931 reads
