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

  1.  
  2. // Copyright (c) 1997 Microsoft Corporation
  3.  
  4. import com.ms.dxmedia.*;
  5. import java.net.*;
  6.  
  7. class UnionModel extends Model {
  8.  
  9.   public static ImageBvr geometryImage(GeometryBvr geo) {
  10.       CameraBvr cam =
  11.           perspectiveCamera(toBvr(1),toBvr(0)).transform(translate(0,0,2));
  12.       Transform2Bvr sc = scale2(0.1);
  13.       
  14.       return geo.render(cam).transform(sc);
  15.   }
  16.  
  17.   public static ColorBvr myColor(double hue,
  18.                                   double saturationRate) {
  19.       NumberBvr sat = add(mul(sin(mul(localTime,
  20.                                       toBvr(saturationRate))),
  21.                               toBvr(0.5)),
  22.                           toBvr(0.5));
  23.  
  24.       return colorHsl(toBvr(hue), sat, toBvr(0.5));
  25.   }
  26.  
  27.   public void createModel(BvrsToRun blst) {
  28.  
  29.       // Make these relative
  30.         URL mediaBase = getImportBase();
  31.       URL geoBase = buildURL(mediaBase,"geometry/");
  32.       URL sndBase = buildURL(mediaBase,"sound/");
  33.       
  34.       GeometryBvr rawSphere = importGeometry(buildURL(geoBase,"sphere.x"));
  35.       GeometryBvr rawCube = importGeometry(buildURL(geoBase,"cube.x"));
  36.       GeometryBvr rawCylinder = importGeometry(buildURL(geoBase,"cylinder.x"));
  37.       GeometryBvr rawCone = importGeometry(buildURL(geoBase,"cone.x"));
  38.       // Import a sound, supply null as the second argument since we don't
  39.       // want a length. 
  40.       SoundBvr snd = importSound(buildURL(sndBase,"etherial.mp2"), null).loop();
  41.  
  42.       Transform3Bvr scaler = scale3(0.25);
  43.  
  44.       GeometryBvr sphere   = rawSphere.transform(scaler);
  45.       GeometryBvr cube     = rawCube.transform(scaler);
  46.       GeometryBvr cone     = rawCone.transform(scaler);
  47.       GeometryBvr cylinder = rawCylinder.transform(scaler);
  48.  
  49.       GeometryBvr xcone    = cone.transform(rotate(xVector3,
  50.                                                    toBvr(Math.PI)));
  51.  
  52.       ColorBvr col1 = myColor(0, 2);
  53.       ColorBvr col2 = myColor(0.25, 3);
  54.       ColorBvr col3 = myColor(0.5, 4);
  55.       ColorBvr col4 = myColor(0.75, 1);
  56.  
  57.       // Shapes to start with (many with time varying colors)
  58.       GeometryBvr shapes[] = { sphere.diffuseColor(col1),
  59.                                sphere.diffuseColor(col1),
  60.                                cone.diffuseColor(col2),
  61.                                xcone.diffuseColor(col2),
  62.                                cube.diffuseColor(col3),
  63.                                cube.diffuseColor(col3),
  64.                                cylinder.diffuseColor(col4) };
  65.  
  66.       // Places to translate them to.
  67.       double tls[][] = { { 0.75, 0, 0 },
  68.                          { -0.75, 0, 0 },
  69.                          { 0, 0.75, 0 },
  70.                          { 0, -0.75, 0 },
  71.                          { 0, 0, 0.75 },
  72.                          { 0, 0, -0.75 },
  73.                          { 0, 0, 0 } };
  74.  
  75.       // Go ahead and accumulate up the translated geometry by cycling
  76.       // through the arrays.
  77.       GeometryBvr geo = emptyGeometry;
  78.       for (int i = 0; i < shapes.length; i++) {
  79.           Transform3Bvr xf = translate(tls[i][0],tls[i][1],tls[i][2]);
  80.           geo = union(geo, shapes[i].transform(xf));
  81.       }
  82.  
  83.       // Set the whole thing in motion
  84.       Transform3Bvr xf =
  85.           compose(rotate(xVector3, localTime),
  86.             compose(rotate(zVector3, mul(localTime, toBvr(1.9))),
  87.               compose(rotate(yVector3, mul(localTime, toBvr(Math.PI))),
  88.                 scale3(0.75))));
  89.  
  90.       geo = geo.transform(xf);
  91.  
  92.       // Render into an image, and overlay atop a solid image.
  93.       ImageBvr geoIm = geometryImage(union(geo, directionalLight));
  94.  
  95.       ImageBvr model = overlay(geoIm, solidColorImage(white));
  96.       
  97.       setImage(model);
  98.       setSound(snd);
  99.   }
  100. }
  101.  
  102. public class Union extends DXMApplet {
  103.     public Union() { setModel(new UnionModel()) ; }
  104. }
  105.  
  106.  
  107.  
  108.