rayzor

diff src/scene.cc @ 17:79609d482762

the renderer renders, also fixed an unnoticed matrix conversion problem between scenegraph and min3d
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 14 Apr 2014 07:34:45 +0300
parents be616b58df99
children 6b11a3f8706e
line diff
     1.1 --- a/src/scene.cc	Sun Apr 13 09:54:51 2014 +0300
     1.2 +++ b/src/scene.cc	Mon Apr 14 07:34:45 2014 +0300
     1.3 @@ -1,4 +1,5 @@
     1.4  #include <string.h>
     1.5 +#include <float.h>
     1.6  #include "scene.h"
     1.7  #include "min3d.h"
     1.8  
     1.9 @@ -6,6 +7,14 @@
    1.10  {
    1.11  	name = 0;
    1.12  	active_cam = 0;
    1.13 +
    1.14 +	fog_exp = 1.0;
    1.15 +
    1.16 +	set_ambient(Vector3(0.05, 0.05, 0.05));
    1.17 +
    1.18 +	set_background(Vector3(0.4, 0.3, 0.05), SCN_BG_LOW);
    1.19 +	set_background(Vector3(0.6, 0.6, 0.6), SCN_BG_MID);
    1.20 +	set_background(Vector3(0.1, 0.5, 1.0), SCN_BG_HIGH);
    1.21  }
    1.22  
    1.23  Scene::~Scene()
    1.24 @@ -41,6 +50,51 @@
    1.25  	return name ? name : "<unknown>";
    1.26  }
    1.27  
    1.28 +void Scene::set_background(const Vector3 &col, int idx)
    1.29 +{
    1.30 +	if(idx == -1) {
    1.31 +		bg[0] = bg[1] = bg[2] = col;
    1.32 +	} else {
    1.33 +		bg[idx] = col;
    1.34 +	}
    1.35 +}
    1.36 +
    1.37 +const Vector3 &Scene::get_background(int idx) const
    1.38 +{
    1.39 +	return bg[idx == -1 ? 0 : idx];
    1.40 +}
    1.41 +
    1.42 +#define LERP(a, b, t)	((a) + ((b) - (a)) * t)
    1.43 +
    1.44 +Vector3 Scene::get_background(const Ray &ray) const
    1.45 +{
    1.46 +	Vector3 dir = normalize(ray.dir);
    1.47 +
    1.48 +	float v;
    1.49 +	if(dir.z == 0.0) {
    1.50 +		v = dir.y > 0.0 ? 1.0 : -1.0;
    1.51 +	} else {
    1.52 +		float angle = atan2(dir.z, dir.y);
    1.53 +		v = angle / M_PI;
    1.54 +	}
    1.55 +
    1.56 +	float r = v >= 0.0 ? LERP(bg[1].x, bg[2].x, v) : LERP(bg[1].x, bg[0].x, -v);
    1.57 +	float g = v >= 0.0 ? LERP(bg[1].y, bg[2].y, v) : LERP(bg[1].y, bg[0].y, -v);
    1.58 +	float b = v >= 0.0 ? LERP(bg[1].z, bg[2].z, v) : LERP(bg[1].z, bg[0].z, -v);
    1.59 +	return Vector3(r, g, b);
    1.60 +}
    1.61 +
    1.62 +void Scene::set_ambient(const Vector3 &col)
    1.63 +{
    1.64 +	ambient = col;
    1.65 +}
    1.66 +
    1.67 +const Vector3 &Scene::get_ambient() const
    1.68 +{
    1.69 +	return ambient;
    1.70 +}
    1.71 +
    1.72 +
    1.73  void Scene::add(SceneNode *node)
    1.74  {
    1.75  	nodes.push_back(node);
    1.76 @@ -180,3 +234,23 @@
    1.77  {
    1.78  	return idx >= 0 && idx < (int)sel.size() ? sel[idx] : -1;
    1.79  }
    1.80 +
    1.81 +bool Scene::intersect(const Ray &ray, RayHit *hit) const
    1.82 +{
    1.83 +	if(hit) {
    1.84 +		hit->dist = FLT_MAX;
    1.85 +		hit->obj = 0;
    1.86 +	}
    1.87 +
    1.88 +	int num_obj = (int)objects.size();
    1.89 +	for(int i=0; i<num_obj; i++) {
    1.90 +		RayHit tmphit;
    1.91 +		if(objects[i]->intersect(ray, hit ? &tmphit : 0)) {
    1.92 +			if(!hit) return true;
    1.93 +			if(tmphit.dist < hit->dist) {
    1.94 +				*hit = tmphit;
    1.95 +			}
    1.96 +		}
    1.97 +	}
    1.98 +	return hit && hit->obj;
    1.99 +}