|
Post by bluatigro on Apr 8, 2018 4:23:47 GMT -5
this is a try at a OOP style raycaster|tracer it is a example on how to do OOP in LB error : i get a yellow square i shoot get a black square whit a yellow circle Attachments:ray-tracer.bas (4.35 KB)
|
|
|
Post by bluatigro on Apr 14, 2018 8:01:32 GMT -5
update :
i taken my idea's over OOP in this to the extreme
''bluatigro 15 apr 2018
''raytracer OOP style
''create v3d object
global v3d.x , v3d.y , v3d.z '' for output of 3d function's
''create a array of sphere object's
global s.max : s.max = 100
dim s.x( s.max ) , s.y( s.max ) , s.z( s.max )
dim s.d( s.max ) , s.d2( s.max )
dim s.r( s.max ) , s.g( s.max ) , s.b( s.max )
global spheretel , infinity
infinity = 1e9
WindowWidth = 600
WindowHeight = 600
global winx , winy
winx = 600
winy = 600
nomainwin
open "rat tracer OOP style" for graphics as #m
#m "trapclose [quit]"
'' scene
spheretel = 0
call sphere 0,0,0 , 30 , rgb(255,255,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
sub setpixel x,y , r,g,b
r = ( r * 256 ) and 255
g = ( g * 256 ) and 255
b = ( b * 256 ) and 255
#m "goto " ; winx / 2 + x ; " " ; winy / 2 - y
#m "color " ; r ; " " ; g ; " " ; b
#m "down"
#m "set " ; winx / 2 + x ; " " ; winy / 2 - y
#m "up"
end sub
sub render o.x,o.y,o.z , d.x,d.y,d.z , depth
scan
call v3d.color 0 '' black backgound
if depth > 0 then
s.low = infinity
s.no = -1
for i = 0 to spheretel
dist = s.hit( i , o.x,o.y,o.z , d.x,d.y,d.z )
if dist < s.low then
s.no = i
s.low = dist
end if
next i
if s.no <> -1 then
''v3d = d - o
call v3d.sub d.x,d.y,d.z , o.x,o.y,o.z
call v3d.nomalize v3d.x,v3d.y,v3d.z
''v3d = v3d * low
call v3d.mul v3d.x,v3d.y,v3d.z , low
''v3d = o + v3d
call v3d.add v3d.x,v3d.y,v3d.z , o.x,o.y,o.z
mx = s.x( s.no )
my = s.y( s.no )
mz = s.z( s.no )
''v3d = v3d - sphere.center
call v3d.sub v3d.x,v3d.y,v3d.z , mx,my,mz
call v3d.nomalize v3d.x,v3d.y,v3d.z
''angle = angle( v3d , light )
angle = v3d.angle( v3d.x,v3d.y,v3d.z , -1,1,-1 )
r = s.r( s.no )
g = s.g( s.no )
b = s.b( s.no )
''v3d = color * cos( angle ) / 2 + .5
call v3d.mul r,g,b , cos( angle ) / 2 + .5
end if
end if
end sub
sub sphere x,y,z , d , kl
if spheretel >= s.max then exit sub
s.x( spheretel ) = x
s.y( spheretel ) = y
s.z( spheretel ) = z
s.d( spheretel ) = d
s.d2( spheretel ) = d * d
call v3d.color kl
s.r( spheretel ) = v3d.x
s.g( spheretel ) = v3d.y
s.b( spheretel ) = v3d.z
spheretel = spheretel + 1
end sub
function s.hit( no , o.x,o.y,o.z , d.x,d.y,d.z )
c.x = s.x( no )
c.y = s.y( no )
c.z = s.z( no )
call v3d.sub o.x,o.y,o.z , c.x,c.y,c.z
a = v3d.dot( v3d.x,v3d.y,v3d.z , v3d.x,v3d.y,v3d.z )
b = 2 * v3d.dot( p.x,p.y,p.z , d.x,d.y,d.z )
c = v3d.dot( p.x,p.y,p.z , p.x,p.y,p.z ) - s.d2( no )
disc = b ^ 2 - 4 * a * c
uit = infinity
if disc < 0 then
uit = inifity
else
e = sqr( disc )
demon = 2 * a
t = ( 0-b - e ) / demon
if t > 1e-12 then
uit = t
end if
t = ( 0-b + e ) / demon
if t > 1e-12 then
uit = t
end if
end if
sphere.hit = uit
end function
sub v3d x,y,z
''constructor of v3d object
v3d.x = x
v3d.y = y
v3d.z = z
end sub
sub v3d.add a.x,a.y,a.z , b.x,b.y,b.z
''v3d = a + b
call v3d a.x + b.x , a.y + b.y , a.z + b.z
end sub
sub v3d.sub a.x,a.y,a.z , b.x,b.y,b.z
''v3d = a - b
call v3d a.x - b.x , a.y - b.y , a.z - b.z
end sub
sub v3d.div a.x,a.y,a.z , b
''v3d = a / b
call v3d a.x / b , a.y / b , a.z / b
end sub
sub v3d.mul a.x,a.y,a.z , b
''v3d = a * b
call v3d a.x * b , a.y * b , a.z * b
end sub
function v3d.dot( a.x,a.y,a.z , b.x,b.y,b.z )
v3d.dot = a.x * b.x + a.y * b.y + a.z * b.z
end function
function v3d.lenght( x,y,z )
''|v(x,y,z)|
v3d.lenght = sqr( v3d.dot( x,y,z , x,y,z ) )
end function
function v3d.angle( a.x,a.y,a.z , b.x,b.y,b.z )
a.len = v3d.lenght( a.x,a.y,a.z )
b.len = v3d.lenght( b.x,b.y,b.z )
d = v3d.dot( a.x,a.y,a.z , b.x,b.y,b.z )
v3d.angle = acs( d / ( a.len * b.len ) )
end function
sub v3d.nomalize x,y,z
call v3d.div x,y,z , v3d.lenght( x,y,z )
end sub
sub v3d.cross a.x,a.y,a.z , b.x,b.y,b.z
call v3d a.y * b.z - a.z * b.y _
, a.z * b.x - a.x * b.z _
, a.x * b.y - a.y * b.x
end sub
sub v3d.color kl
''v = colorObject
call v3d ( int( kl ) and 255 ) / 256 _
, ( int( kl / 256 ) and 255 ) / 256 _
, ( int( kl / 256 ^ 2 ) and 255 ) / 256
end sub
function rgb( r , g , b )
''create colorObject
r = r and 255
g = g and 255
b = b and 255
rgb = r + g * 256 + b * 256 ^ 2
end function
|
|
|
Post by Rod on Apr 14, 2018 10:41:04 GMT -5
In the old forum, long code like this and your oop code did not get posted, it would not fit in the message box. So folks posted a link, usually to a zip. We perhaps did not realize the benefits of this. In this forum there is no restriction on the length of code you can post but we are quickly understanding that this is not a benefit.
People expect short messages and example code. Scrolling through a long list of code spoils folks interaction with the forum. "Recent posts" listing for example requires everyone to scroll through massive lists of oop code whether they are interested or not. Not everyone is on high speed download either.
So we need to find a happy medium. We need folks to be sensible, short code in the body of the text within the code tags, long code as an attachment, but if you intend to repeatedly attach the same code you should consider a linked .zip.
|
|