Merging r50226 through r50236 from trunk into soc-2011-tomato
[blender.git] / intern / cycles / blender / blender_mesh.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 "mesh.h"
20 #include "object.h"
21 #include "scene.h"
22
23 #include "blender_sync.h"
24 #include "blender_util.h"
25
26 #include "subd_mesh.h"
27 #include "subd_patch.h"
28 #include "subd_split.h"
29
30 #include "util_foreach.h"
31
32 CCL_NAMESPACE_BEGIN
33
34 /* Find/Add */
35
36 static void create_mesh(Scene *scene, Mesh *mesh, BL::Mesh b_mesh, const vector<uint>& used_shaders)
37 {
38         /* create vertices */
39         BL::Mesh::vertices_iterator v;
40
41         for(b_mesh.vertices.begin(v); v != b_mesh.vertices.end(); ++v)
42                 mesh->verts.push_back(get_float3(v->co()));
43
44         /* create vertex normals */
45         Attribute *attr_N = mesh->attributes.add(ATTR_STD_VERTEX_NORMAL);
46         float3 *N = attr_N->data_float3();
47
48         for(b_mesh.vertices.begin(v); v != b_mesh.vertices.end(); ++v, ++N)
49                 *N = get_float3(v->normal());
50
51         /* create faces */
52         BL::Mesh::tessfaces_iterator f;
53         vector<int> nverts;
54
55         for(b_mesh.tessfaces.begin(f); f != b_mesh.tessfaces.end(); ++f) {
56                 int4 vi = get_int4(f->vertices_raw());
57                 int n = (vi[3] == 0)? 3: 4;
58                 int mi = clamp(f->material_index(), 0, used_shaders.size()-1);
59                 int shader = used_shaders[mi];
60                 bool smooth = f->use_smooth();
61
62                 mesh->add_triangle(vi[0], vi[1], vi[2], shader, smooth);
63
64                 if(n == 4)
65                         mesh->add_triangle(vi[0], vi[2], vi[3], shader, smooth);
66
67                 nverts.push_back(n);
68         }
69
70         /* create generated coordinates. todo: we should actually get the orco
71          * coordinates from modifiers, for now we use texspace loc/size which
72          * is available in the api. */
73         if(mesh->need_attribute(scene, ATTR_STD_GENERATED)) {
74                 Attribute *attr = mesh->attributes.add(ATTR_STD_GENERATED);
75                 float3 loc = get_float3(b_mesh.texspace_location());
76                 float3 size = get_float3(b_mesh.texspace_size());
77
78                 if(size.x != 0.0f) size.x = 0.5f/size.x;
79                 if(size.y != 0.0f) size.y = 0.5f/size.y;
80                 if(size.z != 0.0f) size.z = 0.5f/size.z;
81
82                 loc = loc*size - make_float3(0.5f, 0.5f, 0.5f);
83
84                 float3 *fdata = attr->data_float3();
85                 size_t i = 0;
86
87                 for(b_mesh.vertices.begin(v); v != b_mesh.vertices.end(); ++v)
88                         fdata[i++] = get_float3(v->co())*size - loc;
89         }
90
91         /* create vertex color attributes */
92         {
93                 BL::Mesh::tessface_vertex_colors_iterator l;
94
95                 for(b_mesh.tessface_vertex_colors.begin(l); l != b_mesh.tessface_vertex_colors.end(); ++l) {
96                         if(!mesh->need_attribute(scene, ustring(l->name().c_str())))
97                                 continue;
98
99                         Attribute *attr = mesh->attributes.add(
100                                 ustring(l->name().c_str()), TypeDesc::TypeColor, Attribute::CORNER);
101
102                         BL::MeshColorLayer::data_iterator c;
103                         float3 *fdata = attr->data_float3();
104                         size_t i = 0;
105
106                         for(l->data.begin(c); c != l->data.end(); ++c, ++i) {
107                                 fdata[0] = color_srgb_to_scene_linear(get_float3(c->color1()));
108                                 fdata[1] = color_srgb_to_scene_linear(get_float3(c->color2()));
109                                 fdata[2] = color_srgb_to_scene_linear(get_float3(c->color3()));
110
111                                 if(nverts[i] == 4) {
112                                         fdata[3] = fdata[0];
113                                         fdata[4] = fdata[2];
114                                         fdata[5] = color_srgb_to_scene_linear(get_float3(c->color4()));
115                                         fdata += 6;
116                                 }
117                                 else
118                                         fdata += 3;
119                         }
120                 }
121         }
122
123         /* create uv map attributes */
124         {
125                 BL::Mesh::tessface_uv_textures_iterator l;
126
127                 for(b_mesh.tessface_uv_textures.begin(l); l != b_mesh.tessface_uv_textures.end(); ++l) {
128                         AttributeStandard std = (l->active_render())? ATTR_STD_UV: ATTR_STD_NONE;
129                         ustring name = ustring(l->name().c_str());
130
131                         if(!(mesh->need_attribute(scene, name) || mesh->need_attribute(scene, std)))
132                                 continue;
133
134                         Attribute *attr;
135
136                         if(l->active_render())
137                                 attr = mesh->attributes.add(std, name);
138                         else
139                                 attr = mesh->attributes.add(name, TypeDesc::TypePoint, Attribute::CORNER);
140
141                         BL::MeshTextureFaceLayer::data_iterator t;
142                         float3 *fdata = attr->data_float3();
143                         size_t i = 0;
144
145                         for(l->data.begin(t); t != l->data.end(); ++t, ++i) {
146                                 fdata[0] =  get_float3(t->uv1());
147                                 fdata[1] =  get_float3(t->uv2());
148                                 fdata[2] =  get_float3(t->uv3());
149                                 fdata += 3;
150
151                                 if(nverts[i] == 4) {
152                                         fdata[0] =  get_float3(t->uv1());
153                                         fdata[1] =  get_float3(t->uv3());
154                                         fdata[2] =  get_float3(t->uv4());
155                                         fdata += 3;
156                                 }
157                         }
158                 }
159         }
160 }
161
162 static void create_subd_mesh(Mesh *mesh, BL::Mesh b_mesh, PointerRNA *cmesh, const vector<uint>& used_shaders)
163 {
164         /* create subd mesh */
165         SubdMesh sdmesh;
166
167         /* create vertices */
168         BL::Mesh::vertices_iterator v;
169
170         for(b_mesh.vertices.begin(v); v != b_mesh.vertices.end(); ++v)
171                 sdmesh.add_vert(get_float3(v->co()));
172
173         /* create faces */
174         BL::Mesh::tessfaces_iterator f;
175
176         for(b_mesh.tessfaces.begin(f); f != b_mesh.tessfaces.end(); ++f) {
177                 int4 vi = get_int4(f->vertices_raw());
178                 int n = (vi[3] == 0) ? 3: 4;
179                 //int shader = used_shaders[f->material_index()];
180
181                 if(n == 4)
182                         sdmesh.add_face(vi[0], vi[1], vi[2], vi[3]);
183 #if 0
184                 else
185                         sdmesh.add_face(vi[0], vi[1], vi[2]);
186 #endif
187         }
188
189         /* finalize subd mesh */
190         sdmesh.link_boundary();
191
192         /* subdivide */
193         DiagSplit dsplit;
194         dsplit.camera = NULL;
195         dsplit.dicing_rate = RNA_float_get(cmesh, "dicing_rate");
196
197         sdmesh.tessellate(&dsplit, false, mesh, used_shaders[0], true);
198 }
199
200 /* Sync */
201
202 Mesh *BlenderSync::sync_mesh(BL::Object b_ob, bool object_updated)
203 {
204         /* test if we can instance or if the object is modified */
205         BL::ID b_ob_data = b_ob.data();
206         BL::ID key = (BKE_object_is_modified(b_ob))? b_ob: b_ob_data;
207         BL::Material material_override = render_layer.material_override;
208
209         /* find shader indices */
210         vector<uint> used_shaders;
211
212         BL::Object::material_slots_iterator slot;
213         for(b_ob.material_slots.begin(slot); slot != b_ob.material_slots.end(); ++slot) {
214                 if(material_override)
215                         find_shader(material_override, used_shaders, scene->default_surface);
216                 else
217                         find_shader(slot->material(), used_shaders, scene->default_surface);
218         }
219
220         if(used_shaders.size() == 0) {
221                 if(material_override)
222                         find_shader(material_override, used_shaders, scene->default_surface);
223                 else
224                         used_shaders.push_back(scene->default_surface);
225         }
226         
227         /* test if we need to sync */
228         Mesh *mesh;
229
230         if(!mesh_map.sync(&mesh, key)) {
231                 /* if transform was applied to mesh, need full update */
232                 if(object_updated && mesh->transform_applied);
233                 /* test if shaders changed, these can be object level so mesh
234                  * does not get tagged for recalc */
235                 else if(mesh->used_shaders != used_shaders);
236                 else {
237                         /* even if not tagged for recalc, we may need to sync anyway
238                          * because the shader needs different mesh attributes */
239                         bool attribute_recalc = false;
240
241                         foreach(uint shader, mesh->used_shaders)
242                                 if(scene->shaders[shader]->need_update_attributes)
243                                         attribute_recalc = true;
244
245                         if(!attribute_recalc)
246                                 return mesh;
247                 }
248         }
249
250         /* ensure we only sync instanced meshes once */
251         if(mesh_synced.find(mesh) != mesh_synced.end())
252                 return mesh;
253         
254         mesh_synced.insert(mesh);
255
256         /* create derived mesh */
257         BL::Mesh b_mesh = object_to_mesh(b_ob, b_scene, true, !preview);
258         PointerRNA cmesh = RNA_pointer_get(&b_ob_data.ptr, "cycles");
259
260         vector<Mesh::Triangle> oldtriangle = mesh->triangles;
261
262         mesh->clear();
263         mesh->used_shaders = used_shaders;
264         mesh->name = ustring(b_ob_data.name().c_str());
265
266         if(b_mesh) {
267                 if(cmesh.data && experimental && RNA_boolean_get(&cmesh, "use_subdivision"))
268                         create_subd_mesh(mesh, b_mesh, &cmesh, used_shaders);
269                 else
270                         create_mesh(scene, mesh, b_mesh, used_shaders);
271
272                 /* free derived mesh */
273                 object_remove_mesh(b_data, b_mesh);
274         }
275
276         /* displacement method */
277         if(cmesh.data) {
278                 int method = RNA_enum_get(&cmesh, "displacement_method");
279
280                 if(method == 0 || !experimental)
281                         mesh->displacement_method = Mesh::DISPLACE_BUMP;
282                 else if(method == 1)
283                         mesh->displacement_method = Mesh::DISPLACE_TRUE;
284                 else
285                         mesh->displacement_method = Mesh::DISPLACE_BOTH;
286         }
287
288         /* tag update */
289         bool rebuild = false;
290
291         if(oldtriangle.size() != mesh->triangles.size())
292                 rebuild = true;
293         else if(oldtriangle.size()) {
294                 if(memcmp(&oldtriangle[0], &mesh->triangles[0], sizeof(Mesh::Triangle)*oldtriangle.size()) != 0)
295                         rebuild = true;
296         }
297         
298         mesh->tag_update(scene, rebuild);
299
300         return mesh;
301 }
302
303 void BlenderSync::sync_mesh_motion(BL::Object b_ob, Mesh *mesh, int motion)
304 {
305         /* todo: displacement, subdivision */
306         size_t size = mesh->verts.size();
307
308         /* skip objects without deforming modifiers. this is not a totally reliable,
309          * would need a more extensive check to see which objects are animated */
310         if(!size || !ccl::BKE_object_is_deform_modified(b_ob, b_scene, preview))
311                 return;
312
313         /* get derived mesh */
314         BL::Mesh b_mesh = object_to_mesh(b_ob, b_scene, true, !preview);
315
316         if(b_mesh) {
317                 BL::Mesh::vertices_iterator v;
318                 AttributeStandard std = (motion == -1)? ATTR_STD_MOTION_PRE: ATTR_STD_MOTION_POST;
319                 Attribute *attr_M = mesh->attributes.add(std);
320                 float3 *M = attr_M->data_float3(), *cur_M;
321                 size_t i = 0;
322
323                 for(b_mesh.vertices.begin(v), cur_M = M; v != b_mesh.vertices.end() && i < size; ++v, cur_M++, i++)
324                         *cur_M = get_float3(v->co());
325
326                 /* if number of vertices changed, or if coordinates stayed the same, drop it */
327                 if(i != size || memcmp(M, &mesh->verts[0], sizeof(float3)*size) == 0)
328                         mesh->attributes.remove(std);
329
330                 /* free derived mesh */
331                 object_remove_mesh(b_data, b_mesh);
332         }
333 }
334
335 CCL_NAMESPACE_END
336