/* * PuppetApplet.java * * (C) 2004-2005, Alex S. */ import java.awt.*; import java.util.*; /* class BodyNode { public Vector children; public pMatrix matrix; public BodyNode(){ children = new Vector(); matrix = new pMatrix(); } } */ public class PuppetApplet extends BufferedApplet { pMatrix matrix = new pMatrix(); pMatrix pmatrix = new pMatrix(); // points/colors that are rendered. Vector points = new Vector(); Vector colors = new Vector(); // for z sorting double[] avgz; int[] polyorder; // pre-computed points. Vector cube = new Vector(); // current rendering color Color currentColor = Color.red; // angle, mouse state private double m_anglex,m_angley; private int m_mousex,m_mousey; /** * dumps phere to rendering pipeline */ public void dumpCube(){ int i,j; j=cube.size(); for (i=0;i=0 && avgz[j] > e2;j--) { avgz[j+1] = avgz[j]; polyorder[j+1] = polyorder[j]; } polyorder[j+1] = e1; avgz[j+1] = e2; } } pMatrix camera = new pMatrix(); /** * function that gets called whenever something needs to be * rendered. */ public void render(Graphics g) { if (damage) { g.setColor(Color.lightGray); g.fillRect(0, 0, bounds().width, bounds().height); matrix.push(); matrix.translate(0,0,-144); matrix.scale(2.6); /* g.setColor(Color.black); g.drawString("sin: "+m_angley+"; cos: "+(m_anglex)+"; ang: "+ Math.atan2(m_anglex,m_angley),10,10); */ if(m_angley != 0 || m_anglex != 0){ pMatrix nm = new pMatrix(); nm.rotatex(m_anglex); nm.rotatey(m_angley); nm.mult(camera); camera = nm; m_anglex = m_angley = 0; } matrix.mult(camera); dumpModel(); // calculate average z for all points. avgz = new double[points.size() / 4]; polyorder = new int[points.size() / 4]; int k,i=0,j=points.size(); double[] plane = new double[4]; for (i=0;i 0) { for (k=0;k<4;k++) { double[] point = (double[])points.elementAt(poly+k); point = pmatrix.mult(point); x[k] = (int)(point[0] / point[3]); y[k] = (int)(point[1] / point[3]); } double color1 = Math.max(0,dotProduct(plane,new double[]{0,0,-1})); g.setColor( new Color( (float)Math.min( ((c.getRed()+50) * color1)/255.0, 1.0 ), (float)Math.min( ((c.getGreen()+50) * color1)/255.0, 1.0 ), (float)Math.min( ((c.getBlue()+50) * color1)/255.0, 1.0 ) ) ); g.fillPolygon(x,y,x.length); g.setColor(Color.black); g.drawPolygon(x,y,x.length); } }else{ for (k=0;k<4;k++) { double[] point = (double[])points.elementAt(poly+k); point = pmatrix.mult(point); x[k] = (int)(point[0] / point[3]); y[k] = (int)(point[1] / point[3]); } g.setColor(Color.black); g.drawPolygon(x,y,x.length); } } avgz = null; polyorder = null; matrix.pop(); points.removeAllElements(); colors.removeAllElements(); } } /** * given 2 points, find their difference */ void vectorSubtract (double[] va, double[] vb, double[] out){ out[0] = va[0]-vb[0]; out[1] = va[1]-vb[1]; out[2] = va[2]-vb[2]; } /** * cross product of two vectors */ void crossProduct(double[] v1, double[] v2, double[] cross ) { cross[0] = v1[1]*v2[2] - v1[2]*v2[1]; cross[1] = v1[2]*v2[0] - v1[0]*v2[2]; cross[2] = v1[0]*v2[1] - v1[1]*v2[0]; } /** * normalize */ double vectorNormalize(double[] in, double[] out ) { double length, ilength; length = Math.sqrt(in[0]*in[0] + in[1]*in[1] + in[2]*in[2]); if (length == 0) { return 0; } ilength = 1.0/length; out[0] = in[0]*ilength; out[1] = in[1]*ilength; out[2] = in[2]*ilength; return length; } /** * dot product */ double dotProduct (double[] v1, double[] v2){ return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]; } /** * gets plane equation from three points on plane */ boolean planeFromPoints(double[] plane, double[] a, double[] b,double[] c,double[] d ){ double[] d1 = new double[4]; double[] d2 = new double[4]; vectorSubtract( b, a, d1 ); if (dotProduct(d1,d1) < 0.02) { vectorSubtract( c, a, d1 ); vectorSubtract( d, a, d2 ); } else { vectorSubtract( c, a, d2 ); if (dotProduct(d2,d2) < 0.02) { vectorSubtract( d, a, d2 ); } } crossProduct( d2, d1, plane ); if ( vectorNormalize( plane, plane ) == 0 ) { return false; } plane[3] = dotProduct( a, plane ); return true; } /** * mouse event handler: let the user move things */ public boolean mouseDown(Event evt, int x, int y){ m_mousex = x; m_mousey = y; return true; } public boolean mouseUp(Event evt, int x, int y){ return true; } /** * mouse event handler: let the user move things */ public boolean mouseDrag(Event evt, int x, int y){ m_angley = m_angley + .015 * (double)(x - m_mousex); m_anglex = m_anglex + .015 * (double)(y - m_mousey); m_mousex = x; m_mousey = y; damage = true; return true; } }