import java.applet.Applet; import java.awt.*; import go.*; class GoLightDemo extends GoInterface { GoSphere sphere; GoMatrix currentLightPosition = new GoMatrix(); GoMatrix mouseDraggedLightPosition = new GoMatrix(); int xStart; int yStart; GoLightDemo() { // // Create a sphere to illustrate this demo. A sphere // is a good shape for understanding how lighting works. // sphere = new GoSphere(1.0, 16, 16); // // Turn on LIGHT_0 // go.light(Go.LIGHT_0, true); // // Set position of LIGHT_0 bases the current modelview matrix, // which is at the point the identity matrix. // setLightPosition(); } // // Interface render method. // public void render() { // // Clear. // go.clear(Go.IMAGE); // // Draw. // go.color(0, 0, 1); // blue sphere.renderSolid(go); go.color(1, 1, 0); // yellow sphere.renderWire(go); // // Display. // swap(); } // // Interface resized method. // public void resized(int width, int height) { // // Setup projection matrix. // go.push(Go.MATRIX_MODE); go.matrixMode(Go.PROJECTION); go.identity(); if(width > height) { go.ortho(-1.0 * width / height, 1.0 * width / height, -1.0, 1.0, -1.0, 1.0); } else { go.ortho(-1.0, 1.0, -1.0 * height / width, 1.0 * height / width, -1.0, 1.0); } go.pop(Go.MATRIX_MODE); } // // Interface mousePressed method. // public void mousePressed(int x, int y) { // // Save values that will be used to track the // mouse in the mouseDragged() method. // xStart = x; yStart = y; } // // Interface mouseReleased method. // public void mouseReleased(int x, int y) { // // Replace the currentLightPosition with the final result // of mouseDraggedLightPosition. // for(int i = 0; i < 16; i++) { currentLightPosition.m[i] = mouseDraggedLightPosition.m[i]; } } // // Interface mouseDragged method. // public void mouseDragged(int x, int y) { int xEnd = x; int yEnd = y; // // Save the modelview matrix. // go.push(Go.MODELVIEW); // // Setup modelview matrix based on mouse position. // go.identity(); double xTheta = (yEnd - yStart) / 2; double yTheta = (xEnd - xStart) / 2; go.rotate(xTheta, 1, 0, 0); go.rotate(yTheta, 0, 1, 0); // // Post multiply current light position. // go.multiply(currentLightPosition); // // Setup the light position with the current modelview matrix. // setLightPosition(); // // Save the "mouse dragged" light position so that // currentLightPosition can be updated in mouseReleased(). // go.getModelview(mouseDraggedLightPosition); // // Restore the modelview matrix. // go.pop(Go.MODELVIEW); // // Redisplay. // rerender(); } // // Light position. // void setLightPosition() { go.light(Go.LIGHT_0, Go.POSITIONAL, 0.0, 0.0, 1.75); } } public class light extends Applet { public void init() { setLayout(new GridLayout(1, 1)); add(new GoLightDemo()); } }