コード例 #1
0
ファイル: smallaabb.cpp プロジェクト: papaboo/smalldacrt
/**
 * Iteratively intersect rays with the given nodes and returns the distance to
 * the closest intersection and stores the id of the intersected sphere in
 * sphereId.
 */
inline float ItrIntersect(const Ray charles, const vector<BVHNode>& nodes, 
                          const vector<Sphere> spheres, unsigned int &sphereId) {
    
    // cout << "=== ItrIntersect:" << charles.ToString() << " ===" << endl;
    
    sphereId = -1;
    float t = 1e30f;
    vector<std::pair<int, float> > stack(60);
    int i = 0;
    stack[i] = std::pair<int, float>(0, 0.0f);

    do {
        // cout << "\n----" << endl;
        // for (int j = i; j >= 0; --j)
        //     cout << "| " << j << ": " << stack[j].second << " - " << nodes[stack[j].first].ToString() << endl;
        // cout << "----" << endl;

        std::pair<int, float> next = stack[i]; --i;
        if (t < next.second) {
            // cout << i << "discard: " << nodes[next.first].ToString() << endl;
            continue;
        }
        
        BVHNode currentNode = nodes[next.first];

        if (currentNode.GetType() == BVHNode::LEAF) // Intersect leaf
            t = Exhaustive(charles, t, currentNode, spheres, sphereId);
        else {
            rayAABBCheck += 2;
        
            const BVHNode& left = nodes[currentNode.GetLeftChild()];
            float tLeft;
            if (!left.aabb.ClosestIntersection(charles, tLeft)) tLeft = 1e32f;
            
            const BVHNode& right = nodes[currentNode.GetRightChild()];
            float tRight;
            if (!right.aabb.ClosestIntersection(charles, tRight)) tRight = 1e32f;

            if (tLeft < tRight) { // Intersect left first
                if (tRight < t) stack[++i] = std::pair<int, float>(currentNode.GetRightChild(), tRight);
                if (tLeft < t) stack[++i] = std::pair<int, float>(currentNode.GetLeftChild(), tLeft);
            } else { // Intersect right first
                if (tLeft < t) stack[++i] = std::pair<int, float>(currentNode.GetLeftChild(), tLeft);
                if (tRight < t) stack[++i] = std::pair<int, float>(currentNode.GetRightChild(), tRight);
            }
        }
    } while (i >= 0);

    return t;
}
コード例 #2
0
ファイル: smallaabb.cpp プロジェクト: papaboo/smalldacrt
/**
 * Recursively intersects Ray Charles with his node and returns the distance to
 * the closest intersection and a stores the id of the sphere in sphereId.
 */
inline float Intersect(const Ray charles, float t, 
                       const BVHNode& node, const vector<BVHNode>& nodes, 
                       const vector<Sphere> spheres, unsigned int &sphereId) {

    if (node.GetType() == BVHNode::LEAF) {
        // Intersect leaf
        return Exhaustive(charles, t, node, spheres, sphereId);
    } else {
        // Traverse further
        const BVHNode left = nodes[node.GetLeftChild()];
        float tLeft;
        if (!left.aabb.ClosestIntersection(charles, tLeft)) tLeft = 1e32f;
        
        const BVHNode right = nodes[node.GetRightChild()];
        float tRight;
        if (!right.aabb.ClosestIntersection(charles, tRight)) tRight = 1e32f;
    
        if (tLeft < tRight) { // Intersect left first
            if (tLeft < t) t = Intersect(charles, t, left, nodes, spheres, sphereId);
            if (tRight < t) t = Intersect(charles, t, right, nodes, spheres, sphereId);
        } else { // Intersect right first
            if (tRight < t) t = Intersect(charles, t, right, nodes, spheres, sphereId);
            if (tLeft < t) t = Intersect(charles, t, left, nodes, spheres, sphereId);
        }

        return t;
    }
}