mercredi 7 juin 2017

filling a triangle with shader color interpolation very useful for Zbuffer algorithm

Type pixel
x as integer
y as integer
r as integer
g as integer
b as integer
end type

ScreenRes 640, 480, 16,2,0
'screenset 1,0
FUNCTION max(x as integer,y as integer) as integer
 if (x>y) then return x else return y
end function    

FUNCTION min(x as integer,y as integer) as integer
 if (x>y) then return y else return x
end function    

sub triangle(a as pixel, b as pixel, c as pixel)
 dim as single xmin,ymin,xmax,ymax,x,y
 dim as single u,v,det
 dim as pixel am,ab,ac,m
 dim as integer red,green,blue

 xmin = min(a.x,min(b.x,c.x)):xmax = max(a.x,max(b.x,c.x))
 ymin = min(a.y,min(b.y,c.y)):ymax = max(a.y,max(b.y,c.y))

  ab.x = b.x-a.x: ab.y = b.y-a.y
  ac.x = c.x-a.x: ac.y = c.y-a.y
  
 for y = ymin to ymax
     for x = xmin to xmax
         m.x = x:m.y= y 
         am.x = x-a.x: am.y = y-a.y
         det  = ab.x*ac.y-ac.x*ab.y
         u  = (am.x*ac.y-ac.x*am.y)/det: v  = -(am.x*ab.y-ab.x*am.y)/det
         
         red  = u*b.r+v*c.r+(1-u-v)*a.r:
         green  = u*b.g+v*c.g+(1-u-v)*a.g
         blue  = u*b.b+v*c.b+(1-u-v)*a.b
         
         if ( (u>=0) and ( v>=0) and ( u+v<=1))  then pset (x,y),RGB(red,green,blue) 
           
       next x
   next y

end sub

dim as single i
dim as pixel a,b,c


for i = 0 to  2000 
a.x = 640*rnd: a.y = 480*rnd
b.x = 640*rnd:b.y = 480*rnd
c.x = 640*rnd:c.y = 480*rnd

 a.r = 255*rnd: a.g = 255*rnd: a.b = 255*rnd
 b.r = 255*rnd: b.g = 255*rnd: b.b = 255*rnd
 c.r = 255*rnd: c.g = 255*rnd: c.b = 255*rnd
triangle(a,b,c)

next i



sleep

Aucun commentaire:

Enregistrer un commentaire

Remarque : Seul un membre de ce blog est autorisé à enregistrer un commentaire.