Created
January 6, 2017 17:07
-
-
Save nothke/edefbadca2cd83491567eef31bea6beb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = 64 | |
y = 64 | |
r = 20 | |
sunz = 10 | |
nmap = {} | |
cmap = {} | |
interleave = 1 | |
function dot(v1, v2) | |
return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z); | |
end | |
function printv(v) | |
print (v.x .. " " .. v.y .. " " .. v.z) | |
end | |
function distance(x, y) | |
return sqrt(x*x + y*y) | |
end | |
function length(v) | |
return sqrt(v.x*v.x + v.y*v.y + v.z*v.z) | |
end | |
function normalize(v) | |
l = length(v) | |
v.x = v.x/l | |
v.y = v.y/l | |
v.z = v.z/l | |
return v | |
end | |
function mouse_normalized() | |
v={} | |
v.x = mouse_x / 128 | |
v.y = mouse_y / 128 | |
v.z = 10 | |
return v | |
end | |
mouse_x = 0 | |
mouse_y = 0 | |
function _init() | |
poke(0x5f2d, 1) -- for the mouse | |
nullv = {} | |
nullv.x = 0 | |
nullv.y = 0 | |
nullv.z = 0 | |
-- initializes the normal and clip map | |
for i=0,128,1 do | |
nmap[i] = {} | |
cmap[i] = {} | |
for j=0,128,1 do | |
nmap[i][j] = nullv | |
cmap[i][j] = false | |
end | |
end | |
end | |
sunv={} | |
function _update() | |
-- controls | |
if btn(0) then r=r+1 end | |
if btn(1) then r=r-1 end | |
if btn(2) then sunz=sunz+1 end | |
if btn(3) then sunz=sunz-1 end | |
if btn(4) then interleave = interleave + 1 end | |
if btn(5) and interleave > 1 then interleave = interleave - 1 end | |
end | |
-- clears the clipmap | |
function clc() | |
for i=0,128,1 do | |
for j=0,128,1 do | |
cmap[i][j] = false | |
end | |
end | |
end | |
function _draw() | |
cls() | |
clc() -- clears the clipmap | |
mouse_x = stat(32) | |
mouse_y = stat(33) | |
normm = mouse_normalized() | |
-- sun light vector | |
sunv.x = (normm.x - 0.5) * 10 | |
sunv.y = (normm.y - 0.5) * 10 | |
sunv.z = sunz | |
sunv = normalize(sunv) | |
-- orbiting planet 1 | |
t = time() | |
op1x = 64 + sin(t * 0.1) * 40 | |
op1y = 64 + cos(t * 0.1) * 15 | |
op2x = 64 + sin(t * 0.02) * 80 | |
op2y = 64 + cos(t * 0.02) * 30 | |
-- planet | |
sphere(x,y,r) | |
-- dip | |
--sphere(64, 64, 8, true) | |
sphere(op1x,op1y,3,1) | |
sphere(op2x,op2y,5,1) | |
render() | |
pset(mouse_x,mouse_y,9) | |
end | |
function sphere(x,y,r,inv) | |
x = flr(x) | |
y = flr(y) | |
--rectfill(x-r,y-r,r*2,r*2,0) | |
--circfill(x,y,r,1) | |
srand(1) | |
xs = x-r | |
ys = y-r | |
xf = xs+r*2 | |
yf = ys+r*2 | |
for yi=ys,yf,1 do | |
for xi=xs,xf,1 do | |
--if (pget(xi,yi) == 2) then | |
if xi > 0 and xi < 128 and | |
yi > 0 and yi < 128 and | |
yi % interleave == 0 | |
then | |
normal={} | |
normal.x = (xi-x) / r-- + rnd(0.3) | |
normal.y = (yi-y) / r-- + rnd(0.3) | |
dist = distance(normal.x, normal.y) | |
if (dist < 1) then | |
cmap[xi][yi] = true | |
normal.z = sqrt(1 - dist*dist) --1-distance(normal.x, normal.y) | |
--c = dot(normal, sunv) | |
if inv == true then | |
normal.x = -normal.x | |
normal.y = -normal.y | |
--normal.z = -normal.z | |
end | |
if xi == mouse_x and yi == mouse_y then | |
printv(normal) | |
--print(c) | |
end | |
nmap[xi][yi] = normal | |
end | |
end | |
-- pset(xi,yi,mapc(c)) | |
--end | |
end | |
end | |
end | |
function render() | |
cls() | |
for xi=0,128,1 do | |
for yi=0,128,interleave do | |
n = nmap[xi][yi] | |
if cmap[xi][yi] == true then | |
-- lambert lighting: | |
c = dot(n, sunv) | |
-- set color: | |
pset(xi,yi,mapc(c)) | |
end | |
end | |
end | |
end | |
-- maps value to color | |
function mapc (c) | |
if c > 0.9 then | |
c = 7 | |
elseif c > 0.5 then | |
c = 9 | |
elseif c > 0 then | |
c = 4 | |
else c = 0 | |
end | |
return c | |
end | |
--main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment