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