import java.awt.*; public class DrawMesh extends PixApplet { int w, h; // width/height of applet window int M = 30, N = 30; int mouseX =0, mouseY = 0, phi = 0, theta = 0; double focalLength = -3; int editedPoint = -1; final double[] bgColor = {0.3, 0.3, 0.3}; double[][] zBuffer; double[][][] frameBuffer; double[][] gx = { { -1.0, -0.5, 0.5, 1.0}, { -1.0, -0.5, 0.5, 1.0}, { -1.0, -0.5, 0.5, 1.0}, { -1.0, -0.5, 0.5, 1.0} }; double[][] gy = { { 1.0, 1.0, 1.0, 1.0}, { 0.5, 0.5, 0.5, 0.5}, { -0.5, -0.5, -0.5, -0.5}, { -1.0, -1.0, -1.0, -1.0} }; double[][] gz = { { -0.0, 4.0, -1.0, 0.0}, { -0.0, 4.0, -1.0, 0.0}, { -0.0, 4.0, -1.0, 0.0}, { -0.0, 4.0, -1.0, 0.0} }; Light light1 = new Light( 1, 1, 1, 0.3, 0.4, 0.8); Light light2 = new Light(-1, -1, 1, 0.2, 0.4, 0.4); Light light3 = new Light( 0, 0, 1, 0.5, 0.2, 0.2); Light[] lights = {light1, light2, light3}; Material sphereMaterial1= new Material(0.3, 0.3, 0.6, 0.2, 0.2, 0.3, 0.4, 0.4, 1.0, 4); Material sphereMaterial2= new Material(0.6, 0.3, 0.3, 0.3, 0.2, 0.2, 1.0, 0.4, 0.4, 4); Material tubeMaterial = new Material(0.3, 0.6, 0.3, 0.3, 0.4, 0.3, 0.4, 1.0, 0.4, 4); Material cubeMaterial = new Material(0.6, 0.6, 0.6, 0.2, 0.2, 0.2, 1.0, 1.0, 1.0, 4); Material patchMaterial = new Material(0.4, 0.8, 0.5, 0.2, 0.6, 0.3, 1.0, 1.0, 1.0, 4); //Material sphereMaterial1= new Material(0.3, 0.3, 0.6, 0.2, 0.2, 0.3, 0.2, 0.2, 0.8, 2); //Material sphereMaterial2= new Material(0.6, 0.3, 0.3, 0.3, 0.2, 0.2, 0.8, 0.1, 0.1, 1.5); //Material tubeMaterial = new Material(0.3, 0.6, 0.3, 0.3, 0.4, 0.3, 0.2, 0.8, 0.0, 2); //Material cubeMaterial = new Material(0.6, 0.6, 0.6, 0.2, 0.2, 0.2, 0.8, 0.8, 0.0, 2); SplinePatch patch = new SplinePatch(M, N, gx, gy, gz); Sphere sphere1 = new Sphere(M, N); Sphere sphere2 = new Sphere(M, N); Cube cube1 = new Cube(); Disc disc1 = new Disc(M); Tube tube = new Tube(M); Cylinder cylinder = new Cylinder(M); MatrixStack stack = new MatrixStack(); Matrix3D t = new Matrix3D(); public void setPix(int frame) { System.out.println("drawmesh: frame " + frame); w = bounds().width; // FIND OUT HOW BIG THE APPLET WINDOW IS h = bounds().height; zBuffer = new double[w][h]; frameBuffer = new double[w][h][3]; for (int i = 0; i < frameBuffer.length; i++) { for (int j = 0; j < frameBuffer[i].length; j++) { frameBuffer[i][j][0] = bgColor[0]; frameBuffer[i][j][1] = bgColor[1]; frameBuffer[i][j][2] = bgColor[2]; zBuffer[i][j] = Double.MIN_VALUE; } } push(); t.translate(0, 0, -6); t.rotateX(phi * Math.PI / 180); t.rotateY(theta * Math.PI / 180); push(); // AFFECT ALL ITEMS IN WORLD patch.setGX(gx); patch.setGY(gy); patch.setGZ(gz); patch.reset(); patch.setMaterial(patchMaterial); patch.transformedBy(t); patch.bufferTo(zBuffer, frameBuffer, lights, focalLength); pop(); pop(); // display frame buffer int n = 0; for (int j = 0 ; j < H ; j++) // LOOP OVER IMAGE ROWS for (int i = 0 ; i < W ; i++) { // LOOP OVER IMAGE COLUMNS pix[n++] = pack(Math.max(0,Math.min(255,(int)(255 * frameBuffer[i][j][0]))), Math.max(0,Math.min(255,(int)(255 * frameBuffer[i][j][1]))), Math.max(0,Math.min(255,(int)(255 * frameBuffer[i][j][2])))); } } private void push() { stack.push(); t = stack.get(); } private void pop() { stack.pop(); t = stack.get(); } public boolean mouseDown(Event e, int x, int y) { mouseX = x; mouseY = y; return true; } public boolean mouseDrag(Event e, int x, int y) { theta = theta + x - mouseX; phi = phi + y - mouseY; mouseX = x; mouseY = y; damage = true; return true; } public void editNextPoint() { editedPoint++; editedPoint = editedPoint % 16; System.out.println("edited point is " + editedPoint ); } public void movePoint(int axis, double scale) { System.out.println("axis: " + axis + "; scale: " + scale); int row = editedPoint / 4; int col = editedPoint % 4; double adjustment = 0.1; adjustment *= scale; if (axis == 0) gx[row][col] += adjustment; else if (axis == 1) gy[row][col] += adjustment; else if (axis == 2) gz[row][col] += adjustment; damage = true; } public boolean keyDown(Event e, int key) { System.out.println("key is " + key); switch (key) { case ' ': editNextPoint(); break; case '+': movePoint(2, 1); break; case '-': movePoint(2, -1); break; case 1004: //UP movePoint(1, 1); break; case 1005: //DOWN movePoint(1, -1); break; case 1006: //LEF movePoint(0, -1); break; case 1007: // Right movePoint(0, 1); break; } return true; } }