lijnen 3d engine

Algemene discussies en vragen omtrent Liberty BASIC programma's. Stuur zoveel mogelijk delen van listings (snippets) in als je hulp wilt.

Moderators: anthonio, Abcott

lijnen 3d engine

Berichtdoor bluatigro » vr nov 15, 2013 1:45 pm

met dit kan je 3d lijn objecten laten draien

helaas is LB n beetje traag
niet teveel lijnen tekenen dus

Code: Selecteer alles
global max
max = 20
dim m( ( max + 5 ) * 4 * 4 + 16 )
global rotx , roty , rotz  , trans , temp , number , pi
trans = max + 1
rotx = max + 2
roty = max + 3
rotz = max + 4
temp = max + 5
pi = atn( 1 ) * 4
global xyz , xzy , yxz , yzx , zxy , zyx
xzy = 1
yxz = 2
yzx = 3
zxy = 4
zyx = 5
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , lastkey$
winx = WindowWidth
winy = WindowHeight
global black , red , green , yellow
global blue , magenta , cyan , white
global pink , gray , orange , purple
black   = rgb( 000 , 000 , 000 )
red     = rgb( 255 , 000 , 000 )
green   = rgb( 000 , 255 , 000 )
yellow  = rgb( 255 , 255 , 000 )
blue    = rgb( 000 , 000 , 255 )
magenta = rgb( 255 , 000 , 255 )
cyan    = rgb( 000 , 255 , 255 )
white   = rgb( 255 , 255 , 255 )
pink    = rgb( 255 , 127 , 127 )
orange  = rgb( 255 , 127 , 000 )
gray    = rgb( 127 , 127 , 127 )
purple  = rgb( 127 , 000 , 127 )
global frame
nomainwin
open "3d matrix test ." for graphics as #m
  #m "trapclose [quit]"
  #m "when characterInput [key]"
  call startmatrix
  timer 40 , [tmr]
wait
[tmr]
  #m "fill black"

  scan

  call free 1 , 0,0,0 , frame,1,1,1 , 0
  call cube 0,0,0 , 100,100,100 , 2 , green
  call link 2 , 120,0,0 , 0,frame,0 , xyz , 1
  call cube 0,0,0 , 20,20,20 , 2 , yellow

  #m "flush"

  frame = frame + 5
wait

[key]
  lastkey$ = right$( Inkey$ , 1 )
wait

[quit]
   close #m
end

''===================================================================
''                    3d engine
''===================================================================

sub link no , x , y , z , xz , yz , xy , ax , p
''set draw matrix : wil efect future drawing

''no : number new matrix
''x,y,z : translation
''xz,yz,xy : rotation in degrees
''ax : sequence of axes
''p : number old matrix

  if no < 1 or no > max then exit sub
  if p < 0 or p > max then exit sub
  if p = no then exit sub
''copy matrix 0 into matrix's
  call copy 0 , rotx
  call copy 0 , roty
  call copy 0 , rotz
  call copy 0 , trans
''create rotation matrix's
  m( in( rotx , 1 , 1 ) ) = cos( rad( yz ) )
  m( in( rotx , 1 , 2 ) ) = 0-sin( rad( yz ) )
  m( in( rotx , 2 , 1 ) ) = sin( rad( yz ) )
  m( in( rotx , 2 , 2 ) ) = cos( rad( yz ) )

  m( in( roty , 0 , 0 ) ) = cos( rad( xz ) )
  m( in( roty , 0 , 2 ) ) = 0-sin( rad( xz ) )
  m( in( roty , 2 , 0 ) ) = sin( rad( xz ) )
  m( in( roty , 2 , 2 ) ) = cos( rad( xz ) )

  m( in( rotz , 0 , 0 ) ) = cos( rad( xy ) )
  m( in( rotz , 0 , 1 ) ) = 0-sin( rad( xy ) )
  m( in( rotz , 1 , 0 ) ) = sin( rad( xy ) )
  m( in( rotz , 1 , 1 ) ) = cos( rad( xy ) )
''create translation matrix
  m( in( trans , 3 , 0 ) ) = x
  m( in( trans , 3 , 1 ) ) = y
  m( in( trans , 3 , 2 ) ) = z
''select axes sequence [ 1 of 6 ] and act on i
  select case ax
    case xyz
      call multiply rotx , roty , temp
      call multiply temp , rotz , no
      call multiply no , trans , temp
      call multiply temp , p , no
    case xzy
      call multiply rotx , rotz , temp
      call multiply temp , roty , no
      call multiply no , trans , temp
      call multiply temp , p , no
    case yxz
      call multiply roty , rotx , temp
      call multiply temp , rotz , no
      call multiply no , trans , temp
      call multiply temp , p , no
    case yzx
      call multiply roty , rotz , temp
      call multiply temp , rotx , no
      call multiply no , trans , temp
      call multiply temp , p , no
    case zxy
      call multiply rotz , rotx , temp
      call multiply temp , roty , no
      call multiply no , trans , temp
      call multiply temp , p , no
    case zyx
      call multiply rotz , roty , temp
      call multiply temp , rotx , no
      call multiply no , trans , temp
      call multiply temp , p , no
    case else
      call multiply rotx , roty , temp
      call multiply temp , rotz , no
      call multiply no , trans , temp
      call multiply temp , p , no
  end select
  number = no
end sub

sub free no , x , y , z , angle , xas , yas , zas , p
''set new matrix : wil afect future drawing

''no : number new matrix
''x,y,z : translation
''angle : rotation in degrees
''xas,yas,zas : vector of rotaion ax
''p : number old matrix

  if no < 1 or no > max then exit sub
  if p < 0 or p > max then exit sub
  if p = no then exit sub
  call copy 0 , rotx
  call copy 0 , trans
  l = sqr( xas ^ 2 + yas ^ 2 + zas ^ 2 )
  if l = 0 then exit sub
  xas = xas / l
  yas = yas / l
  zas = zas / l
  s = sin( rad( angle ) )
  c = cos( rad( angle ) )
  t = 1 - cos( rad( angle ) )
  m( in( rotx , 0 , 0 ) ) = t * xas ^ 2 + c
  m( in( rotx , 0 , 1 ) ) = t * xas * yas - s * zas
  m( in( rotx , 0 , 2 ) ) = t * xas * zas - s * yas
  m( in( trans , 3 , 0 ) ) = x
  m( in( rotx , 1 , 0 ) ) = t * xas * yas + s * zas
  m( in( rotx , 1 , 1 ) ) = t * yas ^ 2 + c
  m( in( rotx , 1 , 2 ) ) = t * yas * zas - s * xas
  m( in( trans , 3 , 1 ) ) = y
  m( in( rotx , 2 , 0 ) ) = t * xas * zas - s * yas
  m( in( rotx , 2 , 1 ) ) = t * yas * zas + s * xas
  m( in( rotx , 2 , 2 ) ) = t * zas ^ 2 + c
  m( in( trans , 3 , 2 ) ) = z
  call multiply rotx , translate , temp
  call multiply temp , p , no
  number = no
end sub

sub copy a , b
''matrix( b ) = matrix( a )
  for i = 0 to 3
    for j = 0 to 3
      m( in( b , i , j ) ) = m( in( a , i , j ) )
    next j
  next i
end sub

sub spot byref x , byref y , byref z
'''lokal coordinates to world coordinates
''x,y,z = matrix( number ) * x,y,z
  no = number
  hx = m( in( no , 0 , 0 ) ) * x _
     + m( in( no , 1 , 0 ) ) * y _
     + m( in( no , 2 , 0 ) ) * z _
     + m( in( no , 3 , 0 ) )
  hy = m( in( no , 0 , 1 ) ) * x _
     + m( in( no , 1 , 1 ) ) * y _
     + m( in( no , 2 , 1 ) ) * z _
     + m( in( no , 3 , 1 ) )
  hz = m( in( no , 0 , 2 ) ) * x _
     + m( in( no , 1 , 2 ) ) * y _
     + m( in( no , 2 , 2 ) ) * z _
     + m( in( no , 3 , 2 ) )
  x = hx
  y = hy
  z = hz
end sub

sub multiply a , b , c
''matrix( c ) = matrix( a ) * matrix( b )
  for i = 0 to 3
    for j = 0 to 3
      m( in( c , i , j ) ) = 0
      for k = 0 to 3
        m( in( c , i , j ) ) = m( in( c , i , j ) ) _
        + m( in( a , i , k ) ) * m( in( b , k , j ) )
      next k
    next j
  next i
end sub

sub startmatrix
''set startmatrix to unity
  for x = 0 to 3
    for y = 0 to 3
      m( in( 0,x,y ) ) = 0
    next y
    m( in( 0,x,x ) ) = 1
  next x
end sub

function in( no , x , y )
''LB4 has no 3d array's
''so i simulate them
  in = x + y * 4 + no * 16
end function

function rad( deg )
''from degrees to radians
  rad = deg * pi / 180
end function

''=======================================================================
''                            graphics
''=======================================================================

sub tree no , l , m , kl
  a = l/30
  if a < 1 then a = 1
  call lijn 0,0,0 , 0,l,0 , a , kl
  if l > m then
    call link no+1 , 0,l,0 , 90,0,60 , xyz , no
    call tree no+1 , l*.7 , m , kl
    call link no+1 , 0,l,0 , -90,0,60 , xyz , no
    call tree no+1 , l*.7 , m , kl
  end if
end sub

sub cube mx , my , mz , dx , dy , dz , dik , kl
  call lijn mx - dx , my - dy , mz - dz _
     , mx + dx , my - dy , mz - dz , dik , kl
  call lijn mx - dx , my + dy , mz - dz _
     , mx + dx , my + dy , mz - dz , dik , kl
  call lijn mx - dx , my - dy , mz + dz _
     , mx + dx , my - dy , mz + dz , dik , kl
  call lijn mx - dx , my + dy , mz + dz _
     , mx + dx , my + dy , mz + dz , dik , kl
  call lijn mx - dx , my - dy , mz - dz _
     , mx - dx , my + dy , mz - dz , dik , kl
  call lijn mx + dx , my - dy , mz - dz _
     , mx + dx , my + dy , mz - dz , dik , kl
  call lijn mx - dx , my - dy , mz + dz _
     , mx - dx , my + dy , mz + dz , dik , kl
  call lijn mx + dx , my - dy , mz + dz _
     , mx + dx , my + dy , mz + dz , dik , kl
  call lijn mx - dx , my - dy , mz - dz _
     , mx - dx , my - dy , mz + dz , dik , kl
  call lijn mx + dx , my - dy , mz - dz _
     , mx + dx , my - dy , mz + dz , dik , kl
  call lijn mx - dx , my + dy , mz - dz _
     , mx - dx , my + dy , mz + dz , dik , kl
  call lijn mx + dx , my + dy , mz - dz _
     , mx + dx , my + dy , mz + dz , dik , kl
end sub

sub lijn x1 , y1 , z1 , x2 , y2 , z2 , dik , kl
   r = int( kl ) and 255
   g = int( kl / 256 ) and 255
   b = int( kl / 256 / 256 ) and 255
   dx = WindowWidth
   dy = WindowHeight
   call spot x1 , y1 , z1
   call spot x2 , y2 , z2
   ax = dx / 2 + x1 / ( z1 + 1000 ) * 1000
   ay = dy / 2 - y1 / ( z1 + 1000 ) * 1000
   bx = dx / 2 + x2 / ( z2 + 1000 ) * 1000
   by = dy / 2 - y2 / ( z2 + 1000 ) * 1000
   #m "size "; dik
   #m "color "; r ;" "; g ;" "; b
   #m "down"
   #m "line "; ax ;" "; ay ;" "; bx ;" "; by
   #m "up"
end sub

sub bol x , y , z , d , dik , kl
  call spot x , y , z
''  d = d * cam( 6 )
  r = int( kl and 255 )
  g = int( kl / 256 ) and 255
  b = int( kl / 256 / 256 ) and 255
  #m "color "; r ;" "; g ;" "; b
  d = d / ( z + 1000 ) * 1000
  dx = WindowWidth
  dy = WindowHeight
  x = dx / 2 + x / ( z + 1000 ) * 1000
  y = dy / 2 - y / ( z + 1000 ) * 1000
  #m "go "; x ;" "; y
  #m "down"
  #m "circle "; d
  #m "up"
end sub

function rainbow( x )
  r = sin( rad( x ) ) * 127 + 128
  g = sin( rad( x - 120 ) ) * 127 + 128
  b = sin( rad( x + 120 ) ) * 127 + 128
  rainbow = rgb( r , g , b )
end function

function rgb( r , g , b )
  r = r and 255
  g = g and 255
  b = b and 255
  rgb = r + g * 256 + b * 256 * 256
end function

bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Keer terug naar Liberty BASIC Code

Wie is er online

Gebruikers op dit forum: Geen geregistreerde gebruikers. en 1 gast

cron