6fb96a3290cbe68e7470f2dd7fdde023065a2371
[blender.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 "scene.h"
23
24 #include "util_foreach.h"
25 #include "util_map.h"
26 #include "util_progress.h"
27 #include "util_vector.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         random_id = 0;
40         pass_id = 0;
41         particle_id = 0;
42         bounds = BoundBox::empty;
43         motion.pre = transform_identity();
44         motion.mid = transform_identity();
45         motion.post = transform_identity();
46         use_motion = false;
47         use_holdout = false;
48         curverender = false;
49         dupli_generated = make_float3(0.0f, 0.0f, 0.0f);
50         dupli_uv = make_float2(0.0f, 0.0f);
51 }
52
53 Object::~Object()
54 {
55 }
56
57 void Object::compute_bounds(bool motion_blur, float shuttertime)
58 {
59         BoundBox mbounds = mesh->bounds;
60
61         if(motion_blur && use_motion) {
62                 DecompMotionTransform decomp;
63                 transform_motion_decompose(&decomp, &motion, &tfm);
64
65                 bounds = BoundBox::empty;
66
67                 /* todo: this is really terrible. according to pbrt there is a better
68                  * way to find this iteratively, but did not find implementation yet
69                  * or try to implement myself */
70                 float start_t = 0.5f - shuttertime*0.25f;
71                 float end_t = 0.5f + shuttertime*0.25f;
72
73                 for(float t = start_t; t < end_t; t += (1.0f/128.0f)*shuttertime) {
74                         Transform ttfm;
75
76                         transform_motion_interpolate(&ttfm, &decomp, t);
77                         bounds.grow(mbounds.transformed(&ttfm));
78                 }
79         }
80         else
81                 bounds = mbounds.transformed(&tfm);
82 }
83
84 void Object::apply_transform()
85 {
86         if(!mesh || tfm == transform_identity())
87                 return;
88
89         float3 c0 = transform_get_column(&tfm, 0);
90         float3 c1 = transform_get_column(&tfm, 1);
91         float3 c2 = transform_get_column(&tfm, 2);
92         float scalar = pow(fabsf(dot(cross(c0, c1), c2)), 1.0f/3.0f);
93
94         for(size_t i = 0; i < mesh->verts.size(); i++)
95                 mesh->verts[i] = transform_point(&tfm, mesh->verts[i]);
96
97         for(size_t i = 0; i < mesh->curve_keys.size(); i++) {
98                 mesh->curve_keys[i].co = transform_point(&tfm, mesh->curve_keys[i].co);
99                 /* scale for strand radius - only correct for uniform transforms*/
100                 mesh->curve_keys[i].radius *= scalar;
101         }
102
103         /* store matrix to transform later. when accessing these as attributes we
104          * do not want the transform to be applied for consistency between static
105          * and dynamic BVH, so we do it on packing. */
106         mesh->transform_normal = transform_transpose(transform_inverse(tfm));
107
108         /* we keep normals pointing in same direction on negative scale, notify
109          * mesh about this in it (re)calculates normals */
110         if(transform_negative_scale(tfm))
111                 mesh->transform_negative_scaled = true;
112
113         if(bounds.valid()) {
114                 mesh->compute_bounds();
115                 compute_bounds(false, 0.0f);
116         }
117
118         /* tfm is not reset to identity, all code that uses it needs to check the
119            transform_applied boolean */
120 }
121
122 void Object::tag_update(Scene *scene)
123 {
124         if(mesh) {
125                 if(mesh->transform_applied)
126                         mesh->need_update = true;
127
128                 foreach(uint sindex, mesh->used_shaders) {
129                         Shader *shader = scene->shaders[sindex];
130
131                         if(shader->use_mis && shader->has_surface_emission)
132                                 scene->light_manager->need_update = true;
133                 }
134         }
135
136         scene->curve_system_manager->need_update = true;
137         scene->mesh_manager->need_update = true;
138         scene->object_manager->need_update = true;
139 }
140
141 /* Object Manager */
142
143 ObjectManager::ObjectManager()
144 {
145         need_update = true;
146 }
147
148 ObjectManager::~ObjectManager()
149 {
150 }
151
152 void ObjectManager::device_update_transforms(Device *device, DeviceScene *dscene, Scene *scene, uint *object_flag, Progress& progress)
153 {
154         float4 *objects;
155         float4 *objects_vector = NULL;
156         int i = 0;
157         map<Mesh*, float> surface_area_map;
158         Scene::MotionType need_motion = scene->need_motion(device->info.advanced_shading);
159         bool have_motion = false;
160         bool have_curves = false;
161
162         objects = dscene->objects.resize(OBJECT_SIZE*scene->objects.size());
163         if(need_motion == Scene::MOTION_PASS)
164                 objects_vector = dscene->objects_vector.resize(OBJECT_VECTOR_SIZE*scene->objects.size());
165
166         foreach(Object *ob, scene->objects) {
167                 Mesh *mesh = ob->mesh;
168                 uint flag = 0;
169
170                 /* compute transformations */
171                 Transform tfm = ob->tfm;
172                 Transform itfm = transform_inverse(tfm);
173
174                 /* compute surface area. for uniform scale we can do avoid the many
175                  * transform calls and share computation for instances */
176                 /* todo: correct for displacement, and move to a better place */
177                 float uniform_scale;
178                 float surface_area = 0.0f;
179                 float pass_id = ob->pass_id;
180                 float random_number = (float)ob->random_id * (1.0f/(float)0xFFFFFFFF);
181
182                 if(transform_uniform_scale(tfm, uniform_scale)) {
183                         map<Mesh*, float>::iterator it = surface_area_map.find(mesh);
184
185                         if(it == surface_area_map.end()) {
186                                 foreach(Mesh::Triangle& t, mesh->triangles) {
187                                         float3 p1 = mesh->verts[t.v[0]];
188                                         float3 p2 = mesh->verts[t.v[1]];
189                                         float3 p3 = mesh->verts[t.v[2]];
190
191                                         surface_area += triangle_area(p1, p2, p3);
192                                 }
193
194                                 foreach(Mesh::Curve& curve, mesh->curves) {
195                                         int first_key = curve.first_key;
196
197                                         for(int i = 0; i < curve.num_segments(); i++) {
198                                                 float3 p1 = mesh->curve_keys[first_key + i].co;
199                                                 float r1 = mesh->curve_keys[first_key + i].radius;
200                                                 float3 p2 = mesh->curve_keys[first_key + i + 1].co;
201                                                 float r2 = mesh->curve_keys[first_key + i + 1].radius;
202
203                                                 /* currently ignores segment overlaps*/
204                                                 surface_area += M_PI_F *(r1 + r2) * len(p1 - p2);
205                                         }
206                                 }
207
208                                 surface_area_map[mesh] = surface_area;
209                         }
210                         else
211                                 surface_area = it->second;
212
213                         surface_area *= uniform_scale;
214                 }
215                 else {
216                         foreach(Mesh::Triangle& t, mesh->triangles) {
217                                 float3 p1 = transform_point(&tfm, mesh->verts[t.v[0]]);
218                                 float3 p2 = transform_point(&tfm, mesh->verts[t.v[1]]);
219                                 float3 p3 = transform_point(&tfm, mesh->verts[t.v[2]]);
220
221                                 surface_area += triangle_area(p1, p2, p3);
222                         }
223
224                         foreach(Mesh::Curve& curve, mesh->curves) {
225                                 int first_key = curve.first_key;
226
227                                 for(int i = 0; i < curve.num_segments(); i++) {
228                                         float3 p1 = mesh->curve_keys[first_key + i].co;
229                                         float r1 = mesh->curve_keys[first_key + i].radius;
230                                         float3 p2 = mesh->curve_keys[first_key + i + 1].co;
231                                         float r2 = mesh->curve_keys[first_key + i + 1].radius;
232
233                                         p1 = transform_point(&tfm, p1);
234                                         p2 = transform_point(&tfm, p2);
235
236                                         /* currently ignores segment overlaps*/
237                                         surface_area += M_PI_F *(r1 + r2) * len(p1 - p2);
238                                 }
239                         }
240                 }
241
242                 /* pack in texture */
243                 int offset = i*OBJECT_SIZE;
244
245                 memcpy(&objects[offset], &tfm, sizeof(float4)*3);
246                 memcpy(&objects[offset+4], &itfm, sizeof(float4)*3);
247                 objects[offset+8] = make_float4(surface_area, pass_id, random_number, __int_as_float(ob->particle_id));
248
249                 if(need_motion == Scene::MOTION_PASS) {
250                         /* motion transformations, is world/object space depending if mesh
251                          * comes with deformed position in object space, or if we transform
252                          * the shading point in world space */
253                         Transform mtfm_pre = ob->motion.pre;
254                         Transform mtfm_post = ob->motion.post;
255
256                         if(!(mesh->attributes.find(ATTR_STD_MOTION_PRE) || mesh->curve_attributes.find(ATTR_STD_MOTION_PRE)))
257                                 mtfm_pre = mtfm_pre * itfm;
258                         if(!(mesh->attributes.find(ATTR_STD_MOTION_POST) || mesh->curve_attributes.find(ATTR_STD_MOTION_POST)))
259                                 mtfm_post = mtfm_post * itfm;
260
261                         memcpy(&objects_vector[i*OBJECT_VECTOR_SIZE+0], &mtfm_pre, sizeof(float4)*3);
262                         memcpy(&objects_vector[i*OBJECT_VECTOR_SIZE+3], &mtfm_post, sizeof(float4)*3);
263                 }
264 #ifdef __OBJECT_MOTION__
265                 else if(need_motion == Scene::MOTION_BLUR) {
266                         if(ob->use_motion) {
267                                 /* decompose transformations for interpolation */
268                                 DecompMotionTransform decomp;
269
270                                 transform_motion_decompose(&decomp, &ob->motion, &ob->tfm);
271                                 memcpy(&objects[offset], &decomp, sizeof(float4)*8);
272                                 flag |= SD_OBJECT_MOTION;
273                                 have_motion = true;
274                         }
275                 }
276 #endif
277
278                 /* dupli object coords */
279                 objects[offset+9] = make_float4(ob->dupli_generated[0], ob->dupli_generated[1], ob->dupli_generated[2], 0.0f);
280                 objects[offset+10] = make_float4(ob->dupli_uv[0], ob->dupli_uv[1], 0.0f, 0.0f);
281
282                 /* object flag */
283                 if(ob->use_holdout)
284                         flag |= SD_HOLDOUT_MASK;
285                 object_flag[i] = flag;
286
287                 /* have curves */
288                 if(mesh->curves.size())
289                         have_curves = true;
290
291                 i++;
292
293                 if(progress.get_cancel()) return;
294         }
295
296         device->tex_alloc("__objects", dscene->objects);
297         if(need_motion == Scene::MOTION_PASS)
298                 device->tex_alloc("__objects_vector", dscene->objects_vector);
299
300         dscene->data.bvh.have_motion = have_motion;
301         dscene->data.bvh.have_curves = have_curves;
302         dscene->data.bvh.have_instancing = true;
303 }
304
305 void ObjectManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
306 {
307         if(!need_update)
308                 return;
309         
310         device_free(device, dscene);
311
312         need_update = false;
313
314         if(scene->objects.size() == 0)
315                 return;
316
317         /* object info flag */
318         uint *object_flag = dscene->object_flag.resize(scene->objects.size());
319
320         /* set object transform matrices, before applying static transforms */
321         progress.set_status("Updating Objects", "Copying Transformations to device");
322         device_update_transforms(device, dscene, scene, object_flag, progress);
323
324         if(progress.get_cancel()) return;
325
326         /* prepare for static BVH building */
327         /* todo: do before to support getting object level coords? */
328         if(scene->params.bvh_type == SceneParams::BVH_STATIC) {
329                 progress.set_status("Updating Objects", "Applying Static Transformations");
330                 apply_static_transforms(dscene, scene, object_flag, progress);
331         }
332
333         /* allocate object flag */
334         device->tex_alloc("__object_flag", dscene->object_flag);
335 }
336
337 void ObjectManager::device_free(Device *device, DeviceScene *dscene)
338 {
339         device->tex_free(dscene->objects);
340         dscene->objects.clear();
341
342         device->tex_free(dscene->objects_vector);
343         dscene->objects_vector.clear();
344
345         device->tex_free(dscene->object_flag);
346         dscene->object_flag.clear();
347 }
348
349 void ObjectManager::apply_static_transforms(DeviceScene *dscene, Scene *scene, uint *object_flag, Progress& progress)
350 {
351         /* todo: normals and displacement should be done before applying transform! */
352         /* todo: create objects/meshes in right order! */
353
354         /* counter mesh users */
355         map<Mesh*, int> mesh_users;
356 #ifdef __OBJECT_MOTION__
357         Scene::MotionType need_motion = scene->need_motion();
358         bool motion_blur = need_motion == Scene::MOTION_BLUR;
359 #else
360         bool motion_blur = false;
361 #endif
362         int i = 0;
363         bool have_instancing = false;
364
365         foreach(Object *object, scene->objects) {
366                 map<Mesh*, int>::iterator it = mesh_users.find(object->mesh);
367
368                 if(it == mesh_users.end())
369                         mesh_users[object->mesh] = 1;
370                 else
371                         it->second++;
372         }
373
374         if(progress.get_cancel()) return;
375
376         /* apply transforms for objects with single user meshes */
377         foreach(Object *object, scene->objects) {
378                 if(mesh_users[object->mesh] == 1) {
379                         if(!(motion_blur && object->use_motion)) {
380                                 if(!object->mesh->transform_applied) {
381                                         object->apply_transform();
382                                         object->mesh->transform_applied = true;
383
384                                         if(progress.get_cancel()) return;
385                                 }
386
387                                 object_flag[i] |= SD_TRANSFORM_APPLIED;
388                         }
389                         else
390                                 have_instancing = true;
391                 }
392                 else
393                         have_instancing = true;
394
395                 i++;
396         }
397
398         dscene->data.bvh.have_instancing = have_instancing;
399 }
400
401 void ObjectManager::tag_update(Scene *scene)
402 {
403         need_update = true;
404         scene->curve_system_manager->need_update = true;
405         scene->mesh_manager->need_update = true;
406         scene->light_manager->need_update = true;
407 }
408
409 CCL_NAMESPACE_END
410