erebus

diff liberebus/src/rt.cc @ 17:e9da2916bc79

fixed the normal bug
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 26 May 2014 05:41:28 +0300
parents e2d9bf168a41
children 09028848f276
line diff
     1.1 --- a/liberebus/src/rt.cc	Sun May 25 02:23:39 2014 +0300
     1.2 +++ b/liberebus/src/rt.cc	Mon May 26 05:41:28 2014 +0300
     1.3 @@ -4,38 +4,45 @@
     1.4  
     1.5  #define MAX_ITER	8
     1.6  
     1.7 -Color ray_trace(const Ray &ray, const Scene *scn, int iter)
     1.8 +Color ray_trace(struct erebus *ctx, const Ray &ray, int iter)
     1.9  {
    1.10 +	const Scene *scn = ctx->scn;
    1.11 +	if(!scn) {
    1.12 +		return Color(1, 0, 0);
    1.13 +	}
    1.14 +
    1.15  	RayHit hit;
    1.16  	if(!(scn->intersect(ray, &hit))) {
    1.17  		return scn->get_env_color(ray);
    1.18  	}
    1.19  
    1.20 -	return shade(hit, scn, iter);
    1.21 +	return shade(ctx, hit, iter);
    1.22  }
    1.23  
    1.24 -Color shade(const RayHit &hit, const Scene *scn, int iter)
    1.25 +Color shade(struct erebus *ctx, const RayHit &hit, int iter)
    1.26  {
    1.27  	assert(hit.obj->get_type() == ObjType::geom);
    1.28 +	int max_iter = erb_getopti(ctx, ERB_OPT_MAX_ITER);
    1.29 +	const Scene *scn = ctx->scn;
    1.30  	const GeomObject *obj = (const GeomObject*)hit.obj;
    1.31  	const Material *mtl = &obj->mtl;
    1.32  	const Reflectance *brdf = obj->brdf;
    1.33  	const Ray &ray = hit.world_ray;
    1.34  
    1.35 -	Vector3 norm = obj->calc_normal(hit);
    1.36 -	Vector2 texcoords = obj->calc_texcoords(hit);
    1.37 +	Vector3 norm = hit.calc_normal();
    1.38 +	Vector2 texcoords = hit.calc_texcoords();
    1.39  
    1.40 -	Color color = mtl->get_attrib_color("albedo", texcoords.x, texcoords.y);
    1.41 +	Color color = mtl->get_attrib_color("diffuse", texcoords.x, texcoords.y);
    1.42  	Color res = mtl->get_attrib_color("emissive") + color * scn->get_env().ambient;
    1.43  
    1.44  	Vector3 sample_dir;
    1.45  	float prob = brdf->sample(norm, -hit.world_ray.dir, &sample_dir);
    1.46 -	if(iter < MAX_ITER && randf() <= prob) {
    1.47 +	if(iter < max_iter && randf() <= prob) {
    1.48  		Ray sample_ray;
    1.49  		sample_ray.origin = ray.origin + ray.dir * hit.dist;
    1.50  		sample_ray.dir = sample_dir;
    1.51  
    1.52 -		res += ray_trace(sample_ray, scn, iter + 1) * color;
    1.53 +		res += ray_trace(ctx, sample_ray, iter + 1) * color;
    1.54  	}
    1.55  	return res;
    1.56  }