Cycles: fix crash deleting emitting objects.
[blender.git] / intern / cycles / render / object.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "device.h"
20 #include "light.h"
21 #include "mesh.h"
22 #include "object.h"
23 #include "scene.h"
24
25 #include "util_foreach.h"
26 #include "util_map.h"
27 #include "util_progress.h"
28
29 CCL_NAMESPACE_BEGIN
30
31 /* Object */
32
33 Object::Object()
34 {
35         name = "";
36         mesh = NULL;
37         tfm = transform_identity();
38         visibility = ~0;
39 }
40
41 Object::~Object()
42 {
43 }
44
45 void Object::compute_bounds()
46 {
47         bounds = mesh->bounds.transformed(&tfm);
48 }
49
50 void Object::apply_transform()
51 {
52         if(!mesh || tfm == transform_identity())
53                 return;
54         
55         for(size_t i = 0; i < mesh->verts.size(); i++)
56                 mesh->verts[i] = transform(&tfm, mesh->verts[i]);
57
58         Attribute *attr_fN = mesh->attributes.find(Attribute::STD_FACE_NORMAL);
59         Attribute *attr_vN = mesh->attributes.find(Attribute::STD_VERTEX_NORMAL);
60
61         Transform ntfm = transform_transpose(transform_inverse(tfm));
62
63         if(attr_fN) {
64                 float3 *fN = attr_fN->data_float3();
65
66                 for(size_t i = 0; i < mesh->triangles.size(); i++)
67                         fN[i] = transform_direction(&ntfm, fN[i]);
68         }
69
70         if(attr_vN) {
71                 float3 *vN = attr_vN->data_float3();
72
73                 for(size_t i = 0; i < mesh->verts.size(); i++)
74                         vN[i] = transform_direction(&ntfm, vN[i]);
75         }
76
77         if(bounds.valid()) {
78                 mesh->compute_bounds();
79                 compute_bounds();
80         }
81         
82         tfm = transform_identity();
83 }
84
85 void Object::tag_update(Scene *scene)
86 {
87         if(mesh) {
88                 if(mesh->transform_applied)
89                         mesh->need_update = true;
90
91                 foreach(uint sindex, mesh->used_shaders)
92                         if(scene->shaders[sindex]->has_surface_emission)
93                                 scene->light_manager->need_update = true;
94         }
95
96         scene->mesh_manager->need_update = true;
97         scene->object_manager->need_update = true;
98 }
99
100 /* Object Manager */
101
102 ObjectManager::ObjectManager()
103 {
104         need_update = true;
105 }
106
107 ObjectManager::~ObjectManager()
108 {
109 }
110
111 void ObjectManager::device_update_transforms(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
112 {
113         float4 *objects = dscene->objects.resize(OBJECT_SIZE*scene->objects.size());
114         int i = 0;
115         map<Mesh*, float> surface_area_map;
116
117         foreach(Object *ob, scene->objects) {
118                 Mesh *mesh = ob->mesh;
119
120                 /* compute transformations */
121                 Transform tfm = ob->tfm;
122                 Transform itfm = transform_inverse(tfm);
123                 Transform ntfm = transform_transpose(itfm);
124
125                 /* compute surface area. for uniform scale we can do avoid the many
126                    transform calls and share computation for instances */
127                 /* todo: correct for displacement, and move to a better place */
128                 float uniform_scale;
129                 float surface_area = 0.0f;
130                 
131                 if(transform_uniform_scale(tfm, uniform_scale)) {
132                         map<Mesh*, float>::iterator it = surface_area_map.find(mesh);
133
134                         if(it == surface_area_map.end()) {
135                                 foreach(Mesh::Triangle& t, mesh->triangles) {
136                                         float3 p1 = mesh->verts[t.v[0]];
137                                         float3 p2 = mesh->verts[t.v[1]];
138                                         float3 p3 = mesh->verts[t.v[2]];
139
140                                         surface_area += triangle_area(p1, p2, p3);
141                                 }
142
143                                 surface_area_map[mesh] = surface_area;
144                         }
145                         else
146                                 surface_area = it->second;
147
148                         surface_area *= uniform_scale;
149                 }
150                 else {
151                         foreach(Mesh::Triangle& t, mesh->triangles) {
152                                 float3 p1 = transform(&tfm, mesh->verts[t.v[0]]);
153                                 float3 p2 = transform(&tfm, mesh->verts[t.v[1]]);
154                                 float3 p3 = transform(&tfm, mesh->verts[t.v[2]]);
155
156                                 surface_area += triangle_area(p1, p2, p3);
157                         }
158                 }
159
160                 /* pack in texture */
161                 int offset = i*OBJECT_SIZE;
162
163                 memcpy(&objects[offset], &tfm, sizeof(float4)*4);
164                 memcpy(&objects[offset+4], &itfm, sizeof(float4)*4);
165                 memcpy(&objects[offset+8], &ntfm, sizeof(float4)*4);
166                 objects[offset+12] = make_float4(surface_area, 0.0f, 0.0f, 0.0f);
167
168                 i++;
169
170                 if(progress.get_cancel()) return;
171         }
172
173         device->tex_alloc("__objects", dscene->objects);
174 }
175
176 void ObjectManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
177 {
178         if(!need_update)
179                 return;
180         
181         device_free(device, dscene);
182
183         if(scene->objects.size() == 0)
184                 return;
185
186         /* set object transform matrices, before applying static transforms */
187         progress.set_status("Updating Objects", "Copying Transformations to device");
188         device_update_transforms(device, dscene, scene, progress);
189
190         if(progress.get_cancel()) return;
191
192         /* prepare for static BVH building */
193         /* todo: do before to support getting object level coords? */
194         if(scene->params.bvh_type == SceneParams::BVH_STATIC) {
195                 progress.set_status("Updating Objects", "Applying Static Transformations");
196                 apply_static_transforms(scene, progress);
197         }
198
199         if(progress.get_cancel()) return;
200
201         need_update = false;
202 }
203
204 void ObjectManager::device_free(Device *device, DeviceScene *dscene)
205 {
206         device->tex_free(dscene->objects);
207         dscene->objects.clear();
208 }
209
210 void ObjectManager::apply_static_transforms(Scene *scene, Progress& progress)
211 {
212         /* todo: normals and displacement should be done before applying transform! */
213         /* todo: create objects/meshes in right order! */
214
215         /* counter mesh users */
216         map<Mesh*, int> mesh_users;
217
218         foreach(Object *object, scene->objects) {
219                 map<Mesh*, int>::iterator it = mesh_users.find(object->mesh);
220
221                 if(it == mesh_users.end())
222                         mesh_users[object->mesh] = 1;
223                 else
224                         it->second++;
225         }
226
227         if(progress.get_cancel()) return;
228
229         /* apply transforms for objects with single user meshes */
230         foreach(Object *object, scene->objects) {
231                 if(mesh_users[object->mesh] == 1) {
232                         object->apply_transform();
233                         object->mesh->transform_applied = true;
234
235                         if(progress.get_cancel()) return;
236                 }
237         }
238 }
239
240 void ObjectManager::tag_update(Scene *scene)
241 {
242         need_update = true;
243         scene->mesh_manager->need_update = true;
244         scene->light_manager->need_update = true;
245 }
246
247 CCL_NAMESPACE_END
248