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