Mark's Coding Creations

Gouraud Triangles

Home
Kaleidascope
Gouraud Ellipses
Gouraud Quads
Gouraud Triangles
Draw And Fill
Gouraud_Circles
Rotate Image

triangles_gouraud.jpg

REM Project: Vector_Tri
REM Created: 13.1.2007 16:17:21
REM Author:  Mark Garrett  aka 'bubba'
REM ***** Main Source File *****
REM

global r5:global g5: global b5


global dim x(4000)
global dim y(4000)

global dim r(4000)
global dim g(4000)
global dim b(4000)


global  n1
global  n2

set display mode 1024,768,16
 sync on:sync rate 0


`------------------------ set up primary colors:
dim culur(6)

culur(1) = rgb(255,0,0):culur(2) = rgb(0,255,0):culur(3) = rgb(0,0,255)
culur(4) = rgb(255,255,0):culur(5) = rgb(0,255,255):culur(6) = rgb(255,0,255)

 

for m = 1 to 20
lock pixels

cc = rnd(5)+1 :color1 = culur(cc)
cc1: cc = rnd(5)+1 :color2 = culur(cc)
if color2 = color1 then goto cc1
cc2: cc = rnd(5)+1 :color3 = culur(cc)
if color3 = color1 or color3 = color2 then goto cc2

 

 

Tri(rnd(400),rnd(350),color1,rnd(300)+700,rnd(300),color2,rnd(300)+700,rnd(300)+400,color3)

`Tri(10,10,color1,500,10,color2,400,400,color3,20,600,color4)

unlock pixels:sync

next m
 ink rgb(0,255,255),0
 text 10,10," To save a j-peg file press the right mouse button.  "
 text 10,25," to exit press the left mouse button."  :sync

wait mouse   `observe results
 if mouseclick() = 2
  get image 1,0,0,1023,767,1: save image "Triangles.jpg",1 :wait 5000
  endif
end          `exit


`&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

function Tri(x11,y11,color11,x22,y22,color22,x33,y33,color33)
global n
global r5:global g5: global b5


global dim x(4000)
global dim y(4000)

global dim r(4000)
global dim g(4000)
global dim b(4000)


global  n1
global  n2
`-------------  get x_mid,y_mid ---------------------------
Point1 = (x11 + x22)/2:  Point2 = (x22 + x33)/2:  Point3 = (x33+x44)/2
   x_MID = (Point1 + Point2 + Point3)/3

   Point1 = (y11 + y22)/2:  Point2 = (y22 + y33)/2:  Point3 = (y33+y44)/2
   y_MID = (Point1 + Point2 + Point3)/3

 

 

 ` text x11,y11, "1"  :text x22,y22, "2":text x33,y33, "3"
`----------------------------------------------------------
`
x1 as float:x2 as float:y1 as float:y2 as float:c1 as float:c2 as float

 

x1 = x11:x1 = x11:  y1 = y11:x2 = x22:y2 = y22
c1 = color11:c2 = color22 :gosub Poke_a_Side `: mid_side_1 = long_span/2
co2 = n     `(corner 2)


x1 = x22:x1 = x22:  y1 = y22:x2 = x33:y2 = y33
c1 = color22:c2 = color33 :  gosub Poke_a_Side
co3 = n


x1 = x33:y1 = y33:  x2 = x11:y2 = y11
c1 = color33:c2 = color11 :  gosub Poke_a_Side
co1 = n

 

goto draw_vertices

 


Poke_a_Side:

`-----------------get add----xy's -and largest-span
spany as float: spanx as float:addx as float:addy as float:long_span as float
`- - - - - - - - - -
 spanx = abs(x2-x1)
 spany = abs(y2-y1)

if spany>=spanx then long_span = spany
if spanx>=spany then long_span = spanx

addy = (y2-y1)/long_span
addx = (x2-x1)/long_span

`-----------------get add---rgb's
r1 as float:g1 as float:b1 as float:r2 as float:g2 as float:b2 as float
rspan as float:gspan as float:bspan as float
addr as float:addg as float:addb as float
`- - - - - - - - - - -
r1 = rgbr(c1):g1 = rgbg(c1):b1 = rgbb(c1)
r2 = rgbr(c2):g2 = rgbg(c2):b2 = rgbb(c2)


addr = (r2-r1)/long_span
addg = (g2-g1)/long_span
addb = (b2-b1)/long_span

 


`---------------poke-a-side-of-the-triangle-
for t = 1 to long_span

 

x1 = x1 + addx: x(n) = x1
y1 = y1 + addy: y(n) = y1


last_y1 = y1:last_x1 = x1

r1 = r1 + addr: r(n) = r1
g1 = g1 + addg: g(n) = g1
b1 = b1 + addb: b(n) = b1


cont:

ink rgb(r1,g1,b1),0  : dot x1,y1
`print x1, y1
inc n
next t

RETURN

`                             DRAW  vertices
 `MMMMMMMMMMMMMMMMMMMMMMMMMMM                   MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 `MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
`---------------------------- draw vertices----------------------

draw_vertices:

 

if y11 <= y22    and y11 <= y33       then Hi_y = co1

if y22 <= y11    and y22 <= y33       then Hi_y = co2

if y33 <= y11    and y33 <= y22       then Hi_y = co3

 


if y11 >= y22    and y11 >= y33       then Lo_y = co1

if y22 >= y11    and y22 >= y33       then Lo_y = co2

if y33 >= y11    and y33 >= y22       then Lo_y = co3


n1 = Hi_y     : n2 =    Hi_y


beggin_LOOP:
n1 = I(n1,1)
n2 = D(n2,1)

n1y = y(n1)
n2y = y(n2)


if n2y > n1y then n2 = I(n2,1): goto beggin_LOOP
if n1y > n2y then n1 = D(n1,1): goto beggin_LOOP

if y(n2) = y(Lo_y) then goto exitfunction_

gosub Poke_a_line
goto beggin_LOOP

`++++++++++++++++++++++++++++ POKE A LINE ++++++++++++++++++++++++++++++++++++
`++++++++++++++++++++++++++++ POKE A LINE ++++++++++++++++++++++++++++++++++++
`++++++++++++++++++++++++++++ POKE A LINE ++++++++++++++++++++++++++++++++++++

Poke_a_line:

`-----------------get add----xy's -and largest-span

`- - - - - - - - - -
 spanx = abs(x(n2)-x(n1))
 spany = abs(y(n2)-y(n1))

if spany>=spanx then long_span = spany
if spanx>=spany then long_span = spanx

addy = (y(n1)-y(n2))/long_span
y2 = y(n2)    ` for poking the dots
addx = (x(n1)-x(n2))/long_span
x2 = x(n2)


r1 = r(n1):g1 = g(n1):b1 = b(n1)
r2 = r(n2):g2 = g(n2):b2 = b(n2)


addr = (r1-r2)/long_span
addg = (g1-g2)/long_span
addb = (b1-b2)/long_span

`----------------prepare-storage-space


`---------------
for tu = 1 to long_span-1


`inc n

x2 = x2 + addx
y2 = y2 + addy

 


r2 = r2 + addr
g2 = g2 + addg
b2 = b2 + addb


ink rgb(r2,g2,b2),0
dot x2,y2
dot x2,y2-1
`dot x2+1,y2 `:dot x2-1,y2
`line x(n2),y(n2),x(n1),y(n1)
next tu

RETURN


`print "  ": print "  "
`print x(n2),"  ",y(n2),"  ",   x(n1),"  ",y(n1)
`print "Hi_y   ", Hi_y
`print "n1  ",n1, "  n2  ", n2
`print "n  ",n
`print "co1  ",co1
`print "co2  ",co2
`print "co3  ",co3
`print "co4  ",co4
`print "D(co4,1)  ",D(c4,1)
`print "Hi_y   ",Hi_y
`print "y(Hi_y)  ",y(Hi_y)

`exitfunction


exitfunction_:
UNDIM x(0):UNDIM y(0)    :UNDIM r(0)

UNDIM g(0) :UNDIM b(0)  : exitfunction

 

endfunction

 


 `                              SUPPORTING  FUNCTIONS
 `&&&&&&&&&&&&&&&&&&&&&&&&&&&&    FUNCTIONS  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
 `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
 `&&&&&&&&&&&&&&&&&&&&&&&& INC &&&&&&&&&&&&&&&&&&&&&&&&&&&

function I(value as integer,times)

for t = 1 to times
if value+1 > n then value = 0
inc value
next t

endfunction value
`&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

 `&&&&&&&&&&&&&&&&&&&&&&&&&& DEC  &&&&&&&&&&&&&&&&&&&&&&&&&


 function D(value,times)

for t = 1 to times
if value - 1 < 1 then value = n
dec value
next t

endfunction value
`&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
`&&&&&&&&&&&&&&&&&&&&&&&&&&&  INT_ &&&&&&&&&&&&&&&&&&&&&
function int_(value as float)
a# = int(value+1): a# = a# - value
b# = int(value)  : b# = value - b#

if a# > b# then true_intuger_value = int(value)
if b# > a# then true_intuger_value = int(value+1)
if b# = a# then true_intuger_value = int(value+1)

endfunction true_intuger_value
`&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
`&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&