clray

changeset 4:3c95d568d3c7

wow a ball
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 15 Jul 2010 07:37:05 +0300
parents 88ac4eb2d18a
children 9f0ddb701882
files rt.cl src/rt.cc
diffstat 2 files changed, 50 insertions(+), 29 deletions(-) [+]
line diff
     1.1 --- a/rt.cl	Tue Jul 13 03:38:29 2010 +0300
     1.2 +++ b/rt.cl	Thu Jul 15 07:37:05 2010 +0300
     1.3 @@ -7,7 +7,8 @@
     1.4  struct Sphere {
     1.5  	float4 pos;
     1.6  	float radius;
     1.7 -	float4 color;
     1.8 +	float4 kd, ks;
     1.9 +	float spow, kr, kt;
    1.10  };
    1.11  
    1.12  struct Light {
    1.13 @@ -21,34 +22,44 @@
    1.14  struct SurfPoint {
    1.15  	float t;
    1.16  	float3 pos, norm;
    1.17 +	global const struct Sphere *obj;
    1.18  };
    1.19  
    1.20  #define EPSILON 1e-6
    1.21  
    1.22 -bool intersect(struct Ray ray, __global const struct Sphere *sph, struct SurfPoint *sp);
    1.23 +float4 shade(struct Ray ray, struct SurfPoint sp);
    1.24 +bool intersect(struct Ray ray, global const struct Sphere *sph, struct SurfPoint *sp);
    1.25  
    1.26 -__kernel void render(__global float4 *fb,
    1.27 -		__global const struct RendInfo *rinf,
    1.28 -		__global const struct Sphere *sphlist,
    1.29 -		__global const struct Light *lights,
    1.30 -		__global const struct Ray *primrays)
    1.31 +
    1.32 +kernel void render(global float4 *fb,
    1.33 +		global const struct RendInfo *rinf,
    1.34 +		global const struct Sphere *sphlist,
    1.35 +		global const struct Light *lights,
    1.36 +		global const struct Ray *primrays)
    1.37  {
    1.38  	int idx = get_global_id(0);
    1.39  
    1.40  	struct Ray ray = primrays[idx];
    1.41 -	struct SurfPoint sp;
    1.42 +	struct SurfPoint sp, sp0;
    1.43  
    1.44 -	if(intersect(ray, sphlist, &sp)) {
    1.45 -		fb[idx] = (float4)(1, 0, 0, 1);
    1.46 -	} else {
    1.47 -		fb[idx] = (float4)(0, 0, 0, 1);
    1.48 +	sp0.t = FLT_MAX;
    1.49 +
    1.50 +	for(int i=0; i<rinf->num_sph; i++) {
    1.51 +		if(intersect(ray, sphlist, &sp) && sp.t < sp0.t) {
    1.52 +			sp0 = sp;
    1.53 +		}
    1.54  	}
    1.55  
    1.56 -	fb[idx] = primrays[idx].dir * 0.5 + 0.5;
    1.57 +	fb[idx] = shade(ray, sp0);
    1.58 +}
    1.59 +
    1.60 +float4 shade(struct Ray ray, struct SurfPoint sp)
    1.61 +{
    1.62 +	return sp.obj->kd;
    1.63  }
    1.64  
    1.65  bool intersect(struct Ray ray,
    1.66 -		__global const struct Sphere *sph,
    1.67 +		global const struct Sphere *sph,
    1.68  		struct SurfPoint *sp)
    1.69  {
    1.70  	float3 dir = ray.dir.xyz;
     2.1 --- a/src/rt.cc	Tue Jul 13 03:38:29 2010 +0300
     2.2 +++ b/src/rt.cc	Thu Jul 15 07:37:05 2010 +0300
     2.3 @@ -13,7 +13,9 @@
     2.4  	cl_float4 pos;
     2.5  	cl_float radius;
     2.6  
     2.7 -	cl_float4 color;
     2.8 +	cl_float4 kd, ks;
     2.9 +	cl_float spow;
    2.10 +	cl_float kr, kt;
    2.11  } __attribute__((packed));
    2.12  
    2.13  struct Ray {
    2.14 @@ -31,21 +33,27 @@
    2.15  static CLProgram *prog;
    2.16  static int global_size;
    2.17  
    2.18 +static Sphere sphlist[] = {
    2.19 +	{{0, 0, 8, 1}, 1.0, {0.7, 0.2, 0.15, 1}, {1, 1, 1, 1}, 60, 0, 0},
    2.20 +	{{-0.2, 0.4, 5, 1}, 0.25, {0.2, 0.9, 0.3, 1}, {1, 1, 1, 1}, 40, 0, 0}
    2.21 +};
    2.22 +
    2.23 +static Light lightlist[] = {
    2.24 +	{{-10, 10, -20, 1}, {1, 1, 1, 1}}
    2.25 +};
    2.26 +
    2.27 +static RendInfo rinf;
    2.28 +
    2.29 +
    2.30  bool init_renderer(int xsz, int ysz, float *fb)
    2.31  {
    2.32 -	Sphere sphlist[] = {
    2.33 -		{{0, 0, 10, 1}, 1.0, {1, 0, 0, 1}}
    2.34 -	};
    2.35 -	Light lightlist[] = {
    2.36 -		{{-10, 10, -20, 1}, {1, 1, 1, 1}}
    2.37 -	};
    2.38 -	RendInfo rinf = {
    2.39 -		xsz, ysz,
    2.40 -		sizeof sphlist / sizeof *sphlist,
    2.41 -		sizeof lightlist / sizeof *lightlist,
    2.42 -		6
    2.43 -	};
    2.44 -	
    2.45 +	// render info
    2.46 +	rinf.xsz = xsz;
    2.47 +	rinf.ysz = ysz;
    2.48 +	rinf.num_sph = sizeof sphlist / sizeof *sphlist;
    2.49 +	rinf.num_lights = sizeof lightlist / sizeof *lightlist;
    2.50 +	rinf.max_iter = 6;
    2.51 +
    2.52  	/* calculate primary rays */
    2.53  	prim_rays = new Ray[xsz * ysz];
    2.54  
    2.55 @@ -105,7 +113,9 @@
    2.56  	float py = 1.0 - ((float)y / (float)h) * ysz;
    2.57  	float pz = 1.0 / tan(0.5 * vfov);
    2.58  
    2.59 -	pz *= 1000.0;
    2.60 +	px *= 100.0;
    2.61 +	py *= 100.0;
    2.62 +	pz *= 100.0;
    2.63  
    2.64  	Ray ray = {{0, 0, 0, 1}, {px, py, pz, 1}};
    2.65  	return ray;