Home » Recent acquisitions » Acorn ADFS disks » adfs_AcornUser_199312.adf » !StarInfo_StarInfo » Cason/c/Gouraud

Cason/c/Gouraud

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Recent acquisitions » Acorn ADFS disks » adfs_AcornUser_199312.adf » !StarInfo_StarInfo
Filename: Cason/c/Gouraud
Read OK:
File size: 1496 bytes
Load address: 0000
Exec address: 0000
File contents
/* c.gouraud 

   Gouraud shading in C

   By  Neil Cason
   For 32-bit machines
   (c) BAU December 1993
*/

#include "bbc.h"
#include "kernel.h"
#include "swis.h"
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    int x0;
    int y0;
    int x1;
    int y1;
    int x2;
    int y2;
} vertices;

void gouraud(vertices *, int *, int, int, int);

#define swap(arg1, arg2) { int temp=0; temp=arg1; arg1=arg2; arg2=temp; }
#define sgn(x) ((x) > 0 ? 1: ((x) == 0 ? 0: (-1))) 

int xl[256], xr[256], il[256], ir[256];

int main(void)
{
    vertices pts;
    int cnt, cols[16];

    bbc_mode(13);
    pts.x0 = 12;
    pts.y0 = 12;
    pts.x1 = 200;
    pts.y1 = 12;
    pts.x2 = 100;
    pts.y2 = 200;

    for (cnt=0; cnt<=3; cnt++)
    {
        cols[cnt] = cnt;
        cols[cnt+4] = cnt+44;
        cols[cnt+8] = cnt+208;
        cols[cnt+12] = cnt+252;
    }
    gouraud(&pts, &cols[0], 0, 8, 15);
}

void __nc_line(int x1, int y1, int x2, int y2, int side, int i0, int i1)
{
    int dy,dx,d,incr1,incr2,inc,end,len,j, is, id;

    dx = abs(x2-x1);
    dy = abs(y2-y1);

    if (dx > dy)
    {
        d = (dy<<1) - dx;
        incr1 = (dy<<1);
        incr2 = (dy - dx)<<1;
        if (y1>=y2) inc=1; else inc=-1;
        if (x1 > x2)
        {
             end=x1;
             x1=x2;
             y1=y2;
        }
        else
        {
            inc=-inc;
            end = x2;
        }
        len = end - x1;
        id = (i1 - i0) << 20;
        is = id / len;
        id = i0 << 20;

        if (side == 0)
        {
            for (j=0; j<=len; j++)
            {
                xl[y1] = x1+j;
                il[y1] = id;
                id += is;
                if (d<0) d+=incr1;
                else
                {
                    y1 += inc;
                    d += incr2;
                }
            }
        }
        if (side == 1)
        {
            for (j=0; j<=len; j++)
            {
                xr[y1] = x1+j;
                ir[y1] = id;
                id += is;
                if (d<0) d+=incr1;
                else
                {
                    y1 += inc;
                    d += incr2;
                }
            }
        }
    }
    else
    {
        d = (dx<<1) - dy;
        incr1 = (dx<<1);
        incr2 = (dx-dy)<<1;
        if (x1 >= x2) inc = 1; else inc=-1;
        if (y1 > y2)
        {
            end = y1;
            y1 = y2;
            x1 = x2;
        }
        else
        {
            inc =-inc;
            end = y2;
        }
        len = end - y1;

        id = (i1 - i0) << 20;
        is = id / len;
        id = i0 << 20;

        if (side == 0)
        {
            for (j=0; j<=len; j++)
            {
                xl[y1+j] = x1;
                il[y1+j] = id;
                id += is;
                if (d<0) d+=incr1;
                else
                {
                    x1+=inc;
                    d+=incr2;
                }
            }
        }
        if (side == 1)
        {
            for (j=0; j<=len; j++)
            {
                xr[y1+j] = x1;
                ir[y1+j] = id;
                id += is;
                if (d<0) d+=incr1;
                else
                {
                    x1+=inc;
                    d+=incr2;
                }
            }
        }
    }
}

void gouraud(vertices *pts, int *colours, int i0, int i1, int i2)
{
    int vdu[2], y, ys, sl, sr, screendist, istart, iend, idelta, len, x;
    int screenstart, error, intens;
    char *ptr;
    _kernel_swi_regs r;

    vdu[0] = 148;
    vdu[1] = -1;
    r.r[0] = r.r[1] = (int) &vdu;
    _kernel_swi(OS_ReadVduVariables,&r,&r);

    screenstart = vdu[0];

    if (pts->y0 < pts->y1)
    {
        swap (pts->x0,pts->x1);
        swap (pts->y0,pts->y1);
    }

    if (pts->y1 < pts->y2)
    {
        swap (pts->x1,pts->x2); /* Make triangle the right way up */
        swap (pts->y1,pts->y2);
    }

    if (pts->y0 < pts->y1)
    {
        swap (pts->x0,pts->x1);
        swap (pts->y0,pts->y1);
    }

    __nc_line(pts->x2,pts->y2,pts->x1,pts->y1,0,i2,i1);
    __nc_line(pts->x1,pts->y1,pts->x0,pts->y0,0,i1,i0); /* Draw lines */
    __nc_line(pts->x2,pts->y2,pts->x0,pts->y0,1,i2,i0);

    ys = sgn(pts->y2 - pts->y0);

    for (y=pts->y2; y<=pts->y0; y++)
    {
        sl = xl[y];
        sr = xr[y];
        istart = il[y];
        iend = ir[y];

        screendist = (y<<8) + (y<<6) + screenstart;

        if (sl > sr)
        {
            sl ^= sr;
            sr = sl ^ sr;
            sl ^= sr;
            istart ^= iend;
            iend = istart ^ iend;
            istart ^= iend;
        }

        if (sr != sl)
            idelta = (iend - istart) / (sr-sl); /* Intens change for each pixel plotted */
        len = screendist + sr;

        for (x=screendist+sl; x<=len; x++)
        {
            intens = istart>>20;
            error += istart - (istart & 535822336); /* Add on error */
            if (error > 1048576)
            {
                intens ++;
                error -= 1048576;                   /* If overflow... */
            }
            ptr = (char *) x;
            *ptr = (char) colours[intens];          /* Plot */
            istart += idelta;   /* Increment intensity */
        }
    }
}
00000000  2f 2a 20 63 2e 67 6f 75  72 61 75 64 20 0a 0a 20  |/* c.gouraud .. |
00000010  20 20 47 6f 75 72 61 75  64 20 73 68 61 64 69 6e  |  Gouraud shadin|
00000020  67 20 69 6e 20 43 0a 0a  20 20 20 42 79 20 20 4e  |g in C..   By  N|
00000030  65 69 6c 20 43 61 73 6f  6e 0a 20 20 20 46 6f 72  |eil Cason.   For|
00000040  20 33 32 2d 62 69 74 20  6d 61 63 68 69 6e 65 73  | 32-bit machines|
00000050  0a 20 20 20 28 63 29 20  42 41 55 20 44 65 63 65  |.   (c) BAU Dece|
00000060  6d 62 65 72 20 31 39 39  33 0a 2a 2f 0a 0a 23 69  |mber 1993.*/..#i|
00000070  6e 63 6c 75 64 65 20 22  62 62 63 2e 68 22 0a 23  |nclude "bbc.h".#|
00000080  69 6e 63 6c 75 64 65 20  22 6b 65 72 6e 65 6c 2e  |include "kernel.|
00000090  68 22 0a 23 69 6e 63 6c  75 64 65 20 22 73 77 69  |h".#include "swi|
000000a0  73 2e 68 22 0a 23 69 6e  63 6c 75 64 65 20 3c 73  |s.h".#include <s|
000000b0  74 64 69 6f 2e 68 3e 0a  23 69 6e 63 6c 75 64 65  |tdio.h>.#include|
000000c0  20 3c 73 74 64 6c 69 62  2e 68 3e 0a 0a 74 79 70  | <stdlib.h>..typ|
000000d0  65 64 65 66 20 73 74 72  75 63 74 0a 7b 0a 20 20  |edef struct.{.  |
000000e0  20 20 69 6e 74 20 78 30  3b 0a 20 20 20 20 69 6e  |  int x0;.    in|
000000f0  74 20 79 30 3b 0a 20 20  20 20 69 6e 74 20 78 31  |t y0;.    int x1|
00000100  3b 0a 20 20 20 20 69 6e  74 20 79 31 3b 0a 20 20  |;.    int y1;.  |
00000110  20 20 69 6e 74 20 78 32  3b 0a 20 20 20 20 69 6e  |  int x2;.    in|
00000120  74 20 79 32 3b 0a 7d 20  76 65 72 74 69 63 65 73  |t y2;.} vertices|
00000130  3b 0a 0a 76 6f 69 64 20  67 6f 75 72 61 75 64 28  |;..void gouraud(|
00000140  76 65 72 74 69 63 65 73  20 2a 2c 20 69 6e 74 20  |vertices *, int |
00000150  2a 2c 20 69 6e 74 2c 20  69 6e 74 2c 20 69 6e 74  |*, int, int, int|
00000160  29 3b 0a 0a 23 64 65 66  69 6e 65 20 73 77 61 70  |);..#define swap|
00000170  28 61 72 67 31 2c 20 61  72 67 32 29 20 7b 20 69  |(arg1, arg2) { i|
00000180  6e 74 20 74 65 6d 70 3d  30 3b 20 74 65 6d 70 3d  |nt temp=0; temp=|
00000190  61 72 67 31 3b 20 61 72  67 31 3d 61 72 67 32 3b  |arg1; arg1=arg2;|
000001a0  20 61 72 67 32 3d 74 65  6d 70 3b 20 7d 0a 23 64  | arg2=temp; }.#d|
000001b0  65 66 69 6e 65 20 73 67  6e 28 78 29 20 28 28 78  |efine sgn(x) ((x|
000001c0  29 20 3e 20 30 20 3f 20  31 3a 20 28 28 78 29 20  |) > 0 ? 1: ((x) |
000001d0  3d 3d 20 30 20 3f 20 30  3a 20 28 2d 31 29 29 29  |== 0 ? 0: (-1)))|
000001e0  20 0a 0a 69 6e 74 20 78  6c 5b 32 35 36 5d 2c 20  | ..int xl[256], |
000001f0  78 72 5b 32 35 36 5d 2c  20 69 6c 5b 32 35 36 5d  |xr[256], il[256]|
00000200  2c 20 69 72 5b 32 35 36  5d 3b 0a 0a 69 6e 74 20  |, ir[256];..int |
00000210  6d 61 69 6e 28 76 6f 69  64 29 0a 7b 0a 20 20 20  |main(void).{.   |
00000220  20 76 65 72 74 69 63 65  73 20 70 74 73 3b 0a 20  | vertices pts;. |
00000230  20 20 20 69 6e 74 20 63  6e 74 2c 20 63 6f 6c 73  |   int cnt, cols|
00000240  5b 31 36 5d 3b 0a 0a 20  20 20 20 62 62 63 5f 6d  |[16];..    bbc_m|
00000250  6f 64 65 28 31 33 29 3b  0a 20 20 20 20 70 74 73  |ode(13);.    pts|
00000260  2e 78 30 20 3d 20 31 32  3b 0a 20 20 20 20 70 74  |.x0 = 12;.    pt|
00000270  73 2e 79 30 20 3d 20 31  32 3b 0a 20 20 20 20 70  |s.y0 = 12;.    p|
00000280  74 73 2e 78 31 20 3d 20  32 30 30 3b 0a 20 20 20  |ts.x1 = 200;.   |
00000290  20 70 74 73 2e 79 31 20  3d 20 31 32 3b 0a 20 20  | pts.y1 = 12;.  |
000002a0  20 20 70 74 73 2e 78 32  20 3d 20 31 30 30 3b 0a  |  pts.x2 = 100;.|
000002b0  20 20 20 20 70 74 73 2e  79 32 20 3d 20 32 30 30  |    pts.y2 = 200|
000002c0  3b 0a 0a 20 20 20 20 66  6f 72 20 28 63 6e 74 3d  |;..    for (cnt=|
000002d0  30 3b 20 63 6e 74 3c 3d  33 3b 20 63 6e 74 2b 2b  |0; cnt<=3; cnt++|
000002e0  29 0a 20 20 20 20 7b 0a  20 20 20 20 20 20 20 20  |).    {.        |
000002f0  63 6f 6c 73 5b 63 6e 74  5d 20 3d 20 63 6e 74 3b  |cols[cnt] = cnt;|
00000300  0a 20 20 20 20 20 20 20  20 63 6f 6c 73 5b 63 6e  |.        cols[cn|
00000310  74 2b 34 5d 20 3d 20 63  6e 74 2b 34 34 3b 0a 20  |t+4] = cnt+44;. |
00000320  20 20 20 20 20 20 20 63  6f 6c 73 5b 63 6e 74 2b  |       cols[cnt+|
00000330  38 5d 20 3d 20 63 6e 74  2b 32 30 38 3b 0a 20 20  |8] = cnt+208;.  |
00000340  20 20 20 20 20 20 63 6f  6c 73 5b 63 6e 74 2b 31  |      cols[cnt+1|
00000350  32 5d 20 3d 20 63 6e 74  2b 32 35 32 3b 0a 20 20  |2] = cnt+252;.  |
00000360  20 20 7d 0a 20 20 20 20  67 6f 75 72 61 75 64 28  |  }.    gouraud(|
00000370  26 70 74 73 2c 20 26 63  6f 6c 73 5b 30 5d 2c 20  |&pts, &cols[0], |
00000380  30 2c 20 38 2c 20 31 35  29 3b 0a 7d 0a 0a 76 6f  |0, 8, 15);.}..vo|
00000390  69 64 20 5f 5f 6e 63 5f  6c 69 6e 65 28 69 6e 74  |id __nc_line(int|
000003a0  20 78 31 2c 20 69 6e 74  20 79 31 2c 20 69 6e 74  | x1, int y1, int|
000003b0  20 78 32 2c 20 69 6e 74  20 79 32 2c 20 69 6e 74  | x2, int y2, int|
000003c0  20 73 69 64 65 2c 20 69  6e 74 20 69 30 2c 20 69  | side, int i0, i|
000003d0  6e 74 20 69 31 29 0a 7b  0a 20 20 20 20 69 6e 74  |nt i1).{.    int|
000003e0  20 64 79 2c 64 78 2c 64  2c 69 6e 63 72 31 2c 69  | dy,dx,d,incr1,i|
000003f0  6e 63 72 32 2c 69 6e 63  2c 65 6e 64 2c 6c 65 6e  |ncr2,inc,end,len|
00000400  2c 6a 2c 20 69 73 2c 20  69 64 3b 0a 0a 20 20 20  |,j, is, id;..   |
00000410  20 64 78 20 3d 20 61 62  73 28 78 32 2d 78 31 29  | dx = abs(x2-x1)|
00000420  3b 0a 20 20 20 20 64 79  20 3d 20 61 62 73 28 79  |;.    dy = abs(y|
00000430  32 2d 79 31 29 3b 0a 0a  20 20 20 20 69 66 20 28  |2-y1);..    if (|
00000440  64 78 20 3e 20 64 79 29  0a 20 20 20 20 7b 0a 20  |dx > dy).    {. |
00000450  20 20 20 20 20 20 20 64  20 3d 20 28 64 79 3c 3c  |       d = (dy<<|
00000460  31 29 20 2d 20 64 78 3b  0a 20 20 20 20 20 20 20  |1) - dx;.       |
00000470  20 69 6e 63 72 31 20 3d  20 28 64 79 3c 3c 31 29  | incr1 = (dy<<1)|
00000480  3b 0a 20 20 20 20 20 20  20 20 69 6e 63 72 32 20  |;.        incr2 |
00000490  3d 20 28 64 79 20 2d 20  64 78 29 3c 3c 31 3b 0a  |= (dy - dx)<<1;.|
000004a0  20 20 20 20 20 20 20 20  69 66 20 28 79 31 3e 3d  |        if (y1>=|
000004b0  79 32 29 20 69 6e 63 3d  31 3b 20 65 6c 73 65 20  |y2) inc=1; else |
000004c0  69 6e 63 3d 2d 31 3b 0a  20 20 20 20 20 20 20 20  |inc=-1;.        |
000004d0  69 66 20 28 78 31 20 3e  20 78 32 29 0a 20 20 20  |if (x1 > x2).   |
000004e0  20 20 20 20 20 7b 0a 20  20 20 20 20 20 20 20 20  |     {.         |
000004f0  20 20 20 20 65 6e 64 3d  78 31 3b 0a 20 20 20 20  |    end=x1;.    |
00000500  20 20 20 20 20 20 20 20  20 78 31 3d 78 32 3b 0a  |         x1=x2;.|
00000510  20 20 20 20 20 20 20 20  20 20 20 20 20 79 31 3d  |             y1=|
00000520  79 32 3b 0a 20 20 20 20  20 20 20 20 7d 0a 20 20  |y2;.        }.  |
00000530  20 20 20 20 20 20 65 6c  73 65 0a 20 20 20 20 20  |      else.     |
00000540  20 20 20 7b 0a 20 20 20  20 20 20 20 20 20 20 20  |   {.           |
00000550  20 69 6e 63 3d 2d 69 6e  63 3b 0a 20 20 20 20 20  | inc=-inc;.     |
00000560  20 20 20 20 20 20 20 65  6e 64 20 3d 20 78 32 3b  |       end = x2;|
00000570  0a 20 20 20 20 20 20 20  20 7d 0a 20 20 20 20 20  |.        }.     |
00000580  20 20 20 6c 65 6e 20 3d  20 65 6e 64 20 2d 20 78  |   len = end - x|
00000590  31 3b 0a 20 20 20 20 20  20 20 20 69 64 20 3d 20  |1;.        id = |
000005a0  28 69 31 20 2d 20 69 30  29 20 3c 3c 20 32 30 3b  |(i1 - i0) << 20;|
000005b0  0a 20 20 20 20 20 20 20  20 69 73 20 3d 20 69 64  |.        is = id|
000005c0  20 2f 20 6c 65 6e 3b 0a  20 20 20 20 20 20 20 20  | / len;.        |
000005d0  69 64 20 3d 20 69 30 20  3c 3c 20 32 30 3b 0a 0a  |id = i0 << 20;..|
000005e0  20 20 20 20 20 20 20 20  69 66 20 28 73 69 64 65  |        if (side|
000005f0  20 3d 3d 20 30 29 0a 20  20 20 20 20 20 20 20 7b  | == 0).        {|
00000600  0a 20 20 20 20 20 20 20  20 20 20 20 20 66 6f 72  |.            for|
00000610  20 28 6a 3d 30 3b 20 6a  3c 3d 6c 65 6e 3b 20 6a  | (j=0; j<=len; j|
00000620  2b 2b 29 0a 20 20 20 20  20 20 20 20 20 20 20 20  |++).            |
00000630  7b 0a 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |{.              |
00000640  20 20 78 6c 5b 79 31 5d  20 3d 20 78 31 2b 6a 3b  |  xl[y1] = x1+j;|
00000650  0a 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |.               |
00000660  20 69 6c 5b 79 31 5d 20  3d 20 69 64 3b 0a 20 20  | il[y1] = id;.  |
00000670  20 20 20 20 20 20 20 20  20 20 20 20 20 20 69 64  |              id|
00000680  20 2b 3d 20 69 73 3b 0a  20 20 20 20 20 20 20 20  | += is;.        |
00000690  20 20 20 20 20 20 20 20  69 66 20 28 64 3c 30 29  |        if (d<0)|
000006a0  20 64 2b 3d 69 6e 63 72  31 3b 0a 20 20 20 20 20  | d+=incr1;.     |
000006b0  20 20 20 20 20 20 20 20  20 20 20 65 6c 73 65 0a  |           else.|
000006c0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
000006d0  7b 0a 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |{.              |
000006e0  20 20 20 20 20 20 79 31  20 2b 3d 20 69 6e 63 3b  |      y1 += inc;|
000006f0  0a 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |.               |
00000700  20 20 20 20 20 64 20 2b  3d 20 69 6e 63 72 32 3b  |     d += incr2;|
00000710  0a 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |.               |
00000720  20 7d 0a 20 20 20 20 20  20 20 20 20 20 20 20 7d  | }.            }|
00000730  0a 20 20 20 20 20 20 20  20 7d 0a 20 20 20 20 20  |.        }.     |
00000740  20 20 20 69 66 20 28 73  69 64 65 20 3d 3d 20 31  |   if (side == 1|
00000750  29 0a 20 20 20 20 20 20  20 20 7b 0a 20 20 20 20  |).        {.    |
00000760  20 20 20 20 20 20 20 20  66 6f 72 20 28 6a 3d 30  |        for (j=0|
00000770  3b 20 6a 3c 3d 6c 65 6e  3b 20 6a 2b 2b 29 0a 20  |; j<=len; j++). |
00000780  20 20 20 20 20 20 20 20  20 20 20 7b 0a 20 20 20  |           {.   |
00000790  20 20 20 20 20 20 20 20  20 20 20 20 20 78 72 5b  |             xr[|
000007a0  79 31 5d 20 3d 20 78 31  2b 6a 3b 0a 20 20 20 20  |y1] = x1+j;.    |
000007b0  20 20 20 20 20 20 20 20  20 20 20 20 69 72 5b 79  |            ir[y|
000007c0  31 5d 20 3d 20 69 64 3b  0a 20 20 20 20 20 20 20  |1] = id;.       |
000007d0  20 20 20 20 20 20 20 20  20 69 64 20 2b 3d 20 69  |         id += i|
000007e0  73 3b 0a 20 20 20 20 20  20 20 20 20 20 20 20 20  |s;.             |
000007f0  20 20 20 69 66 20 28 64  3c 30 29 20 64 2b 3d 69  |   if (d<0) d+=i|
00000800  6e 63 72 31 3b 0a 20 20  20 20 20 20 20 20 20 20  |ncr1;.          |
00000810  20 20 20 20 20 20 65 6c  73 65 0a 20 20 20 20 20  |      else.     |
00000820  20 20 20 20 20 20 20 20  20 20 20 7b 0a 20 20 20  |           {.   |
00000830  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000840  20 79 31 20 2b 3d 20 69  6e 63 3b 0a 20 20 20 20  | y1 += inc;.    |
00000850  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000860  64 20 2b 3d 20 69 6e 63  72 32 3b 0a 20 20 20 20  |d += incr2;.    |
00000870  20 20 20 20 20 20 20 20  20 20 20 20 7d 0a 20 20  |            }.  |
00000880  20 20 20 20 20 20 20 20  20 20 7d 0a 20 20 20 20  |          }.    |
00000890  20 20 20 20 7d 0a 20 20  20 20 7d 0a 20 20 20 20  |    }.    }.    |
000008a0  65 6c 73 65 0a 20 20 20  20 7b 0a 20 20 20 20 20  |else.    {.     |
000008b0  20 20 20 64 20 3d 20 28  64 78 3c 3c 31 29 20 2d  |   d = (dx<<1) -|
000008c0  20 64 79 3b 0a 20 20 20  20 20 20 20 20 69 6e 63  | dy;.        inc|
000008d0  72 31 20 3d 20 28 64 78  3c 3c 31 29 3b 0a 20 20  |r1 = (dx<<1);.  |
000008e0  20 20 20 20 20 20 69 6e  63 72 32 20 3d 20 28 64  |      incr2 = (d|
000008f0  78 2d 64 79 29 3c 3c 31  3b 0a 20 20 20 20 20 20  |x-dy)<<1;.      |
00000900  20 20 69 66 20 28 78 31  20 3e 3d 20 78 32 29 20  |  if (x1 >= x2) |
00000910  69 6e 63 20 3d 20 31 3b  20 65 6c 73 65 20 69 6e  |inc = 1; else in|
00000920  63 3d 2d 31 3b 0a 20 20  20 20 20 20 20 20 69 66  |c=-1;.        if|
00000930  20 28 79 31 20 3e 20 79  32 29 0a 20 20 20 20 20  | (y1 > y2).     |
00000940  20 20 20 7b 0a 20 20 20  20 20 20 20 20 20 20 20  |   {.           |
00000950  20 65 6e 64 20 3d 20 79  31 3b 0a 20 20 20 20 20  | end = y1;.     |
00000960  20 20 20 20 20 20 20 79  31 20 3d 20 79 32 3b 0a  |       y1 = y2;.|
00000970  20 20 20 20 20 20 20 20  20 20 20 20 78 31 20 3d  |            x1 =|
00000980  20 78 32 3b 0a 20 20 20  20 20 20 20 20 7d 0a 20  | x2;.        }. |
00000990  20 20 20 20 20 20 20 65  6c 73 65 0a 20 20 20 20  |       else.    |
000009a0  20 20 20 20 7b 0a 20 20  20 20 20 20 20 20 20 20  |    {.          |
000009b0  20 20 69 6e 63 20 3d 2d  69 6e 63 3b 0a 20 20 20  |  inc =-inc;.   |
000009c0  20 20 20 20 20 20 20 20  20 65 6e 64 20 3d 20 79  |         end = y|
000009d0  32 3b 0a 20 20 20 20 20  20 20 20 7d 0a 20 20 20  |2;.        }.   |
000009e0  20 20 20 20 20 6c 65 6e  20 3d 20 65 6e 64 20 2d  |     len = end -|
000009f0  20 79 31 3b 0a 0a 20 20  20 20 20 20 20 20 69 64  | y1;..        id|
00000a00  20 3d 20 28 69 31 20 2d  20 69 30 29 20 3c 3c 20  | = (i1 - i0) << |
00000a10  32 30 3b 0a 20 20 20 20  20 20 20 20 69 73 20 3d  |20;.        is =|
00000a20  20 69 64 20 2f 20 6c 65  6e 3b 0a 20 20 20 20 20  | id / len;.     |
00000a30  20 20 20 69 64 20 3d 20  69 30 20 3c 3c 20 32 30  |   id = i0 << 20|
00000a40  3b 0a 0a 20 20 20 20 20  20 20 20 69 66 20 28 73  |;..        if (s|
00000a50  69 64 65 20 3d 3d 20 30  29 0a 20 20 20 20 20 20  |ide == 0).      |
00000a60  20 20 7b 0a 20 20 20 20  20 20 20 20 20 20 20 20  |  {.            |
00000a70  66 6f 72 20 28 6a 3d 30  3b 20 6a 3c 3d 6c 65 6e  |for (j=0; j<=len|
00000a80  3b 20 6a 2b 2b 29 0a 20  20 20 20 20 20 20 20 20  |; j++).         |
00000a90  20 20 20 7b 0a 20 20 20  20 20 20 20 20 20 20 20  |   {.           |
00000aa0  20 20 20 20 20 78 6c 5b  79 31 2b 6a 5d 20 3d 20  |     xl[y1+j] = |
00000ab0  78 31 3b 0a 20 20 20 20  20 20 20 20 20 20 20 20  |x1;.            |
00000ac0  20 20 20 20 69 6c 5b 79  31 2b 6a 5d 20 3d 20 69  |    il[y1+j] = i|
00000ad0  64 3b 0a 20 20 20 20 20  20 20 20 20 20 20 20 20  |d;.             |
00000ae0  20 20 20 69 64 20 2b 3d  20 69 73 3b 0a 20 20 20  |   id += is;.   |
00000af0  20 20 20 20 20 20 20 20  20 20 20 20 20 69 66 20  |             if |
00000b00  28 64 3c 30 29 20 64 2b  3d 69 6e 63 72 31 3b 0a  |(d<0) d+=incr1;.|
00000b10  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000b20  65 6c 73 65 0a 20 20 20  20 20 20 20 20 20 20 20  |else.           |
00000b30  20 20 20 20 20 7b 0a 20  20 20 20 20 20 20 20 20  |     {.         |
00000b40  20 20 20 20 20 20 20 20  20 20 20 78 31 2b 3d 69  |           x1+=i|
00000b50  6e 63 3b 0a 20 20 20 20  20 20 20 20 20 20 20 20  |nc;.            |
00000b60  20 20 20 20 20 20 20 20  64 2b 3d 69 6e 63 72 32  |        d+=incr2|
00000b70  3b 0a 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |;.              |
00000b80  20 20 7d 0a 20 20 20 20  20 20 20 20 20 20 20 20  |  }.            |
00000b90  7d 0a 20 20 20 20 20 20  20 20 7d 0a 20 20 20 20  |}.        }.    |
00000ba0  20 20 20 20 69 66 20 28  73 69 64 65 20 3d 3d 20  |    if (side == |
00000bb0  31 29 0a 20 20 20 20 20  20 20 20 7b 0a 20 20 20  |1).        {.   |
00000bc0  20 20 20 20 20 20 20 20  20 66 6f 72 20 28 6a 3d  |         for (j=|
00000bd0  30 3b 20 6a 3c 3d 6c 65  6e 3b 20 6a 2b 2b 29 0a  |0; j<=len; j++).|
00000be0  20 20 20 20 20 20 20 20  20 20 20 20 7b 0a 20 20  |            {.  |
00000bf0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 78 72  |              xr|
00000c00  5b 79 31 2b 6a 5d 20 3d  20 78 31 3b 0a 20 20 20  |[y1+j] = x1;.   |
00000c10  20 20 20 20 20 20 20 20  20 20 20 20 20 69 72 5b  |             ir[|
00000c20  79 31 2b 6a 5d 20 3d 20  69 64 3b 0a 20 20 20 20  |y1+j] = id;.    |
00000c30  20 20 20 20 20 20 20 20  20 20 20 20 69 64 20 2b  |            id +|
00000c40  3d 20 69 73 3b 0a 20 20  20 20 20 20 20 20 20 20  |= is;.          |
00000c50  20 20 20 20 20 20 69 66  20 28 64 3c 30 29 20 64  |      if (d<0) d|
00000c60  2b 3d 69 6e 63 72 31 3b  0a 20 20 20 20 20 20 20  |+=incr1;.       |
00000c70  20 20 20 20 20 20 20 20  20 65 6c 73 65 0a 20 20  |         else.  |
00000c80  20 20 20 20 20 20 20 20  20 20 20 20 20 20 7b 0a  |              {.|
00000c90  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000ca0  20 20 20 20 78 31 2b 3d  69 6e 63 3b 0a 20 20 20  |    x1+=inc;.   |
00000cb0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000cc0  20 64 2b 3d 69 6e 63 72  32 3b 0a 20 20 20 20 20  | d+=incr2;.     |
00000cd0  20 20 20 20 20 20 20 20  20 20 20 7d 0a 20 20 20  |           }.   |
00000ce0  20 20 20 20 20 20 20 20  20 7d 0a 20 20 20 20 20  |         }.     |
00000cf0  20 20 20 7d 0a 20 20 20  20 7d 0a 7d 0a 0a 76 6f  |   }.    }.}..vo|
00000d00  69 64 20 67 6f 75 72 61  75 64 28 76 65 72 74 69  |id gouraud(verti|
00000d10  63 65 73 20 2a 70 74 73  2c 20 69 6e 74 20 2a 63  |ces *pts, int *c|
00000d20  6f 6c 6f 75 72 73 2c 20  69 6e 74 20 69 30 2c 20  |olours, int i0, |
00000d30  69 6e 74 20 69 31 2c 20  69 6e 74 20 69 32 29 0a  |int i1, int i2).|
00000d40  7b 0a 20 20 20 20 69 6e  74 20 76 64 75 5b 32 5d  |{.    int vdu[2]|
00000d50  2c 20 79 2c 20 79 73 2c  20 73 6c 2c 20 73 72 2c  |, y, ys, sl, sr,|
00000d60  20 73 63 72 65 65 6e 64  69 73 74 2c 20 69 73 74  | screendist, ist|
00000d70  61 72 74 2c 20 69 65 6e  64 2c 20 69 64 65 6c 74  |art, iend, idelt|
00000d80  61 2c 20 6c 65 6e 2c 20  78 3b 0a 20 20 20 20 69  |a, len, x;.    i|
00000d90  6e 74 20 73 63 72 65 65  6e 73 74 61 72 74 2c 20  |nt screenstart, |
00000da0  65 72 72 6f 72 2c 20 69  6e 74 65 6e 73 3b 0a 20  |error, intens;. |
00000db0  20 20 20 63 68 61 72 20  2a 70 74 72 3b 0a 20 20  |   char *ptr;.  |
00000dc0  20 20 5f 6b 65 72 6e 65  6c 5f 73 77 69 5f 72 65  |  _kernel_swi_re|
00000dd0  67 73 20 72 3b 0a 0a 20  20 20 20 76 64 75 5b 30  |gs r;..    vdu[0|
00000de0  5d 20 3d 20 31 34 38 3b  0a 20 20 20 20 76 64 75  |] = 148;.    vdu|
00000df0  5b 31 5d 20 3d 20 2d 31  3b 0a 20 20 20 20 72 2e  |[1] = -1;.    r.|
00000e00  72 5b 30 5d 20 3d 20 72  2e 72 5b 31 5d 20 3d 20  |r[0] = r.r[1] = |
00000e10  28 69 6e 74 29 20 26 76  64 75 3b 0a 20 20 20 20  |(int) &vdu;.    |
00000e20  5f 6b 65 72 6e 65 6c 5f  73 77 69 28 4f 53 5f 52  |_kernel_swi(OS_R|
00000e30  65 61 64 56 64 75 56 61  72 69 61 62 6c 65 73 2c  |eadVduVariables,|
00000e40  26 72 2c 26 72 29 3b 0a  0a 20 20 20 20 73 63 72  |&r,&r);..    scr|
00000e50  65 65 6e 73 74 61 72 74  20 3d 20 76 64 75 5b 30  |eenstart = vdu[0|
00000e60  5d 3b 0a 0a 20 20 20 20  69 66 20 28 70 74 73 2d  |];..    if (pts-|
00000e70  3e 79 30 20 3c 20 70 74  73 2d 3e 79 31 29 0a 20  |>y0 < pts->y1). |
00000e80  20 20 20 7b 0a 20 20 20  20 20 20 20 20 73 77 61  |   {.        swa|
00000e90  70 20 28 70 74 73 2d 3e  78 30 2c 70 74 73 2d 3e  |p (pts->x0,pts->|
00000ea0  78 31 29 3b 0a 20 20 20  20 20 20 20 20 73 77 61  |x1);.        swa|
00000eb0  70 20 28 70 74 73 2d 3e  79 30 2c 70 74 73 2d 3e  |p (pts->y0,pts->|
00000ec0  79 31 29 3b 0a 20 20 20  20 7d 0a 0a 20 20 20 20  |y1);.    }..    |
00000ed0  69 66 20 28 70 74 73 2d  3e 79 31 20 3c 20 70 74  |if (pts->y1 < pt|
00000ee0  73 2d 3e 79 32 29 0a 20  20 20 20 7b 0a 20 20 20  |s->y2).    {.   |
00000ef0  20 20 20 20 20 73 77 61  70 20 28 70 74 73 2d 3e  |     swap (pts->|
00000f00  78 31 2c 70 74 73 2d 3e  78 32 29 3b 20 2f 2a 20  |x1,pts->x2); /* |
00000f10  4d 61 6b 65 20 74 72 69  61 6e 67 6c 65 20 74 68  |Make triangle th|
00000f20  65 20 72 69 67 68 74 20  77 61 79 20 75 70 20 2a  |e right way up *|
00000f30  2f 0a 20 20 20 20 20 20  20 20 73 77 61 70 20 28  |/.        swap (|
00000f40  70 74 73 2d 3e 79 31 2c  70 74 73 2d 3e 79 32 29  |pts->y1,pts->y2)|
00000f50  3b 0a 20 20 20 20 7d 0a  0a 20 20 20 20 69 66 20  |;.    }..    if |
00000f60  28 70 74 73 2d 3e 79 30  20 3c 20 70 74 73 2d 3e  |(pts->y0 < pts->|
00000f70  79 31 29 0a 20 20 20 20  7b 0a 20 20 20 20 20 20  |y1).    {.      |
00000f80  20 20 73 77 61 70 20 28  70 74 73 2d 3e 78 30 2c  |  swap (pts->x0,|
00000f90  70 74 73 2d 3e 78 31 29  3b 0a 20 20 20 20 20 20  |pts->x1);.      |
00000fa0  20 20 73 77 61 70 20 28  70 74 73 2d 3e 79 30 2c  |  swap (pts->y0,|
00000fb0  70 74 73 2d 3e 79 31 29  3b 0a 20 20 20 20 7d 0a  |pts->y1);.    }.|
00000fc0  0a 20 20 20 20 5f 5f 6e  63 5f 6c 69 6e 65 28 70  |.    __nc_line(p|
00000fd0  74 73 2d 3e 78 32 2c 70  74 73 2d 3e 79 32 2c 70  |ts->x2,pts->y2,p|
00000fe0  74 73 2d 3e 78 31 2c 70  74 73 2d 3e 79 31 2c 30  |ts->x1,pts->y1,0|
00000ff0  2c 69 32 2c 69 31 29 3b  0a 20 20 20 20 5f 5f 6e  |,i2,i1);.    __n|
00001000  63 5f 6c 69 6e 65 28 70  74 73 2d 3e 78 31 2c 70  |c_line(pts->x1,p|
00001010  74 73 2d 3e 79 31 2c 70  74 73 2d 3e 78 30 2c 70  |ts->y1,pts->x0,p|
00001020  74 73 2d 3e 79 30 2c 30  2c 69 31 2c 69 30 29 3b  |ts->y0,0,i1,i0);|
00001030  20 2f 2a 20 44 72 61 77  20 6c 69 6e 65 73 20 2a  | /* Draw lines *|
00001040  2f 0a 20 20 20 20 5f 5f  6e 63 5f 6c 69 6e 65 28  |/.    __nc_line(|
00001050  70 74 73 2d 3e 78 32 2c  70 74 73 2d 3e 79 32 2c  |pts->x2,pts->y2,|
00001060  70 74 73 2d 3e 78 30 2c  70 74 73 2d 3e 79 30 2c  |pts->x0,pts->y0,|
00001070  31 2c 69 32 2c 69 30 29  3b 0a 0a 20 20 20 20 79  |1,i2,i0);..    y|
00001080  73 20 3d 20 73 67 6e 28  70 74 73 2d 3e 79 32 20  |s = sgn(pts->y2 |
00001090  2d 20 70 74 73 2d 3e 79  30 29 3b 0a 0a 20 20 20  |- pts->y0);..   |
000010a0  20 66 6f 72 20 28 79 3d  70 74 73 2d 3e 79 32 3b  | for (y=pts->y2;|
000010b0  20 79 3c 3d 70 74 73 2d  3e 79 30 3b 20 79 2b 2b  | y<=pts->y0; y++|
000010c0  29 0a 20 20 20 20 7b 0a  20 20 20 20 20 20 20 20  |).    {.        |
000010d0  73 6c 20 3d 20 78 6c 5b  79 5d 3b 0a 20 20 20 20  |sl = xl[y];.    |
000010e0  20 20 20 20 73 72 20 3d  20 78 72 5b 79 5d 3b 0a  |    sr = xr[y];.|
000010f0  20 20 20 20 20 20 20 20  69 73 74 61 72 74 20 3d  |        istart =|
00001100  20 69 6c 5b 79 5d 3b 0a  20 20 20 20 20 20 20 20  | il[y];.        |
00001110  69 65 6e 64 20 3d 20 69  72 5b 79 5d 3b 0a 0a 20  |iend = ir[y];.. |
00001120  20 20 20 20 20 20 20 73  63 72 65 65 6e 64 69 73  |       screendis|
00001130  74 20 3d 20 28 79 3c 3c  38 29 20 2b 20 28 79 3c  |t = (y<<8) + (y<|
00001140  3c 36 29 20 2b 20 73 63  72 65 65 6e 73 74 61 72  |<6) + screenstar|
00001150  74 3b 0a 0a 20 20 20 20  20 20 20 20 69 66 20 28  |t;..        if (|
00001160  73 6c 20 3e 20 73 72 29  0a 20 20 20 20 20 20 20  |sl > sr).       |
00001170  20 7b 0a 20 20 20 20 20  20 20 20 20 20 20 20 73  | {.            s|
00001180  6c 20 5e 3d 20 73 72 3b  0a 20 20 20 20 20 20 20  |l ^= sr;.       |
00001190  20 20 20 20 20 73 72 20  3d 20 73 6c 20 5e 20 73  |     sr = sl ^ s|
000011a0  72 3b 0a 20 20 20 20 20  20 20 20 20 20 20 20 73  |r;.            s|
000011b0  6c 20 5e 3d 20 73 72 3b  0a 20 20 20 20 20 20 20  |l ^= sr;.       |
000011c0  20 20 20 20 20 69 73 74  61 72 74 20 5e 3d 20 69  |     istart ^= i|
000011d0  65 6e 64 3b 0a 20 20 20  20 20 20 20 20 20 20 20  |end;.           |
000011e0  20 69 65 6e 64 20 3d 20  69 73 74 61 72 74 20 5e  | iend = istart ^|
000011f0  20 69 65 6e 64 3b 0a 20  20 20 20 20 20 20 20 20  | iend;.         |
00001200  20 20 20 69 73 74 61 72  74 20 5e 3d 20 69 65 6e  |   istart ^= ien|
00001210  64 3b 0a 20 20 20 20 20  20 20 20 7d 0a 0a 20 20  |d;.        }..  |
00001220  20 20 20 20 20 20 69 66  20 28 73 72 20 21 3d 20  |      if (sr != |
00001230  73 6c 29 0a 20 20 20 20  20 20 20 20 20 20 20 20  |sl).            |
00001240  69 64 65 6c 74 61 20 3d  20 28 69 65 6e 64 20 2d  |idelta = (iend -|
00001250  20 69 73 74 61 72 74 29  20 2f 20 28 73 72 2d 73  | istart) / (sr-s|
00001260  6c 29 3b 20 2f 2a 20 49  6e 74 65 6e 73 20 63 68  |l); /* Intens ch|
00001270  61 6e 67 65 20 66 6f 72  20 65 61 63 68 20 70 69  |ange for each pi|
00001280  78 65 6c 20 70 6c 6f 74  74 65 64 20 2a 2f 0a 20  |xel plotted */. |
00001290  20 20 20 20 20 20 20 6c  65 6e 20 3d 20 73 63 72  |       len = scr|
000012a0  65 65 6e 64 69 73 74 20  2b 20 73 72 3b 0a 0a 20  |eendist + sr;.. |
000012b0  20 20 20 20 20 20 20 66  6f 72 20 28 78 3d 73 63  |       for (x=sc|
000012c0  72 65 65 6e 64 69 73 74  2b 73 6c 3b 20 78 3c 3d  |reendist+sl; x<=|
000012d0  6c 65 6e 3b 20 78 2b 2b  29 0a 20 20 20 20 20 20  |len; x++).      |
000012e0  20 20 7b 0a 20 20 20 20  20 20 20 20 20 20 20 20  |  {.            |
000012f0  69 6e 74 65 6e 73 20 3d  20 69 73 74 61 72 74 3e  |intens = istart>|
00001300  3e 32 30 3b 0a 20 20 20  20 20 20 20 20 20 20 20  |>20;.           |
00001310  20 65 72 72 6f 72 20 2b  3d 20 69 73 74 61 72 74  | error += istart|
00001320  20 2d 20 28 69 73 74 61  72 74 20 26 20 35 33 35  | - (istart & 535|
00001330  38 32 32 33 33 36 29 3b  20 2f 2a 20 41 64 64 20  |822336); /* Add |
00001340  6f 6e 20 65 72 72 6f 72  20 2a 2f 0a 20 20 20 20  |on error */.    |
00001350  20 20 20 20 20 20 20 20  69 66 20 28 65 72 72 6f  |        if (erro|
00001360  72 20 3e 20 31 30 34 38  35 37 36 29 0a 20 20 20  |r > 1048576).   |
00001370  20 20 20 20 20 20 20 20  20 7b 0a 20 20 20 20 20  |         {.     |
00001380  20 20 20 20 20 20 20 20  20 20 20 69 6e 74 65 6e  |           inten|
00001390  73 20 2b 2b 3b 0a 20 20  20 20 20 20 20 20 20 20  |s ++;.          |
000013a0  20 20 20 20 20 20 65 72  72 6f 72 20 2d 3d 20 31  |      error -= 1|
000013b0  30 34 38 35 37 36 3b 20  20 20 20 20 20 20 20 20  |048576;         |
000013c0  20 20 20 20 20 20 20 20  20 20 2f 2a 20 49 66 20  |          /* If |
000013d0  6f 76 65 72 66 6c 6f 77  2e 2e 2e 20 2a 2f 0a 20  |overflow... */. |
000013e0  20 20 20 20 20 20 20 20  20 20 20 7d 0a 20 20 20  |           }.   |
000013f0  20 20 20 20 20 20 20 20  20 70 74 72 20 3d 20 28  |         ptr = (|
00001400  63 68 61 72 20 2a 29 20  78 3b 0a 20 20 20 20 20  |char *) x;.     |
00001410  20 20 20 20 20 20 20 2a  70 74 72 20 3d 20 28 63  |       *ptr = (c|
00001420  68 61 72 29 20 63 6f 6c  6f 75 72 73 5b 69 6e 74  |har) colours[int|
00001430  65 6e 73 5d 3b 20 20 20  20 20 20 20 20 20 20 2f  |ens];          /|
00001440  2a 20 50 6c 6f 74 20 2a  2f 0a 20 20 20 20 20 20  |* Plot */.      |
00001450  20 20 20 20 20 20 69 73  74 61 72 74 20 2b 3d 20  |      istart += |
00001460  69 64 65 6c 74 61 3b 20  20 20 2f 2a 20 49 6e 63  |idelta;   /* Inc|
00001470  72 65 6d 65 6e 74 20 69  6e 74 65 6e 73 69 74 79  |rement intensity|
00001480  20 2a 2f 0a 20 20 20 20  20 20 20 20 7d 0a 20 20  | */.        }.  |
00001490  20 20 7d 0a 7d 0a                                 |  }.}.|
00001496