[toxiclibs] intersect mesh with ray

It seems to me that intersection ray with mesh does not works.

This is my code:

import javax.print.attribute.standard.PresentationDirection;

import peasy.PeasyCam;
import processing.core.PApplet;
import toxi.geom.AABB;
import toxi.geom.IsectData3D;
import toxi.geom.Ray3D;
import toxi.geom.Vec3D;
import toxi.geom.mesh.STLReader;
import toxi.geom.mesh.TriangleMesh;
import toxi.physics.VerletParticle;
import toxi.physics.VerletPhysics;
import toxi.physics.VerletSpring;
import toxi.physics.behaviors.GravityBehavior;
import toxi.physics.constraints.BoxConstraint;
import toxi.processing.ToxiclibsSupport;


public class RayMeshIntersectionTest extends PApplet {

    public static void main(String[] args) {
        PApplet.main(new String[] { "RayMeshIntersectionTest" });
    }

    PeasyCam cam;
    ToxiclibsSupport gfx;

    TriangleMesh meshSTL;
    Ray3D ray;

    final int MOVE_RAY = 0;
    final int MOVE_CAM = 1;
    private int mode = MOVE_CAM;


    public void setup()
    {
        size( 960, 720, P3D);
        gfx = new ToxiclibsSupport(this);
        cam = new PeasyCam(this, 400);
        cam.setMinimumDistance(50);
        cam.setMaximumDistance(1500);

        meshSTL = (TriangleMesh) new STLReader().loadBinary(
                sketchPath("bumps_deformed.stl"), STLReader.TRIANGLEMESH);
        meshSTL.scale(100);

        ray = new Ray3D(new Vec3D(0,0,-200), new Vec3D(0,0,1));

    }

    public void draw()
    {
        update();

        background(151);
        lights();
        directionalLight(255,255,255,-500,200,300);
        specular(255);
        shininess(32);

        gfx.origin(new Vec3D(), 100);

        stroke(255,255,0);
        noFill();
        gfx.mesh(meshSTL, true, 0);

        fill(255,0,0);
        stroke(255,0,0);
        gfx.ray(ray, 400);
    }

    private void update() {
        if(mode == MOVE_RAY) {
            ray.setX(mouseX - width/2);
            ray.setY(mouseY - height/2);
        }

        System.out.println( meshSTL.intersectsRay(ray) );
        IsectData3D isec = meshSTL.getIntersectionData();
        System.out.println(isec.pos);
    }

    public void keyPressed() {
        if(key==' '){
            mode = MOVE_RAY;
        } 
    }

    public void keyReleased() {
        mode = MOVE_CAM;
    }
}

isec.pos is always null

Answers

Sign In or Register to comment.