import java.awt.*; /** * Uses Shirley's formulation of ray-triangle, w. barycentric coords. * This version has a black background and white text. * This ray traces a triangle using barycentric coordinates. * @author Bob Futrelle * @version 0.2, 20 October 2005 (was 3 April 2003) */ class TinyRayTrace { double eihf, gfdi, dheg, akjb, jcal, blkc; double a, b, c, d, e, f, g, h, ii, jj, k, l; double beta, gamma, t, t0, t1, M; int xdim, ydim; Color[][] colorArray; public TinyRayTrace(int xdim, int ydim){ this.xdim = xdim; this.ydim = ydim; } public void doTrace(Tri tri, Color[][] colorArray){ this.colorArray = colorArray; for(int i =0; i < 200; i++) for(int j=0; j < 200; j++) // First point is location, second is displacement // Result is orthographic projection traceOne(tri,new P3d(i,j,30.0),new P3d(0,0,-60.0),i,j); } public void traceOne(Tri tri, P3d raye, P3d rayd,int i, int j) { setParams(tri,raye, rayd); double t = comp_t(); if(t < 0.0 || t > 1.0) return; double gamma = comp_gamma(); if(gamma < 0.0 || gamma > 1.0) return; double beta = comp_beta(); if(beta < 0.0 || beta > (1.0 - gamma)) return; colorArray[i][j] = tri.clr; } // Given a triangle, and ray, we compute all needed coefficients. void setParams(Tri tri, P3d raye, P3d rayd) { a = tri.pa.x - tri.pb.x; b = tri.pa.y - tri.pb.y; c = tri.pa.z - tri.pb.z; d = tri.pa.x - tri.pc.x; e = tri.pa.y - tri.pc.y; f = tri.pa.z - tri.pc.z; g = rayd.x; h = rayd.y; ii = rayd.z; jj = tri.pa.x - raye.x; k = tri.pa.y - raye.y; l = tri.pa.z - raye.z; eihf = e*ii - h*f; gfdi = g*f - d*ii; dheg = d*h - e*g; akjb = a*k - jj*b; jcal = jj*c - a*l; blkc = b*l - k*c; M = a*eihf + b*gfdi + c*dheg; } double comp_t() { return -(f*akjb + e*jcal + d*blkc)/M; } double comp_beta() { return (jj*eihf + k*gfdi + l*dheg)/M; } double comp_gamma() { return (ii*akjb + h*jcal + g*blkc)/M; } } // class TinyRayTrace