<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
 * Render.java
 *
 * Alex S.
 */

public class Render {

    public int width,height;
    public int[] pix;
    public float[] pixz;

    public int[][] faces;
    public float[][] vertices;

    public Render(int w,int h,int[] pix){
        width = w;
        height = h;
        this.pix = pix;
        pixz = new float[pix.length];
    }
    
    /**
     * clear both screen and depth buffer
     */
    public void clear(){
        for(int i=0;i&lt;pix.length;i++){
            pixz[i] = (float)0.0;
            pix[i] = PixApplet.pack(0xEE,0xEE,0xEE);
        }
    }

    /**
     * set current faces/vertices
     */
    public void setshape(int[][] faces,float[][] vertices){
        this.faces = faces;
        this.vertices = vertices;
    }

    /**
     * render shape (all faces)
     * (assume shape has been transformed, and color info
     * has been computed).
     */
    public void rendershape(){
        for(int i=0;i&lt;faces.length;i++){
            renderface(i);
        }
    }

    /**
     * render face
     * (assumes all vertices have been properly setup)
     */
    public void renderface(int n){
        int[] face = faces[n];
        int i,j = face.length-1;
        for(i=1;i&lt;j;i++){
            triangle(
                vertices[face[0]],
                vertices[face[i]],
                vertices[face[i+1]]
            );
        }        
    }

    /**
     * render trinagle
     */
    public void triangle(float[] a,float[] b,float[] c)
    {
        // sort 3 points by Y
        float[] tmp;
        if(a[1] &lt; b[1]){
            tmp = a;
            a = b;
            b = tmp;
        }
        if(a[1] &lt; c[1]){
            tmp = a;
            a = c;
            c = tmp;
        }
        if(b[1] &lt; c[1]){
            tmp = b;
            b = c;
            c = tmp;
        }
        // check special cases
        if(c[1] == b[1]){
            if(c[0] &lt; b[0]){
                trapezoid(c,b,a,a);
            }else{
                trapezoid(b,c,a,a);
            }
        }else if(b[1] == a[1]){
            if(b[0] &lt; a[0]){
                trapezoid(c,c,b,a);
            }else{
                trapezoid(c,c,a,b);
            }
        }else{
            float t = (a[1]-b[1])/(a[1]-c[1]);
            float d[] = {
                a[0]+t*(c[0]-a[0]),
                b[1],
                a[2]+t*(c[2]-a[2]),
                a[3]+t*(c[3]-a[3]),
                a[4]+t*(c[4]-a[4]),
                a[5]+t*(c[5]-a[5]),
            };
            /*
            String s = "";
            for(int i=0;i&lt;6;i++){
                s += "a["+i+"]:"+a[i]+"; ";
                s += "b["+i+"]:"+b[i]+"; ";
                s += "c["+i+"]:"+c[i]+"; ";
                s += "d["+i+"]:"+d[i]+"; ";
            }
            System.out.println("chopping case: t:"+t+"; "+s);
            */
            if(d[0] &lt; b[0]){
                trapezoid(c,c,d,b);
                trapezoid(d,b,a,a);
            }else{
                trapezoid(c,c,b,d);
                trapezoid(b,d,a,a);
            }
        }
    }

    /**
     * render a trapezoid 
     *
     * a b
     * c d
     */
    public void trapezoid(float[] a,float[] b,float[] c,float[] d)
    {
        float ax = a[0];
        float ay = a[1];
        float az = a[2];
        float ar = a[3];
        float ag = a[4];
        float ab = a[5];

        float bx = b[0];
        float by = b[1];
        float bz = b[2];
        float br = b[3];
        float bg = b[4];
        float bb = b[5];

        float cx = c[0];
        float cy = c[1];
        float cz = c[2];
        float cr = c[3];
        float cg = c[4];
        float cb = c[5];

        float dx = d[0];
        float dy = d[1];
        float dz = d[2];
        float dr = d[3];
        float dg = d[4];
        float db = d[5];

        float ydiff = cy - ay;

        float axt = (cx - ax) / ydiff;
        float azt = (cz - az) / ydiff;
        float art = (cr - ar) / ydiff;
        float agt = (cg - ag) / ydiff;
        float abt = (cb - ab) / ydiff;

        float bxt = (dx - bx) / ydiff;
        float bzt = (dz - bz) / ydiff;
        float brt = (dr - br) / ydiff;
        float bgt = (dg - bg) / ydiff;
        float bbt = (db - bb) / ydiff;

        float x,y,z,r,g,bcolor,
            tz,tr,tg,tb,xdiff;
        
        float w;

        //System.out.println("ay:"+ay+"; cy:"+cy);

        if(ay &lt; 0)
            ay = 0;
        if(cy &gt;= height)
            cy = height-1;

        while(ay &lt;= cy){

            w = width * ay;

            xdiff = bx - ax;

            if(xdiff &gt; 0){
                x = ax;
                z = az;
                r = ar;
                g = ag;
                bcolor = ab;

                tz = (bz - az) / xdiff;
                tr = (br - ar) / xdiff;
                tg = (bg - ag) / xdiff;
                tb = (bb - ab) / xdiff;

                while(x &lt; bx){
                    
                    if(pixz[(int)(w + x)] &gt; z){
                        pix[(int)(w + x)] = PixApplet.pack(
                            (int)(0xFF*r),
                            (int)(0xFF*g),
                            (int)(0xFF*bcolor)
                        );
                        pixz[(int)(w + x)] = z;
                    }

                    x += 1;
                    z += tz;
                    r += tr;
                    g += tg;
                    bcolor += tb;
                }
            }else{
                if(pixz[(int)(w + ax)] &gt; az){                 
                    pix[(int)(w + ax)] = PixApplet.pack(
                        (int)(0xFF*ar),
                        (int)(0xFF*ag),
                        (int)(0xFF*ab)
                    );
                    pixz[(int)(w + ax)] = az;
                }
            }

            ay += 1;
            //by += 1;
            
            ax += axt;
            az += azt;
            ar += art;
            ag += agt;
            ab += abt;
            
            bx += bxt;
            bz += bzt;
            br += brt;
            bg += bgt;
            bb += bbt;
        }

    }


    /**
     * draw a line of a particular color
     */
    public void line(int x1,int y1,int x2,int y2,int color)
    {
        int xunit=1,yunit=1,xdiff,ydiff,length,error=0;
        int X1=x1,Y1=y1;
        if ((xdiff=x2-x1) &lt; 0) {
            xdiff=-xdiff;
            xunit=-1;
        }
        if ((ydiff=y2-y1) &lt; 0) {
            ydiff=-ydiff;
            yunit=-1;
        }
        if (ydiff &gt; xdiff) {
            for (length=ydiff;length &gt; 0;length--) {
                //if ((X1 &gt; 0) &amp;&amp; (X1 &lt; width) &amp;&amp; (Y1 &gt; 0) &amp;&amp; (Y1 &lt; height))
                    pix[Y1*width+X1] = color;

                Y1+=yunit;
                error+=xdiff;
                if (error &gt; ydiff) {
                    X1+=xunit;
                    error-=ydiff;
                }
            }
        } else {
            for (length=xdiff;length &gt; 0;length--) {
                //if ((X1 &gt; 0) &amp;&amp; (X1 &lt; width) &amp;&amp; (Y1 &gt; 0) &amp;&amp; (Y1 &lt; height))
                    pix[Y1*width+X1] = color;
                X1+=xunit;
                error+=ydiff;
                if (error &gt; xdiff) {
                    Y1+=yunit;
                    error-=xdiff;
                }
            }
        }
    }
}


</pre></body></html>