3d polygon sprite maker

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

Moderators: anthonio, Abcott

3d polygon sprite maker

Berichtdoor bluatigro » ma jan 06, 2014 11:44 am

dit is n programma om 3d polygon sprites te maken
t maakt op t ogenblik n roterende kubus

this is code whats creating a rotating cube sprite

Code: Selecteer alles
''bluatigro 5-jan-2014
''3d poly sprite maker
global mmax
mmax = 20
dim m( ( mmax + 5 ) * 4 * 4 + 16 )
dim skx( 64 ) , sky( 64 ) , skz( 64 )
dim px( 256 ) , py( 256 ) , pz( 256 )
dim t( 1000 ,  15 )
global tx1,ty1,tz1,tx2,ty2,tz2,tx3,ty3,tz3,txz,tyz,tzz,tkl
global txn,tyn,tzn
global tritel
tx1 = 0
ty1 = 1
tz1 = 2
tx2 = 3
ty2 = 4
tz2 = 5
tx3 = 6
ty3 = 7
tz3 = 8
txz = 9
tyz = 10
tzz = 11
tkl = 12
txn = 13
tyn = 14
tzn = 15
global rotx , roty , rotz  , trans , temp , number , pi
trans = mmax + 1
rotx = mmax + 2
roty = mmax + 3
rotz = mmax + 4
temp = mmax + 5
pi = atn( 1 ) * 4
global xyz , xzy , yxz , yzx , zxy , zyx
xzy = 1
yxz = 2
yzx = 3
zxy = 4
zyx = 5
nomainwin
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global hdc , hand , winx , winy , pi , sprx , spry
winx = WindowWidth
winy = WindowHeight
global bmx , bmy , bmz , bdx , bdy , bdz
pi = atn( 1 ) * 4
global black , red , green , yellow
global blue , magenta , cyan , white
global pink , purple , gray , orange
black   = rgb(   0 ,   0 ,   0 )
red     = rgb( 255 ,   0 ,   0 )
green   = rgb(   0 , 255 ,   0 )
yellow  = rgb( 255 , 255 ,   0 )
blue    = rgb(   0 ,   0 , 255 )
magenta = rgb( 255 ,   0 , 255 )
cyan    = rgb(   0 , 255 , 255 )
white   = rgb( 255 , 255 , 255 )
pink    = rgb( 255 , 127 , 127 )
orange  = rgb( 255 , 127 ,   0 )
gray    = rgb( 127 , 127 , 127 )
purple  = rgb( 127 ,   0 , 127 )
global frame
open "3D polygon world ." for graphics as #m
  #m "trapclose [quit]"
  #m "fill black"

  hand = hwnd( #m )  'window handle

'get device context for window:
  calldll #user32 , _
    "GetDC" , _
    hand as long , _ 'window handle
    hdc as long 'returns handle to device context

  STRUCT PolyPoints , _
    x1 as long , _
    y1 as long , _
    x2 as long , _
    y2 as long , _
    x3 as long , _
    y3 as long

  for frame = 0 to 31
  scan
  call startmatrix
  tritel = 0

''=============== begin world code

  call clear 200 , 200
  call link 1 , 0,0,0 , frame*360/32,0,0 , xyz , 0
  call box 0,0,0 , 70,70,70
  call cube red,cyan,green,magenta,blue,yellow

''=============== end world code

  sort t() , 0 , tritel - 1 , tzz

  for i = tritel - 1 to 0 step -1

    x1 = sprx / 2 + t( i , tx1 )
    y1 = spry / 2 - t( i , ty1 ) - t( i , tz1 ) / 4
    x2 = sprx / 2 + t( i , tx2 )
    y2 = spry / 2 - t( i , ty2 ) - t( i , tz2 ) / 4
    x3 = sprx / 2 + t( i , tx3 )
    y3 = spry / 2 - t( i , ty3 ) - t( i , tz3 ) / 4
''    a = getangle(t(i,txn),t(i,tyn),t(i,tzn),0,1,0)
    kl = t( i , tkl )
''    kl= mix( kl , cos( a ) / 2 +.5 , black )

    call triangle x1,y1 , x2,y2 , x3,y3 , black
    call triangle x1,y1+spry , x2,y2+spry , x3,y3+spry , kl
  next i

  ''======================================================
  ''sprite save code

  call savesprite "vr-cube" + nr$( frame , 2 )
  next frame
  notice "ready"
wait

''  3d engine

function pend( fase , amp )
  pend = sin( rad( fase ) ) * amp
end function

sub skelet lim , x , y , z
''for animating avatar lim's
  skx( lim ) = x
  sky( lim ) = y
  skz( lim ) = z
end sub

sub child no , x , y , z , lim , ax , p
''for creating lim's of a avatar
  if lim < 0 or lim > 64 then exit sub
  call link no , x , y , z _
  , sky( lim ) , skx( lim ) , skz( lim ) , ax , p
end sub

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 > mmax then exit sub
  if p < 0 or p > mmax 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 > mmax then exit sub
  if p < 0 or p > mmax 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

function getangle( a , b , c , d , e , f )
  dot = a * d + b * e + c * f
  len1 = sqr( a ^ 2 + b ^ 2 + c ^ 2 )
  len2 = sqr( d ^ 2 + e ^ 2 + f ^ 2 )
  getangle = acs( dot / ( len1 * len2 ) )
end function

''sprite stuf

sub savesprite spr$
  #m "getbmp sprite 0 0 " ; sprx ; " " ; spry * 2
  bmpsave "sprite" , DefaultDir$ + "\BMP\" ; spr$ ; ".bmp"
end sub

function nr$( no , m )
  nr$ = right$( "00000000" ; no , m )
end function

sub clear x , y
  #m "cls"
  #m "color white"
  #m "backcolor white"
  #m "goto 0 0"
  #m "down"
  #m "boxfilled " ; x ; " " ; y
  #m "up"
  #m "goto 0 " ; y
  #m "down"
  #m "color black"
  #m "backcolor black"
  #m "boxfilled " ; x ; " " ; 2 * y
  #m "up"
  sprx = x
  spry = y
end sub

''graphics

function rgb( r , g , b )
''create a colorobject
  rgb = r + g * 256 + b * 256 ^ 2
end function

function mix( kl1 , f , kl2 )
  r1 = int( kl1 and 255 )
  g1 = int( kl1 / 256 ) and 255
  b1 = int( kl1 / 256 / 256 ) and 255
  r2 = int( kl2 and 255 )
  g2 = int( kl2 / 256 ) and 255
  b2 = int( kl2 / 256 / 256 ) and 255
  dr = r2 - r1
  dg = g2 - g1
  db = b2 - b1
  dr = dr * f
  dg = dg * f
  db = db * f
  r = r1 + dr
  g = g1 + dg
  b = b1 + db
  mix = rgb( r , g , b )
end function

sub point no , x , y , z
''create a point in the swarm
  call spot x , y , z
  px( no ) = x
  py( no ) = y
  pz( no ) = z
end sub

sub tri p1 , p2 , p3 , kl
''add a triangle to the wold
  if tritel >= 1000 then exit sub

  t( tritel , tx1 ) = px( p1 )
  t( tritel , ty1 ) = py( p1 )
  t( tritel , tz1 ) = pz( p1 )

  t( tritel , tx2 ) = px( p2 )
  t( tritel , ty2 ) = py( p2 )
  t( tritel , tz2 ) = pz( p2 )

  t( tritel , tx3 ) = px( p3 )
  t( tritel , ty3 ) = py( p3 )
  t( tritel , tz3 ) = pz( p3 )

  t( tritel , txz ) = (px(p1)+px(p2)+px(p3))/3
  t( tritel , tyz ) = (py(p1)+py(p2)+py(p3))/3
  t( tritel , tzz ) = (pz(p1)+pz(p2)+pz(p3))/3

  t( tritel , tkl ) = kl

  x1 = px( p2 ) - px( p1 )
  y1 = py( p2 ) - py( p1 )
  z1 = pz( p2 ) - pz( p1 )
  x2 = px( p3 ) - px( p1 )
  y2 = py( p3 ) - py( p1 )
  z2 = pz( p3 ) - pz( p1 )

  t( tritel , txn ) = y1 * z2 - y2 * z1
  t( tritel , tyn ) = z1 * x2 - z2 * x1
  t( tritel , tzn ) = x1 * y2 - x2 * y1

  tritel = tritel + 1
end sub

sub quad p1 , p2 , p3 , p4 , kl
''add a quad angle to the world
  call tri p1 , p2 , p4 , kl
  call tri p2 , p3 , p4 , kl
end sub

sub box mx , my , mz , dx , dy , dz
''bonding box for meshes
  bmx = mx
  bmy = my
  bmz = mz
  bdx = dx
  bdy = dy
  bdz = dz
end sub

sub cube left , right , bot , top , front , back
''create a cube mesh whit 6 colors
  call point 0 , bmx + bdx , bmy + bdy , bmz + bdz
  call point 1 , bmx + bdx , bmy + bdy , bmz - bdz
  call point 2 , bmx + bdx , bmy - bdy , bmz + bdz
  call point 3 , bmx + bdx , bmy - bdy , bmz - bdz
  call point 4 , bmx - bdx , bmy + bdy , bmz + bdz
  call point 5 , bmx - bdx , bmy + bdy , bmz - bdz
  call point 6 , bmx - bdx , bmy - bdy , bmz + bdz
  call point 7 , bmx - bdx , bmy - bdy , bmz - bdz

  call quad 7 , 6 , 4 , 5 , left
  call quad 0 , 1 , 3 , 2 , right
  call quad 7 , 6 , 2 , 3 , bot
  call quad 0 , 1 , 5 , 4 , top
  call quad 7 , 5 , 1 , 3 , front
  call quad 0 , 2 , 6 , 4 , back
end sub

sub triangle x1 , y1 , x2 , y2 , x3 , y3 , kl
''draw a triangle on the window
    PolyPoints.x1.struct = x1
    PolyPoints.y1.struct = y1
    PolyPoints.x2.struct = x2
    PolyPoints.y2.struct = y2
    PolyPoints.x3.struct = x3
    PolyPoints.y3.struct = y3
    r = int( kl and 255 )
    g = int( kl / 256 ) and 255
    b = int( kl / 256 ^ 2 ) and 255
    #m "color ";r;" ";g;" ";b
    #m "backcolor ";r;" ";g;" ";b
  calldll #gdi32 , _
    "Polygon" , _
    hdc as long , _ 'device context of window or control
    PolyPoints as struct , _'array of points
    3 as long , _      'number of x,y pairs in array
    result as boolean
end sub
[quit]
  close #m
  calldll #user32 , _
    "ReleaseDC" , _
    hand as long , _     'window handle
    hdc as long , _   'device context
    ret as long
end
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: 3d polygon sprite maker

Berichtdoor bluatigro » ma jan 06, 2014 11:49 am

deze test werkt
Code: Selecteer alles
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight

for i = 0 to 31
  loadbmp "bmp" ; i , DefaultDir$ _
  + "\BMP\vr-cube" + nr$( i , 2 ) + ".bmp"
  anim$ = anim$ + "bmp" ; i ; " "
next i
nomainwin
open "sprite test" for graphics as #m
  #m "trapclose [quit]"
  #m "addsprite spr " ; anim$
  #m "cyclesprite spr 1"
  timer 40 , [tmr]
wait
[tmr]
  #m "drawsprites"
wait
function nr$( no , max )
  nr$ = right$( "00000000" + str$( no ) , max )
end function
[quit]
  close #m
end

deze niet , waarom ?
Code: Selecteer alles
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy
winx = WindowWidth
winy = WindowHeight
dim t( 9 * 9 , 5 )
global tx , ty , tz , tangle , ttype , twal , tempty
tx = 0
ty = 1
tz = 2
tangle = 3
ttype = 4
twal = 1
for i = 0 to 31
  loadbmp "wal" ; i , DefaultDir$ _
  + "\BMP\vr-cube" + nr$( i , 2 ) + ".bmp"
  anim$ = anim$ + "wal" ; i ; " "
next i
loadbmp "empty" , DefaultDir$ + "\BMP\empty.bmp"
anim$ = anim$ + "empty"
nomainwin
open "vr sprite test" for graphics as #m
  #m "trapclose [quit]"
  for x = 0 to 9
    for y = 0 to 9
      #m "addsprite spr" ; in( x , y ) ; " " ;  anim$
    next y
  next x
  y = 0
  while a$ <> "end maze"
    read a$
    for x = 1 to len( a$ )
      select case mid$( a$ , x , 1 )
        case "m"
          t( in( x , y ) , ttype ) = twal
        case else
          t( in( x , y ) , ttype ) = tempty
      end select
    next x
  wend
  data "mmmmmmmmm"
  data "m       m"
  data "m mm mm m"
  data "m m   m m"
  data "m   m   m"
  data "m m   m m"
  data "m mm mm m"
  data "m       m"
  data "mmmmmmmmm"
  data "end maze"

  timer 40 , [tmr]
wait
function in( x , y )
  in = x + y * 9
end function
[tmr]
  for x = 0 to 9
    for y = 0 to 9
      a = x * 100 - 450
      b = y * 100 - 450
      call rotate a , b , frame * 360 / 32
      t( in( x , y ) , tx ) = a
      t( in( x , y ) , tz ) = b
      t( in( x , y ) , tangle ) = frame
    next y
  next x

  sort t() , 0 , 9 * 9 , tz

  for i = 0 to 9 * 9
    #m "spritexy spr" ; i _
    ; " " ; winx / 2 + t( i , tx ) _
    ; " " ; winy / 2 - t( i , ty ) - t( i , tz ) / 4
    if t( i , ttype ) = twal then
      #m "spriteimage spr" ; i ; " wal" ; t( i , tangle )
    else
      #m "spriteimage spr" ; i ; " empty"
    end if
    #m "spritetofrond spr" ; i
  next i

  #m "drawsprites"
wait
function rad( deg )
  rad = deg * pi / 180
end function
sub rotate byref k , byref l , deg
  s = sin( rad( deg ) )
  c = cos( rad( deg ) )
  hk = k * c - l * s
  hl = k * s + l * c
  k = hk
  l = hl
end sub
function nr$( no , max )
  nr$ = right$( "00000000" + str$( no ) , max )
end function
[quit]
  close #m
end
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