gpuray_glsl
view src/scene.cc @ 3:297dbc5080c4
cone intersection
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 09 Nov 2014 20:13:33 +0200 |
parents | |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <float.h>
4 #include <algorithm>
5 #include "scene.h"
6 #include "texture.h"
8 bool load_scene_file(Scene *scn, const char *fname);
9 bool save_scene_file(const Scene *scn, const char *fname);
12 // default camera
13 TargetCamera Scene::def_cam(Vector3(0, 0, -10), Vector3(0, 0, 0));
15 Scene::Scene()
16 {
17 camera = &def_cam;
18 bgcolor = Color(0, 0, 0);
19 envmap = envmap_conv = 0;
20 fog_start = fog_end = -1;
21 }
23 Scene::~Scene()
24 {
25 for(auto obj: objects) {
26 delete obj;
27 }
28 for(auto lt: lights) {
29 delete lt;
30 }
31 if(camera != &def_cam) {
32 delete camera;
33 }
35 delete envmap;
36 if(envmap_conv != envmap) {
37 delete envmap_conv;
38 }
39 }
41 bool Scene::load(const char *fname)
42 {
43 return load_scene_file(this, fname);
44 }
46 bool Scene::save(const char *fname) const
47 {
48 return save_scene_file(this, fname);
49 }
51 void Scene::set_background_color(const Color &color)
52 {
53 bgcolor = color;
54 }
56 void Scene::set_fog(float fog_start, float fog_end)
57 {
58 this->fog_start = fog_start;
59 this->fog_end = fog_end;
60 }
62 void Scene::get_fog(float *fog_start, float *fog_end) const
63 {
64 *fog_start = this->fog_start;
65 *fog_end = this->fog_end;
66 }
68 void Scene::set_environment_map(TextureCube *map, TextureCube *map_conv)
69 {
70 envmap = map;
71 envmap_conv = map_conv;//map_conv ? map_conv : map;
72 }
74 void Scene::add_object(Object *obj)
75 {
76 objects.push_back(obj);
77 }
79 Object *Scene::get_object(int i) const
80 {
81 if(i < 0 || i >= (int)objects.size()) {
82 return 0;
83 }
84 return objects[i];
85 }
87 int Scene::get_object_count() const
88 {
89 return (int)objects.size();
90 }
92 void Scene::add_light(Light *lt)
93 {
94 lights.push_back(lt);
95 }
97 Light *Scene::get_light(int i) const
98 {
99 if(i < 0 || i >= (int)lights.size()) {
100 return 0;
101 }
102 return lights[i];
103 }
105 int Scene::get_light_count() const
106 {
107 return (int)lights.size();
108 }
110 void Scene::set_camera(Camera *cam)
111 {
112 if(camera != &def_cam) {
113 delete camera;
114 }
115 camera = cam;
116 }
118 Camera *Scene::get_camera() const
119 {
120 return camera;
121 }
123 bool Scene::intersect(const Ray &ray, HitPoint *nearest_hit) const
124 {
125 nearest_hit->obj = 0;
126 nearest_hit->dist = FLT_MAX;
128 // find the nearest hit (if any)
129 for(Object *obj: objects) {
130 HitPoint hit;
131 if(obj->intersect(ray, &hit) && hit.dist < nearest_hit->dist) {
132 *nearest_hit = hit;
133 }
134 }
135 return nearest_hit->obj != 0;
136 }
138 Color Scene::env_color(const Ray &ray) const
139 {
140 if(envmap) {
141 Vector3 dir = ray.dir.normalized();
142 return envmap->sample(dir.x, dir.y, dir.z);
143 }
144 return bgcolor;
145 }
147 void Scene::prepare_xform(long msec)
148 {
149 int nobj = get_object_count();
150 for(int i=0; i<nobj; i++) {
151 objects[i]->prepare_xform(msec);
152 }
153 }