gpuray_glsl

diff src/scene.cc @ 0:f234630e38ff

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 09 Nov 2014 13:03:36 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/scene.cc	Sun Nov 09 13:03:36 2014 +0200
     1.3 @@ -0,0 +1,153 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <float.h>
     1.7 +#include <algorithm>
     1.8 +#include "scene.h"
     1.9 +#include "texture.h"
    1.10 +
    1.11 +bool load_scene_file(Scene *scn, const char *fname);
    1.12 +bool save_scene_file(const Scene *scn, const char *fname);
    1.13 +
    1.14 +
    1.15 +// default camera
    1.16 +TargetCamera Scene::def_cam(Vector3(0, 0, -10), Vector3(0, 0, 0));
    1.17 +
    1.18 +Scene::Scene()
    1.19 +{
    1.20 +	camera = &def_cam;
    1.21 +	bgcolor = Color(0, 0, 0);
    1.22 +	envmap = envmap_conv = 0;
    1.23 +	fog_start = fog_end = -1;
    1.24 +}
    1.25 +
    1.26 +Scene::~Scene()
    1.27 +{
    1.28 +	for(auto obj: objects) {
    1.29 +		delete obj;
    1.30 +	}
    1.31 +	for(auto lt: lights) {
    1.32 +		delete lt;
    1.33 +	}
    1.34 +	if(camera != &def_cam) {
    1.35 +		delete camera;
    1.36 +	}
    1.37 +
    1.38 +	delete envmap;
    1.39 +	if(envmap_conv != envmap) {
    1.40 +		delete envmap_conv;
    1.41 +	}
    1.42 +}
    1.43 +
    1.44 +bool Scene::load(const char *fname)
    1.45 +{
    1.46 +	return load_scene_file(this, fname);
    1.47 +}
    1.48 +
    1.49 +bool Scene::save(const char *fname) const
    1.50 +{
    1.51 +	return save_scene_file(this, fname);
    1.52 +}
    1.53 +
    1.54 +void Scene::set_background_color(const Color &color)
    1.55 +{
    1.56 +	bgcolor = color;
    1.57 +}
    1.58 +
    1.59 +void Scene::set_fog(float fog_start, float fog_end)
    1.60 +{
    1.61 +	this->fog_start = fog_start;
    1.62 +	this->fog_end = fog_end;
    1.63 +}
    1.64 +
    1.65 +void Scene::get_fog(float *fog_start, float *fog_end) const
    1.66 +{
    1.67 +	*fog_start = this->fog_start;
    1.68 +	*fog_end = this->fog_end;
    1.69 +}
    1.70 +
    1.71 +void Scene::set_environment_map(TextureCube *map, TextureCube *map_conv)
    1.72 +{
    1.73 +	envmap = map;
    1.74 +	envmap_conv = map_conv;//map_conv ? map_conv : map;
    1.75 +}
    1.76 +
    1.77 +void Scene::add_object(Object *obj)
    1.78 +{
    1.79 +	objects.push_back(obj);
    1.80 +}
    1.81 +
    1.82 +Object *Scene::get_object(int i) const
    1.83 +{
    1.84 +	if(i < 0 || i >= (int)objects.size()) {
    1.85 +		return 0;
    1.86 +	}
    1.87 +	return objects[i];
    1.88 +}
    1.89 +
    1.90 +int Scene::get_object_count() const
    1.91 +{
    1.92 +	return (int)objects.size();
    1.93 +}
    1.94 +
    1.95 +void Scene::add_light(Light *lt)
    1.96 +{
    1.97 +	lights.push_back(lt);
    1.98 +}
    1.99 +
   1.100 +Light *Scene::get_light(int i) const
   1.101 +{
   1.102 +	if(i < 0 || i >= (int)lights.size()) {
   1.103 +		return 0;
   1.104 +	}
   1.105 +	return lights[i];
   1.106 +}
   1.107 +
   1.108 +int Scene::get_light_count() const
   1.109 +{
   1.110 +	return (int)lights.size();
   1.111 +}
   1.112 +
   1.113 +void Scene::set_camera(Camera *cam)
   1.114 +{
   1.115 +	if(camera != &def_cam) {
   1.116 +		delete camera;
   1.117 +	}
   1.118 +	camera = cam;
   1.119 +}
   1.120 +
   1.121 +Camera *Scene::get_camera() const
   1.122 +{
   1.123 +	return camera;
   1.124 +}
   1.125 +
   1.126 +bool Scene::intersect(const Ray &ray, HitPoint *nearest_hit) const
   1.127 +{
   1.128 +	nearest_hit->obj = 0;
   1.129 +	nearest_hit->dist = FLT_MAX;
   1.130 +
   1.131 +	// find the nearest hit (if any)
   1.132 +	for(Object *obj: objects) {
   1.133 +		HitPoint hit;
   1.134 +		if(obj->intersect(ray, &hit) && hit.dist < nearest_hit->dist) {
   1.135 +			*nearest_hit = hit;
   1.136 +		}
   1.137 +	}
   1.138 +	return nearest_hit->obj != 0;
   1.139 +}
   1.140 +
   1.141 +Color Scene::env_color(const Ray &ray) const
   1.142 +{
   1.143 +	if(envmap) {
   1.144 +		Vector3 dir = ray.dir.normalized();
   1.145 +		return envmap->sample(dir.x, dir.y, dir.z);
   1.146 +	}
   1.147 +	return bgcolor;
   1.148 +}
   1.149 +
   1.150 +void Scene::prepare_xform(long msec)
   1.151 +{
   1.152 +	int nobj = get_object_count();
   1.153 +	for(int i=0; i<nobj; i++) {
   1.154 +		objects[i]->prepare_xform(msec);
   1.155 +	}
   1.156 +}