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

  1. // Pick3.java - Demonstrates 3D picking
  2. // DirectX Media Java Demo 
  3. //
  4. // Copyright (c) 1997 Microsoft Corporation
  5.  
  6. import com.ms.dxmedia.*;
  7. import java.net.*; //added to support URL's
  8.  
  9. class Pick3Test extends Model {
  10.  
  11.     NumberBvr speed = toBvr(0.07);
  12.  
  13.   public void createModel(BvrsToRun blst) {
  14.       Transform3Bvr size  = scale3(0.25);
  15.  
  16.     URL mediaBase = getImportBase();
  17.     URL geoBase = buildURL(mediaBase,"geometry/");
  18.     URL imgBase = buildURL(mediaBase,"image/");
  19.  
  20.       // import the Geometry
  21.       GeometryBvr rawCube     =
  22.           importGeometry(buildURL(geoBase,"cube.x")).transform(size);
  23.             GeometryBvr rawCylinder =
  24.           importGeometry(buildURL(geoBase,"cylinder.x")).transform(size);
  25.             GeometryBvr rawCone = 
  26.                   importGeometry(buildURL(geoBase,"cone.x")).transform(size);
  27.  
  28.       // import images
  29.       ImageBvr stillSky = importImage(buildURL(imgBase,"cldtile.jpg"));
  30.  
  31.       // activate the Geometry (make it pickable)
  32.       GeometryBvr cone1 = activate(rawCone,green);
  33.       GeometryBvr cube1 = activate(rawCube,magenta);
  34.       GeometryBvr cube2 = activate(rawCube,colorHsl(div(localTime,toBvr(8)),
  35.                                                     toBvr(1),
  36.                                                     toBvr(0.5)));
  37.       GeometryBvr cylinder = activate(rawCylinder,colorRgb(0.8,
  38.                                                            0.4,
  39.                                                            0.3));
  40.  
  41.       GeometryBvr multigeo =
  42.           union(cone1.transform(translate(0,1,0)),
  43.                 union(cube1.transform(translate(0,0,1)),
  44.                       union(cube2.transform(translate(0,0,-1)),
  45.                             cylinder)));
  46.  
  47.       GeometryBvr geo =
  48.           multigeo.transform(scale(add(abs(sin(mul(localTime,toBvr(0.2)))),toBvr(0.5)),
  49.                                    add(abs(sin(mul(localTime,toBvr(0.26)))),toBvr(0.5)),
  50.                                    add(abs(sin(mul(localTime,toBvr(0.14)))),toBvr(0.5))));
  51.       
  52.       Point2Bvr maxSky         = stillSky.boundingBox().getMax();
  53.  
  54.       ImageBvr tiledSky        = stillSky.tile();
  55.       ImageBvr movingSky       =
  56.           tiledSky.transform(translate(mul(localTime,div(maxSky.getX(),toBvr(8))),
  57.                                        mul(localTime,div(maxSky.getX(),toBvr(16)))));
  58.  
  59.       ImageBvr movingGeoImg=
  60.           geometryImage(geo.transform(compose(rotate(zVector3,mul(speed,mul(localTime,toBvr(1.9)))),
  61.                                               rotate(yVector3,mul(speed,mul(localTime,toBvr(Math.PI)))))));
  62.  
  63.       FontStyleBvr fs = defaultFont.color(black);
  64.       
  65.       ImageBvr titleIm =
  66.           stringImage(toBvr("Left Click On An Object"), fs)
  67.              .transform(translate(0,0.025));
  68.                 
  69.       // set the Image
  70.       setImage(overlay(titleIm,overlay(movingGeoImg,movingSky)));
  71.   }
  72.  
  73.     GeometryBvr activate(GeometryBvr unpickedGeo, ColorBvr col) {
  74.  
  75.         PickableGeometry pickGeo = new PickableGeometry(unpickedGeo);
  76.  
  77.         DXMEvent pickEvent =
  78.             andEvent(leftButtonDown, pickGeo.getPickEvent());
  79.  
  80.         NumberBvr numcyc = NumberBvr.newUninitBvr();
  81.  
  82.         numcyc.init(until(toBvr(0), pickEvent,
  83.                           until(toBvr(1), pickEvent, numcyc)));
  84.  
  85.         ColorBvr colcyc = ColorBvr.newUninitBvr();
  86.  
  87.         colcyc.init(until(white, pickEvent,
  88.                           until(col, pickEvent, colcyc)));
  89.                 
  90.         Transform3Bvr xf  = rotate(xVector3,integral(numcyc));
  91.  
  92.         return(pickGeo.getGeometryBvr().diffuseColor(colcyc).transform(xf));
  93.     }
  94.  
  95.     ImageBvr geometryImage(GeometryBvr geo) {
  96.                         
  97.         NumberBvr scaleFactor   = toBvr(0.04);
  98.  
  99.         GeometryBvr light =
  100.             union(directionalLight.transform(rotate(xVector3, div(toBvr(Math.PI),toBvr(2)))),
  101.                   directionalLight);
  102.  
  103.         // allow change of transform3Bvr with rightMouseDown
  104.         //Transform3Bvr perspTransform = Transform3Bvr.newUninitBvr();
  105.  
  106.         NumberBvr zero = toBvr(0);
  107.         NumberBvr one = toBvr(1);
  108.  
  109.         //perspTransform.init(until(translate(0, 0, 0.2),
  110.         //                          rightButtonDown,
  111.         //                          until(compose(translate(0,0,0),
  112.          //                                       scale(1,1,1e8)),
  113.          //                               rightButtonDown, perspTransform)));
  114.         
  115.         // allow change of stringBvr with rightMouseDown
  116.         StringBvr strcyl = StringBvr.newUninitBvr();
  117.         
  118.         strcyl.init(until(toBvr("Perspective - Right Click to Switch"),
  119.                           rightButtonDown,
  120.                           until(toBvr("Parallel - Right Click to Switch"),
  121.                                 rightButtonDown, strcyl)));
  122.         
  123.         // create the camera
  124.         CameraBvr  perspectiveCam = (perspectiveCamera(one,zero)).transform(
  125.                     compose(rotate(xVector3,mul(speed,localTime)),translate(0, 0, 0.2)));
  126.  
  127.         CameraBvr  parallelCam = (parallelCamera(one)).transform(
  128.                     rotate(xVector3,mul(speed,localTime)));
  129.  
  130.                 CameraBvr camera = CameraBvr.newUninitBvr();
  131.                 camera.init(until(perspectiveCam,rightButtonDown,until(parallelCam,
  132.           rightButtonDown,camera)));
  133.  
  134.         FontStyleBvr fs = defaultFont.color(red);
  135.         ImageBvr txtIm  = stringImage(strcyl, fs);
  136.         ImageBvr xltTxt = txtIm.transform(translate(0, -0.025));
  137.  
  138.         return(overlay(xltTxt,union(geo.transform(scale3(scaleFactor)),
  139.                                     light).render(camera)));                
  140.     }
  141. }
  142.                                         
  143. public class Pick3 extends DXMApplet {
  144.         public Pick3() { setModel(new Pick3Test()); }
  145. }
  146.                                                          
  147.  
  148.  
  149.