2 * Copyright 2011, Blender Foundation.
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.
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.
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.
25 #include "util_foreach.h"
27 #include "util_progress.h"
28 #include "util_vector.h"
38 tfm = transform_identity();
43 bounds = BoundBox::empty;
44 motion.pre = transform_identity();
45 motion.post = transform_identity();
54 void Object::compute_bounds(bool motion_blur)
56 BoundBox mbounds = mesh->bounds;
58 if(motion_blur && use_motion) {
59 MotionTransform decomp;
60 transform_motion_decompose(&decomp, &motion);
62 bounds = BoundBox::empty;
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) {
70 transform_motion_interpolate(&ttfm, &decomp, t);
71 bounds.grow(mbounds.transformed(&ttfm));
75 bounds = mbounds.transformed(&tfm);
78 void Object::apply_transform()
80 if(!mesh || tfm == transform_identity())
83 for(size_t i = 0; i < mesh->verts.size(); i++)
84 mesh->verts[i] = transform_point(&tfm, mesh->verts[i]);
86 Attribute *attr_fN = mesh->attributes.find(ATTR_STD_FACE_NORMAL);
87 Attribute *attr_vN = mesh->attributes.find(ATTR_STD_VERTEX_NORMAL);
89 Transform ntfm = transform_transpose(transform_inverse(tfm));
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;
97 float3 *fN = attr_fN->data_float3();
99 for(size_t i = 0; i < mesh->triangles.size(); i++)
100 fN[i] = transform_direction(&ntfm, fN[i]);
104 float3 *vN = attr_vN->data_float3();
106 for(size_t i = 0; i < mesh->verts.size(); i++)
107 vN[i] = transform_direction(&ntfm, vN[i]);
111 mesh->compute_bounds();
112 compute_bounds(false);
115 tfm = transform_identity();
118 void Object::tag_update(Scene *scene)
121 if(mesh->transform_applied)
122 mesh->need_update = true;
124 foreach(uint sindex, mesh->used_shaders) {
125 Shader *shader = scene->shaders[sindex];
127 if(shader->sample_as_light && shader->has_surface_emission)
128 scene->light_manager->need_update = true;
132 scene->mesh_manager->need_update = true;
133 scene->object_manager->need_update = true;
138 ObjectManager::ObjectManager()
143 ObjectManager::~ObjectManager()
147 void ObjectManager::device_update_transforms(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
149 float4 *objects = dscene->objects.resize(OBJECT_SIZE*scene->objects.size());
150 uint *object_flag = dscene->object_flag.resize(scene->objects.size());
152 map<Mesh*, float> surface_area_map;
153 Scene::MotionType need_motion = scene->need_motion();
155 foreach(Object *ob, scene->objects) {
156 Mesh *mesh = ob->mesh;
159 /* compute transformations */
160 Transform tfm = ob->tfm;
161 Transform itfm = transform_inverse(tfm);
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 */
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);
171 if(transform_uniform_scale(tfm, uniform_scale)) {
172 map<Mesh*, float>::iterator it = surface_area_map.find(mesh);
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]];
180 surface_area += triangle_area(p1, p2, p3);
183 surface_area_map[mesh] = surface_area;
186 surface_area = it->second;
188 surface_area *= uniform_scale;
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]]);
196 surface_area += triangle_area(p1, p2, p3);
200 /* pack in texture */
201 int offset = i*OBJECT_SIZE;
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));
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;
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;
219 memcpy(&objects[offset+8], &mtfm_pre, sizeof(float4)*4);
220 memcpy(&objects[offset+12], &mtfm_post, sizeof(float4)*4);
222 else if(need_motion == Scene::MOTION_BLUR) {
224 /* decompose transformations for interpolation */
225 MotionTransform decomp;
227 transform_motion_decompose(&decomp, &ob->motion);
228 memcpy(&objects[offset+8], &decomp, sizeof(float4)*8);
229 flag |= SD_OBJECT_MOTION;
232 float4 no_motion = make_float4(FLT_MAX);
233 memcpy(&objects[offset+8], &no_motion, sizeof(float4));
239 flag |= SD_HOLDOUT_MASK;
240 object_flag[i] = flag;
244 if(progress.get_cancel()) return;
247 device->tex_alloc("__objects", dscene->objects);
248 device->tex_alloc("__object_flag", dscene->object_flag);
251 void ObjectManager::device_update_particles(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
254 * adds one dummy particle at the beginning to avoid invalid lookups,
255 * in case a shader uses particle info without actual particle data.
257 int num_particles = 1;
258 foreach(Object *ob, scene->objects)
259 num_particles += ob->particles.size();
261 float4 *particles = dscene->particles.resize(PARTICLE_SIZE*num_particles);
264 particles[0] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
267 foreach(Object *ob, scene->objects) {
268 foreach(Particle &pa, ob->particles) {
269 /* pack in texture */
270 int offset = i*PARTICLE_SIZE;
272 particles[offset] = make_float4(pa.index, pa.age, pa.lifetime, 0.0f);
276 if(progress.get_cancel()) return;
280 device->tex_alloc("__particles", dscene->particles);
283 void ObjectManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
288 device_free(device, dscene);
290 if(scene->objects.size() == 0)
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);
297 if(progress.get_cancel()) return;
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);
306 if(progress.get_cancel()) return;
308 progress.set_status("Updating Objects", "Copying Particles to device");
309 device_update_particles(device, dscene, scene, progress);
311 if(progress.get_cancel()) return;
316 void ObjectManager::device_free(Device *device, DeviceScene *dscene)
318 device->tex_free(dscene->objects);
319 dscene->objects.clear();
321 device->tex_free(dscene->object_flag);
322 dscene->object_flag.clear();
324 device->tex_free(dscene->particles);
325 dscene->particles.clear();
328 void ObjectManager::apply_static_transforms(Scene *scene, Progress& progress)
330 /* todo: normals and displacement should be done before applying transform! */
331 /* todo: create objects/meshes in right order! */
333 /* counter mesh users */
334 map<Mesh*, int> mesh_users;
335 bool motion_blur = scene->need_motion() == Scene::MOTION_BLUR;
337 foreach(Object *object, scene->objects) {
338 map<Mesh*, int>::iterator it = mesh_users.find(object->mesh);
340 if(it == mesh_users.end())
341 mesh_users[object->mesh] = 1;
346 if(progress.get_cancel()) return;
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;
356 if(progress.get_cancel()) return;
363 void ObjectManager::tag_update(Scene *scene)
366 scene->mesh_manager->need_update = true;
367 scene->light_manager->need_update = true;