/* * CubeApplet.java * * (C) 2004, Alex S. */ import java.awt.*; import java.util.Vector; public class CubeApplet 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 curve_points = new Vector(); Vector object_points = new Vector(); // current rendering color Color currentColor = Color.red; int detail = 64; //256; private double v1_angle = 0; private double v1_speed = Math.PI/41; // angle, mouse state private double m_anglex,m_angley; private int m_mousex,m_mousey; private double m_xrotspeed = Math.PI/768; private double m_yrotspeed = Math.PI/512; /** * initialize things */ public void init(){ super.init(); m_mousex = m_mousey = 0; m_anglex = m_angley = Math.PI/4; pmatrix.translate(bounds().width/2,bounds().height/2,0); pmatrix.perspective(bounds().width,-1); // initialize curve Vector v_points = new Vector(); Vector v_faces = new Vector(); for(int 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; } } /** * function that gets called whenever something needs to be * rendered. */ public void render(Graphics g) { if (true || damage) { g.setColor(Color.lightGray); g.fillRect(0, 0, bounds().width, bounds().height); matrix.push(); matrix.translate(0,0,-144); matrix.scale(3); matrix.rotatey(m_angley); matrix.rotatex(m_anglex); dumpScene(); // 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]); } Color c = (Color)colors.elementAt(poly); double color1 = dotProduct(plane,new double[]{0,0,-1}); if(color1 < 0) color1 = 0; g.setColor( new Color( (float)(((c.getRed()+50) * color1)/306.0), (float)(((c.getGreen()+50) * color1)/306.0), (float)(((c.getBlue()+50) * color1)/306.0) ) ); g.fillPolygon(x,y,x.length); //g.setColor(c); //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[] d1 = new double[4]; double[] d2 = new double[4]; vectorSubtract( b, a, d1 ); vectorSubtract( c, 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_xrotspeed = m_yrotspeed = 0.0; return true; } public boolean mouseUp(Event evt, int x, int y){ m_xrotspeed = Math.PI/768; m_yrotspeed = Math.PI/512; return true; } /** * mouse event handler: let the user move things */ public boolean mouseDrag(Event evt, int x, int y){ if((m_mousey - y) < 0) m_anglex += Math.PI/45; if((m_mousey - y) > 0) m_anglex -= Math.PI/45; if((m_mousex - x) < 0) m_angley -= Math.PI/45; if((m_mousex - x) > 0) m_angley += Math.PI/45; m_mousex = x; m_mousey = y; damage = true; return true; } }