Darkover
Angelic
- Jul 29, 2021
- 4,738
Light and depth
3d byte array to 2d render
z axis depth determines brightness of red
32*32*32 =32 768 bytes
Screen render 32 by 32 width and height
create sphere fills the 3d array with a sphere shape
Spot Light diffuse lighting using dot product
3d byte array to 2d render
z axis depth determines brightness of red
32*32*32 =32 768 bytes
Screen render 32 by 32 width and height
create sphere fills the 3d array with a sphere shape
Spot Light diffuse lighting using dot product
C:
constant byte_width =640,byte_height=480, byte_depth=64
sequence byte_array
byte_array={}
--x,y,z
function distance3d(integer x1, integer y1, integer z1, integer x2, integer y2, integer z2)
integer x3,y3,z3
x3 = power(x1 - x2, 2)
y3 = power(y1 - y2, 2)
z3 = power(z1 - z2, 2)
return sqrt(x3 + y3 + z3 )
end function
for i=1 to byte_depth do
byte_array=append(byte_array, repeat(repeat(0,byte_width),byte_height) )
end for
for z=1 to byte_depth do
--byte_array=append(byte_array,{})
for y=1 to byte_height do
for x=1 to byte_width do
end for
end for
end for
procedure sphere(integer center_x, integer center_y, integer center_z, integer dist, atom colour)
atom dd
for x=1 to byte_width do
for y=1 to byte_height do
for z=1 to byte_depth do
dd=distance3d(x,y,z ,center_x,center_y,center_z)
if dd<dist then
byte_array[z][x][y]=colour
end if
end for
end for
end for
end procedure
procedure cube(integer start_x, integer start_y, integer start_z, integer size, atom colour)
for x=start_x to start_x+size do
for y=start_y to start_y+size do
for z=start_z to start_z+size do
byte_array[z][x][y]=colour
end for
end for
end for
end procedure
sphere(320,240,24, 24, rgb(255,0,0))
sphere(320,340,32, 32, rgb(255,255,0))
sphere(120,140,16, 16, rgb(255,0,255))
sphere(32,32,32, 32, rgb(0,255,0))
sphere(280,180,32, 32, rgb(0,0,255))
cube(320,240,1, 32 , rgb(0,0,255) )
function rgbs(atom rgb)
return ( {and_bits(#000000FF, rgb),
and_bits(#0000FF00, rgb) / #100,
and_bits(#00FF0000, rgb) / #10000 } )
end function
sequence screen_pixels
screen_pixels=repeat(repeat({0,0,0},byte_width),byte_height)
integer light_x,light_y,light_z
integer light2_x,light2_y,light2_z
light_x=320
light_y=240
light_z=-32
light2_y=320
light2_x=300
light2_z=-32
integer surface_x,surface_y,surface_z
atom length1
surface_x=0
surface_y=0
surface_z=-1
length1=sqrt(0 + 0 + surface_z*surface_z )
surface_x=surface_x/length1
surface_y=surface_y/length1
surface_z=surface_z/length1
sequence dib,temp
dib = newDib(byte_width, byte_height)
atom lightness,lightness2,vx,vy,vz,ttt
ttt=time()
sequence active_pixels
active_pixels={}
for x=1 to byte_width-1 do
for y=1 to byte_height-1 do
for z=1 to byte_depth do
if byte_array[z][y][x]!=0 then
active_pixels=append(active_pixels,{x,y,z})
vx= light_x-x
vy= light_y-y
vz= light_z-z
length1=sqrt(vx*vx + vy*vy + vz*vz )
if length1!=0 then
vx= vx/length1
vy= vy/length1
vz= vz/length1
lightness=(vx*surface_x) + (vy*surface_y) + (vz*surface_z)
end if
--screen_pixels[x][y]={255 * ((1/byte_depth)* (byte_depth-z)) ,0,0}
--screen_pixels[x][y]={255 *lightness ,0,0}
temp=rgbs(byte_array[z][y][x])
temp*=lightness
screen_pixels[x][y]=temp--rgbs(byte_array[z][y][x]*lightness)
fastPutDibPixel( dib, y, x, screen_pixels[x][y] )
exit
end if
end for
end for
end for
procedure render_update()
integer x,y,z
for i=1 to length(active_pixels) do
x=active_pixels[i][1]
y=active_pixels[i][2]
z=active_pixels[i][3]
vx= light_x-x
vy= light_y-y
vz= light_z+z
lightness=0
lightness2=0
length1=sqrt(vx*vx + vy*vy + vz*vz )
if length1!=0 then
vx= vx/length1
vy= vy/length1
vz= vz/length1
lightness=(vx*surface_x) + (vy*surface_y) + (vz*surface_z)
end if
vx= light2_x-x
vy= light2_y-y
vz= light2_z-z
length1=sqrt(vx*vx + vy*vy + vz*vz )
if length1!=0 then
vx= vx/length1
vy= vy/length1
vz= vz/length1
lightness2=(vx*surface_x) + (vy*surface_y) + (vz*surface_z)
end if
--screen_pixels[x][y]={255 * ((1/byte_depth)* (byte_depth-z)) ,0,0}
--screen_pixels[x][y]={255 *lightness ,0,0}
temp=rgbs(byte_array[z][y][x])
ttt=lightness+lightness2
if ttt>1 then
ttt=1
end if
temp*=ttt
screen_pixels[x][y]=temp--rgbs(byte_array[z][y][x]*lightness)
fastPutDibPixel( dib, y, x, screen_pixels[x][y] )
end for
end procedure
setTimer(Window1,1000,100)