/* * pCube2.java * * By using this software you have agreed to: * NOBODY is responsible for ANYTHING undesirable this program does. * (this includes any software thats derived from this code) * * http://www.theparticle.com * * Copyright(c) 2001, Particle */ import java.applet.*; import java.awt.*; import java.util.Vector; /** * pCube2 applet class * * use it as you see fit, but please give credit when it is due. * * * Some of this code has been derived from Quake1 tools, since * I was too lazy to type my own, and cut+paste seemed like * a quick & easy way of reusing good parts of those tools. * * There are also a few pieces of code from Michael Abrash's books. */ public class pCube2 extends Applet { // implements Runnable{ // private Thread m_pCube = null; private int m_width,m_height; // support for double buffer private Image m_image; private Graphics m_g; // angle, mouse state private double m_anglex,m_angley; private int m_mousex,m_mousey; // points for the cube private double[][] points = { {15,15,15,1},{15,15,-15,1},{-15,15,-15,1},{-15,15,15,1}, {15,-15,15,1},{15,-15,-15,1},{-15,-15,-15,1},{-15,-15,15,1} }; // cube's faces private int[][] faces = { {0,1,2,3},{7,6,5,4}, {4,5,1,0},{5,6,2,1}, {6,7,3,2},{7,4,0,3} }; // method is called when applet initializes public void init(){ m_width = size().width; m_height = size().height; // init angle + mouse m_mousex = m_mousey = 0; m_anglex = m_angley = Math.PI/4; // init double buffer m_image = createImage(m_width,m_height); m_g = m_image.getGraphics(); // triangulate the cube (using a very ugly approach) Vector p = new Vector(); Vector f = new Vector(); int i; for(i=0;i bx) return 1; else if(ax < bx) return -1; return 0; } /** * sort faces by their middle Z using insertion sort * (could be time consuming for large number of faces) */ public void sortFacesByZ(int[][] a,double[][] p){ int i,j; int[] e; for(i=1;i=0 && comparePointsByZ(a[j],e,p) > 0;j--) a[j+1] = a[j]; a[j+1] = e; } } // paint public void paint(Graphics g){ // clear screen (in double buffer) m_g.setColor(Color.white); m_g.fillRect(0,0,m_width,m_height); // make our transformed points double[][] _points1 = new double[points.length][4]; // make our transformed points double[][] _points = new double[points.length][4]; // create initial matrix double[][] m1 = new double[4][4]; clearMatrix(m1); double[][] m2 = new double[4][4]; // scale cube scaleMatrix(m1,3,m2); // rotate it (using the angles) rotateMatrixX(m2,m_anglex,m1); rotateMatrixY(m1,m_angley,m2); // translate it away from the origin translateMatrix(m2,0,0,-200,m1); // trnansform all points in the cube using the matrix for(int i=0;i 0){ // prepare params for java's fill poly int[] x = new int[face.length]; int[] y = new int[face.length]; // apply perspective transform, and move to center of viewing area for(int j=0;j 0xFF) color = 0xFF; // set color for fill m_g.setColor(new Color(color, color, color)); // fill poly m_g.fillPolygon(x,y,face.length); // draw borders, and vertex numbers. m_g.setColor(Color.black); m_g.drawPolygon(x,y,face.length); for(int j=0;j 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; // update screen with new angles repaint(); return true; } }