/** * EarthApplet.java * * Alex S. */ import java.util.*; import java.awt.*; import java.awt.image.*; public class EarthApplet extends PixApplet { /** * the renderer */ Render r; pMatrix matrix = new pMatrix(); // angle, mouse state private float m_anglex,m_angley; private int m_mousex,m_mousey; public float m_angledx = (float)Math.PI/156; public float m_angledy = (float)Math.PI/128; public boolean dragged = false; float[] vect = new float[4]; float[] norm = new float[4]; Vector lights = new Vector(); public Shape shape; public Shape sphere = new Sphere( new Material( 0,0,1, // ambient 1,1,1, // diffuse 1,1,1, // specular (float)2 // the higher, the more concentrated ) ); public Shape cube = new Cube( new Material( 1,0,0, // ambient 1,1,1, // diffuse 1,1,1, // specular (float)0.2 // the higher, the more concentrated ) ); MediaTracker tracker; Image earth,moon; Texture eartht,moont; /** * initialize */ public void init(){ super.init(); r = new Render(W,H,pix); tracker = new MediaTracker(this); earth = getImage(getDocumentBase(), "EarthMap.jpg"); tracker.addImage(earth, 0); //moon = getImage(getDocumentBase(), "MoonMap.jpg"); //tracker.addImage(moon, 0); try { tracker.waitForID(0); eartht = new Texture(earth,this); //moont = new Texture(moon,this); } catch (InterruptedException e) { return; } m_mousex = m_mousey = 0; m_anglex = m_angley = 0; lights.addElement( new Light( -30, -30,-70, 1,1,1 ) ); //lights.addElement( new Light( 0, 0,-60, 1,1,1 ) ); fixlights(); shape = sphere; } /** * ensure all lights sum upto 1. */ public void fixlights(){ float r,g,b; Enumeration enum; r=g=b=(float)0.0; enum = lights.elements(); while(enum.hasMoreElements()){ Light l = (Light)enum.nextElement(); r += l.r; g += l.g; b += l.b; } enum = lights.elements(); while(enum.hasMoreElements()){ Light l = (Light)enum.nextElement(); l.r /= r; l.g /= g; l.b /= b; } } /** * SET PIXELS FOR THIS ANIMATION FRAME * (THIS OVERRIDES A METHOD IN PIXAPPLET CLASS) */ public void setPix(int frame) { m_angley += m_angledy; m_anglex += m_angledx; r.clear(); matrix.push(); matrix.translate(0,0,-100); matrix.scale(60); matrix.rotatey(m_angley); matrix.rotatex(m_anglex); //matrix.translate(0,0,-1.4); // dump objects //shape = cube; //dumpObject(); //matrix.translate(0,0,+2.8); shape = sphere; dumpObject(); matrix.pop(); } public void dumpObject() { matrix.computeInvTrans(); float focallength = W/2; r.setshape(shape.faces,shape.verticest); r.settexture(eartht); float[] v; for(int i=0;i 0) m_anglex -= (float)Math.PI/45; if((m_mousex - x) < 0) m_angley -= (float)Math.PI/45; if((m_mousex - x) > 0) m_angley += (float)Math.PI/45; m_mousex = x; m_mousey = y; damage = true; return true; } }