home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / dxma.exe / DXMA05.cab / samples / da / java / showcase / Solar / Solar.java < prev   
Encoding:
Java Source  |  1997-11-13  |  16.4 KB  |  342 lines

  1. // Solar.java - The solar system in 3D
  2. // DirectX Media Java Demo 
  3. //
  4. // Copyright (c) 1997, Microsoft Corporation
  5. import com.ms.dxmedia.*;
  6. import java.net.*;
  7.  
  8. class SolarModel extends Model implements UntilNotifier
  9. {
  10.     // create the camera
  11.     NumberBvr               time   = (NumberBvr)localTime.runOnce();
  12.     Point3Bvr               planetPosition[]; 
  13.     Transform3Bvr   planetTransforms[];
  14.     DXMEvent                changePlanetEvent;
  15.  
  16.  
  17.     SoundBvr mySoundImporter(String file, boolean loop) {        
  18.         SoundBvr snd = importSound(buildURL(sndBase, file),
  19.                                    null);
  20.         
  21.         if (loop) {
  22.             snd = snd.loop();
  23.         }
  24.         return snd;
  25.     }
  26.     
  27.     ImageBvr myImageImporter(String file) {        
  28.         return importImage(buildURL(imgBase, file));
  29.     }
  30.     
  31.   public void createModel(BvrsToRun blst) {
  32.     CameraBvr camera = perspectiveCamera(toBvr(1),toBvr(0)); 
  33.     mediaBase = getImportBase();
  34.     sndBase = buildURL(mediaBase,"sound/");
  35.     imgBase = buildURL(mediaBase,"image/");
  36.     geoBase = buildURL(mediaBase,"geometry/");
  37.  
  38.  
  39.  
  40.         int index;      // counter
  41.         planetPosition   = new Point3Bvr[6];
  42.         planetTransforms = new Transform3Bvr[6];
  43.  
  44.         // import the images
  45.         ImageBvr inup           = myImageImporter("inup.gif");
  46.         ImageBvr outup          = myImageImporter("outup.gif");
  47.         ImageBvr zoomup         = myImageImporter("zoomup.gif");
  48.         ImageBvr zoomdown       = myImageImporter("zoomdown.gif");
  49.         ImageBvr rainup         = myImageImporter("rainup.gif");
  50.         ImageBvr raindown       = myImageImporter("raindown.gif");
  51.         ImageBvr sunjpg         = myImageImporter("sun.jpg");
  52.         ImageBvr mercuryjpg     = myImageImporter("mercury.jpg");
  53.         ImageBvr venusjpg       = myImageImporter("venus.jpg");
  54.         ImageBvr moonjpg        = myImageImporter("moon.jpg");
  55.         ImageBvr marsjpg        = myImageImporter("mars.jpg");
  56.         ImageBvr rainyPic       = myImageImporter("rain.jpg");
  57.         ImageBvr sunnyPic       = myImageImporter("earth.jpg");
  58.         ImageBvr stars          = myImageImporter("stars.jpg");
  59.                                 
  60.  
  61.         // import the sounds
  62.         SoundBvr click          = mySoundImporter("butin.wav",false);
  63.         SoundBvr sunwav         = mySoundImporter("sun.mp2",true);
  64.         SoundBvr mercurywav     = mySoundImporter("mercury.mp2",true);
  65.         SoundBvr venuswav       = mySoundImporter("venus.mp2",true);
  66.         SoundBvr moonwav        = mySoundImporter("moon.mp2",true);
  67.         SoundBvr marswav        = mySoundImporter("mars.mp2",true);
  68.         SoundBvr torain         = mySoundImporter("torain.mp2",false);
  69.         SoundBvr toearth        = mySoundImporter("toearth.mp2",false);
  70.         SoundBvr zoomin         = mySoundImporter("zoomin.mp2",false);
  71.         SoundBvr zoomout        = mySoundImporter("zoomout.mp2",false);
  72.         SoundBvr rainySound     = mySoundImporter("rain.mp2",true);
  73.         SoundBvr sunnySound     = mySoundImporter("earth.mp2",true);
  74.  
  75.  
  76.         // change the sound & texture based on the rainEvent         
  77.         PickableImage raindownPick = new PickableImage(raindown);
  78.         PickableImage rainupPick   = new PickableImage(rainup);
  79.         DXMEvent earthEvent = orEvent(andEvent(raindownPick.getPickEvent(),leftButtonDown),
  80.                                       andEvent(rainupPick.getPickEvent(),leftButtonDown));
  81.  
  82.                 SoundBvr earthSoundBvrs[]     = {sunnySound,rainySound};
  83.         Cycler earthSoundcyl          = new Cycler(earthSoundBvrs, earthEvent);
  84.         SoundBvr earthSound           = (SoundBvr)earthSoundcyl.getBvr();                                      
  85.  
  86.         ImageBvr earthBvrs[]  = {raindownPick.getImageBvr(),rainupPick.getImageBvr()};
  87.         Cycler earthStatuscyl             = new Cycler(earthBvrs,earthEvent);
  88.         PickableImage earthStatus  = new PickableImage((ImageBvr)earthStatuscyl.getBvr());
  89.  
  90.         ImageBvr earthImageBvrs[]     = {sunnyPic,rainyPic};
  91.         Cycler earthImagecyl          = new Cycler(earthImageBvrs, earthEvent);
  92.         ImageBvr earthTexture         = (ImageBvr)earthImagecyl.getBvr();
  93.  
  94.         // Describe the planets and fill in the data associated with it.
  95.         StringBvr planetNames[] = {toBvr("Sun"),
  96.                                    toBvr("Mercury"),
  97.                                    toBvr("Venus"),
  98.                                    toBvr("Earth"),
  99.                                    toBvr("Moon"),
  100.                                    toBvr("Mars")};
  101.         
  102.         ArrayBvr displayText = new ArrayBvr(planetNames);                       
  103.         ImageBvr pstatus[]        = {emptyImage,emptyImage,emptyImage,earthStatus.getImageBvr(),emptyImage,emptyImage};
  104.         ArrayBvr displayImg  = new ArrayBvr(pstatus);   
  105.                 
  106.         planetData planets[];
  107.         planets     = new planetData[6];
  108.         planets[0] = new planetData(toBvr(109.50), toBvr(0.0),   toBvr(2.00),   toBvr(1.00), toBvr(0.0),  sunwav,         sunjpg);
  109.         planets[1] = new planetData(toBvr(0.38),   toBvr(57.9),  toBvr(5.70),   toBvr(0.24), toBvr(7.0),  mercurywav, mercuryjpg);
  110.         planets[2] = new planetData(toBvr(0.95),   toBvr(108.2), toBvr(-20.00), toBvr(0.62), toBvr(3.4),  venuswav,   venusjpg);
  111.         planets[3] = new planetData(toBvr(1.00),   toBvr(149.5), toBvr(1.00),   toBvr(1.00), toBvr(23.3), earthSound, earthTexture);
  112.         planets[4] = new planetData(toBvr(0.27),   toBvr(30.0),  toBvr(27.29),  toBvr(0.25), toBvr(0.0),  moonwav,        moonjpg);
  113.         planets[5] = new planetData(toBvr(0.53),   toBvr(227.9), toBvr(1.00),   toBvr(1.88), toBvr(1.9),  marswav,        marsjpg);
  114.  
  115.         // scale the planet attributes so they look nice
  116.         for(index=0;index<6;index++)
  117.             planets[index] = scaleData(planets[index]);
  118.                 
  119.  
  120.         // place the zoom button & make sound associated with each
  121.         PickableImage  zoomUpButton     = new PickableImage(zoomup);
  122.         PickableImage  zoomDownButton   = new PickableImage(zoomdown);
  123.         ImageBvr  PickButtons[]    = {zoomUpButton.getImageBvr(),zoomDownButton.getImageBvr()};
  124.         DXMEvent buttonEvent                       = orEvent(andEvent(zoomUpButton.getPickEvent(),leftButtonDown),
  125.                                                              andEvent(zoomDownButton.getPickEvent(),leftButtonDown));
  126.         Cycler PickButtonscyl                      = new Cycler(PickButtons, buttonEvent);
  127.         PickableImage  zoomButton       = new PickableImage((ImageBvr)PickButtonscyl.getBvr());
  128.         PickableImage  placedZoomButton = new PickableImage(zoomButton.getImageBvr().transform(translate(toBvr(.05),toBvr(-.025))));
  129.         SoundBvr buttonSounds[]                    = {zoomin,zoomout};
  130.         Cycler buttonSoundscyl                     = new Cycler(buttonSounds, buttonEvent);
  131.         SoundBvr buttonSound                       = (SoundBvr)buttonSoundscyl.getBvr();
  132.  
  133.         // place the arrows on the screen
  134.         PickableImage inUpButton                = new PickableImage(inup);
  135.         PickableImage outUpButton       = new PickableImage(outup);
  136.         PickableImage placedInUpButton  = new PickableImage(inUpButton.getImageBvr().transform(translate(toBvr(.03),toBvr(-.025))));
  137.         PickableImage placedOutUpButton = new PickableImage(outUpButton.getImageBvr().transform(translate(toBvr(.037),toBvr(-.025))));
  138.  
  139.  
  140.         // center the correct planet, via events attached to the arrow images
  141.         DXMEvent evLeftButton  = andEvent(placedInUpButton.getPickEvent(),leftButtonDown).attachData(new Integer(-1));
  142.         DXMEvent evRightButton = andEvent(placedOutUpButton.getPickEvent(),leftButtonDown).attachData(new Integer(1));
  143.         changePlanetEvent      = orEvent(evLeftButton,evRightButton);
  144.         centerPlanet = (NumberBvr)untilNotify(toBvr(0),changePlanetEvent, this).runOnce();
  145.  
  146.         // place the text on the screen
  147.         ImageBvr text =
  148.             stringImage(((StringBvr)displayText.nth(centerPlanet)),
  149.                       defaultFont.color(white).bold());
  150.  
  151.         text = text.transform(translate(-.048,-.025));
  152.  
  153.         // fill in the planetPositions
  154.         planetPos(planets); 
  155.         ArrayBvr  planetPosArray   = new ArrayBvr(planetPosition);
  156.  
  157.               Point3Bvr currPlanet = (Point3Bvr)planetPosArray.nth(centerPlanet);
  158.  
  159.         // set the camera to look at the correct place....
  160.         Transform3Bvr c = Transform3Bvr.newUninitBvr();
  161.         c.init(until(lookAtFrom(currPlanet,point3(toBvr(0),toBvr(35),toBvr(40)),yVector3), orEvent(andEvent(zoomUpButton.getPickEvent(),leftButtonDown),
  162.                                                                                                                                        andEvent(zoomDownButton.getPickEvent(),leftButtonDown)),
  163.                      until(lookAtFrom(currPlanet,point3(toBvr(0),sub(toBvr(35),mul(localTime,toBvr(35))),sub(toBvr(40),mul(localTime,toBvr(20)))),yVector3),
  164.                            predicate(lte(sub(toBvr(35),mul(localTime,toBvr(35))),toBvr(0))),
  165.                            until(lookAtFrom(currPlanet,point3(toBvr(0),toBvr(0), toBvr(15)),yVector3), orEvent(andEvent(zoomUpButton.getPickEvent(),leftButtonDown),
  166.                                                                                                                                                    andEvent(zoomDownButton.getPickEvent(),leftButtonDown)),
  167.                                  until(lookAtFrom(currPlanet,point3(toBvr(0),add(toBvr(0),mul(localTime,toBvr(35))),add(toBvr(15),mul(localTime,toBvr(25)))),yVector3),
  168.                                        predicate(lte(sub(toBvr(35),mul(localTime,toBvr(35))),toBvr(0))),
  169.                         
  170.                                        c)))));
  171.  
  172.             
  173.         camera = camera.transform(c);
  174.  
  175.         // Create the microphone to be transformed to the same
  176.         // place the camera is.
  177.         MicrophoneBvr mic = defaultMicrophone.transform(c);
  178.  
  179.         // create the planets in the scene, based on the camera
  180.         GeometryBvr worldGeo = placePlanets(planets);                        
  181.         ImageBvr worldImage  = worldGeo.render(camera);
  182.  
  183.         // Create the sound, and increase its gain to make it more
  184.         // audible.  May need to play with this number.
  185.         SoundBvr worldSound  = worldGeo.render(mic);
  186.  
  187.                 worldSound = worldSound.gain(0.5);
  188.  
  189.         // If planet in center is Earth then show 
  190.         ImageBvr EarthWeather = ((ImageBvr)displayImg.nth(centerPlanet)).transform(translate(toBvr(-.035),toBvr(-.025)));
  191.                 
  192.                 
  193.         // create the model
  194.         setImage(overlay(text,
  195.                          overlay(EarthWeather,
  196.                                  overlay(placedInUpButton.getImageBvr(),
  197.                                          overlay(placedOutUpButton.getImageBvr(),
  198.                                                  overlay(placedZoomButton.getImageBvr(),
  199.                                                          overlay(worldImage,stars)))))));
  200.  
  201.         setSound(mix(buttonSound, worldSound));
  202.  
  203.     }
  204.     public void cleanup() {
  205.       super.cleanup();
  206.       centerPlanet=null;
  207.     }
  208.  
  209.     // set up where the planets should be and the transform3bvr to use on each planet
  210.     void planetPos(planetData planets[])
  211.     {
  212.         int index;
  213.                 
  214.         for(index=0;index<6;index++)
  215.           {
  216.               Transform3Bvr xfpt;
  217.               if(index == 4) // moon
  218.                 {
  219.                     xfpt = compose(translate(sub(planetPosition[3],origin3)),
  220.                                    compose(rotate(yVector3,div(mul(toBvr(2),mul(toBvr(Math.PI),time)), planets[index].orbit)),
  221.                                            translate(planets[index].distance,toBvr(0),toBvr(0))));
  222.                 }
  223.               else
  224.                 {
  225.                     xfpt = compose(identityTransform3,
  226.                                    compose(rotate(yVector3,div(mul(toBvr(2),mul(toBvr(Math.PI),time)), planets[index].orbit)),
  227.                                            translate(planets[index].distance,toBvr(0),toBvr(0))));
  228.                 }
  229.               planetTransforms[index] = xfpt;
  230.               planetPosition[index]   = origin3.transform(xfpt);
  231.           }
  232.  
  233.     }
  234.  
  235.         NumberBvr interpolate1(NumberBvr from, NumberBvr to, NumberBvr num)  {
  236. //      NumberBvr dt = sin(localTime);//mul(cycle,add(toBvr(.5),div(sin(localTime),toBvr(2))));
  237. //          NumberBvr dx = div(mul(num,sub(X(P2),X(P1))),dt);
  238. //          NumberBvr dy = div(mul(num,sub(Y(P2),Y(P1))),dt);
  239.  
  240.             NumberBvr temp1 = sub(to,from);
  241.             NumberBvr temp2 = mul(temp1,num);
  242.             NumberBvr temp3 = add(from, temp2);
  243.             return temp3;
  244.         }
  245.  
  246.         Point3Bvr interpolatePoint3(Point3Bvr from, Point3Bvr to, NumberBvr num)  {
  247.             return point3(
  248.                 interpolate1(from.getX(),to.getX(),num),
  249.                 interpolate1(from.getY(),to.getY(),num),
  250.                 interpolate1(from.getZ(),to.getZ(),num)
  251.             );
  252.         }
  253.  
  254.  
  255.     // Place the planets in the corrent spot in space.
  256.     GeometryBvr placePlanets(planetData planets[])
  257.     {
  258.         int index;
  259.         GeometryBvr sphere =
  260.             importGeometry(buildURL(geoBase,"sphere.x"));
  261.         GeometryBvr ss                                  = emptyGeometry;
  262.         
  263.         for(index=0;index<6;index++)
  264.           {
  265.               Transform3Bvr xfGeo = compose(rotate(zVector3,div(mul(planets[index].inclination,toBvr(Math.PI)),toBvr(180))),
  266.                                             compose(rotate(yVector3, div(mul(toBvr(2),mul(toBvr(Math.PI),time)),planets[index].period)),
  267.                                                     scale3(planets[index].radius)));
  268.  
  269.               GeometryBvr snd = soundSource(planets[index].sound);
  270.               GeometryBvr sndplanet = union(snd, sphere);
  271.  
  272.               ss = union(ss,
  273.                          ((sndplanet.texture((planets[index].image).mapToUnitSquare())).transform(xfGeo).transform(planetTransforms[index])));
  274.  
  275.           }                                       
  276.  
  277.         return(union(ss, directionalLight));
  278.     }
  279.  
  280.     // scale the planet attributes so they look nice
  281.     planetData scaleData(planetData planet)
  282.     {
  283.         // scale factors
  284.         NumberBvr periodScale   = toBvr(0.1);
  285.         NumberBvr timeScale             = toBvr(10);
  286.         NumberBvr distScale             = toBvr(100);
  287.         NumberBvr radiusScale   = toBvr(6);
  288.         
  289.         // speed up everything by a timescale
  290.         planet.period   = div(planet.period,periodScale);
  291.         planet.orbit    = div(mul(planet.orbit,toBvr(365)),timeScale);
  292.  
  293.         // adjust the sizes larger
  294.         planet.distance = div(planet.distance,distScale);
  295.         planet.radius   = div(add(toBvr(1),log10(planet.radius)),radiusScale);
  296.  
  297.         return(planet);
  298.     }
  299.  
  300.     // Handle the change from one planet to another, via the arrow buttons
  301.   public Behavior notify(Object eventData,
  302.                          Behavior currentRunningBvr,
  303.                          BvrsToRun blst) {
  304.  
  305.       NumberBvr currentNum = (NumberBvr)currentRunningBvr;
  306.       int increment            = ((Integer)eventData).intValue();     
  307.       NumberBvr newValue   = add(currentNum,toBvr(increment));
  308.       newValue                         = (NumberBvr) cond(lt(newValue,toBvr(0)) ,toBvr(5),newValue);
  309.       newValue                         = (NumberBvr) cond(gt(newValue,toBvr(5)) ,toBvr(0),newValue);          
  310.       return untilNotify(newValue,changePlanetEvent,this);
  311.   }
  312.   URL mediaBase;
  313.   URL sndBase;
  314.   URL imgBase;
  315.   URL geoBase;
  316.   NumberBvr centerPlanet;
  317. }
  318.  
  319. // Class used from storing the data associated with each planet
  320. class planetData {
  321.         NumberBvr       radius,distance,period,orbit,inclination;
  322.         SoundBvr        sound;
  323.         ImageBvr        image;
  324.  
  325.         planetData(NumberBvr radiusIN,NumberBvr distanceIN,
  326.                            NumberBvr periodIN,NumberBvr orbitIN, NumberBvr inclinationIN,SoundBvr soundIN,ImageBvr imageIN)
  327.         {
  328.                 radius          = radiusIN;
  329.                 distance        = distanceIN;
  330.                 period          = periodIN;
  331.                 orbit           = orbitIN;
  332.                 inclination     = inclinationIN;
  333.                 sound           = soundIN;
  334.                 image           = imageIN;
  335.         }
  336. }
  337.                                         
  338. public class Solar extends DXMApplet {
  339.         public Solar() { setModel(new SolarModel()); }
  340. }
  341.                                                          
  342.