87fb962072512ecbc660273f23243b421e602435
[blender-staging.git] / intern / cycles / blender / blender_volume.cpp
1
2 /*
3  * Copyright 2011-2013 Blender Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "render/colorspace.h"
19 #include "render/mesh.h"
20 #include "render/object.h"
21
22 #include "blender/blender_sync.h"
23 #include "blender/blender_util.h"
24
25 CCL_NAMESPACE_BEGIN
26
27 static void sync_smoke_volume(Scene *scene, BL::Object &b_ob, Mesh *mesh, float frame)
28 {
29   BL::FluidDomainSettings b_domain = object_fluid_gas_domain_find(b_ob);
30   if (!b_domain) {
31     return;
32   }
33
34   ImageManager *image_manager = scene->image_manager;
35   AttributeStandard attributes[] = {ATTR_STD_VOLUME_DENSITY,
36                                     ATTR_STD_VOLUME_COLOR,
37                                     ATTR_STD_VOLUME_FLAME,
38                                     ATTR_STD_VOLUME_HEAT,
39                                     ATTR_STD_VOLUME_TEMPERATURE,
40                                     ATTR_STD_VOLUME_VELOCITY,
41                                     ATTR_STD_NONE};
42
43   for (int i = 0; attributes[i] != ATTR_STD_NONE; i++) {
44     AttributeStandard std = attributes[i];
45     if (!mesh->need_attribute(scene, std)) {
46       continue;
47     }
48
49     mesh->volume_isovalue = b_domain.clipping();
50
51     Attribute *attr = mesh->attributes.add(std);
52     VoxelAttribute *volume_data = attr->data_voxel();
53     ImageMetaData metadata;
54     bool animated = false;
55
56     volume_data->manager = image_manager;
57     volume_data->slot = image_manager->add_image(Attribute::standard_name(std),
58                                                  b_ob.ptr.data,
59                                                  animated,
60                                                  frame,
61                                                  INTERPOLATION_LINEAR,
62                                                  EXTENSION_CLIP,
63                                                  IMAGE_ALPHA_AUTO,
64                                                  u_colorspace_raw,
65                                                  metadata);
66   }
67
68   /* Create a matrix to transform from object space to mesh texture space.
69    * This does not work with deformations but that can probably only be done
70    * well with a volume grid mapping of coordinates. */
71   if (mesh->need_attribute(scene, ATTR_STD_GENERATED_TRANSFORM)) {
72     Attribute *attr = mesh->attributes.add(ATTR_STD_GENERATED_TRANSFORM);
73     Transform *tfm = attr->data_transform();
74
75     BL::Mesh b_mesh(b_ob.data());
76     float3 loc, size;
77     mesh_texture_space(b_mesh, loc, size);
78
79     *tfm = transform_translate(-loc) * transform_scale(size);
80   }
81 }
82
83 void BlenderSync::sync_volume(BL::Object &b_ob, Mesh *mesh)
84 {
85   bool old_has_voxel_attributes = mesh->has_voxel_attributes();
86
87   /* Smoke domain. */
88   sync_smoke_volume(scene, b_ob, mesh, b_scene.frame_current());
89
90   /* Tag update. */
91   bool rebuild = (old_has_voxel_attributes != mesh->has_voxel_attributes());
92   mesh->tag_update(scene, rebuild);
93 }
94
95 CCL_NAMESPACE_END