Author Topic: need help drawing triangles  (Read 5075 times)

0 Members and 1 Guest are viewing this topic.

xteraco

  • Guest
need help drawing triangles
« on: May 16, 2006 »
could someone show me how to draw triangles using ptc? moreso the triangle bit than the ptc bit, such as algo's and how that works, thnx :)

ooh, almost forgot, i'm retarded lol, so pretend your explaining it to a small animal or something of the such, lol
« Last Edit: May 16, 2006 by xteraco »

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: need help drawing triangles
« Reply #1 on: May 16, 2006 »
Ok, here's some source first of all. Luckilly I had this knocking around on my HD, then I'll explain how it works in a minute..

Code: [Select]
'By Shockwave ^ DBF
'-------------------------------------------------------------------------

'-------------------------------------------------------------------------
' Includes.
'-------------------------------------------------------------------------
        '#define PTC_WIN
        #define dbfcls redim shared buffer (640*480) as integer
        #Include Once "tinyptc.bi"
        option dynamic
'-------------------------------------------------------------------------
' Open 640 X 480 Screen.
'-------------------------------------------------------------------------

        If( ptc_open( "Blah", 640, 480 ) = 0 ) Then
        End -1
        End If
   
'-------------------------------------------------------------------------
' Variable Definitions.
'-------------------------------------------------------------------------
        dim shared pi as double
        dim shared ca as integer
        pi=3.1415926535897932385
        Dim Shared LP As Integer: ' Used In Loops.
        Dim Shared As Integer Buffer( 640 * 480 ):' Screen Buffer.
        DIM SHARED AS INTEGER CPAL (255):' Holds Palette.
        Dim Shared GADD As Integer:' General Purpose Variable Just Ticks Up.
        Dim Shared SMSIN As DOUBLE:' Holds X Movement.
        DIM SHARED SMCOS As DOUBLE:' Holds Y Movement.
        DIM SHARED ZMCOS As DOUBLE:' Holds Z Movement.
        DIM SHARED STPTR AS INTEGER:' Pointer Used When Looping Through Stars.
        DIM SHARED STST AS INTEGER:' Holds Deepest Star Pos.
        DIM SHARED A AS INTEGER:' Holds Colour Multiplier.
               
       
'-----------------------------------------------------------------
'Read In Our Font;
'-----------------------------------------------------------------
        dim shared FONT (81 * 59) as integer
        FOR LP=1 TO (81*59)
                READ FONT(LP)
        NEXT
       
'-------------------------------------------------------------------------
' Subroutine Definitions.
'-------------------------------------------------------------------------

        DECLARE SUB CONSTRUCT()
        DECLARE SUB ROTATE()       
        declare sub DBFTEXT(BYVAL BX AS INTEGER , BYVAL BY AS INTEGER , BYVAL CH AS INTEGER , BYVAL CLR AS INTEGER)       
        declare SUB Millisecs()
        declare sub DBFWPF(BYVAL BX AS INTEGER, BYVAL BY AS INTEGER , BYVAL CH AS INTEGER )
        declare sub DBFTRI(BYVAL QX1 AS DOUBLE , BYVAL QY1 AS DOUBLE ,BYVAL QX2 AS DOUBLE , BYVAL QY2 AS DOUBLE,BYVAL QX3 AS DOUBLE , BYVAL QY3 AS DOUBLE , BYVAL QCLR AS INTEGER, BYVAL EDGE AS INTEGER)
       
        DIM SHARED XRES,YRES AS INTEGER
        XRES=640
        YRES=480
'----------------------------
'---    Set Up Starfield; ---
'----------------------------

       
'---------------
'-Debug Stuff; -
'---------------
    dim shared oldtime,newtime as double
    dim shared TST as string
    dim shared ticks as integer
    ticks=0
   
    '---------------------------------------------------------
'            Define the necessary variables;
'---------------------------------------------------------
    DIM shared N AS DOUBLE
    dim shared vx1 as double
    dim shared vx2 as double
    dim shared vy1 as double
    dim shared vy2 as double


    DIM SHARED SIZE AS DOUBLE
    DIM SHARED POINTS AS INTEGER
    DIM SHARED FACES AS INTEGER

    DIM SHARED VXR as double
    dim shared VYR as double
    dim shared VZR AS double

    size=19
    points=14
    faces=24

    Dim SHARED Vx(points) AS DOUBLE
    Dim SHARED Vy(points) AS DOUBLE
    Dim SHARED Vz(points) AS DOUBLE
   
    Dim SHARED Vtx(points) AS INTEGER
    Dim SHARED Vty(points) AS INTEGER
    Dim SHARED Vtz(points) AS INTEGER
   
    Dim SHARED Vf1(faces) AS DOUBLE
    Dim SHARED Vf2(faces) AS DOUBLE
    Dim SHARED Vf3(faces) AS DOUBLE
    Dim SHARED Vf4(faces) AS DOUBLE
    Dim SHARED Vr(faces) AS DOUBLE
    Dim SHARED Vg(faces) AS DOUBLE
    Dim SHARED Vb(faces) AS DOUBLE

   
   
 
'---------------------------------------------------------
'               Read in the Object's points;
'---------------------------------------------------------

For a=1 To points
 Read Vx(a)
 read Vy(a)
 read Vz(a)
Next

For a=1 To faces
Read Vf1(a)
Read Vf2(a)
Read Vf3(a)
Read Vf4(a)
Read Vr(a)
READ Vg(a)
READ Vb(a)
Next

'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

   

'--------------------------------------------------
'---    Main Loop, Repeat Until Escape Pressed. ---
'--------------------------------------------------
oldtime=timer

DO
    GADD=GADD+1               
    dbfcls
    MILLISECS()
   
    ROTATE()
    CONSTRUCT()
       
    Ptc_Update @Buffer(0):' Update The Buffer.
    ticks=ticks+1
   
LOOP UNTIL INKEY$ = CHR$(27)
Ptc_Close()
END

'-------------------------------------------------------------------------------
' Triangle Routine By Shockwave / DBF 2006.
' Dependent on DBFWPF sub and also needs an array called buffer of 640*480
' to plot the pixels.
'-------------------------------------------------------------------------------

SUB DBFTRI(BYVAL QX1 AS DOUBLE , BYVAL QY1 AS DOUBLE ,BYVAL QX2 AS DOUBLE , BYVAL QY2 AS DOUBLE,BYVAL QX3 AS DOUBLE , BYVAL QY3 AS DOUBLE, BYVAL QCLR AS INTEGER , BYVAL EDGE AS INTEGER)
DIM QFTY1 AS DOUBLE , QFTY2 AS DOUBLE ,QFTY3 AS DOUBLE ,QFTX1 AS DOUBLE ,QFTX2 AS DOUBLE ,QFTX3 AS DOUBLE

'-------------------------------------------------------------------------------
'Get rid of flat bottomed and flat topped tris, fuck accuracy, lets go for speed :-)
'-------------------------------------------------------------------------------

IF QY1=QY2 OR QY1=QY3 THEN QY1=QY1-1
IF QY2=QY3 THEN QY3=QY3+1

        QFTY1=QY1
        QFTY2=QY2
        QFTY3=QY3
       
        QFTX1=QX1
        QFTX2=QX2
        QFTX3=QX3
'-------------------------------------------------------------------------------
'SORT POINTS INTO ORDER..
'-------------------------------------------------------------------------------

'----------------------
'       ISOLATE TOP.---
'----------------------

        IF QFTY2<QFTY1 AND QFTY2<QFTY3 THEN
                QY1=QFTY2
                QX1=QFTX2
        END IF
        IF QFTY1<QFTY2 AND QFTY1<QFTY3 THEN
                QY1=QFTY1
                QX1=QFTX1
        END IF
        IF QFTY3<QFTY2 AND QFTY3<QFTY1 THEN
                QY1=QFTY3
                QX1=QFTX3
        END IF
       
'-------------------------
'       ISOLATE MIDDLE.---
'-------------------------

        IF QFTY1>QFTY2 AND QFTY1<QFTY3 THEN
                QY2=QFTY1
                QX2=QFTX1
        END IF
        IF QFTY1<QFTY2 AND QFTY1>QFTY3 THEN
                QY2=QFTY1
                QX2=QFTX1
        END IF
        IF QFTY2>QFTY1 AND QFTY2<QFTY3 THEN
                QY2=QFTY2
                QX2=QFTX2
        END IF
        IF QFTY2<QFTY1 AND QFTY2>QFTY3 THEN
                QY2=QFTY2
                QX2=QFTX2
        END IF
        IF QFTY3>QFTY1 AND QFTY3<QFTY2 THEN
                QY2=QFTY3
                QX2=QFTX3
        END IF
        IF QFTY3<QFTY1 AND QFTY3>QFTY2 THEN
                QY2=QFTY3
                QX2=QFTX3
        END IF
       
'-------------------------       
'       ISOLATE BOTTOM;---
'-------------------------

        IF QFTY1>QFTY2 AND QFTY1>QFTY3 THEN
                QY3=QFTY1
                QX3=QFTX1
        END IF
        IF QFTY2>QFTY1 AND QFTY2>QFTY3 THEN
                QY3=QFTY2
                QX3=QFTX2
        END IF
        IF QFTY3>QFTY2 AND QFTY3>QFTY1 THEN
                QY3=QFTY3
                QX3=QFTX3
        END IF


'------------------------------------------
'Interpolation, Edge And Loop Variables;---
'------------------------------------------

                DIM QT1,QT2 AS INTEGER:' loops.

                DIM QSTART1 AS DOUBLE:'Edge 1
                DIM QSTART2 AS DOUBLE:'Edge 2
                DIM QSTADD1 AS DOUBLE'Interpolation 1
                DIM QSTADD2 AS DOUBLE'Interpolation 2
               
               'Set Start Points
               
                QSTART1=QX1               
                QSTART2=QX1
               
                'Calculate Interpolations;
               
                 QSTADD1 = (QX2-QX1) / (QY2 - QY1)                         
                 QSTADD2 = (QX3-QX1) / (QY3 - QY1)
                 
'-------------------------
' Draw Top Of Triangle;---
'-------------------------

FOR QA=QY1 TO QY2-1
   
    IF QSTART1<=QSTART2 THEN
        QT1=QSTART1
        QT2=QSTART2
    ELSE
        QT1=QSTART2
        QT2=QSTART1
    END IF
    FOR QBB=QT1 TO QT2
        DBFWPF(QBB,QA,QCLR)
    NEXT
    DBFWPF(QT1,QA,EDGE)
    DBFWPF(QT2,QA,EDGE)
   
   
    QSTART1=QSTART1+QSTADD1   
    QSTART2=QSTART2+QSTADD2
       
NEXT


QSTADD1= (QX3-QSTART1) / (QY3 - QY2)   

'----------------------------
' Draw Bottom Of Triangle;---
'----------------------------

FOR QA=QY2 TO QY3
    IF QSTART1<=QSTART2 THEN
        QT1=QSTART1
        QT2=QSTART2
    ELSE
        QT1=QSTART2
        QT2=QSTART1
    END IF
    FOR QBB=QT1 TO QT2
        DBFWPF(QBB,QA,QCLR)
    NEXT
    DBFWPF(QT1,QA,EDGE)
    DBFWPF(QT2,QA,EDGE)
   

   
    QSTART1=QSTART1+QSTADD1
    QSTART2=QSTART2+QSTADD2
NEXT
END SUB

'---------------------------------------------------------------------------
'DBF WRITEPIXELFAST COMMAND BY SHOCKWAVE 2006. NEEDS BUFFER ARRAY 640*480---
'---------------------------------------------------------------------------

SUB DBFWPF (BYVAL BX AS INTEGER , BYVAL BY AS INTEGER , BYVAL CH AS INTEGER)
   
    IF (BX>0) AND (BX<640) AND (BY>0) AND (BY<480) THEN
        BUFFER ((BY*640)+BX)=CH       
    END IF
   
END SUB

'-------------------------------------------------------------------------------
' Display FPS.
'-------------------------------------------------------------------------------
SUB Millisecs()
    t=timer

if  t-oldtime >=1 then
    newtime = ticks
    ticks=0
    oldtime=timer
end if

    TST = str( (newtime) )
    TST = "FPS "+TST
    for LP=1 to len(tst)
        CH=(ASC(MID(TST,LP,1)))-31
        DBFTEXT((LP*10),1,CH,&h443333)
    NEXT
   
end sub

'-------------------------------------------------------------------------
'Sub To Draw A Letter AnyWhere On The Screen (With Clipping);
'-------------------------------------------------------------------------
sub DBFTEXT(BYVAL BX AS INTEGER , BYVAL BY AS INTEGER , BYVAL CH AS INTEGER , BYVAL CLR AS INTEGER)
dim blx,bly as integer
    '---------------------------------
    'Calculate Offset In Font Data;---
    '---------------------------------
    bm=(ch*81)-81
    FOR BLY=0 TO 8
    FOR BLX=1 TO 9
        '--------
        'Clip;---
        '--------
        IF (BX+BLX>0) AND (BX+BLX<639) AND (BY+BLY>0) AND (BY+BLY<479) THEN
           
            '----------------------------------------------------
            'Draw Pixel In Buffer If Onscreen And If Binary 1 ---
            '----------------------------------------------------
           
            MM= FONT(((BLY*9)+BLX)+BM)
            IF MM >0 THEN BUFFER (((BY+BLY)*640)+BX+BLX)=CLR
        END IF
    NEXT
    NEXT
END SUB



'---------------------------------------------------------
'                     Draw The Object;
'---------------------------------------------------------

SUB construct()
    DIM N AS DOUBLE

    For a=1 To faces

'---------------------------------------------------------
'              Draw A Face Of The Object;
'---------------------------------------------------------


  vx1= Vtx(Vf1(a))-Vtx(Vf2(a))
  vy1= Vty(Vf1(a))-Vty(Vf2(a))
  vx2= Vtx(Vf3(a))-Vtx(Vf2(a))
  vy2= Vty(Vf3(a))-Vty(Vf2(a))
  n=  vx1*vy2-vx2*vy1
 
If n<0 THEN

    n=-(n/400)
                        rd=Vr(a)+n : If rd>155 THEN rd=155
                        gr=Vg(a)+n : If gr>155 THEN gr=155
                        bl=Vb(a)+n : If bl>155 THEN bl=155
                        DBFTRI(Vtx(Vf1(a)),Vty(Vf1(a)),Vtx(Vf2(a)),Vty(Vf2(a)),Vtx(Vf3(a)),Vty(Vf3(a)),RGB(RD,GR,BL),RGB(RD/2,GR/2,BL/2))
                        DBFTRI(Vtx(Vf1(a)),Vty(Vf1(a)),Vtx(Vf4(a)),Vty(Vf4(a)),Vtx(Vf3(a)),Vty(Vf3(a)),RGB(RD,GR,BL),RGB(RD/2,GR/2,BL/2))
    End If
    NEXT

END SUB


SUB ROTATE()

    DIM VX1 AS DOUBLE
    dim VY1 AS DOUBLE
    dim VZ1 AS DOUBLE
   
    DIM VZZ AS DOUBLE   
    dim vxx as double
    dim vyy as double
   
    DIM VDV AS DOUBLE

'###############################################
'## Rotate And Scale Each Point! Store Result ##
'###############################################
 For a=1 To points

    VX1=Vx(a)
    VY1=Vy(a)
    VZ1=Vz(a)
   
'######################
'## X,Y,Z rotations! ##
'######################
  Vxx=Vx1
  Vyy=Vy1*cos(Vxr)+Vz1*sin(Vxr)
  Vzz=Vz1*cos(Vxr)-Vy1*sin(Vxr)
 
  Vy1=Vyy
  Vx1=Vxx*cos(Vyr)-Vzz*sin(Vyr)
  Vz1=Vxx*sin(Vyr)+Vzz*cos(Vyr)
 
  Vzz=Vz1
  Vxx=Vx1*cos(Vzr)-Vy1*sin(Vzr)
  Vyy=Vx1*sin(Vzr)+Vy1*cos(Vzr)
'########################
'## Apply Perspective! ##
'########################
  Vdv=(Vzz/40)+1

  Vxx=(size*(Vxx/Vdv))+320
  Vyy=(size*(Vyy/Vdv))+240
  Vtx(a)=Int(Vxx)
  Vty(a)=Int(Vyy)
  Vtz(a)=Int(Vzz)
 
 Next
        Vxr=Vxr+0.008
        Vyr=Vyr+0.007
        Vzr=Vzr+0.006
       
       
END SUB




'==============================================================================
' Binary Font By Shockwave / DBF; (59 Chars)
'==============================================================================

'space
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
'!
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
'"
data 0,1,1,0,1,1,0,0,0
data 0,1,1,0,1,1,0,0,0
data 0,1,1,0,1,1,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
'#
data 0,0,0,0,0,0,0,0,0
data 0,1,1,0,0,0,1,1,0
data 1,1,1,1,1,1,1,1,1
data 0,1,1,0,0,0,1,1,0
data 0,1,1,0,0,0,1,1,0
data 0,1,1,0,0,0,1,1,0
data 1,1,1,1,1,1,1,1,1
data 0,1,1,0,0,0,1,1,0
data 0,0,0,0,0,0,0,0,0
'£
data 0,0,1,1,1,1,0,0,0
data 0,1,1,1,1,1,0,0,0
data 0,1,1,0,0,0,0,0,0
data 0,1,1,0,0,0,0,0,0
data 0,1,1,1,1,0,0,0,0
data 0,1,1,0,0,0,0,0,0
data 0,1,1,0,0,0,0,0,0
data 1,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,0
'%
data 0,0,0,0,0,0,0,0,0
data 0,1,1,0,0,0,0,0,0
data 0,1,1,0,0,0,1,0,0
data 0,0,0,0,0,1,0,0,0
data 0,0,0,0,1,0,0,0,0
data 0,0,0,1,0,0,0,0,0
data 0,0,1,0,0,0,1,1,0
data 0,0,0,0,0,0,1,1,0
data 0,0,0,0,0,0,0,0,0
'&
data 0,0,0,1,1,1,0,0,0
data 0,0,1,1,1,1,1,0,0
data 0,0,1,1,0,1,1,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,1,1,0,1,1,0,0
data 0,1,1,0,0,1,1,1,1
data 0,1,1,1,0,0,1,1,0
data 0,0,1,1,1,1,1,0,0
data 0,0,0,0,1,1,0,0,0
''
data 0,1,1,0,0,0,0,0,0
data 0,1,1,0,0,0,0,0,0
data 0,1,1,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
'(
data 0,0,0,0,0,1,1,1,0
data 0,0,0,0,1,1,1,1,0
data 0,0,0,0,1,1,0,0,0
data 0,0,0,0,1,1,0,0,0
data 0,0,0,0,1,1,1,0,0
data 0,0,0,0,1,1,1,0,0
data 0,0,0,0,1,1,1,0,0
data 0,0,0,0,1,1,1,1,0
data 0,0,0,0,0,1,1,1,0
')
data 0,1,1,1,0,0,0,0,0
data 0,1,1,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,1,1,1,0,0,0,0
data 0,0,1,1,1,0,0,0,0
data 0,0,1,1,1,0,0,0,0
data 0,1,1,1,1,0,0,0,0
data 0,1,1,1,0,0,0,0,0
'*
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,1,0,0,0,0
data 0,0,1,0,1,0,1,0,0
data 0,0,0,1,1,1,0,0,0
data 0,1,1,1,1,1,1,1,0
data 0,0,0,1,1,1,0,0,0
data 0,0,1,0,1,0,1,0,0
data 0,0,0,0,1,0,0,0,0
data 0,0,0,0,0,0,0,0,0
'+
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,1,0,0,0,0
data 0,0,0,0,1,0,0,0,0
data 0,0,1,1,1,1,1,0,0
data 0,0,0,0,1,1,0,0,0
data 0,0,0,0,1,1,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
''
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,0,1,0,0,0,0
'-
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,1,1,1,1,1,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
'.
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
'/
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,1,0
data 0,0,0,0,0,0,1,1,0
data 0,0,0,0,0,1,1,0,0
data 0,0,0,0,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,1,1,1,0,0,0,0
data 0,1,1,1,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
'0
data 0,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,1,1,1
data 1,1,0,0,0,1,0,1,1
data 1,1,0,0,1,0,0,1,1
data 1,1,0,1,1,0,0,1,1
data 1,1,1,1,0,0,0,1,1
data 1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,0
'1
data 0,0,0,1,1,0,0,0,0
data 0,0,1,1,1,0,0,0,0
data 0,0,1,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,1,1,1,1,1,1,0,0
data 0,1,1,1,1,1,1,0,0
'2
data 0,0,1,1,1,1,1,1,0
data 0,0,1,1,1,1,1,1,1
data 0,0,0,0,0,0,0,1,1
data 0,0,0,0,0,0,0,1,1
data 0,1,1,1,1,1,1,1,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1
'3
data 1,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,0
data 0,0,0,0,0,0,0,1,1
data 0,0,0,0,0,0,0,1,1
data 0,0,0,0,1,1,1,1,0
data 0,0,0,0,0,0,1,1,1
data 0,0,0,0,0,0,1,1,1
data 1,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,0
'4
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,1,1,0,0,0
data 1,1,0,0,1,1,0,0,0
data 1,1,1,1,1,1,1,1,1
data 0,0,0,0,0,1,1,1,0
data 0,0,0,0,0,1,1,1,0
data 0,0,0,0,0,1,1,1,0
data 0,0,0,0,0,1,1,1,0
'5
data 0,1,1,1,1,1,1,0,0
data 0,1,1,1,1,1,1,0,0
data 0,1,1,0,0,0,0,0,0
data 0,1,1,0,0,0,0,0,0
data 0,1,1,1,1,1,1,1,0
data 0,0,0,0,0,0,1,1,1
data 0,0,0,0,0,0,1,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,0
'6
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 1,1,1,1,1,1,1,1,0
data 1,1,0,0,0,0,1,1,1
data 1,1,0,0,0,0,1,1,1
data 1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,0
'7
data 0,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,1
data 0,0,0,0,0,0,0,1,1
data 0,0,0,0,0,0,0,1,1
data 0,0,0,0,1,1,1,1,1
data 0,0,0,0,0,0,1,1,1
data 0,0,0,0,0,1,1,1,0
data 0,0,0,0,0,1,1,1,0
data 0,0,0,0,0,1,1,1,0
'8
data 0,0,1,1,1,1,1,0,0
data 0,1,1,1,1,1,1,1,0
data 0,1,1,0,0,0,1,1,0
data 0,1,1,0,0,0,1,1,0
data 0,0,1,1,1,1,1,0,0
data 1,1,1,0,0,0,1,1,1
data 1,1,0,0,0,0,1,1,1
data 1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,0
'9
data 0,0,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,1
data 0,1,1,0,0,0,0,1,1
data 0,1,1,0,0,0,0,1,1
data 0,0,1,1,1,1,1,1,1
data 0,0,0,0,0,0,1,1,1
data 0,0,0,0,0,0,1,1,1
data 0,0,0,0,0,0,1,1,1
data 0,0,0,0,0,0,1,1,1
':
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
';
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,0,1,0,0,0,0
'<
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,1,1,0,0
data 0,0,0,0,1,1,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,1,1,1,0,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,0,1,1,1,0,0
data 0,0,0,0,0,1,1,0,0
data 0,0,0,0,0,0,0,0,0
'=
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,1,1,1,1,1,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,1,1,1,1,1,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
'>
data 0,0,0,0,0,0,0,0,0
data 0,0,1,1,0,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,0,1,1,0,0,0
data 0,0,0,0,1,1,1,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,1,1,1,0,0,0,0
data 0,1,1,1,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0
'?
data 0,0,1,1,1,1,1,0,0
data 0,1,1,1,1,1,1,1,0
data 0,1,1,0,0,0,1,1,0
data 0,0,0,0,0,0,1,1,0
data 0,0,0,1,1,1,1,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,0,0,0,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
'@
data 0,0,0,0,0,0,0,0,0
data 0,0,1,1,1,1,1,0,0
data 0,1,0,0,0,0,0,0,0
data 0,1,0,0,1,1,1,0,0
data 0,1,0,1,0,0,0,1,0
data 0,1,0,1,1,1,0,1,0
data 0,1,0,0,0,0,0,1,0
data 0,0,1,1,1,1,1,0,0
data 0,0,0,0,0,0,0,0,0
'a
data 0,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,0
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
'b
data 1,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,1,1,1,1,1,0
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,0
'c
data 0,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,1
'd
data 1,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,0
'e
data 0,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 1,1,1,1,1,1,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,1
'f
data 0,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 1,1,1,1,1,1,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
'g
data 0,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,1,1,1,1
data 1,1,0,0,0,0,1,1,1
data 1,1,0,0,0,0,1,1,1
data 1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,0
'h
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
'i
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1
'j
data 1,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,0
data 0,0,0,0,0,0,0,1,1
data 0,0,0,0,0,0,0,1,1
data 0,0,0,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,0
'k
data 1,1,0,0,0,0,1,1,0
data 1,1,0,0,0,0,1,1,0
data 1,1,0,0,0,0,1,1,0
data 1,1,0,0,0,0,1,1,0
data 1,1,1,1,1,1,1,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
'l
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,1
'm
data 0,1,1,1,0,1,1,1,0
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,1,0,0,1,1
data 1,1,0,0,1,0,0,1,1
data 1,1,0,0,1,0,0,1,1
data 1,1,1,0,1,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
'n
data 1,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1

'o
data 0,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,0
'p
data 1,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,1,1,1,1,1,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
'q
data 0,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,1,0,1,1
data 1,1,1,0,0,0,1,1,1
data 1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,0
'r
data 1,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,1,1,1,1,1,0
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
's
data 0,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0
data 0,1,1,1,1,1,1,1,0
data 0,0,0,0,0,0,1,1,1
data 0,0,0,0,0,0,1,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,0
't
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,0,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
data 0,0,0,1,1,1,0,0,0
'u
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,0
'v
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,1,0,0,1,1,1
data 0,1,1,1,1,1,1,1,0
data 0,0,1,1,1,1,1,0,0
'w
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,1,0,1,0,0,1,1
data 1,1,1,0,1,0,0,1,1
data 1,1,1,0,1,0,0,1,1
data 1,1,1,1,1,1,1,1,1
data 0,1,1,1,0,1,1,1,0
'x
data 0,1,1,0,0,0,1,1,0
data 0,1,1,0,0,0,1,1,0
data 0,1,1,0,0,0,1,1,0
data 0,1,1,0,0,0,1,1,0
data 0,1,1,1,1,1,1,1,0
data 1,1,1,1,0,0,1,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
data 1,1,1,0,0,0,0,1,1
'y
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 1,1,0,0,0,0,0,1,1
data 0,1,1,1,1,1,1,1,1
data 0,0,0,0,0,0,1,1,1
data 0,0,0,0,0,0,1,1,1
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,0
'z
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1
data 0,0,0,0,0,0,0,1,1
data 0,0,0,0,0,0,1,1,1
data 0,1,1,1,1,1,1,1,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,0,0,0,0,0,0
data 1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1


'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'   The Object Description As Data!
'   The Data Below Describes A Cube.
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

'Points definition;
'~~~~~~~~~~~~~~~~~~
'Below are the points of the Object defined as x,y,z;

Data 5,-5,-5,5,5,-5,-5,5,-5,-5,-5,-5,0,0,-8,8,0,0,0
Data 8,0,-8,0,0,0,-8,0,5,-5,5,5,5,5,-5,5,5,-5,-5,5,0,0,8


'Connection definition;
'Below are the faces of the Object defined as vertice
'numbers, specified in clockwise order. These are followed
'by r,g,b values For the face And finally cell shaded
'parameter (0)=off (1)=on.

Data 10,9,13,13,50,0,0
Data 14,10,13,13,0,50,0
Data 14,13,12,12,0,50,0
Data 8,12,13,13,0,0,50
Data 4,8,13,13,0,0,50
Data 10,14,11,11,0,50,0
Data 10,11,6,6,50,0,50
Data 4,13,9,9,50,0,0
Data 1,4,9,9,50,0,0
Data 1,9,10,10,50,0,0
Data 6,1,10,10,50,0,50
Data 5,4,1,1,0,50,50
Data 8,4,3,3,0,0,50
Data 3,12,8,8,0,0,50
Data 3,4,5,5,0,50,50
Data 7,12,3,3,50,50,0
Data 14,12,11,11,0,50,0
Data 11,12,7,7,50,50,0
Data 6,11,2,2,50,0,50
Data 11,7,2,2,50,50,0
Data 1,6,2,2,50,0,50
Data 2,5,1,1,0,50,50
Data 2,7,3,3,50,50,0
Data 2,3,5,5,0,50,50

'===============================================================================
'                                     ***END***
'===============================================================================
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: need help drawing triangles
« Reply #2 on: May 16, 2006 »
The thing with any sort of triangle is that you need to look at the information you have first of all.
For a flat shaded triangle, the info will be;

X1,Y1,X2,Y2,X3,Y3

The method I have used above is a rasteriser, the code has a couple of bugs that I know about (I'll get on to those in a minute) but to explain how a rasteriser works is quite easy.

First go off, we need to know which way we're drawing the triangle.

We go from top to bottom in the above code so the first thing to do is to sort the points into order of height since we don't know for sure the order that they are going to be passed to the triangle algo.

So dependent on the Y co-ordinates, the points are sorted.

The next most important thing is the triangles edges, we need to know where the edges are so that we can draw lines between them as we come down the triangle so that we make a solid looking traingle.

We need two sets of x co-ordinate, set them both at the top point of the triangle.
Then we split the drawing of the triangle into two parts, basically one of the x points has to "interpolate" towards to bottom point of the triangle and the other has to interpolate to the middle.
After the top half of the triangle is drawn, both points interpolate towards the end.

In practical terms it means you do this;

X1 = TOPX
X2 = TOPX

XINTERPOLATE1= (MIDDLEX-X1) / (MIDDLEY-TOPY)
XINTERPOLATE2= (BOTTOM-X1) / (BOTTOMY-TOPY)

We divide the distance X between start and target with the amount in pixels between start and target (Y) this will give us a constant value to add to the interpolate variables as we come down the triangle.

the two halves of the triangle can be rendered with two for next loops but don't forget to re-calculate the value that interpolated to the middle after the first loop as we want to interpolate that to the bottom point.

The draw loop is simply a matter of filling pixels in between the two interpolate variables as we scan down the triangle.

BUGS
There is a known bug in my example, please don't beat me up over it because I hadn't had time to fix it yet.
This is when you get a flat topped or flat bottomed triangle.
As a quick work around I checked the Y co-ords and if two of them matched, I altered them by one pixel so that the loops would still work.

The lightsourcing on the object cheats by using the area of the triangle instead of the dot product to light the faces, again I know this :)

Fron rendering a triangle like this, other render methods like gourad shading and affine texture mapping become easy. Once you grasp the concept that the important bit is the edges then you'll be ready to move onto other triangle types.

Shockwave ^ Codigos
Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: need help drawing triangles
« Reply #4 on: May 17, 2006 »
Dang, that's a neat tutorial and one of the best I've seen at explaining 3D math and polygons. Must have taken you an age to make that one Relsoft :) You are so generous with your sources and help. Have some good karma :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: need help drawing triangles
« Reply #5 on: May 17, 2006 »
Hehehehe. Yeah, it's a series intended for Qbasicnews but turned out to be hosted on a couple of sites. The best ones can be found in Phatcode.net and Petesqbsite.com.  Have you finished reading till chapter 5? (the lookat camera)
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: need help drawing triangles
« Reply #6 on: May 18, 2006 »
@Shockave, what would code for flat triangle be like.
@Relsoft - Nice mate, I saw those a bit ago. Welldone.

Cheers.
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: need help drawing triangles
« Reply #7 on: May 18, 2006 »
Here:


Code: [Select]
option explicit
defint a - z
#include "TinyPTC.bi"






'============================main

const SCR_WIDTH = 320  * 2
const SCR_HEIGHT = 240 * 2
const SCR_SIZE = SCR_WIDTH*SCR_HEIGHT

declare sub Rel_Line_H (  buffer(), byval x1 as integer, byval y as integer,_
                         byval x2 as integer, byval col as integer)
declare sub Rel_Tri_F( buffer() as integer, byval x1 as integer, byval y1 as integer,_
                     byval x2 as integer, byval y2 as integer, byval x3 as integer,_
                     byval y3 as integer, byval col as integer)


redim Buffer(SCR_SIZE - 1) as integer

if( ptc_open( "Triangle stuff", SCR_WIDTH, SCR_HEIGHT ) = 0 ) then
end -1
end if

dim as integer x1,y1,x2,y2,x3,y3,colour
randomize timer

do
   
    x1 = rnd*SCR_WIDTH
    y1 = rnd*SCR_HEIGHT
    x2 = rnd*SCR_WIDTH
    y2 = rnd*SCR_HEIGHT
    x3 = rnd*SCR_WIDTH
    y3 = rnd*SCR_HEIGHT
    colour = (rnd * 255) shl 16 or (rnd * 255) shl 8 or (rnd * 255)
   
    Rel_Tri_F( buffer(), x1, y1, x2, y2, x3, y3, colour)
   
    ptc_update @buffer(0)
loop until inkey$<>""


ptc_close

end


'==========================subs

sub Rel_Line_H (  buffer(), byval x1 as integer, byval y as integer, byval x2 as integer, byval col as integer)

const SCR_X_MAX = SCR_WIDTH - 1
const SCR_Y_MAX = SCR_HEIGHT - 1

dim wid as integer
dim offset as long
dim temp as integer
dim p as integer ptr


if (y < 0) or (y > SCR_Y_MAX)  then exit sub

if (x1 > x2) then
temp = x1
x1 = x2
x2 = temp
    end if

if x1 > SCR_X_MAX then exit sub

if x2 < 0 then exit sub

if x1 < 0 then
x1 = 0
if (x2 - x1) < 0 then exit sub
end if

if x2 > SCR_X_MAX then
x2 = SCR_X_MAX
if (x2 - x1) < 0 then exit sub
end if

wid = (x2 - x1) + 1
if wid <= 0  then exit sub

p = @buffer(0) + (y * SCR_WIDTH + x1)
asm
   mov edi, dword ptr [p]
   mov eax, dword ptr [col]
   mov ecx, dword ptr [wid]
   rep stosd
end asm

end sub


sub Rel_Tri_F( buffer() as integer, byval x1 as integer, byval y1 as integer,_
                     byval x2 as integer, byval y2 as integer, byval x3 as integer,_
                     byval y3 as integer, byval col as integer)



    dim dx1 as integer, dy1 as integer
    dim dx2 as integer, dy2 as integer
    dim dx3 as integer, dy3 as integer
   

    dim delta1 as integer, delta2 as integer, delta3 as integer
    dim Lx as integer, Rx  as integer
   
    dim p_buffer as integer ptr
   
   
    IF y2 < y1 THEN
        SWAP y1, y2
        SWAP x1, x2
    END IF
    IF y3 < y1 THEN
        SWAP y3, y1
        SWAP x3, x1
    END IF
   
    IF y3 < y2 THEN
        SWAP y3, y2
        SWAP x3, x2
    END IF
   
    dx1 = x2 - x1
    dy1 = y2 - y1
    IF dy1 <> 0 THEN
        delta1 = (dx1 shl 16) \ dy1
    ELSE
        delta1 = 0
    END IF
   
    dx2 = x3 - x2
    dy2 = y3 - y2
    IF dy2 <> 0 THEN
        delta2 = (dx2 shl 16) \ dy2
    ELSE
        delta2 = 0
    END IF


    dx3 = x1 - x3
    dy3 = y1 - y3
    IF dy3 <> 0 THEN
        delta3 = (dx3 shl 16) \ dy3
    ELSE
        delta3 = 0
    END IF
   
   
    'Flat bottom
    'Top part of triangle

    dim y as integer
    dim Lxo as integer, Rxo as integer

    Lx = x1 shl 16
    Rx = Lx
   
    FOR y = y1 TO y2 - 1
        Lxo = Lx shr 16
        Rxo = Rx shr 16
        Rel_Line_H buffer(), Lxo, y, Rxo, col
        Lx = Lx + delta1
        Rx = Rx + delta3
    NEXT y

    'Flat top
    'Lower part of triangle

    Lx = x2 shl 16
    FOR y = y2 TO y3
        Lxo = Lx shr 16
        Rxo = Rx shr 16
        Rel_Line_H buffer(), Lxo, y, Rxo, col
        Lx = Lx + delta2
        Rx = Rx + delta3
    NEXT y

end sub
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: need help drawing triangles
« Reply #8 on: May 18, 2006 »
Cheers.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: