Cycles: Code cleanup, spaces around keywords
[blender-staging.git] / intern / cycles / render / object.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "device.h"
18 #include "light.h"
19 #include "mesh.h"
20 #include "curves.h"
21 #include "object.h"
22 #include "particles.h"
23 #include "scene.h"
24
25 #include "util_foreach.h"
26 #include "util_map.h"
27 #include "util_progress.h"
28 #include "util_vector.h"
29
30 CCL_NAMESPACE_BEGIN
31
32 /* Object */
33
34 Object::Object()
35 {
36         name = "";
37         mesh = NULL;
38         tfm = transform_identity();
39         visibility = ~0;
40         random_id = 0;
41         pass_id = 0;
42         particle_system = NULL;
43         particle_index = 0;
44         bounds = BoundBox::empty;
45         motion.pre = transform_identity();
46         motion.mid = transform_identity();
47         motion.post = transform_identity();
48         use_motion = false;
49         use_holdout = false;
50         dupli_generated = make_float3(0.0f, 0.0f, 0.0f);
51         dupli_uv = make_float2(0.0f, 0.0f);
52 }
53
54 Object::~Object()
55 {
56 }
57
58 void Object::compute_bounds(bool motion_blur)
59 {
60         BoundBox mbounds = mesh->bounds;
61
62         if(motion_blur && use_motion) {
63                 DecompMotionTransform decomp;
64                 transform_motion_decompose(&decomp, &motion, &tfm);
65
66                 bounds = BoundBox::empty;
67
68                 /* todo: this is really terrible. according to pbrt there is a better
69                  * way to find this iteratively, but did not find implementation yet
70                  * or try to implement myself */
71                 for(float t = 0.0f; t < 1.0f; t += (1.0f/128.0f)) {
72                         Transform ttfm;
73
74                         transform_motion_interpolate(&ttfm, &decomp, t);
75                         bounds.grow(mbounds.transformed(&ttfm));
76                 }
77         }
78         else {
79                 if(mesh->transform_applied) {
80                         bounds = mbounds;
81                 }
82                 else {
83                         bounds = mbounds.transformed(&tfm);
84                 }
85         }
86 }
87
88 void Object::apply_transform(bool apply_to_motion)
89 {
90         if(!mesh || tfm == transform_identity())
91                 return;
92         
93         /* triangles */
94         if(mesh->verts.size()) {
95                 /* store matrix to transform later. when accessing these as attributes we
96                  * do not want the transform to be applied for consistency between static
97                  * and dynamic BVH, so we do it on packing. */
98                 mesh->transform_normal = transform_transpose(transform_inverse(tfm));
99
100                 /* apply to mesh vertices */
101                 for(size_t i = 0; i < mesh->verts.size(); i++)
102                         mesh->verts[i] = transform_point(&tfm, mesh->verts[i]);
103                 
104                 if(apply_to_motion) {
105                         Attribute *attr = mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
106
107                         if(attr) {
108                                 size_t steps_size = mesh->verts.size() * (mesh->motion_steps - 1);
109                                 float3 *vert_steps = attr->data_float3();
110
111                                 for(size_t i = 0; i < steps_size; i++)
112                                         vert_steps[i] = transform_point(&tfm, vert_steps[i]);
113                         }
114
115                         Attribute *attr_N = mesh->attributes.find(ATTR_STD_MOTION_VERTEX_NORMAL);
116
117                         if(attr_N) {
118                                 Transform ntfm = mesh->transform_normal;
119                                 size_t steps_size = mesh->verts.size() * (mesh->motion_steps - 1);
120                                 float3 *normal_steps = attr_N->data_float3();
121
122                                 for(size_t i = 0; i < steps_size; i++)
123                                         normal_steps[i] = normalize(transform_direction(&ntfm, normal_steps[i]));
124                         }
125                 }
126         }
127
128         /* curves */
129         if(mesh->curve_keys.size()) {
130                 /* compute uniform scale */
131                 float3 c0 = transform_get_column(&tfm, 0);
132                 float3 c1 = transform_get_column(&tfm, 1);
133                 float3 c2 = transform_get_column(&tfm, 2);
134                 float scalar = pow(fabsf(dot(cross(c0, c1), c2)), 1.0f/3.0f);
135
136                 /* apply transform to curve keys */
137                 for(size_t i = 0; i < mesh->curve_keys.size(); i++) {
138                         float3 co = transform_point(&tfm, float4_to_float3(mesh->curve_keys[i]));
139                         float radius = mesh->curve_keys[i].w * scalar;
140
141                         /* scale for curve radius is only correct for uniform scale */
142                         mesh->curve_keys[i] = float3_to_float4(co);
143                         mesh->curve_keys[i].w = radius;
144                 }
145
146                 if(apply_to_motion) {
147                         Attribute *curve_attr = mesh->curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
148
149                         if(curve_attr) {
150                                 /* apply transform to motion curve keys */
151                                 size_t steps_size = mesh->curve_keys.size() * (mesh->motion_steps - 1);
152                                 float4 *key_steps = curve_attr->data_float4();
153
154                                 for(size_t i = 0; i < steps_size; i++) {
155                                         float3 co = transform_point(&tfm, float4_to_float3(key_steps[i]));
156                                         float radius = key_steps[i].w * scalar;
157
158                                         /* scale for curve radius is only correct for uniform scale */
159                                         key_steps[i] = float3_to_float4(co);
160                                         key_steps[i].w = radius;
161                                 }
162                         }
163                 }
164         }
165
166         /* we keep normals pointing in same direction on negative scale, notify
167          * mesh about this in it (re)calculates normals */
168         if(transform_negative_scale(tfm))
169                 mesh->transform_negative_scaled = true;
170
171         if(bounds.valid()) {
172                 mesh->compute_bounds();
173                 compute_bounds(false);
174         }
175
176         /* tfm is not reset to identity, all code that uses it needs to check the
177            transform_applied boolean */
178 }
179
180 void Object::tag_update(Scene *scene)
181 {
182         if(mesh) {
183                 if(mesh->transform_applied)
184                         mesh->need_update = true;
185
186                 foreach(uint sindex, mesh->used_shaders) {
187                         Shader *shader = scene->shaders[sindex];
188
189                         if(shader->use_mis && shader->has_surface_emission)
190                                 scene->light_manager->need_update = true;
191                 }
192         }
193
194         scene->curve_system_manager->need_update = true;
195         scene->mesh_manager->need_update = true;
196         scene->object_manager->need_update = true;
197 }
198
199 vector<float> Object::motion_times()
200 {
201         /* compute times at which we sample motion for this object */
202         vector<float> times;
203
204         if(!mesh || mesh->motion_steps == 1)
205                 return times;
206
207         int motion_steps = mesh->motion_steps;
208
209         for(int step = 0; step < motion_steps; step++) {
210                 if(step != motion_steps / 2) {
211                         float time = 2.0f * step / (motion_steps - 1) - 1.0f;
212                         times.push_back(time);
213                 }
214         }
215
216         return times;
217 }
218
219 /* Object Manager */
220
221 ObjectManager::ObjectManager()
222 {
223         need_update = true;
224         need_flags_update = true;
225 }
226
227 ObjectManager::~ObjectManager()
228 {
229 }
230
231 void ObjectManager::device_update_transforms(Device *device, DeviceScene *dscene, Scene *scene, uint *object_flag, Progress& progress)
232 {
233         float4 *objects;
234         float4 *objects_vector = NULL;
235         int i = 0;
236         map<Mesh*, float> surface_area_map;
237         map<ParticleSystem*, int> particle_offset;
238         Scene::MotionType need_motion = scene->need_motion(device->info.advanced_shading);
239         bool have_motion = false;
240         bool have_curves = false;
241
242         objects = dscene->objects.resize(OBJECT_SIZE*scene->objects.size());
243         if(need_motion == Scene::MOTION_PASS)
244                 objects_vector = dscene->objects_vector.resize(OBJECT_VECTOR_SIZE*scene->objects.size());
245
246         /* particle system device offsets
247          * 0 is dummy particle, index starts at 1
248          */
249         int numparticles = 1;
250         foreach(ParticleSystem *psys, scene->particle_systems) {
251                 particle_offset[psys] = numparticles;
252                 numparticles += psys->particles.size();
253         }
254
255         foreach(Object *ob, scene->objects) {
256                 Mesh *mesh = ob->mesh;
257                 uint flag = 0;
258
259                 /* compute transformations */
260                 Transform tfm = ob->tfm;
261                 Transform itfm = transform_inverse(tfm);
262
263                 /* compute surface area. for uniform scale we can do avoid the many
264                  * transform calls and share computation for instances */
265                 /* todo: correct for displacement, and move to a better place */
266                 float uniform_scale;
267                 float surface_area = 0.0f;
268                 float pass_id = ob->pass_id;
269                 float random_number = (float)ob->random_id * (1.0f/(float)0xFFFFFFFF);
270                 int particle_index = (ob->particle_system)? ob->particle_index + particle_offset[ob->particle_system]: 0;
271
272                 if(transform_uniform_scale(tfm, uniform_scale)) {
273                         map<Mesh*, float>::iterator it = surface_area_map.find(mesh);
274
275                         if(it == surface_area_map.end()) {
276                                 foreach(Mesh::Triangle& t, mesh->triangles) {
277                                         float3 p1 = mesh->verts[t.v[0]];
278                                         float3 p2 = mesh->verts[t.v[1]];
279                                         float3 p3 = mesh->verts[t.v[2]];
280
281                                         surface_area += triangle_area(p1, p2, p3);
282                                 }
283
284                                 surface_area_map[mesh] = surface_area;
285                         }
286                         else
287                                 surface_area = it->second;
288
289                         surface_area *= uniform_scale;
290                 }
291                 else {
292                         foreach(Mesh::Triangle& t, mesh->triangles) {
293                                 float3 p1 = transform_point(&tfm, mesh->verts[t.v[0]]);
294                                 float3 p2 = transform_point(&tfm, mesh->verts[t.v[1]]);
295                                 float3 p3 = transform_point(&tfm, mesh->verts[t.v[2]]);
296
297                                 surface_area += triangle_area(p1, p2, p3);
298                         }
299                 }
300
301                 /* pack in texture */
302                 int offset = i*OBJECT_SIZE;
303
304                 /* OBJECT_TRANSFORM */
305                 memcpy(&objects[offset], &tfm, sizeof(float4)*3);
306                 /* OBJECT_INVERSE_TRANSFORM */
307                 memcpy(&objects[offset+4], &itfm, sizeof(float4)*3);
308                 /* OBJECT_PROPERTIES */
309                 objects[offset+8] = make_float4(surface_area, pass_id, random_number, __int_as_float(particle_index));
310
311                 if(need_motion == Scene::MOTION_PASS) {
312                         /* motion transformations, is world/object space depending if mesh
313                          * comes with deformed position in object space, or if we transform
314                          * the shading point in world space */
315                         Transform mtfm_pre = ob->motion.pre;
316                         Transform mtfm_post = ob->motion.post;
317
318                         if(!mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION)) {
319                                 mtfm_pre = mtfm_pre * itfm;
320                                 mtfm_post = mtfm_post * itfm;
321                         }
322                         else {
323                                 flag |= SD_OBJECT_HAS_VERTEX_MOTION;
324                         }
325
326                         memcpy(&objects_vector[i*OBJECT_VECTOR_SIZE+0], &mtfm_pre, sizeof(float4)*3);
327                         memcpy(&objects_vector[i*OBJECT_VECTOR_SIZE+3], &mtfm_post, sizeof(float4)*3);
328                 }
329 #ifdef __OBJECT_MOTION__
330                 else if(need_motion == Scene::MOTION_BLUR) {
331                         if(ob->use_motion) {
332                                 /* decompose transformations for interpolation */
333                                 DecompMotionTransform decomp;
334
335                                 transform_motion_decompose(&decomp, &ob->motion, &ob->tfm);
336                                 memcpy(&objects[offset], &decomp, sizeof(float4)*8);
337                                 flag |= SD_OBJECT_MOTION;
338                                 have_motion = true;
339                         }
340                 }
341 #endif
342
343                 if(mesh->use_motion_blur)
344                         have_motion = true;
345
346                 /* dupli object coords and motion info */
347                 int totalsteps = mesh->motion_steps;
348                 int numsteps = (totalsteps - 1)/2;
349                 int numverts = mesh->verts.size();
350                 int numkeys = mesh->curve_keys.size();
351
352                 objects[offset+9] = make_float4(ob->dupli_generated[0], ob->dupli_generated[1], ob->dupli_generated[2], __int_as_float(numkeys));
353                 objects[offset+10] = make_float4(ob->dupli_uv[0], ob->dupli_uv[1], __int_as_float(numsteps), __int_as_float(numverts));
354
355                 /* object flag */
356                 if(ob->use_holdout)
357                         flag |= SD_HOLDOUT_MASK;
358                 object_flag[i] = flag;
359
360                 /* have curves */
361                 if(mesh->curves.size())
362                         have_curves = true;
363
364                 i++;
365
366                 if(progress.get_cancel()) return;
367         }
368
369         device->tex_alloc("__objects", dscene->objects);
370         if(need_motion == Scene::MOTION_PASS)
371                 device->tex_alloc("__objects_vector", dscene->objects_vector);
372
373         dscene->data.bvh.have_motion = have_motion;
374         dscene->data.bvh.have_curves = have_curves;
375         dscene->data.bvh.have_instancing = true;
376 }
377
378 void ObjectManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
379 {
380         if(!need_update)
381                 return;
382         
383         device_free(device, dscene);
384
385         if(scene->objects.size() == 0)
386                 return;
387
388         /* object info flag */
389         uint *object_flag = dscene->object_flag.resize(scene->objects.size());
390
391         /* set object transform matrices, before applying static transforms */
392         progress.set_status("Updating Objects", "Copying Transformations to device");
393         device_update_transforms(device, dscene, scene, object_flag, progress);
394
395         if(progress.get_cancel()) return;
396
397         /* prepare for static BVH building */
398         /* todo: do before to support getting object level coords? */
399         if(scene->params.bvh_type == SceneParams::BVH_STATIC) {
400                 progress.set_status("Updating Objects", "Applying Static Transformations");
401                 apply_static_transforms(dscene, scene, object_flag, progress);
402         }
403 }
404
405 void ObjectManager::device_update_flags(Device *device,
406                                         DeviceScene *dscene,
407                                         Scene *scene,
408                                         Progress& /*progress*/)
409 {
410         if(!need_update && !need_flags_update)
411                 return;
412
413         need_update = false;
414         need_flags_update = false;
415
416         if(scene->objects.size() == 0)
417                 return;
418
419         /* object info flag */
420         uint *object_flag = dscene->object_flag.get_data();
421
422         vector<Object *> volume_objects;
423         foreach(Object *object, scene->objects) {
424                 if(object->mesh->has_volume) {
425                         volume_objects.push_back(object);
426                 }
427         }
428
429         int object_index = 0;
430         foreach(Object *object, scene->objects) {
431                 if(object->mesh->has_volume) {
432                         object_flag[object_index] |= SD_OBJECT_HAS_VOLUME;
433                 }
434                 else {
435                         object_flag[object_index] &= ~SD_OBJECT_HAS_VOLUME;
436                 }
437
438                 foreach(Object *volume_object, volume_objects) {
439                         if(object == volume_object) {
440                                 continue;
441                         }
442                         if(object->bounds.intersects(volume_object->bounds)) {
443                                 object_flag[object_index] |= SD_OBJECT_INTERSECTS_VOLUME;
444                                 break;
445                         }
446                 }
447                 ++object_index;
448         }
449
450         /* allocate object flag */
451         device->tex_alloc("__object_flag", dscene->object_flag);
452 }
453
454 void ObjectManager::device_free(Device *device, DeviceScene *dscene)
455 {
456         device->tex_free(dscene->objects);
457         dscene->objects.clear();
458
459         device->tex_free(dscene->objects_vector);
460         dscene->objects_vector.clear();
461
462         device->tex_free(dscene->object_flag);
463         dscene->object_flag.clear();
464 }
465
466 void ObjectManager::apply_static_transforms(DeviceScene *dscene, Scene *scene, uint *object_flag, Progress& progress)
467 {
468         /* todo: normals and displacement should be done before applying transform! */
469         /* todo: create objects/meshes in right order! */
470
471         /* counter mesh users */
472         map<Mesh*, int> mesh_users;
473 #ifdef __OBJECT_MOTION__
474         Scene::MotionType need_motion = scene->need_motion();
475         bool motion_blur = need_motion == Scene::MOTION_BLUR;
476         bool apply_to_motion = need_motion != Scene::MOTION_PASS;
477 #else
478         bool motion_blur = false;
479 #endif
480         int i = 0;
481         bool have_instancing = false;
482
483         foreach(Object *object, scene->objects) {
484                 map<Mesh*, int>::iterator it = mesh_users.find(object->mesh);
485
486                 if(it == mesh_users.end())
487                         mesh_users[object->mesh] = 1;
488                 else
489                         it->second++;
490         }
491
492         if(progress.get_cancel()) return;
493
494         /* apply transforms for objects with single user meshes */
495         foreach(Object *object, scene->objects) {
496                 if(mesh_users[object->mesh] == 1) {
497                         if(!(motion_blur && object->use_motion)) {
498                                 if(!object->mesh->transform_applied) {
499                                         object->apply_transform(apply_to_motion);
500                                         object->mesh->transform_applied = true;
501
502                                         if(progress.get_cancel()) return;
503                                 }
504
505                                 object_flag[i] |= SD_TRANSFORM_APPLIED;
506                                 if(object->mesh->transform_negative_scaled)
507                                         object_flag[i] |= SD_NEGATIVE_SCALE_APPLIED;
508                         }
509                         else
510                                 have_instancing = true;
511                 }
512                 else
513                         have_instancing = true;
514
515                 i++;
516         }
517
518         dscene->data.bvh.have_instancing = have_instancing;
519 }
520
521 void ObjectManager::tag_update(Scene *scene)
522 {
523         need_update = true;
524         scene->curve_system_manager->need_update = true;
525         scene->mesh_manager->need_update = true;
526         scene->light_manager->need_update = true;
527 }
528
529 CCL_NAMESPACE_END
530