Post by bluatigro on Nov 11, 2018 16:57:45 GMT
error :
the red triangle is bigger but shoot be smaler then picture on screen
the red triangle is bigger but shoot be smaler then picture on screen
global trimax , tritel
trimax = 100
global v3d.x , v3d.y , v.3d.z , infinity
infinity = 1e9
dim tx1( trimax ) , ty1( trimax ) , tz1( trimax )
dim tx2( trimax ) , ty2( trimax ) , tz2( trimax )
dim tx3( trimax ) , ty3( trimax ) , tz3( trimax )
dim txn( trimax ) , tyn( trimax ) , tzn( trimax )
dim tr( trimax ) , tg( trimax ) , tb( trimax )
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy
winx = WindowWidth
winy = WindowHeight
nomainwin
open "ray" for graphics as #m
#m "trapclose [quit]"
tritel = 0
call triangle 0,0,0 , 30,0,0 , 15,20,0 , rgb(255,0,0)
for x = -100 to 100
for y = -100 to 100
call render 0,0,-1000 , x,y,1000 , 1
call setpixel x , y , v3d.x,v3d.y,v3d.z
next y
next x
notice "ready"
wait
[quit]
close #m
end
function rgb( r , g , b )
r = r and 255
g = g and 255
b = b and 255
rgb = r + g * 256 + b * 256 ^ 2
end function
sub setpixel x , y , r , g , b
r = int( r * 256 ) and 255
g = int( g * 256 ) and 255
b = int( b * 256 ) and 255
#m "color " ; r ; " " ; g ; " " ; b
#m "goto "; winx / 2 + x ; " " ; winy / 2 - y
#m "down"
#m "set " ; winx / 2 + x ; " " ; winy / 2 - y
#m "up"
end sub
sub render ox,oy,oz , dx,dy,dz , dept
low = infinity
itri = -1
for i = 0 to tritel
dist = triangle.hit( i , ox,oy,oz , dx,dy,dz )
if dist < low then
low = dist
itri = i
end if
next i
if itri = -1 then
call v3d 0 , 0 , 0
exit sub
end if
call v3d tr(itri),tg(itri),tb(itri)
end sub
sub triangle x1,y1,z1 , x2,y2,z2 , x3,y3,z3 , kl
tx1( tritel ) = x1
ty1( tritel ) = y1
tz1( tritel ) = z1
tx2( tritel ) = x2
ty2( tritel ) = y2
tz2( tritel ) = z2
tx3( tritel ) = x3
ty3( tritel ) = y3
tz3( tritel ) = z3
call v3d.cross x2-x1,y2-y1,z2-z1 , x3-x1,y3-y1,z3-z1
txn( tritel ) = v3d.x
tyn( tritel ) = v3d.y
tzn( tritel ) = v3d.z
tr( tritel ) = int( kl and 255 ) / 256
tg( tritel ) = ( int( kl / 256 ) and 255 ) / 256
tb( tritel ) = ( int( kl / 256 ^ 2 ) and 255 ) / 256
tritel = tritel + 1
end sub
function triangle.hit( i , ox , oy , oz , dx , dy , dz )
call v3d.sub tx2(i),ty2(i),tz2(i) , tx1(i),ty1(i),tz1(i)
e1x = v3d.x : e1y = v3d.y : e1z = v3d.z
call v3d.sub tx3(i),ty3(i),tz3(i) , tx1(i),ty1(i),tz1(i)
e2x = v3d.x : e2y = v3d.y : e2z = v3d.z
call v3d.cross dx,dy,dz , e2x,e2y,e2z
px = v3d.x : py = v3d.y : pz = v3d.z
a = dot( e1x,e1y,e1z , px,py,pz )
if abs( a ) < 1e-9 then
triangle.hit = infinity
exit function
end if
f = 1 / a
call v3d.sub ox,oy,oz , tx1(i),ty1(i),tz1(i)
sx = v3d.x : sy = v3d.y : sz = v3d.z
u = f * dot( sx,sy,sz , px,py,pz )
if u < 0 or u > 1 then
triangle.hit = infinity
exit function
end if
call v3d.cross sx,sy,sz , e1x,e1y,e1z
qx = v3d.x : qy = v3d.y : qz = v3d.z
v = f * dot( dx,dy,dz , qx,qy,qz )
if v < 0 or u + v > 1 then
triangle.hit = infinity
exit function
end if
triangle.hit = f * dot( e2x,e2y,e2z , qx,qy,qz )
end function
sub v3d x , y , z
'' constructor vector3D
v3d.x = x
v3d.y = y
v3d.z = z
end sub
sub v3d.add ax,ay,az , bx,by,bz
'' v = a + b
call v3d ax + bx , ay + by , az + bz
end sub
sub v3d.sub ax,ay,az , bx,by,bz
'' v = a - b
call v3d ax - bx , ay - by , az - bz
end sub
sub v3d.mul ax,ay,az , b
'' v = a * b
call v3d ax * b , ay * b , az * b
end sub
sub v3d.div ax,ay,az , b
'' v = a / b
call v3d ax / b , ay / b , az / b
end sub
sub v3d.cross ax,ay,az , bx,by,bz
'' v = a . b
call v3d ay * bz - az * by , az * bx - ax * bz , ax * by - ay * bx
end sub
function dot( ax,ay,az , bx,by,bz )
dot = ax * bx + ay * by + az * bz
end function
function lenght( x,y,z )
lenght = dot( x,y,z , x,y,z )
end function