RASTERIZATON TRY

General FreeBASIC programming questions.
Post Reply
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

RASTERIZATON TRY

Post by bluatigro »

this is a try at a rasterizor

i get a image
but it is a bit dark

main

Code: Select all

'' bluatigro 8 aug 2021
'' raserizor

#include "t3s.bas"
#include "matix.bas"

dim shared as integer scrnw , scrnh
screen 20 , 32 , , fb.gfx_fullscreen
screeninfo scrnw , scrnh

dim shared as t3s pnt( 255 )
dim shared as t3s light = t3s( 1 , 1 , 1 )
sub setpoint( no as integer _
  , x as single , y as single , z as single )
  spot x , y , z
  pnt( no ) = t3s( x , y , z )
end sub
dim shared as integer tritel = 0
type ttriangle
  dim as t3s p( 2 ) , led , normal , kleur
  declare sub fill( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  declare sub show()
end type 
sub ttriangle.fill( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  p( 0 ) = pnt( p1 )
  p( 1 ) = pnt( p2 )
  p( 2 ) = pnt( p3 )
  led = ( p( 0 ) + p( 1 ) + p( 2 ) ) / 3
  normal = cross( p( 0 ) - led , p( 1 ) - led )
  kleur = kl
end sub
sub ttriangle.show()
  dim as single x1,y1,x2,y2,x3,y3,a,b,i,angle
  
  x1 = scrnw / 2 + p( 0 ).x / ( p( 0 ).z + 1000 ) * 1000
  y1 = scrnh / 2 - p( 0 ).y / ( p( 0 ).z + 1000 ) * 1000
  x2 = scrnw / 2 + p( 1 ).x / ( p( 1 ).z + 1000 ) * 1000
  y2 = scrnh / 2 - p( 1 ).y / ( p( 1 ).z + 1000 ) * 1000
  x3 = scrnw / 2 + p( 2 ).x / ( p( 2 ).z + 1000 ) * 1000
  y3 = scrnh / 2 - p( 2 ).y / ( p( 2 ).z + 1000 ) * 1000
  
  if y1 = y2 then y1 = y1 - 1e-7
  if y2 = y3 then y3 = y3 + 1e-7
  if y1 > y3 then
    swap y1 , y3
    swap x1 , x3
  end if
  if y1 > y2 then
    swap y1 , y2
    swap x1 , x2
  end if
  if y2 > y3 then
    swap y2 , y3
    swap x2 , x3
  end if
  for i = y1 to y3
    a = x1 + ( x3 - x1 ) * (i-y1) / ( y3 - y1 )
    if i < y2 then
      b = x1 + ( x2 - x1 ) * (i-y1) / ( y2 - y1 )
    else
      b = x2 + ( x3 - x2 ) * (i-y2) / ( y3 - y2 )
    end if
    angle = getangle( light , normal )
    kleur = kleur * ( cos( angle ) / 4 + .75 )
    line ( a , i ) - ( b , i ) , kleur.toColor()
    
  next i
end sub
dim shared as ttriangle triangles( 1000 )
sub tri( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  triangles( tritel ).fill p1 , p2 , p3 , kl
  tritel += 1
end sub
sub quad(p1 as integer,p2 as integer,p3 as integer _
  ,p4 as integer,kl as t3s)
  tri p1,p2,p3 , kl
  tri p1,p3,p4 , kl
end sub
dim shared as single box(5)
sub setbox( x as single , y as single , z as single _
  , dx as single , dy as single , dz as single )
  box(0)=x
  box(1)=y
  box(2)=z
  box(3)=dx
  box(4)=dy
  box(5)=dz
end sub
sub sphere( a as integer , b as integer , kl as t3s )
  dim as single i , j , i2 , j2 
  dim as single x , y , z
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 6
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI / 2 to PI / 2 - pi / b * 2 step PI / b * 2 
      j2 = j + PI / b * 2 

      x = sin( i ) * cos( j ) * box(3)+box(0)
      y = sin( j )            * box(4)+box(1)
      z = cos( i ) * cos( j ) * box(5)+box(2)
      setpoint 0 , x,y,z

      x = sin( i2 ) * cos( j ) * box(3)+box(0)
      y = sin( j )             * box(4)+box(1)
      z = cos( i2 ) * cos( j ) * box(5)+box(2)

      setpoint 1 ,x,y,z
      
      x = sin( i2 ) * cos( j2 ) * box(3)+box(0)
      y = sin( j2 )             * box(4)+box(1)
      z = cos( i2 ) * cos( j2 ) * box(5)+box(2)
      
      setpoint 2 , x,y,z
      
      x = sin( i ) * cos( j2 ) * box(3)+box(0)
      y = sin( j2 )            * box(4)+box(1)
      z = cos( i ) * cos( j2 ) * box(5)+box(2)

      setpoint 3 , x,y,z
      
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

dim as single angle
dim as integer hi , lo
''do
  cls
  paint (1,1),rgb(0,100,100)
  tritel = 0
  camera 0,0,0 , 0,0,0 , 1
  link 1 , -300,0,0 , angle,angle,0 , xyz , 0
  setpoint 0 , 100 , 100 , 100
  setpoint 1 , -100 , 100 , -100
  setpoint 2 , -100 , -100 , 100
  setpoint 3 , 100 , -100 , -100
  tri 0 , 1 , 2 , red
  tri 0 , 2 , 3 , yellow
  tri 0 , 1 , 3 , blue
  tri 1 , 2 , 3 , green
  link 1 , 200,0,0 , angle,angle,0 , xyz , 0
  setbox 0,0,0 , 100,100,100
  sphere 12 , 12 , yellow
  for hi = 1 to tritel
    for lo = 0 to hi - 1
      if triangles(lo).led.z > triangles(hi).led.z then
        swap triangles(lo) , triangles(hi)
      end if
    next lo
  next hi
  for lo = 0 to tritel - 1
    triangles(lo).show
  next lo
  angle += 5
  sleep 
''loop while inkey = ""
  


t3s.bas

Code: Select all

'''bluatigro 7 aug 2021
''t3s : 3dvector class

type t3s
  dim as single x,y,z
  declare constructor()
  declare constructor( a as single _
  , b as single , c as single )
  declare function toColor() as ulong
end type
constructor t3s()
end constructor
constructor t3s( a as single , b as single , c as single )
  x = a
  y = b
  z = c
end constructor
function t3s.toColor() as ulong
  dim as integer r , g , b
  r = cint( x * 255 ) and 255
  g = cint( y * 255 ) and 255
  b = cint( z * 255 ) and 255
  return rgb( r , g , b )
end function
operator +( a as t3s , b as t3s ) as t3s
  return t3s( a.x + b.x , a.y + b.y , a.z + b.z )
end operator 
operator -( a as t3s , b as t3s ) as t3s
  return t3s( a.x - b.x , a.y - b.y , a.z - b.z )
end operator 
operator /( a as t3s , b as single ) as t3s
  return t3s( a.x / b , a.y / b , a.z / b )
end operator 
operator *( a as t3s , b as single ) as t3s
  return t3s( a.x * b , a.y * b , a.z * b )
end operator 
function dot( a as t3s , b as t3s ) as single
  return a.x * b.x + a.y * b.y + a.z * b.z
end function
function lenght( a as t3s ) as single
  return sqr( dot( a , a ) )
end function
function getangle( a as t3s , b as t3s ) as single
  dim as single la , lb , d
  d = dot( a , b )
  la = lenght( a )
  lb = lenght( b )
  return acos( d / ( la * lb ) )
end function
function cross( a as t3s , b as t3s ) as t3s
  return t3s( 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 function
dim shared as t3s black = t3s( 0 , 0 , 0 )
dim shared as t3s red = t3s( 1 , 0 , 0 )
dim shared as t3s green = t3s( 0 , 1 , 0 )
dim shared as t3s yellow = t3s( 1 , 1 , 0 )
dim shared as t3s blue = t3s( 0 , 0 , 1 )
dim shared as t3s magenta = t3s( 1 , 0 , 1 )
dim shared as t3s cyan = t3s( 0 , 1 , 1 )
dim shared as t3s white = t3s( 1 , 1 , 1 )
matrixx.bas

Code: Select all

''bluatigro 30-dec-2013
''graphics lib : 3d engine

#ifndef BASIC3D_H
#define BASIC3D_H

#include "math.bas"

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
  Using FB '' Scan code constants are stored in the FB namespace in lang FB
#endif

''create matrix type to hold position & orentation
type matrix
  dim as double m( 3 , 3 )
end type
''create multyplycation operator for matrix
operator * ( a as matrix , b as matrix ) as matrix
  dim as integer i , j , k
  dim uit as matrix
  for i = 0 to 3
    for j = 0 to 3
''      uit.m( i , j ) = 0
      for k = 0 to 3
''        uit.m( i , j ) += a.m( i , k ) * b.m( k , j )
        uit.m( j , k ) += a.m( j , i ) * b.m( i , k )
      next k
    next j
  next i
  return uit
end operator 
function inverse( q as matrix ) as matrix
  dim as single d
  dim as matrix uit
  
  d = q.m(0,0)*q.m(1,1)*q.m(2,2) _
    - q.m(0,0)*q.m(2,1)*q.m(1,2) _
    + q.m(1,0)*q.m(2,1)*q.m(0,2) _
    - q.m(1,0)*q.m(0,1)*q.m(2,2) _
    + q.m(2,0)*q.m(0,1)*q.m(1,2) _
    - q.m(2,0)*q.m(1,1)*q.m(0,2)
    
  uit.m(0,0)=(q.m(1,1)*q.m(2,2)-q.m(1,2)*q.m(2,1))/d
  uit.m(1,0)=(q.m(0,1)*q.m(2,2)-q.m(0,2)*q.m(2,1))/d
  uit.m(2,0)=(q.m(0,1)*q.m(1,2)-q.m(0,2)*q.m(1,1))/d
  uit.m(0,1)=(q.m(1,0)*q.m(2,2)-q.m(1,2)*q.m(2,0))/d
  uit.m(1,1)=(q.m(0,0)*q.m(2,2)-q.m(0,2)*q.m(2,0))/d
  uit.m(2,1)=(q.m(0,0)*q.m(1,2)-q.m(0,2)*q.m(1,0))/d
  uit.m(0,2)=(q.m(1,0)*q.m(2,1)-q.m(1,1)*q.m(2,0))/d
  uit.m(1,2)=(q.m(0,0)*q.m(2,1)-q.m(0,1)*q.m(2,0))/d
  uit.m(2,2)=(q.m(0,0)*q.m(1,1)-q.m(0,1)*q.m(1,2))/d
  
  return uit
  
end function
''create array of matrix's
dim shared as matrix v( 20 )
''create unity matrix
v( 0 ).m( 0 , 0 ) = 1
v( 0 ).m( 1 , 1 ) = 1
v( 0 ).m( 2 , 2 ) = 1
v( 0 ).m( 3 , 3 ) = 1
''create array to hold skeletal angles
dim shared as single sk( 64 , 2 )
''create some stuf for 3D engine
declare sub link( no as integer , x as single , y as single , z as single _
, pan as single , tilt as single , rol as single , ax as integer , p as integer )
declare sub child( no as integer , x as single , y as single , z as single _
, lim as integer , ax as integer , p as integer )
declare sub spot( byref x as single , byref y as single , byref z as single )
declare sub rotate( byref k as single , byref l as single , deg as single )
dim shared as integer number
dim shared as single cam( 6 )
declare sub camara( x as single , y as single , z as single _
, pan as single , tilt as single , rol as single , zoom as single )
declare function pend( f as single , a as single ) as single
declare sub skelet( no as integer , x as single , y as single , z as single )

const as integer xyz = 0
const as integer xzy = 1
const as integer yxz = 2
const as integer yzx = 3
const as integer zxy = 4
const as integer zyx = 5




function pend( f as single , a as single ) as single
''pendular motion for animation
''looks verry natural
  return sin( rad( f ) ) * a
end function
sub skelet( lim as integer , x as single , y as single , z as single )
''set angles of skeletal lim
''for animated avatars
  if lim < 0 or lim > 64 then exit sub
  sk( lim , 0 ) = x
  sk( lim , 1 ) = y
  sk( lim , 2 ) = z
end sub
sub camera( x as single , y as single , z as single _
, pan as single , tilt as single , rol as single , zoom as single )
''set look from point & look angles
  cam( 0 ) = x
  cam( 1 ) = y
  cam( 2 ) = z
  cam( 3 ) = pan
  cam( 4 ) = tilt
  cam( 5 ) = rol
  cam( 6 ) = zoom
end sub
sub moveCamera( x as single , y as single , z as single _
  , pan as single )
  rotate x , z , cam( 3 )
  cam( 0 ) += x
  cam( 1 ) += y
  cam( 2 ) += z
  cam( 3 ) = ( cam( 3 ) + pan ) mod 360
end sub
Sub link( no As Integer, x As single, y As single, z As single, pan As single, tilt As single, rol As single, ax As Integer, p As Integer )
''set curent matrix wil afect folowing drawing comands
   If no < 1 Or no > 20 Then Exit Sub
   If p < 0 Or p > 20 Then Exit Sub
   If p = no Then Exit Sub
   ''create some lokal matrix's and fill them
   Dim As matrix mp, rotx, roty, rotz, translate
   mp = v( p )
   rotx = v( 0 )
   roty = v( 0 )
   rotz = v( 0 )
   translate = v( 0 )
   rotz.m( 0, 0 ) = Cos( rad( rol ))
   rotz.m( 0, 1 ) = -Sin( rad( rol ))
   rotz.m( 1, 0 ) = Sin( rad( rol ))
   rotz.m( 1, 1 ) = Cos( rad( rol ))
   roty.m( 0, 0 ) = Cos( rad( pan ))
   roty.m( 0, 2 ) = -Sin( rad( pan ))
   roty.m( 2, 0 ) = Sin( rad( pan ))
   roty.m( 2, 2 ) = Cos( rad( pan ))
   rotx.m( 1, 1 ) = Cos( rad( tilt ))
   rotx.m( 1, 2 ) = -Sin( rad( tilt ))
   rotx.m( 2, 1 ) = Sin( rad( tilt ))
   rotx.m( 2, 2 ) = Cos( rad( tilt ))
   translate.m( 3, 0 ) = x
   translate.m( 3, 1 ) = y
   translate.m( 3, 2 ) = z
   ''angles can permutate 6 ways
   Select Case ax
      Case xyz
         v( no ) = rotx * roty * rotz * translate * mp
      Case xzy
         v( no ) = rotx * rotz * roty * translate * mp
      Case yxz
         v( no ) = roty * rotx * rotz * translate * mp
      Case yzx
         v( no ) = roty * rotz * rotx * translate * mp
      Case zxy
         v( no ) = rotz * rotx * roty * translate * mp
      Case zyx
         v( no ) = rotz * roty * rotx * translate * mp
      Case Else
   End Select
   number = no
End Sub
sub child( no as integer , x as single , y as single , z as single _
, lim as integer , ax as integer , p as integer )
''set curent matrix for lim of animated avatar
''wil efect folowing drawings
  if lim < 0 or lim > 64 then exit sub
  link no , x , y , z _
  , sk( lim , 1 ) , sk( lim , 0 ) , sk( lim , 2 ), ax , p
end sub
sub spot( byref x as single , byref y as single , byref z as single )
''calulate world coordinates from lokal coordinates
''using curent matrix
  dim as single hx , hy , hz
  dim as integer i
  ''use curent matrix
  i = number
  hx = x * v( i ).m( 0 , 0 ) + y * v( i ).m( 1 , 0 ) _
  + z * v( i ).m( 2 , 0 ) + v( i ).m( 3 , 0 )
  hy = x * v( i ).m( 0 , 1 ) + y * v( i ).m( 1 , 1 ) _
  + z * v( i ).m( 2 , 1 ) + v( i ).m( 3 , 1 )
  hz = x * v( i ).m( 0 , 2 ) + y * v( i ).m( 1 , 2 ) _
  + z * v( i ).m( 2 , 2 ) + v( i ).m( 3 , 2 )
  x = hx 
  y = hy
  z = hz
  ''use camara matrix
  x += - cam( 0 )
  y += - cam( 1 )
  z += - cam( 2 )
  x *= cam( 6 )
  y *= cam( 6 )
  z *= cam( 6 )
  rotate x , z , -cam( 3 )
  rotate y , z , -cam( 4 )
  rotate x , y , -cam( 5 )
end sub
sub lokal( byref x as single , byref y as single _
  , byref z as single , q as matrix )
  dim as single hx,hy,hz
  hx = q.m(0,0)*x+q.m(1,0)*y+q.m(2,0)*z
  hy = q.m(0,1)*x+q.m(1,1)*y+q.m(2,1)*z
  hz = q.m(0,2)*x+q.m(1,2)*y+q.m(2,2)*z
  x = hx
  y = hy
  z = hz
end sub
sub rotate( byref k as single , byref l as single , deg as single )
''help sub for rotating coordinates
 dim as single hk , hl , s , c
  s = sin( rad( deg ) )
  c = cos( rad( deg ) )
  hk = k * c - l * s
  hl = k * s + l * c
  k = hk
  l = hl
end sub

#endif

math.bas

Code: Select all

''bluatigro 6-nov-2013
''game lib : math helper functions + connstands

#ifndef MATH_H
#define MATH_H

const as single pi = atn( 1 ) * 4
const as single golden_ratio = ( sqr( 5 ) - 1 ) / 2

function rad( x as single ) as single
''help function degrees to radians 
  return x * pi / 180
end function

function range( l as single , h as single ) as single
  return rnd * ( h - l ) + l
end function

function nr( no as integer , m as integer ) as string
  return right( "0000000000" + str( no ) , m )
end function

#endif

bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: RASTERIZATON TRY

Post by bluatigro »

update :
supersphere , torus and banana

Code: Select all

'' bluatigro 8 aug 2021
'' raserizor

#include "t3s.bas"
#include "matix.bas"

dim shared as integer scrnw , scrnh
screen 20 , 32 , , fb.gfx_fullscreen
screeninfo scrnw , scrnh

dim shared as t3s pnt( 255 )
dim shared as t3s light = t3s( 1 , 1 , 1 )
sub setpoint( no as integer _
  , x as single , y as single , z as single )
  spot x , y , z
  pnt( no ) = t3s( x , y , z )
end sub
dim shared as integer tritel = 0
type ttriangle
  dim as t3s p( 2 ) , led , normal , kleur
  declare sub fill( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  declare sub show()
end type 
sub ttriangle.fill( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  p( 0 ) = pnt( p1 )
  p( 1 ) = pnt( p2 )
  p( 2 ) = pnt( p3 )
  led = ( p( 0 ) + p( 1 ) + p( 2 ) ) / 3
  normal = cross( p( 0 ) - led , p( 1 ) - led )
  kleur = kl
end sub
sub ttriangle.show()
  dim as single x1,y1,x2,y2,x3,y3,a,b,i,angle
  
  x1 = scrnw / 2 + p( 0 ).x / ( p( 0 ).z + 1000 ) * 1000
  y1 = scrnh / 2 - p( 0 ).y / ( p( 0 ).z + 1000 ) * 1000
  x2 = scrnw / 2 + p( 1 ).x / ( p( 1 ).z + 1000 ) * 1000
  y2 = scrnh / 2 - p( 1 ).y / ( p( 1 ).z + 1000 ) * 1000
  x3 = scrnw / 2 + p( 2 ).x / ( p( 2 ).z + 1000 ) * 1000
  y3 = scrnh / 2 - p( 2 ).y / ( p( 2 ).z + 1000 ) * 1000
  
  if y1 = y2 then y1 = y1 - 1e-7
  if y2 = y3 then y3 = y3 + 1e-7
  if y1 > y3 then
    swap y1 , y3
    swap x1 , x3
  end if
  if y1 > y2 then
    swap y1 , y2
    swap x1 , x2
  end if
  if y2 > y3 then
    swap y2 , y3
    swap x2 , x3
  end if
  for i = y1 to y3
    a = x1 + ( x3 - x1 ) * (i-y1) / ( y3 - y1 )
    if i < y2 then
      b = x1 + ( x2 - x1 ) * (i-y1) / ( y2 - y1 )
    else
      b = x2 + ( x3 - x2 ) * (i-y2) / ( y3 - y2 )
    end if
    angle = getangle( light , normal )
    kleur = kleur * ( cos( angle ) / 4 + .75 )
    line ( a , i ) - ( b , i ) , kleur.toColor()
    
  next i
end sub
dim shared as ttriangle triangles( 1000 )
sub tri( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  triangles( tritel ).fill p1 , p2 , p3 , kl
  tritel += 1
end sub
sub quad(p1 as integer,p2 as integer,p3 as integer _
  ,p4 as integer,kl as t3s)
  tri p1,p2,p3 , kl
  tri p1,p3,p4 , kl
end sub
dim shared as single box(5)
sub setbox( x as single , y as single , z as single _
  , dx as single , dy as single , dz as single )
  box(0)=x
  box(1)=y
  box(2)=z
  box(3)=dx
  box(4)=dy
  box(5)=dz
end sub
sub sphere( a as integer , b as integer _
  , da as single , db as single , kl as t3s )
  dim as single i , j , i2 , j2 
  dim as single x , y , z
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 6
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI / 2 to PI / 2 - pi / b * 2 step PI / b * 2 
      j2 = j + PI / b * 2 

      x = sin( i ) * cos( j ) 
      y = sin( j )            
      z = cos( i ) * cos( j ) 
      setpoint 0 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      x = sin( i2 ) * cos( j ) 
      y = sin( j )             
      z = cos( i2 ) * cos( j ) 
      setpoint 1 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)
      
      x = sin( i2 ) * cos( j2 ) 
      y = sin( j2 )            
      z = cos( i2 ) * cos( j2 ) 
      
      setpoint 2 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      
      x = sin( i ) * cos( j2 ) 
      y = sin( j2 )            
      z = cos( i ) * cos( j2 ) 
      setpoint 3 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

sub torus( a as integer , b as integer , kl as t3s )
  dim i as double , j as double , i2 as double , j2 as double
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as double , my as double , mz as double , dx as double , dy as double , dz as double
  
  mx = box(0)
  my = box(1) 
  mz = box(2) 
  dx = box(3) 
  dy = box(4) 
  dz = box(5) 

  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI to PI step PI / b * 2 
      j2 = j + PI / b * 2 
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) ) * cos( j ) _
      , my + ( dx + dy * cos( i ) ) * sin( j ) _
      , mz + sin( i ) * dz  
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i ) ) * sin( j2 ) _
      , mz + sin( i ) * dz 
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j2 ) _
      , mz + sin( i2 ) * dz 
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) ) * cos( j ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j ) _
      , mz + sin( i2 ) * dz 
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

sub banana( a as integer , b as integer , kl as t3s )
  dim i as double , j as double , i2 as double , j2 as double
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as double , my as double , mz as double , dx as double , dy as double , dz as double 
  mx = box(0)
  my = box(1) 
  mz = box(2) 
  dx = box(3) 
  dy = box(4) 
  dz = box(5) 
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI/1.99 to PI/1.99 - pi/b*2 step PI / b * 1.99
      j2 = j + PI / b * 1.99
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) * cos( j ) ) _
      * cos( j ) _
      , my + ( dx + dy * cos( i ) * cos( j ) ) _
      * sin( j ) _
      , mz + sin( i ) * dz * cos( j )
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) * cos( j2 ) ) _
      * cos( j2 ) _
      , my + ( dx + dy * cos( i ) * cos( j2 ) ) _
      * sin( j2 ) _
      , mz + sin( i ) * dz * cos( j2 )
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) * cos( j2 ) ) _
      * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) * cos( j2 ) ) _
      * sin( j2 ) _
      , mz + sin( i2 ) * dz * cos( j2 )
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) * cos( j ) ) _
      * cos( j ) _
      , my + ( dx + dy * cos( i2 ) * cos( j ) ) _
      * sin( j ) _
      , mz + sin( i2 ) * dz * cos( j )
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

dim as single angle
dim as integer hi , lo
''do
  cls
  paint (1,1),rgb(0,100,100)
  tritel = 0
  camera 0,0,0 , 0,0,0 , 1
  link 1 , 0,0,0 , angle,angle,angle , xyz , 0
  setbox 0,0,0 , 100,100,100
  sphere 20 , 20 , 3 , 3 , blue
  setbox -200,0,0 , 100,50,50
  torus 12 , 12 , red
  setbox 200,0,0 , 100,50,50
  banana 12 , 12 , green
  for hi = 1 to tritel
    for lo = 0 to hi - 1
      if triangles(lo).led.z > triangles(hi).led.z then
        swap triangles(lo) , triangles(hi)
      end if
    next lo
  next hi
  for lo = 0 to tritel - 1
    triangles(lo).show
  next lo
  angle += 5
  sleep 
''loop while inkey = ""
  


bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: RASTERIZATON TRY

Post by bluatigro »

update :
shading solved

please :
who want te build quick sort in this example ?

Code: Select all

'' bluatigro 30 aug 2021
'' raserizor

#include "t3s.bas"
#include "matix.bas"

dim shared as integer scrnw , scrnh
screen 20 , 32 , , fb.gfx_fullscreen
screeninfo scrnw , scrnh

dim shared as t3s pnt( 255 )
dim shared as t3s light = t3s( 1 , 1 , 1 )
sub setpoint( no as integer _
  , x as single , y as single , z as single )
  spot x , y , z
  pnt( no ) = t3s( x , y , z )
end sub
dim shared as integer tritel = 0
type ttriangle
  dim as t3s p( 2 ) , led , normal , kleur
  declare sub fill( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  declare sub show()
end type 
sub ttriangle.fill( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  p( 0 ) = pnt( p1 )
  p( 1 ) = pnt( p2 )
  p( 2 ) = pnt( p3 )
  led = ( p( 0 ) + p( 1 ) + p( 2 ) ) / 3
  normal = cross( p( 0 ) - led , p( 1 ) - led )
  kleur = kl
end sub
sub tria( x1 as single , y1 as single , x2 as single _
  , y2 as single , x3 as single , y3 as single _
  , kl as t3s , normal as t3s  )
  dim as single a , b , i , angle
  if y1 = y2 then y1 = y1 - 1e-7
  if y2 = y3 then y3 = y3 + 1e-7
  if y1 > y3 then
    swap y1 , y3
    swap x1 , x3
  end if
  if y1 > y2 then
    swap y1 , y2
    swap x1 , x2
  end if
  if y2 > y3 then
    swap y2 , y3
    swap x2 , x3
  end if
  for i = y1 to y3
    a = x1 + ( x3 - x1 ) * (i-y1) / ( y3 - y1 )
    if i < y2 then
      b = x1 + ( x2 - x1 ) * (i-y1) / ( y2 - y1 )
    else
      b = x2 + ( x3 - x2 ) * (i-y2) / ( y3 - y2 )
    end if
    line ( a , i ) - ( b , i ) , kl.toColor()
    
  next i
end sub
sub ttriangle.show()
  dim as single x1,y1,x2,y2,x3,y3,angle
  
  x1 = scrnw / 2 + p( 0 ).x / ( p( 0 ).z + 1000 ) * 1000
  y1 = scrnh / 2 - p( 0 ).y / ( p( 0 ).z + 1000 ) * 1000
  x2 = scrnw / 2 + p( 1 ).x / ( p( 1 ).z + 1000 ) * 1000
  y2 = scrnh / 2 - p( 1 ).y / ( p( 1 ).z + 1000 ) * 1000
  x3 = scrnw / 2 + p( 2 ).x / ( p( 2 ).z + 1000 ) * 1000
  y3 = scrnh / 2 - p( 2 ).y / ( p( 2 ).z + 1000 ) * 1000
    angle = getangle( light , normal )
    kleur = kleur * ( cos( angle ) / 2 + .5 )
    tria x1,y1,x2,y2,x3,y3,kleur,normal
end sub
dim shared as ttriangle triangles( 1000 )
sub tri( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  triangles( tritel ).fill p1 , p2 , p3 , kl
  tritel += 1
end sub
sub quad(p1 as integer,p2 as integer,p3 as integer _
  ,p4 as integer,kl as t3s)
  tri p1,p2,p3 , kl
  tri p1,p3,p4 , kl
end sub
dim shared as single box(5)
sub setbox( x as single , y as single , z as single _
  , dx as single , dy as single , dz as single )
  box(0)=x
  box(1)=y
  box(2)=z
  box(3)=dx
  box(4)=dy
  box(5)=dz
end sub
sub sphere( a as integer , b as integer _
  , da as single , db as single , kl as t3s )
  dim as single i , j , i2 , j2 
  dim as single x , y , z
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 6
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI / 2 to PI / 2 - pi / b * 2 step PI / b * 2 
      j2 = j + PI / b * 2 

      x = sin( i ) * cos( j ) 
      y = sin( j )            
      z = cos( i ) * cos( j ) 
      setpoint 0 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      x = sin( i2 ) * cos( j ) 
      y = sin( j )             
      z = cos( i2 ) * cos( j ) 
      setpoint 1 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)
      
      x = sin( i2 ) * cos( j2 ) 
      y = sin( j2 )            
      z = cos( i2 ) * cos( j2 ) 
      
      setpoint 2 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      
      x = sin( i ) * cos( j2 ) 
      y = sin( j2 )            
      z = cos( i ) * cos( j2 ) 
      setpoint 3 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

sub torus( a as integer , b as integer , kl as t3s )
  dim i as double , j as double , i2 as double , j2 as double
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as double , my as double , mz as double , dx as double , dy as double , dz as double
  
  mx = box(0)
  my = box(1) 
  mz = box(2) 
  dx = box(3) 
  dy = box(4) 
  dz = box(5) 

  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI to PI step PI / b * 2 
      j2 = j + PI / b * 2 
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) ) * cos( j ) _
      , my + ( dx + dy * cos( i ) ) * sin( j ) _
      , mz + sin( i ) * dz  
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i ) ) * sin( j2 ) _
      , mz + sin( i ) * dz 
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j2 ) _
      , mz + sin( i2 ) * dz 
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) ) * cos( j ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j ) _
      , mz + sin( i2 ) * dz 
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

sub banana( a as integer , b as integer , kl as t3s )
  dim i as double , j as double , i2 as double , j2 as double
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as double , my as double , mz as double , dx as double , dy as double , dz as double 
  mx = box(0)
  my = box(1) 
  mz = box(2) 
  dx = box(3) 
  dy = box(4) 
  dz = box(5) 
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI/1.99 to PI/1.99 - pi/b*2 step PI / b * 1.99
      j2 = j + PI / b * 1.99
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) * cos( j ) ) _
      * cos( j ) _
      , my + ( dx + dy * cos( i ) * cos( j ) ) _
      * sin( j ) _
      , mz + sin( i ) * dz * cos( j )
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) * cos( j2 ) ) _
      * cos( j2 ) _
      , my + ( dx + dy * cos( i ) * cos( j2 ) ) _
      * sin( j2 ) _
      , mz + sin( i ) * dz * cos( j2 )
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) * cos( j2 ) ) _
      * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) * cos( j2 ) ) _
      * sin( j2 ) _
      , mz + sin( i2 ) * dz * cos( j2 )
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) * cos( j ) ) _
      * cos( j ) _
      , my + ( dx + dy * cos( i2 ) * cos( j ) ) _
      * sin( j ) _
      , mz + sin( i2 ) * dz * cos( j )
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

dim as single angle
dim as integer hi , lo
''do
  cls
  paint (1,1),rgb(0,100,100)
  tritel = 0
  camera 0,0,0 , 0,0,0 , 1
  link 1 , 0,0,0 , angle,angle,angle , xyz , 0
  setbox 0,0,0 , 100,100,100
  sphere 20 , 20 , 3 , 3 , blue
  setbox -200,0,0 , 100,50,50
  torus 12 , 12 , red
  setbox 200,0,0 , 100,50,50
  banana 12 , 12 , green
  for hi = 1 to tritel
    for lo = 0 to hi - 1
      if triangles(lo).led.z > triangles(hi).led.z then
        swap triangles(lo) , triangles(hi)
      end if
    next lo
  next hi
  for lo = 0 to tritel - 1
    triangles(lo).show
  next lo
  angle += 5
  sleep 
''loop while inkey = ""
 
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: RASTERIZATON TRY

Post by dafhi »

Code: Select all

/' ---- qsort
'/
type my_sort_type as ttriangle
 
#define direction <


#macro ifswap(x,y,dotdef)
  if a(y)dotdef direction a(x)dotdef then
    swap a(x),a(y)
  endif
#EndMacro

'' normally pushed to stack
Dim shared as integer              j, i
dim shared as my_sort_type      swap_tmp


#macro SetQsort_nSquaredResist(datatype,fname,dotdef) '' dodicat's macro system
  Sub fname(a() as datatype, L as integer, r as integer)
    
    '' n^2 resistant & faster than average well-balanced quicksort
    
    var j=(l+r)\2
    ifswap(j,r,dotdef)
    ifswap(L,r,dotdef) '' L becomes pivot
    ifswap(j,l,dotdef)
    if L < r - 1 then
      j=r: i=L '' shared var i
      whiLe i < j
        j -= 1: whiLe a(L)dotdef direction a(j)dotdef: j-=1: wend
        i += 1: while a(i)dotdef direction a(L)dotdef: i+=1: wend
        if j<=i then i=j: exit while
        swap_tmp=a(i) : a(i)=a(j) : a(j)=swap_tmp
        'swap a(i),a(j)
      Wend
      ifswap(L,i,dotdef)
      i -= 1:  if L < i then fname a(), L, i
      j += 1:  if j < r then fname a(), j, r
    endif
  End Sub
#endmacro

setqsort_nSquaredResist( my_sort_type, qsort, .led.z )

Code: Select all

  #if 1
    qsort triangles(), 0, tritel
  #else  
    for hi = 1 to tritel
      for lo = 0 to hi - 1
        if triangles(lo).led.z > triangles(hi).led.z then
          swap triangles(lo) , triangles(hi)
        end if
      next lo
    next hi
  #EndIf
Last edited by dafhi on Sep 04, 2021 5:10, edited 1 time in total.
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: RASTERIZATON TRY

Post by bluatigro »

i have never worked whit marco's
so this has a lot of error's

please help

Code: Select all

'' bluatigro 8 aug 2021
'' raserizor

#include "t3s.bas"
#include "matix.bas"

' ---- qsort

 
#define direction <


#macro ifswap(x,y,dotdef)
  if a(y)dotdef direction a(x)dotdef then
    swap a(x),a(y)
  end if
#EndMacro

'' normally pushed to stack
Dim shared as integer              j, i


#macro SetQsort_nSquaredResist(datatype,fname,dotdef) '' dodicat's macro system
  Sub fname(a() as datatype, L as integer, r as integer)
   
    '' n^2 resistant & faster than average well-balanced quicksort
   
    var j=(l+r)\2
    ifswap(j,r,dotdef)
    ifswap(L,r,dotdef) '' L becomes pivot
    ifswap(j,l,dotdef)
    if L < r - 1 then
      j=r: i=L '' shared var i
      whiLe i < j
        j -= 1: whiLe a(L)dotdef direction a(j)dotdef: j-=1: wend
        i += 1: while a(i)dotdef direction a(L)dotdef: i+=1: wend
        if j<=i then i=j: exit while
        swap a(i),a(j)
      Wend
      ifswap(L,i,dotdef)
      i -= 1:  if L < i then fname a(), L, i
      j += 1:  if j < r then fname a(), j, r
    endif
  End Sub
#endmacro



dim shared as integer scrnw , scrnh
screen 20 , 32 , , fb.gfx_fullscreen
screeninfo scrnw , scrnh

dim shared as t3s pnt( 255 )
dim shared as t3s light = t3s( 1 , 1 , 1 )
sub setpoint( no as integer _
  , x as single , y as single , z as single )
  spot x , y , z
  pnt( no ) = t3s( x , y , z )
end sub
dim shared as integer tritel = 0
type ttriangle
  dim as t3s p( 2 ) , led , normal , kleur
  declare sub fill( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  declare sub show()
end type 
sub ttriangle.fill( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  p( 0 ) = pnt( p1 )
  p( 1 ) = pnt( p2 )
  p( 2 ) = pnt( p3 )
  led = ( p( 0 ) + p( 1 ) + p( 2 ) ) / 3
  normal = cross( p( 0 ) - led , p( 1 ) - led )
  kleur = kl
end sub
sub tria( x1 as single , y1 as single , x2 as single _
  , y2 as single , x3 as single , y3 as single _
  , kl as t3s , normal as t3s  )
  dim as single a , b , i , angle
  if y1 = y2 then y1 = y1 - 1e-7
  if y2 = y3 then y3 = y3 + 1e-7
  if y1 > y3 then
    swap y1 , y3
    swap x1 , x3
  end if
  if y1 > y2 then
    swap y1 , y2
    swap x1 , x2
  end if
  if y2 > y3 then
    swap y2 , y3
    swap x2 , x3
  end if
  for i = y1 to y3
    a = x1 + ( x3 - x1 ) * (i-y1) / ( y3 - y1 )
    if i < y2 then
      b = x1 + ( x2 - x1 ) * (i-y1) / ( y2 - y1 )
    else
      b = x2 + ( x3 - x2 ) * (i-y2) / ( y3 - y2 )
    end if
    line ( a , i ) - ( b , i ) , kl.toColor()
    
  next i
end sub
sub ttriangle.show()
  dim as single x1,y1,x2,y2,x3,y3,angle
  
  x1 = scrnw / 2 + p( 0 ).x / ( p( 0 ).z + 1000 ) * 1000
  y1 = scrnh / 2 - p( 0 ).y / ( p( 0 ).z + 1000 ) * 1000
  x2 = scrnw / 2 + p( 1 ).x / ( p( 1 ).z + 1000 ) * 1000
  y2 = scrnh / 2 - p( 1 ).y / ( p( 1 ).z + 1000 ) * 1000
  x3 = scrnw / 2 + p( 2 ).x / ( p( 2 ).z + 1000 ) * 1000
  y3 = scrnh / 2 - p( 2 ).y / ( p( 2 ).z + 1000 ) * 1000
    angle = getangle( light , normal )
    kleur = kleur * ( cos( angle ) / 2 + .5 )
    tria x1,y1,x2,y2,x3,y3,kleur,normal
end sub
dim shared as ttriangle triangles( 1000 )
sub tri( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  triangles( tritel ).fill p1 , p2 , p3 , kl
  tritel += 1
end sub
sub quad(p1 as integer,p2 as integer,p3 as integer _
  ,p4 as integer,kl as t3s)
  tri p1,p2,p3 , kl
  tri p1,p3,p4 , kl
end sub
dim shared as single box(5)
sub setbox( x as single , y as single , z as single _
  , dx as single , dy as single , dz as single )
  box(0)=x
  box(1)=y
  box(2)=z
  box(3)=dx
  box(4)=dy
  box(5)=dz
end sub
sub sphere( a as integer , b as integer _
  , da as single , db as single , kl as t3s )
  dim as single i , j , i2 , j2 
  dim as single x , y , z
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 6
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI / 2 to PI / 2 - pi / b * 2 step PI / b * 2 
      j2 = j + PI / b * 2 

      x = sin( i ) * cos( j ) 
      y = sin( j )            
      z = cos( i ) * cos( j ) 
      setpoint 0 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      x = sin( i2 ) * cos( j ) 
      y = sin( j )             
      z = cos( i2 ) * cos( j ) 
      setpoint 1 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)
      
      x = sin( i2 ) * cos( j2 ) 
      y = sin( j2 )            
      z = cos( i2 ) * cos( j2 ) 
      
      setpoint 2 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      
      x = sin( i ) * cos( j2 ) 
      y = sin( j2 )            
      z = cos( i ) * cos( j2 ) 
      setpoint 3 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

sub torus( a as integer , b as integer , kl as t3s )
  dim i as double , j as double , i2 as double , j2 as double
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as double , my as double , mz as double , dx as double , dy as double , dz as double
  
  mx = box(0)
  my = box(1) 
  mz = box(2) 
  dx = box(3) 
  dy = box(4) 
  dz = box(5) 

  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI to PI step PI / b * 2 
      j2 = j + PI / b * 2 
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) ) * cos( j ) _
      , my + ( dx + dy * cos( i ) ) * sin( j ) _
      , mz + sin( i ) * dz  
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i ) ) * sin( j2 ) _
      , mz + sin( i ) * dz 
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j2 ) _
      , mz + sin( i2 ) * dz 
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) ) * cos( j ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j ) _
      , mz + sin( i2 ) * dz 
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

sub banana( a as integer , b as integer , kl as t3s )
  dim i as double , j as double , i2 as double , j2 as double
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as double , my as double , mz as double , dx as double , dy as double , dz as double 
  mx = box(0)
  my = box(1) 
  mz = box(2) 
  dx = box(3) 
  dy = box(4) 
  dz = box(5) 
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI/1.99 to PI/1.99 - pi/b*2 step PI / b * 1.99
      j2 = j + PI / b * 1.99
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) * cos( j ) ) _
      * cos( j ) _
      , my + ( dx + dy * cos( i ) * cos( j ) ) _
      * sin( j ) _
      , mz + sin( i ) * dz * cos( j )
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) * cos( j2 ) ) _
      * cos( j2 ) _
      , my + ( dx + dy * cos( i ) * cos( j2 ) ) _
      * sin( j2 ) _
      , mz + sin( i ) * dz * cos( j2 )
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) * cos( j2 ) ) _
      * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) * cos( j2 ) ) _
      * sin( j2 ) _
      , mz + sin( i2 ) * dz * cos( j2 )
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) * cos( j ) ) _
      * cos( j ) _
      , my + ( dx + dy * cos( i2 ) * cos( j ) ) _
      * sin( j ) _
      , mz + sin( i2 ) * dz * cos( j )
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

dim as single angle
dim as integer hi , lo

  cls
  paint (1,1),rgb(0,100,100)
  tritel = 0
  camera 0,0,0 , 0,0,0 , 1
  link 1 , 0,0,0 , angle,angle,angle , xyz , 0
  setbox 0,0,0 , 100,100,100
  sphere 20 , 20 , 3 , 3 , blue
  setbox -200,0,0 , 100,50,50
  torus 12 , 12 , red
  setbox 200,0,0 , 100,50,50
  banana 12 , 12 , green
  setqsort_nSquaredResist(triangles(),qsort,.led.z)

  for lo = 0 to tritel - 1
    triangles(lo).show
  next lo
  angle += 5
  sleep 

  


dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: RASTERIZATON TRY

Post by dafhi »

Code: Select all

  setqsort_nSquaredResist(ttriangle,qsort,.led.z)
  qsort triangles(), 0, tritel
updated my previous post for speed

Code: Select all

'' normally pushed to stack
Dim shared as integer           j, i
dim shared as my_sort_type      swap_tmp


#macro SetQsort_nSquaredResist(datatype,fname,dotdef) '' dodicat's macro system
  Sub fname(a() as datatype, L as integer, r as integer)
   
    '' n^2 resistant & faster than average well-balanced quicksort
   
    var j=(l+r)\2
    ifswap(j,r,dotdef)
    ifswap(L,r,dotdef) '' L becomes pivot
    ifswap(j,l,dotdef)
    if L < r - 1 then
      j=r: i=L '' shared var i
      whiLe i < j
        j -= 1: whiLe a(L)dotdef direction a(j)dotdef: j-=1: wend
        i += 1: while a(i)dotdef direction a(L)dotdef: i+=1: wend
        if j<=i then i=j: exit while
        swap_tmp=a(i) : a(i)=a(j) : a(j)=swap_tmp
        'swap a(i),a(j)
      Wend
      ifswap(L,i,dotdef)
      i -= 1:  if L < i then fname a(), L, i
      j += 1:  if j < r then fname a(), j, r
    endif
  End Sub
#endmacro
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: RASTERIZATON TRY

Post by bluatigro »

@dafhi :
thanks for help
update :
last sugestion build in

i keep getting error's

Code: Select all

'' bluatigro 8 aug 2021
'' raserizor

#include "t3s.bas"
#include "matix.bas"

' ---- qsort

 
#define direction <

type qsort_type as ttriangle


#macro ifswap(x,y,dotdef)
  if a(y)dotdef direction a(x)dotdef then
    swap a(x),a(y)
  end if
#EndMacro

'' normally pushed to stack
Dim shared as integer              j, i
dim shared as qsort_type           swap_tmp



#macro SetQsort_nSquaredResist(datatype,fname,dotdef) '' dodicat's macro system
  Sub fname(a() as datatype, L as integer, r as integer)
   
    '' n^2 resistant & faster than average well-balanced quicksort
   
    var j=(l+r)\2
    ifswap(j,r,dotdef)
    ifswap(L,r,dotdef) '' L becomes pivot
    ifswap(j,l,dotdef)
    if L < r - 1 then
      j=r: i=L '' shared var i
      whiLe i < j
        j -= 1: whiLe a(L)dotdef direction a(j)dotdef: j-=1: wend
        i += 1: while a(i)dotdef direction a(L)dotdef: i+=1: wend
        if j<=i then i=j: exit while
        swap a(i),a(j)
      Wend
      ifswap(L,i,dotdef)
      i -= 1:  if L < i then fname a(), L, i
      j += 1:  if j < r then fname a(), j, r
    endif
  End Sub
#endmacro

  setqsort_nSquaredResist(qsort_type,qsort,.led.z)


dim shared as integer scrnw , scrnh
screen 20 , 32 , , fb.gfx_fullscreen
screeninfo scrnw , scrnh

dim shared as t3s pnt( 255 )
dim shared as t3s light = t3s( 1 , 1 , 1 )
sub setpoint( no as integer _
  , x as single , y as single , z as single )
  spot x , y , z
  pnt( no ) = t3s( x , y , z )
end sub
dim shared as integer tritel = 0
type ttriangle
  dim as t3s p( 2 ) , led , normal , kleur
  declare sub fill( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  declare sub show()
end type 
sub ttriangle.fill( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  p( 0 ) = pnt( p1 )
  p( 1 ) = pnt( p2 )
  p( 2 ) = pnt( p3 )
  led = ( p( 0 ) + p( 1 ) + p( 2 ) ) / 3
  normal = cross( p( 0 ) - led , p( 1 ) - led )
  kleur = kl
end sub
sub tria( x1 as single , y1 as single , x2 as single _
  , y2 as single , x3 as single , y3 as single _
  , kl as t3s , normal as t3s  )
  dim as single a , b , i , angle
  if y1 = y2 then y1 = y1 - 1e-7
  if y2 = y3 then y3 = y3 + 1e-7
  if y1 > y3 then
    swap y1 , y3
    swap x1 , x3
  end if
  if y1 > y2 then
    swap y1 , y2
    swap x1 , x2
  end if
  if y2 > y3 then
    swap y2 , y3
    swap x2 , x3
  end if
  for i = y1 to y3
    a = x1 + ( x3 - x1 ) * (i-y1) / ( y3 - y1 )
    if i < y2 then
      b = x1 + ( x2 - x1 ) * (i-y1) / ( y2 - y1 )
    else
      b = x2 + ( x3 - x2 ) * (i-y2) / ( y3 - y2 )
    end if
    line ( a , i ) - ( b , i ) , kl.toColor()
    
  next i
end sub
sub ttriangle.show()
  dim as single x1,y1,x2,y2,x3,y3,angle
  
  x1 = scrnw / 2 + p( 0 ).x / ( p( 0 ).z + 1000 ) * 1000
  y1 = scrnh / 2 - p( 0 ).y / ( p( 0 ).z + 1000 ) * 1000
  x2 = scrnw / 2 + p( 1 ).x / ( p( 1 ).z + 1000 ) * 1000
  y2 = scrnh / 2 - p( 1 ).y / ( p( 1 ).z + 1000 ) * 1000
  x3 = scrnw / 2 + p( 2 ).x / ( p( 2 ).z + 1000 ) * 1000
  y3 = scrnh / 2 - p( 2 ).y / ( p( 2 ).z + 1000 ) * 1000
    angle = getangle( light , normal )
    kleur = kleur * ( cos( angle ) / 2 + .5 )
    tria x1,y1,x2,y2,x3,y3,kleur,normal
end sub
dim shared as ttriangle triangles( 1000 )
sub tri( p1 as integer , p2 as integer _
  , p3 as integer , kl as t3s )
  triangles( tritel ).fill p1 , p2 , p3 , kl
  tritel += 1
end sub
sub quad(p1 as integer,p2 as integer,p3 as integer _
  ,p4 as integer,kl as t3s)
  tri p1,p2,p3 , kl
  tri p1,p3,p4 , kl
end sub
dim shared as single box(5)
sub setbox( x as single , y as single , z as single _
  , dx as single , dy as single , dz as single )
  box(0)=x
  box(1)=y
  box(2)=z
  box(3)=dx
  box(4)=dy
  box(5)=dz
end sub
sub sphere( a as integer , b as integer _
  , da as single , db as single , kl as t3s )
  dim as single i , j , i2 , j2 
  dim as single x , y , z
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 6
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI / 2 to PI / 2 - pi / b * 2 step PI / b * 2 
      j2 = j + PI / b * 2 

      x = sin( i ) * cos( j ) 
      y = sin( j )            
      z = cos( i ) * cos( j ) 
      setpoint 0 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      x = sin( i2 ) * cos( j ) 
      y = sin( j )             
      z = cos( i2 ) * cos( j ) 
      setpoint 1 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)
      
      x = sin( i2 ) * cos( j2 ) 
      y = sin( j2 )            
      z = cos( i2 ) * cos( j2 ) 
      
      setpoint 2 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      
      x = sin( i ) * cos( j2 ) 
      y = sin( j2 )            
      z = cos( i ) * cos( j2 ) 
      setpoint 3 _
      , abs( x ) ^ da * sgn( x ) * box(3)+box(0) _
      , abs( y ) ^ db * sgn( y ) * box(4)+box(1) _
      , abs( z ) ^ da * sgn( z ) * box(5)+box(2)

      
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

sub torus( a as integer , b as integer , kl as t3s )
  dim i as double , j as double , i2 as double , j2 as double
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as double , my as double , mz as double , dx as double , dy as double , dz as double
  
  mx = box(0)
  my = box(1) 
  mz = box(2) 
  dx = box(3) 
  dy = box(4) 
  dz = box(5) 

  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI to PI step PI / b * 2 
      j2 = j + PI / b * 2 
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) ) * cos( j ) _
      , my + ( dx + dy * cos( i ) ) * sin( j ) _
      , mz + sin( i ) * dz  
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i ) ) * sin( j2 ) _
      , mz + sin( i ) * dz 
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j2 ) _
      , mz + sin( i2 ) * dz 
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) ) * cos( j ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j ) _
      , mz + sin( i2 ) * dz 
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

sub banana( a as integer , b as integer , kl as t3s )
  dim i as double , j as double , i2 as double , j2 as double
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as double , my as double , mz as double , dx as double , dy as double , dz as double 
  mx = box(0)
  my = box(1) 
  mz = box(2) 
  dx = box(3) 
  dy = box(4) 
  dz = box(5) 
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI/1.99 to PI/1.99 - pi/b*2 step PI / b * 1.99
      j2 = j + PI / b * 1.99
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) * cos( j ) ) _
      * cos( j ) _
      , my + ( dx + dy * cos( i ) * cos( j ) ) _
      * sin( j ) _
      , mz + sin( i ) * dz * cos( j )
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) * cos( j2 ) ) _
      * cos( j2 ) _
      , my + ( dx + dy * cos( i ) * cos( j2 ) ) _
      * sin( j2 ) _
      , mz + sin( i ) * dz * cos( j2 )
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) * cos( j2 ) ) _
      * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) * cos( j2 ) ) _
      * sin( j2 ) _
      , mz + sin( i2 ) * dz * cos( j2 )
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) * cos( j ) ) _
      * cos( j ) _
      , my + ( dx + dy * cos( i2 ) * cos( j ) ) _
      * sin( j ) _
      , mz + sin( i2 ) * dz * cos( j )
      quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

dim as single angle
dim as integer hi , lo

  cls
  paint (1,1),rgb(0,100,100)
  tritel = 0
  camera 0,0,0 , 0,0,0 , 1
  link 1 , 0,0,0 , angle,angle,angle , xyz , 0
  setbox 0,0,0 , 100,100,100
  sphere 20 , 20 , 3 , 3 , blue
  setbox -200,0,0 , 100,50,50
  torus 12 , 12 , red
  setbox 200,0,0 , 100,50,50
  banana 12 , 12 , green
  qsort triangles,qsort,.led.z

  for lo = 0 to tritel - 1
    triangles(lo).show
  next lo
  angle += 5
  sleep 

  


dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: RASTERIZATON TRY

Post by dafhi »

sort definition must proceed ttriangle type

and then

Code: Select all

  banana 12 , 12 , green
  qsort triangles(),0,tritel

  for lo = 0 to tritel - 1
    triangles(lo).show
  next lo
Post Reply