clray

diff rt.cl @ 29:353d80127627

doh ... it doesn't work
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Aug 2010 20:51:57 +0100
parents 97cfd9675310
children 04803c702014
line diff
     1.1 --- a/rt.cl	Sat Aug 21 03:42:49 2010 +0100
     1.2 +++ b/rt.cl	Sat Aug 21 20:51:57 2010 +0100
     1.3 @@ -59,7 +59,7 @@
     1.4  };
     1.5  
     1.6  struct KDNode {
     1.7 -	AABBox aabb;
     1.8 +	struct AABBox aabb;
     1.9  	int face_idx[32];
    1.10  	int num_faces;
    1.11  	int padding[3];
    1.12 @@ -161,9 +161,51 @@
    1.13  	return dcol + scol;
    1.14  }
    1.15  
    1.16 +#define STACK_SIZE	128
    1.17  bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *spres)
    1.18  {
    1.19 -	return false;
    1.20 +	struct SurfPoint sp0;
    1.21 +	sp0.t = 1.0;
    1.22 +	sp0.obj = 0;
    1.23 +
    1.24 +	int idxstack[STACK_SIZE];
    1.25 +	int sp = 0;			// points at the topmost element of the stack
    1.26 +	idxstack[sp] = 1;	// root at tree[1] (heap)
    1.27 +
    1.28 +	while(sp >= 0) {
    1.29 +		int idx = idxstack[sp--];	// remove this index from the stack and process it
    1.30 +
    1.31 +		global struct KDNode *node = scn->kdtree + idx;
    1.32 +
    1.33 +		if(intersect_aabb(ray, node->aabb)) {
    1.34 +			// leaf node ...
    1.35 +			if(node->num_faces) {
    1.36 +				// check each face in turn and update the nearest intersection as needed
    1.37 +				for(int i=0; i<node->num_faces; i++) {
    1.38 +					struct SurfPoint sp;
    1.39 +					int fidx = node->face_idx[i];
    1.40 +
    1.41 +					if(intersect(ray, scn->faces + fidx, &sp) && sp.t < sp0.t) {
    1.42 +						sp0 = sp;
    1.43 +					}
    1.44 +				}
    1.45 +			}
    1.46 +		} else {
    1.47 +			// internal node ... recurse to the children
    1.48 +			idxstack[++sp] = idx * 2;
    1.49 +			idxstack[++sp] = idx * 2 + 1;
    1.50 +		}
    1.51 +	}
    1.52 +
    1.53 +	if(!sp0.obj) {
    1.54 +		return false;
    1.55 +	}
    1.56 +
    1.57 +	if(spres) {
    1.58 +		*spres = sp0;
    1.59 +		spres->mat = scn->matlib[sp0.obj->matid];
    1.60 +	}
    1.61 +	return true;
    1.62  }
    1.63  
    1.64  /*bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *spres)
    1.65 @@ -236,7 +278,10 @@
    1.66  		return true;
    1.67  	}
    1.68  
    1.69 -	float4 bbox[2] = {aabb.min, aabb.max};
    1.70 +	float4 bbox[2] = {
    1.71 +		aabb.min.x, aabb.min.y, aabb.min.z, 0,
    1.72 +		aabb.max.x, aabb.max.y, aabb.max.z, 0
    1.73 +	};
    1.74  
    1.75  	int xsign = (int)(ray.dir.x < 0.0);
    1.76  	float invdirx = 1.0 / ray.dir.x;
    1.77 @@ -264,7 +309,7 @@
    1.78  		return false;
    1.79  	}
    1.80  
    1.81 -	return tmin < t1 && tmax > t0;
    1.82 +	return tmin < 1.0 && tmax > 0.0;
    1.83  }
    1.84  
    1.85  float4 reflect(float4 v, float4 n)