Ray Intersection Question

Hi.



I just started experimenting with jme and I am particularly interested in the ray picking routines.



I have managed to use TrianglePickResults to pick a specific triangle from a mesh.  My question is, is it possible to get the color and normal at the point of intersection?  I can compute the normal using the triangle vertices but I cannot seem to figure out how to get the color.  If this is not possible, is there a way to get the colors at the vertices of the traingle instead?



Thanks.

the PickResults will return the objects that were picked as well as their triangle indices. You can use these indices to access the buffers of the object picked. I.e. getNormalBuffer(), getColorBuffer() etc.

Thanks for the reply.  :slight_smile:



I am doing something along these lines:



In the below code I am using a bounding volume picker to determine if there is any chance the ray will hit my mesh.  If so, I use a Triangle picker to pick the specific triangle hit by the ray, if any.

     



                        this.rootNode.findPick(mouseRay, pr);

                        TrianglePickResults pr2 = new TrianglePickResults();
         pr2.clear();
         if (pr.getNumber() > 0) {
            pr.getPickData(0).getTargetMesh().findPick(mouseRay, pr2);

            System.out.println(pr.getNumber() + " volumes picked.");
            System.out.println("Closest pick is: " + pr.getPickData(0).getTargetMesh().getParentGeom().getName());

            System.out.println(pr2.getNumber() + " triangles picked.");            

            if (pr2.getNumber() > 0) {

               PickData pData = pr2.getPickData(0);
               List<Integer> tris = pData.getTargetTris();
               TriangleBatch mesh = (TriangleBatch) pData.getTargetMesh();
               
               System.out.println(tris.size() + " tris found.");
               System.out.println("Selected mesh contains " + mesh.getTriangleCount() + " triangles.");

               int triIndex = tris.get(0);
               Vector3f[] vec = new Vector3f[3];
               mesh.getTriangle(triIndex, vec);
               
               System.out.println("Vector " + 0 + " point 0:" + vec[0]);
               System.out.println("Vector " + 0 + " point 1:" + vec[1]);
               System.out.println("Vector " + 0 + " point 2:" + vec[2]);
               
               Triangle tri = new Triangle(vec[0], vec[1], vec[2]);
               tri.calculateNormal();
               System.out.println("Normal: " + tri.getNormal());
                              }
                      }
               



How would I go about retrieving the normal and colors from their respective buffers?
Should I use mesh.getTriangleIndices or can I use the 'tris' returned by the picker directly?

Thanks.

One getTriangle method allows you to retrieve the indexes:


int[] indexes = new int[3];
mesh.getTriangle(triIndex, indexes);

ColorRGBA color = new ColorRGBA();

// get color in vertex 0
BufferUtils.populateFromBuffer(color, mesh.getColorBuffer(0), indexes[0]);