Added a particle index output to the Particle Info Cycles node. This is required...
[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 #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_id = 0;
43         bounds = BoundBox::empty;
44         motion.pre = transform_identity();
45         motion.post = transform_identity();
46         use_motion = false;
47         use_holdout = false;
48 }
49
50 Object::~Object()
51 {
52 }
53
54 void Object::compute_bounds(bool motion_blur)
55 {
56         BoundBox mbounds = mesh->bounds;
57
58         if(motion_blur && use_motion) {
59                 MotionTransform decomp;
60                 transform_motion_decompose(&decomp, &motion);
61
62                 bounds = BoundBox::empty;
63
64                 /* todo: this is really terrible. according to pbrt there is a better
65                  * way to find this iteratively, but did not find implementation yet
66                  * or try to implement myself */
67                 for(float t = 0.0f; t < 1.0f; t += 1.0f/128.0f) {
68                         Transform ttfm;
69
70                         transform_motion_interpolate(&ttfm, &decomp, t);
71                         bounds.grow(mbounds.transformed(&ttfm));
72                 }
73         }
74         else
75                 bounds = mbounds.transformed(&tfm);
76 }
77
78 void Object::apply_transform()
79 {
80         if(!mesh || tfm == transform_identity())
81                 return;
82         
83         for(size_t i = 0; i < mesh->verts.size(); i++)
84                 mesh->verts[i] = transform_point(&tfm, mesh->verts[i]);
85
86         Attribute *attr_fN = mesh->attributes.find(ATTR_STD_FACE_NORMAL);
87         Attribute *attr_vN = mesh->attributes.find(ATTR_STD_VERTEX_NORMAL);
88
89         Transform ntfm = transform_transpose(transform_inverse(tfm));
90
91         /* we keep normals pointing in same direction on negative scale, notify
92          * mesh about this in it (re)calculates normals */
93         if(transform_negative_scale(tfm))
94                 mesh->transform_negative_scaled = true;
95
96         if(attr_fN) {
97                 float3 *fN = attr_fN->data_float3();
98
99                 for(size_t i = 0; i < mesh->triangles.size(); i++)
100                         fN[i] = transform_direction(&ntfm, fN[i]);
101         }
102
103         if(attr_vN) {
104                 float3 *vN = attr_vN->data_float3();
105
106                 for(size_t i = 0; i < mesh->verts.size(); i++)
107                         vN[i] = transform_direction(&ntfm, vN[i]);
108         }
109
110         if(bounds.valid()) {
111                 mesh->compute_bounds();
112                 compute_bounds(false);
113         }
114         
115         tfm = transform_identity();
116 }
117
118 void Object::tag_update(Scene *scene)
119 {
120         if(mesh) {
121                 if(mesh->transform_applied)
122                         mesh->need_update = true;
123
124                 foreach(uint sindex, mesh->used_shaders) {
125                         Shader *shader = scene->shaders[sindex];
126
127                         if(shader->sample_as_light && shader->has_surface_emission)
128                                 scene->light_manager->need_update = true;
129                 }
130         }
131
132         scene->mesh_manager->need_update = true;
133         scene->object_manager->need_update = true;
134 }
135
136 /* Object Manager */
137
138 ObjectManager::ObjectManager()
139 {
140         need_update = true;
141 }
142
143 ObjectManager::~ObjectManager()
144 {
145 }
146
147 void ObjectManager::device_update_transforms(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
148 {
149         float4 *objects = dscene->objects.resize(OBJECT_SIZE*scene->objects.size());
150         uint *object_flag = dscene->object_flag.resize(scene->objects.size());
151         int i = 0;
152         map<Mesh*, float> surface_area_map;
153         Scene::MotionType need_motion = scene->need_motion();
154
155         foreach(Object *ob, scene->objects) {
156                 Mesh *mesh = ob->mesh;
157                 uint flag = 0;
158
159                 /* compute transformations */
160                 Transform tfm = ob->tfm;
161                 Transform itfm = transform_inverse(tfm);
162
163                 /* compute surface area. for uniform scale we can do avoid the many
164                  * transform calls and share computation for instances */
165                 /* todo: correct for displacement, and move to a better place */
166                 float uniform_scale;
167                 float surface_area = 0.0f;
168                 float pass_id = ob->pass_id;
169                 float random_number = (float)ob->random_id * (1.0f/(float)0xFFFFFFFF);
170                 
171                 if(transform_uniform_scale(tfm, uniform_scale)) {
172                         map<Mesh*, float>::iterator it = surface_area_map.find(mesh);
173
174                         if(it == surface_area_map.end()) {
175                                 foreach(Mesh::Triangle& t, mesh->triangles) {
176                                         float3 p1 = mesh->verts[t.v[0]];
177                                         float3 p2 = mesh->verts[t.v[1]];
178                                         float3 p3 = mesh->verts[t.v[2]];
179
180                                         surface_area += triangle_area(p1, p2, p3);
181                                 }
182
183                                 surface_area_map[mesh] = surface_area;
184                         }
185                         else
186                                 surface_area = it->second;
187
188                         surface_area *= uniform_scale;
189                 }
190                 else {
191                         foreach(Mesh::Triangle& t, mesh->triangles) {
192                                 float3 p1 = transform_point(&tfm, mesh->verts[t.v[0]]);
193                                 float3 p2 = transform_point(&tfm, mesh->verts[t.v[1]]);
194                                 float3 p3 = transform_point(&tfm, mesh->verts[t.v[2]]);
195
196                                 surface_area += triangle_area(p1, p2, p3);
197                         }
198                 }
199
200                 /* pack in texture */
201                 int offset = i*OBJECT_SIZE;
202
203                 memcpy(&objects[offset], &tfm, sizeof(float4)*3);
204                 memcpy(&objects[offset+3], &itfm, sizeof(float4)*3);
205                 objects[offset+6] = make_float4(surface_area, pass_id, random_number, __int_as_float(ob->particle_id));
206
207                 if(need_motion == Scene::MOTION_PASS) {
208                         /* motion transformations, is world/object space depending if mesh
209                          * comes with deformed position in object space, or if we transform
210                          * the shading point in world space */
211                         Transform mtfm_pre = ob->motion.pre;
212                         Transform mtfm_post = ob->motion.post;
213
214                         if(!mesh->attributes.find(ATTR_STD_MOTION_PRE))
215                                 mtfm_pre = mtfm_pre * itfm;
216                         if(!mesh->attributes.find(ATTR_STD_MOTION_POST))
217                                 mtfm_post = mtfm_post * itfm;
218
219                         memcpy(&objects[offset+8], &mtfm_pre, sizeof(float4)*4);
220                         memcpy(&objects[offset+12], &mtfm_post, sizeof(float4)*4);
221                 }
222                 else if(need_motion == Scene::MOTION_BLUR) {
223                         if(ob->use_motion) {
224                                 /* decompose transformations for interpolation */
225                                 MotionTransform decomp;
226
227                                 transform_motion_decompose(&decomp, &ob->motion);
228                                 memcpy(&objects[offset+8], &decomp, sizeof(float4)*8);
229                                 flag |= SD_OBJECT_MOTION;
230                         }
231                         else {
232                                 float4 no_motion = make_float4(FLT_MAX);
233                                 memcpy(&objects[offset+8], &no_motion, sizeof(float4));
234                         }
235                 }
236
237                 /* object flag */
238                 if(ob->use_holdout)
239                         flag |= SD_HOLDOUT_MASK;
240                 object_flag[i] = flag;
241
242                 i++;
243
244                 if(progress.get_cancel()) return;
245         }
246
247         device->tex_alloc("__objects", dscene->objects);
248         device->tex_alloc("__object_flag", dscene->object_flag);
249 }
250
251 void ObjectManager::device_update_particles(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
252 {
253         /* count particles.
254          * adds one dummy particle at the beginning to avoid invalid lookups,
255          * in case a shader uses particle info without actual particle data.
256          */
257         int num_particles = 1;
258         foreach(Object *ob, scene->objects)
259                 num_particles += ob->particles.size();
260         
261         float4 *particles = dscene->particles.resize(PARTICLE_SIZE*num_particles);
262         
263         /* dummy particle */
264         particles[0] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
265         
266         int i = 1;
267         foreach(Object *ob, scene->objects) {
268                 foreach(Particle &pa, ob->particles) {
269                         /* pack in texture */
270                         int offset = i*PARTICLE_SIZE;
271                         
272                         particles[offset] = make_float4(pa.index, pa.age, pa.lifetime, 0.0f);
273                         
274                         i++;
275                         
276                         if(progress.get_cancel()) return;
277                 }
278         }
279         
280         device->tex_alloc("__particles", dscene->particles);
281 }
282
283 void ObjectManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
284 {
285         if(!need_update)
286                 return;
287         
288         device_free(device, dscene);
289
290         if(scene->objects.size() == 0)
291                 return;
292
293         /* set object transform matrices, before applying static transforms */
294         progress.set_status("Updating Objects", "Copying Transformations to device");
295         device_update_transforms(device, dscene, scene, progress);
296
297         if(progress.get_cancel()) return;
298
299         /* prepare for static BVH building */
300         /* todo: do before to support getting object level coords? */
301         if(scene->params.bvh_type == SceneParams::BVH_STATIC) {
302                 progress.set_status("Updating Objects", "Applying Static Transformations");
303                 apply_static_transforms(scene, progress);
304         }
305
306         if(progress.get_cancel()) return;
307
308         progress.set_status("Updating Objects", "Copying Particles to device");
309         device_update_particles(device, dscene, scene, progress);
310         
311         if(progress.get_cancel()) return;
312         
313         need_update = false;
314 }
315
316 void ObjectManager::device_free(Device *device, DeviceScene *dscene)
317 {
318         device->tex_free(dscene->objects);
319         dscene->objects.clear();
320
321         device->tex_free(dscene->object_flag);
322         dscene->object_flag.clear();
323         
324         device->tex_free(dscene->particles);
325         dscene->particles.clear();
326 }
327
328 void ObjectManager::apply_static_transforms(Scene *scene, Progress& progress)
329 {
330         /* todo: normals and displacement should be done before applying transform! */
331         /* todo: create objects/meshes in right order! */
332
333         /* counter mesh users */
334         map<Mesh*, int> mesh_users;
335         bool motion_blur = scene->need_motion() == Scene::MOTION_BLUR;
336
337         foreach(Object *object, scene->objects) {
338                 map<Mesh*, int>::iterator it = mesh_users.find(object->mesh);
339
340                 if(it == mesh_users.end())
341                         mesh_users[object->mesh] = 1;
342                 else
343                         it->second++;
344         }
345
346         if(progress.get_cancel()) return;
347
348         /* apply transforms for objects with single user meshes */
349         foreach(Object *object, scene->objects) {
350                 if(mesh_users[object->mesh] == 1) {
351                         if(!(motion_blur && object->use_motion)) {
352                                 if(!object->mesh->transform_applied) {
353                                         object->apply_transform();
354                                         object->mesh->transform_applied = true;
355
356                                         if(progress.get_cancel()) return;
357                                 }
358                         }
359                 }
360         }
361 }
362
363 void ObjectManager::tag_update(Scene *scene)
364 {
365         need_update = true;
366         scene->mesh_manager->need_update = true;
367         scene->light_manager->need_update = true;
368 }
369
370 CCL_NAMESPACE_END
371