Home | History | Annotate | Download | only in collision
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package jme3test.collision;
     34 
     35 import com.jme3.collision.CollisionResults;
     36 import com.jme3.math.Ray;
     37 import com.jme3.math.Vector2f;
     38 import com.jme3.math.Vector3f;
     39 import com.jme3.renderer.Camera;
     40 import com.jme3.scene.Spatial;
     41 import java.awt.FlowLayout;
     42 import java.awt.image.BufferedImage;
     43 import javax.swing.ImageIcon;
     44 import javax.swing.JFrame;
     45 import javax.swing.JLabel;
     46 
     47 public class RayTrace {
     48 
     49     private BufferedImage image;
     50     private Camera cam;
     51     private Spatial scene;
     52     private CollisionResults results = new CollisionResults();
     53     private JFrame frame;
     54     private JLabel label;
     55 
     56     public RayTrace(Spatial scene, Camera cam, int width, int height){
     57         image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
     58         this.scene = scene;
     59         this.cam = cam;
     60     }
     61 
     62     public void show(){
     63         frame = new JFrame("HDR View");
     64         label = new JLabel(new ImageIcon(image));
     65         frame.getContentPane().add(label);
     66         frame.setLayout(new FlowLayout());
     67         frame.pack();
     68         frame.setVisible(true);
     69     }
     70 
     71     public void update(){
     72         int w = image.getWidth();
     73         int h = image.getHeight();
     74 
     75         float wr = (float) cam.getWidth()  / image.getWidth();
     76         float hr = (float) cam.getHeight() / image.getHeight();
     77 
     78         scene.updateGeometricState();
     79 
     80         for (int y = 0; y < h; y++){
     81             for (int x = 0; x < w; x++){
     82                 Vector2f v = new Vector2f(x * wr,y * hr);
     83                 Vector3f pos = cam.getWorldCoordinates(v, 0.0f);
     84                 Vector3f dir = cam.getWorldCoordinates(v, 0.3f);
     85                 dir.subtractLocal(pos).normalizeLocal();
     86 
     87                 Ray r = new Ray(pos, dir);
     88 
     89                 results.clear();
     90                 scene.collideWith(r, results);
     91                 if (results.size() > 0){
     92                     image.setRGB(x, h - y - 1, 0xFFFFFFFF);
     93                 }else{
     94                     image.setRGB(x, h - y - 1, 0xFF000000);
     95                 }
     96             }
     97         }
     98 
     99         label.repaint();
    100     }
    101 
    102 }
    103