library example v3d oop

Code snippets illustrating how LBB can be used
bluatigro
Posts: 5
Joined: Sat Apr 14, 2018 6:35 am

library example v3d oop

Post by bluatigro »

Code: Select all

''bluatigro 31 jun 2022
''test lirary v3d

gosub [v3d]
call v3d 1,2,3
call v3d.uit x,y,z
print "v3d 1 =",x,y,z
call v3d 4,5,7
call v3d.uit a,b,c
print "v3d 2 =",a,b,c
call v3d.add x,y,z , a,b,c
call v3d.uit e,f,g
print "v3d 2+1 =",e,f,g
call v3d.sub x,y,z , a,b,c
call v3d.uit e,f,g
print "v3d 1-2 =",e,f,g
call v3d.div x,y,z , 2
call v3d.uit e,f,g
print "v3d 1 / 2 =",e,f,g
call v3d.mul x,y,z , 2
call v3d.uit e,f,g
print "v3d 1 * 2 =",e,f,g
print "len v3d 1 =" ,v3d.len(x,y,z)
print "len v3d 2 =" ,v3d.len(a,b,c)
print "angle v1 v2 =" ,v3d.angle( x,y,z , a,b,c )
call v3d.cross x,y,z , a,b,c
call v3d.uit e,f,g
print "v3d 1 2 cross =",e,f,g
end
'include "@v3d.bas"
@v3d.bas :

Code: Select all

''bluatigro 31 jul 2022
''v3d library
[v3d]
''declaration
global v3d.x,v3d.y,v3d.z
return
sub v3d x , y , z
''constructor
  v3d.x = x
  v3d.y = y
  v3d.z = z
end sub
sub v3d.uit byref x , byref y , byref z
  x = v3d.x
  y = v3d.y
  z = v3d.z
end sub
sub v3d.add x1,y1,z1 , x2,y2,z2
''operator v3d1+v3d2
  call v3d x1+x2 , y1+y2 , z1+z2
end sub
sub v3d.sub x1,y1,z1 , x2,y2,z2
''operator v3d1-v3d2
  call v3d x1-x2 , y1-y2 , z1-z2
end sub
sub v3d.div x1,y1,z1 , d
''operator v3d1/d
  call v3d x1/d , y1/d , z1/d
end sub
sub v3d.mul x1,y1,z1 , d
''operator v3d1*d
  call v3d x1*d , y1*d , z1*d
end sub
function v3d.dot( x1,y1,z1 , x2,y2,z2 )
  v3d.dot = x1*x2 + y1*y2 + z1*z2
end function
function v3d.len( x1,y1,z1 )
'' |v| 
   v3d.len = sqr( v3d.dot( x1,y1,z1 , x1,y1,z1 ) )
end function
function v3d.angle( x1,y1,z1 , x2,y2,z2 )
  l1 = v3d.len( x1,y1,z1 )
  l2 = v3d.len( x2,y2,z2 )
  d = v3d.dot( x1,y1,z1 , x2,y2,z2 )
  v3d.angle = acs( d / ( l1 * l2 ) )
end function
sub v3d.cross x1,y1,z1 , x2,y2,z2
  call v3d y1*z2 - z1*y2 _
         , z1*x2 - x1*z2 _
         , x1*y2 - y2*x2
end sub
bluatigro
Posts: 5
Joined: Sat Apr 14, 2018 6:35 am

Re: library example v3d oop

Post by bluatigro »

i get a function not exsist error
why ?

Code: Select all

gosub [v3d]
global s.x,s.y,s.z,s.d,s.r,s.g,s.b,infinity,stel
s.x=0:s.y=1:s.z=2
s.d=3:s.d2=4
s.r=5:s.g=6:s.b=7
infinity=1e9
stel=0
dim s(100,7)
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global scrnw,srcnh
scrnw=WindowWidth
scrnh=WindowHeight
open "ray" for graphics as #m
  #m "trapclose [quit]"
  stel=0
  call sphere 20,0,10 , 30 , rgb(255,0,0)
  call sphere 0,0,0 , 30 , rgb(0,255,0)
  for x = -50 to 50
    for y = -50 to 50
      call render 0,0,-1000 , x,y,1000 , 1
      call v3d.uit 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"place ";x+scrnw;" ";scrnh-y
      #m"down"
      #m"set ";x+scrnw;" ";scrnh-y  
      #m"up"
    next y
  next x
  notice "ready !!"    
wait
function rgb( r,g,b )
  rgb = r + g * 256 + b * 256 ^ 2
end function
sub render ox,oy,oz , dx,dy,dz , dept
  call v3d 0,0,0 ''background
  if dept < 1 then exit sub
  low = infinity
  sphno = -1
  for i = 0 to stel
    dist = sphere.hit( i , ox,oy,oz , dx,dy,dz )
    if dist < low then
      low = dist
      sphno = i
    end if
  next i
  if sphno = -1 then exit sub
  r = s(sphno,r)
  g = s(sphno,g)
  b = s(sphno,b)
  call v3d r,g,b
end sub
sub sphere
  if stel >= 100 then exit sub
   s(stel,s.x)=x
   s(stel,s.y)=y
   s(stel,s.z)=z
   s(stel,s.d)=d
   s(stel,s.d2)=d*d
   s(stel,s.r)=(int(kl)and 255)/256
   s(stel,s.g)=(int(kl/256)and 255)/256
   s(stel,s.b)=(int(kl/256^2)and 255)/256
   stel=stel+1
  end if
end sub   
function sphere.hit( no , ox,oy,oz , dx,dy,dz )
  call v3d.sub ox,oy,oz,s(no,s.x),s(no,s.y),s(no,s.z)
  call v3d.uit tx,ty,tz
  a = v3d.dot( dx,dy,dz , dx,dy,dz )
  b = 2 * v3d.dot( tx,ty,tz , dx,dy,dz )
  c = v3d.dot( tx,ty,tz , tx,ty,tz ) - s(no,s.d)
  disc = b ^ 2 - 4 * a * c
  uit = infinity
  if disc < 0 then 
    uit = infinity
  else
    e = sqr( disc ) 
    demon = 2 * a
    t = ( -b - e ) / demon
    if t > 1e-12 then
      uit = t
    end if
    
    t = ( -b + e ) / demon
    if t > 1e-12 then 
      uit = t
    end if
  end if
  sphere.hit = uit
end function
'include "@v3d.bas"
guest
Site Admin
Posts: 192
Joined: Tue Apr 03, 2018 1:34 pm

Re: library example v3d oop

Post by guest »

bluatigro wrote: Sun Jul 31, 2022 1:38 pm i get a function not exsist error
why ?
It's because you call sub sphere with parameters:

Code: Select all

  call sphere 20,0,10 , 30 , rgb(255,0,0)
  call sphere 0,0,0 , 30 , rgb(0,255,0)
but you define it with no parameters:

Code: Select all

sub sphere
  if stel >= 100 then exit sub
   s(stel,s.x)=x
   s(stel,s.y)=y
   s(stel,s.z)=z
   s(stel,s.d)=d
   s(stel,s.d2)=d*d
   s(stel,s.r)=(int(kl)and 255)/256
   s(stel,s.g)=(int(kl/256)and 255)/256
   s(stel,s.b)=(int(kl/256^2)and 255)/256
   stel=stel+1
  end if
end sub 
The number of parameters in the call must match the number of parameters in the definition.
bluatigro
Posts: 5
Joined: Sat Apr 14, 2018 6:35 am

Re: library example v3d oop

Post by bluatigro »

thanks for help

error :
i see no sphere

Code: Select all

gosub [v3d]
global s.x,s.y,s.z,s.d,s.r,s.g,s.b,infinity,stel
s.x=0:s.y=1:s.z=2
s.d=3:s.d2=4
s.r=5:s.g=6:s.b=7
infinity=1e9
stel=0
dim s(100,7)
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global scrnw,srcnh
scrnw=WindowWidth
scrnh=WindowHeight
nomainwin
open "ray" for graphics as #m
  #m "trapclose [quit]"
  stel=0
  call sphere 20,0,10 , 30 , rgb(255,0,0)
  call sphere 0,0,0 , 30 , rgb(0,255,0)
  for x = -50 to 50
    for y = -50 to 50
      call render 0,0,-1000 , x,y,1000 , 1
      call v3d.uit 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"place ";x+scrnw/2;" ";scrnh/2-y
      #m"down"
      #m"set ";x+scrnw/2;" ";scrnh/2-y  
      #m"up"
    next y
  next x
  notice "ready !!"    
  scan
wait
[quit]
  close #m
end
function rgb( r,g,b )
  rgb = r + g * 256 + b * 256 ^ 2
end function
sub render ox,oy,oz , dx,dy,dz , dept
  call v3d 0,0,0 ''background
  if dept < 0 then exit sub
  low = infinity
  sphno = -1
  for i = 0 to stel
    dist = sphere.hit( i , ox,oy,oz , dx,dy,dz )
    if dist < low then
      low = dist
      sphno = i
    end if
  next i
  if sphno = -1 then exit sub
  r = s(sphno,r)
  g = s(sphno,g)
  b = s(sphno,b)
  call v3d r,g,b
end sub
sub sphere x,y,z , d , kl
  if stel >= 100 then exit sub
   s(stel,s.x)=x
   s(stel,s.y)=y
   s(stel,s.z)=z
   s(stel,s.d)=d
   s(stel,s.d2)=d*d
   s(stel,s.r)=(int(kl)and 255)/256
   s(stel,s.g)=(int(kl/256)and 255)/256
   s(stel,s.b)=(int(kl/256^2)and 255)/256
   stel=stel+1
  end if
end sub   
function sphere.hit( no , ox,oy,oz , dx,dy,dz )
  call v3d.sub ox,oy,oz,s(no,s.x),s(no,s.y),s(no,s.z)
  call v3d.uit tx,ty,tz
  a = v3d.dot( dx,dy,dz , dx,dy,dz )
  b = 2 * v3d.dot( tx,ty,tz , dx,dy,dz )
  c = v3d.dot( tx,ty,tz , tx,ty,tz ) - s(no,s.d)
  disc = b ^ 2 - 4 * a * c
  uit = infinity
  if disc < 0 then 
    uit = infinity
  else
    e = sqr( disc ) 
    demon = 2 * a
    t = ( -b - e ) / demon
    if t > 1e-12 then
      uit = t
    end if
    
    t = ( -b + e ) / demon
    if t > 1e-12 then 
      uit = t
    end if
  end if
  sphere.hit = uit
end function
'include "@v3d.bas"