jeudi 9 mars 2017

Check if ofRay intersects with ofMeshFace

I've tried many examples of code from the openframeworks forum and documentation, but I couldn't get one to do what I want.

I have an ofRay (from ofxRay), and a list of of3dPrimitive. I am trying to figure out if the ray intersects a primitive, and if so, to know which primitive the ray intersected with "first" (as in, which one is the closest to the screen).

ofVec3f screenToWorld = camera.screenToWorld(ofVec3f(x, y, 0.0));

std::list<primitive>::iterator iterator;

ofRay ray(camera.getPosition(), screenToWorld - camera.getPosition(), true);

for (iterator = primitives.begin(); iterator != primitives.end(); ++iterator)
{
    if (!shiftHeld)
    {
       iterator->setSelected(false);
    }

    float* distance = 0;

    bool found = iterator->FindIntersection(ray, distance);
}

And my findIntersection looks like this:

bool primitive::calcTriangleIntersection(ofRay ray, float *result) const {

    ofMesh mesh = prim->getMesh();
    std::vector<ofMeshFace> indices = mesh.getUniqueFaces();

    for (std::vector<ofMeshFace>::iterator i = indices.begin(); i != indices.end(); ++i)
    {
        ofMeshFace face = *i;

        // Detection here

        if (detected intersection)
        {
            *result = distance;
            return true;
        }
    }
    return false;
}

I've tried so many things, but none of them works. I'm also drawing my rays to the screen, so I know for a fact that they are indeed created correctly and go in the right direction for an infinite distance

Just to be clear, I removed a lot of the code to make this easily readable. I'm only missing the // Detection Here part in the second method, because I have no idea how to make it work.

Aucun commentaire:

Enregistrer un commentaire