Monday, September 8, 2014

List of hitable in Swift ray tracer

I added a list of hitables.  This uses the "array" in swift which happily has an append function.  I decided to eliminate the "normal" member function and add the normal to the tuple returned by the hit() function (as predicted by a sharp commenter on the last post).  Other than the tuple, it doesn't look very different than the analogous function in C++.  I am still a rookie on declaration and initialization so this may be verbose.

class hitable_list : hitable  {
    var members : [hitable] = []
    func add(h : hitable) {
    func hit(r : ray, tmin : Double) -> (Bool, Double, vec3) {
        var t_clostest_so_far = 1.0e6 // not defined: Double.max
        var hit_anything : Bool = false
        var new_t : Double
        var hit_it : Bool
        var normal : vec3 = vec3(x:1.0, y:0.0, z:0.0)
        var new_normal : vec3

        for item in members {
            (hit_it , new_t, new_normal) = item.hit(r, tmin: tmin)
            if (hit_it && new_t < t_clostest_so_far) {
                hit_anything = true
                t_clostest_so_far = new_t
                normal = new_normal
        return (hit_anything, t_clostest_so_far, normal)

No comments: