public void alignContent() {
    try {
      bounds = calculateBoundingBox(wrap(content));
    } catch (Exception e) {
      return;
    }
    removeZeroExtends(bounds);
    double scale = 1;
    double[] e = bounds.getExtent();
    double[] center = bounds.getCenter();
    double objectSize = Math.max(Math.max(e[0], e[1]), e[2]);
    scale = contentSize / objectSize;
    Matrix matrix = MatrixBuilder.euclidean().scale(scale).getMatrix();

    matrix.assignTo(scalingComponent);

    bounds = bounds.transformByMatrix(bounds, matrix.getArray());
    center = bounds.getCenter();

    FactoredMatrix factoredMatrix = new FactoredMatrix(transformationComponent.getTransformation());
    double angle = factoredMatrix.getRotationAngle();
    double[] axis = factoredMatrix.getRotationAxis();

    Matrix m =
        MatrixBuilder.euclidean()
            .translate(0, verticalOffset, 0)
            .rotate(angle, axis)
            .translate(-center[0], -bounds.getMinY(), -center[2])
            .getMatrix();
    m.assignTo(transformationComponent);
    bounds = bounds.transformByMatrix(bounds, m.getArray());
  }
 public static void main(String[] args) {
   // a little Scene (two boxes and a bangle, transfomation, appearance)
   IndexedFaceSet box = Primitives.box(2, .5, .5, false);
   IndexedFaceSet box2 = Primitives.box(2, .6, 0.4, true);
   IndexedFaceSet zyl = Primitives.cylinder(20, 1, 0, .5, 5);
   SceneGraphComponent original = new SceneGraphComponent();
   SceneGraphComponent childNode1 = new SceneGraphComponent();
   MatrixBuilder.euclidean().translate(0, 0, 1).assignTo(childNode1);
   SceneGraphComponent childNode2 = new SceneGraphComponent();
   Appearance app = new Appearance();
   app.setAttribute(
       CommonAttributes.POLYGON_SHADER + "." + CommonAttributes.DIFFUSE_COLOR,
       new Color(255, 255, 0));
   childNode2.setAppearance(app);
   original.addChild(childNode1);
   original.addChild(childNode2);
   original.setGeometry(box2);
   childNode1.setGeometry(box);
   childNode2.setGeometry(zyl);
   // the Factory:
   GeometryMergeFactory mergeFact = new GeometryMergeFactory();
   // play with the following 3 optional settings (by default they are true)
   mergeFact.setRespectFaces(true);
   mergeFact.setRespectEdges(true);
   mergeFact.setGenerateVertexNormals(true);
   // you can set some defaults:
   List defaultAtts = new LinkedList();
   List defaultAttValue = new LinkedList();
   List value = new LinkedList();
   defaultAtts.add(Attribute.COLORS);
   defaultAttValue.add(value);
   value.add(new double[] {0, 1, 0, 1}); // remember: only 4d colors
   mergeFact.setDefaultFaceAttributes(defaultAtts, defaultAttValue);
   // merge a list of geometrys:
   // IndexedFaceSet[] list= new IndexedFaceSet[]{box2,zyl};
   // IndexedFaceSet result=mergeFact.mergeIndexedFaceSets(list);
   // or  a complete tree:
   IndexedFaceSet result = mergeFact.mergeGeometrySets(original);
   SceneGraphComponent world = SceneGraphUtility.createFullSceneGraphComponent("world");
   SceneGraphComponent merged = SceneGraphUtility.createFullSceneGraphComponent("merged");
   merged.setGeometry(result);
   MatrixBuilder.euclidean().translate(3, 0, 0).assignTo(merged);
   world.addChildren(merged, original);
   JRViewer.display(world);
 }
Esempio n. 3
0
 private void updateTexture() {
   image.setRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
   ImageData img = new ImageData(image);
   Texture2D tex =
       (Texture2D)
           AttributeEntityUtility.createAttributeEntity(Texture2D.class, "lightMap", app, false);
   tex.setApplyMode(Texture2D.GL_MODULATE);
   tex.setImage(img);
   Matrix texMat = MatrixBuilder.euclidean().scale(1, -1, 1).getMatrix();
   tex.setTextureMatrix(texMat);
 }
Esempio n. 4
0
  /**
   * *******************************************************************************
   * initializeAppearance
   *
   * <p>This method is used to load each texture from disk the first time it is needed. It should
   * only be called once (and the results stored) which is why this method is private.
   * *******************************************************************************
   */
  private static Appearance initializeAppearance(TextureDescriptor td) {
    Appearance app = new Appearance();
    initializeShaders(app, Color.white);

    ImageData id = null;

    try {
      File ff = null;
      switch (td) {
        case WATER:
          ff = AssetManager.getAssetFile("textures/water.jpg");
          break;
        case GRASS:
          ff = AssetManager.getAssetFile("textures/grass.jpg");
          break;
        case BATHROOMTILE:
          ff = AssetManager.getAssetFile("textures/bathroomtile.jpg");
          break;
        case CHECKER:
          ff = AssetManager.getAssetFile("textures/checker.jpg");
          break;
        case CLAY:
          ff = AssetManager.getAssetFile("textures/clay.jpg");
          break;
        case COBBLESTONE:
          ff = AssetManager.getAssetFile("textures/cobblestone.jpg");
          break;
        case DOTS:
          ff = AssetManager.getAssetFile("textures/dots.jpg");
          break;
        case GRID:
          ff = AssetManager.getAssetFile("textures/grid.jpg");
          break;
        case LIGHTHOUSE:
          ff = AssetManager.getAssetFile("textures/lighthouse.jpg");
          break;
        case MARBLE:
          ff = AssetManager.getAssetFile("textures/marble.jpg");
          break;
        case PLAID:
          ff = AssetManager.getAssetFile("textures/plaid.jpg");
          break;
        case STUCCO:
          ff = AssetManager.getAssetFile("textures/stucco.jpg");
          break;
        case SWIRLS:
          ff = AssetManager.getAssetFile("textures/swirls.jpg");
          break;
        case ZIGZAG:
          ff = AssetManager.getAssetFile("textures/zigzag.jpg");
          break;
        case WORLDCUBE0:
          ff = AssetManager.getAssetFile("textures/worldcube0.png");
          break;
        case WORLDCUBE1:
          ff = AssetManager.getAssetFile("textures/worldcube1.png");
          break;
        case WORLDCUBE2:
          ff = AssetManager.getAssetFile("textures/worldcube2.png");
          break;
        case WORLDCUBE3:
          ff = AssetManager.getAssetFile("textures/worldcube3.png");
          break;
        case WORLDCUBE4:
          ff = AssetManager.getAssetFile("textures/worldcube4.png");
          break;
        case WORLDCUBE5:
          ff = AssetManager.getAssetFile("textures/worldcube5.png");
          break;
        case WORLDCUBE6:
          ff = AssetManager.getAssetFile("textures/worldcube6.png");
          break;
        case WORLDCUBE7:
          ff = AssetManager.getAssetFile("textures/worldcube7.png");
          break;
        case WORLDCUBE8:
          ff = AssetManager.getAssetFile("textures/worldcube8.png");
          break;
        case WORLDCUBE9:
          ff = AssetManager.getAssetFile("textures/worldcube9.png");
          break;
        case WORLDCUBE10:
          ff = AssetManager.getAssetFile("textures/worldcube10.png");
          break;
        case WORLDCUBE11:
          ff = AssetManager.getAssetFile("textures/worldcube11.png");
          break;
      }
      id = ImageData.load(Input.getInput(ff));
    } catch (Exception ee) {
      System.err.println("Error: Unable to load texture " + td);
      ee.printStackTrace();
    }

    Texture2D tex = TextureUtility.createTexture(app, POLYGON_SHADER, id);
    tex.setTextureMatrix(MatrixBuilder.euclidean().scale(0.5).getMatrix());

    return app;
  }
Esempio n. 5
0
 protected Transformation getScalingTransformation() {
   double[] mat = MatrixBuilder.euclidean().scale(this.scale).getMatrix().getArray();
   return new Transformation(mat);
 }
Esempio n. 6
0
  public Terrain() {
    appearance.setAttribute(CommonAttributes.EDGE_DRAW, false);
    appearance.setAttribute(CommonAttributes.VERTEX_DRAW, false);
    terrain.setAppearance(appearance);

    MatrixBuilder.euclidean().rotateX(Math.PI / 2).assignTo(plane);
    plane.setGeometry(bigMesh(50, 50, 2000));
    plane.getGeometry().setGeometryAttributes("infinite plane", Boolean.TRUE);
    PickUtility.assignFaceAABBTrees(plane);
    terrain.addChild(plane);

    textures.put("2 Grid", "textures/grid.jpeg");
    textures.put("3 Black Grid", "textures/gridBlack.jpg");
    textures.put("4 Tiles", "textures/recycfloor1_clean2.png");
    textures.put("5 Rust", "textures/outfactory3.png");
    textures.put("1 None", null);

    shrinkPanel.setLayout(new GridLayout());
    shrinkPanel.setIcon(getPluginInfo().icon);
    shrinkPanel.setShrinked(true);
    shrinkPanel.add(panel);
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(1, 0, 1, 0);
    c.fill = BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.gridwidth = REMAINDER;
    c.anchor = WEST;
    colorPanel.setLayout(new GridBagLayout());
    faceColorChooser.setMinimumSize(new Dimension(10, 230));
    faceColorChooser.setPreferredSize(new Dimension(10, 230));
    colorPanel.add(faceColorChooser, c);
    c.fill = VERTICAL;
    c.weighty = 0.0;
    colorPanel.add(closeButton, c);

    // panel
    panel.setLayout(new MinSizeGridBagLayout());
    c.fill = BOTH;

    visibleCheckBox.setSelected(DEFAULT_TERRAIN_VISIBLE);
    c.weightx = 0.0;
    c.gridwidth = RELATIVE;
    panel.add(visibleCheckBox, c);
    c.weightx = 1.0;
    c.gridwidth = REMAINDER;
    panel.add(faceColorButton, c);

    c.weightx = 0.0;
    c.weighty = 0.0;
    c.gridwidth = RELATIVE;
    panel.add(facesReflecting, c);
    c.weightx = 1.0;
    c.gridwidth = REMAINDER;
    panel.add(faceReflectionSlider, c);

    c.weightx = 0.0;
    c.gridwidth = RELATIVE;
    panel.add(transparency, c);
    c.weightx = 1.0;
    c.gridwidth = REMAINDER;
    panel.add(transparencySlider, c);

    c.weightx = 1.0;
    c.gridwidth = REMAINDER;
    panel.add(reflectScene, c);
    reflectScene.setEnabled(false);
    reflectScene.setToolTipText("Coming soon...");

    textureInspector.setMaximalTextureScale(MAXIMAL_TEXTURE_SCALE);
    textureInspector.setLogarithmicRange(LOGARITHMIC_RANGE);
    textureInspector.setTextureUScale(DEFAULT_TEXTURE_SCALE);
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.gridwidth = REMAINDER;
    textureShrinker.setIcon(ImageHook.getIcon("photo.png"));
    textureShrinker.setShrinked(true);
    textureShrinker.setLayout(new GridLayout());
    textureShrinker.add(textureInspector);
    panel.add(textureShrinker, c);

    closeButton.addActionListener(this);
    visibleCheckBox.addActionListener(this);
    facesReflecting.addActionListener(this);
    transparency.addActionListener(this);
    faceColorButton.addActionListener(this);
    reflectScene.addActionListener(this);
    faceReflectionSlider.addChangeListener(this);
    transparencySlider.addChangeListener(this);
    faceColorChooser.getColorPanel().addChangeListener(this);
  }
  public static void main(String[] args) throws Exception {

    SceneGraphComponent cmp = new SceneGraphComponent();
    DraggingTool dragtool = new DraggingTool();

    //		Input wavWaterdrops =
    // Input.getInput("/Users/tim/Documents/workspace/jreality/data/Gun1.wav");
    //		final AudioSource s1 = new CachedAudioInputStreamSource("wavnode", wavWaterdrops, true);
    final ElectricBass s1 = new ElectricBass("Bass");
    final SceneGraphComponent cmp1 = new SceneGraphComponent();
    cmp1.setGeometry(new Sphere());
    MatrixBuilder.euclidean().translate(-4, 0, 0).assignTo(cmp1);
    cmp.addChild(cmp1);
    cmp1.setAudioSource(s1);
    ActionTool at1 = new ActionTool("PanelActivation");
    at1.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (s1.getState() == AudioSource.State.RUNNING) {
              s1.noteOff();
              s1.pause();
            } else {
              int pitch = (int) (cmp1.getTransformation().getMatrix()[11] * 4) + 20;
              System.out.println("pitch raw " + pitch);
              pitch = Math.min(100, pitch);
              pitch = Math.max(pitch, 10);
              System.out.println("pitch map " + pitch);
              s1.noteOn(pitch, 90);
              s1.start();
            }
          }
        });
    OnOffTool ata =
        new OnOffTool() {
          @Override
          public void activate(ToolContext tc) {
            super.activate(tc);
            // System.out.println("active");
            int pitch = (int) (cmp1.getTransformation().getMatrix()[11] * 3) + 20;

            pitch = Math.min(100, pitch);
            pitch = Math.max(pitch, 10);

            s1.noteOn(pitch, 90);
            // s1.start();
          }

          @Override
          public void deactivate(ToolContext tc) {
            super.deactivate(tc);
            // System.out.println("inactive");
            if (s1.getState() == AudioSource.State.RUNNING) {
              s1.noteOff();
              // s1.pause();
            }
          }
        };
    // cmp1.addTool(at1);
    cmp1.addTool(ata);
    cmp1.addTool(dragtool);
    s1.start();

    SceneGraphComponent cmp2 = new SceneGraphComponent();
    cmp.addChild(cmp2);

    final SynthSource sin =
        new SynthSource("wave", 44100) {
          float amplitude = 0.03f;
          double frequency = 440;

          @Override
          public float nextSample() {
            return amplitude * (float) Math.sin(2 * Math.PI * index * frequency / sampleRate);
          }
        };

    cmp2.setGeometry(Primitives.icosahedron());
    MatrixBuilder.euclidean().translate(0, 0, 0).assignTo(cmp2);
    cmp2.setAudioSource(sin);
    ActionTool at2 = new ActionTool("PanelActivation");
    at2.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (sin.getState() == AudioSource.State.RUNNING) sin.pause();
            else sin.start();
          }
        });
    cmp2.addTool(at2);
    cmp2.addTool(dragtool);
    sin.start();

    JRViewer v = new JRViewer();
    v.addBasicUI();
    v.addAudioSupport();
    v.addVRSupport();
    v.setPropertiesFile("TestVR.jrw");
    v.addContentSupport(ContentType.TerrainAligned);
    v.setContent(cmp);
    v.startup();
  }