Cycles: Anisotropic BSDF enabled, with tangents now computed from the active UV map.
[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 float3 tri_calc_tangent(float3 v0, float3 v1, float3 v2, float3 tx0, float3 tx1, float3 tx2)
37 {
38         float3 duv1 = tx2 - tx0;
39         float3 duv2 = tx2 - tx1;
40         float3 dp1 = v2 - v0;
41         float3 dp2 = v2 - v1;
42         float det = duv1[0] * duv2[1] - duv1[1] * duv2[0];
43
44         if(det != 0.0f) {
45                 return normalize(dp1 * duv2[1] - dp2 * duv1[1]);
46         }
47         else {
48                 /* give back a sane default, using a valid edge as a fallback */
49                 float3 edge = v1 - v0;
50
51                 if(len(edge) == 0.0f)
52                         edge = v2 - v0;
53
54                 return normalize(edge);
55         }
56 }
57
58 static void create_mesh(Scene *scene, Mesh *mesh, BL::Mesh b_mesh, const vector<uint>& used_shaders)
59 {
60         /* create vertices */
61         BL::Mesh::vertices_iterator v;
62
63         for(b_mesh.vertices.begin(v); v != b_mesh.vertices.end(); ++v)
64                 mesh->verts.push_back(get_float3(v->co()));
65
66         /* create vertex normals */
67         Attribute *attr_N = mesh->attributes.add(ATTR_STD_VERTEX_NORMAL);
68         float3 *N = attr_N->data_float3();
69
70         for(b_mesh.vertices.begin(v); v != b_mesh.vertices.end(); ++v, ++N)
71                 *N = get_float3(v->normal());
72
73         /* create faces */
74         BL::Mesh::tessfaces_iterator f;
75         vector<int> nverts;
76
77         for(b_mesh.tessfaces.begin(f); f != b_mesh.tessfaces.end(); ++f) {
78                 int4 vi = get_int4(f->vertices_raw());
79                 int n = (vi[3] == 0)? 3: 4;
80                 int mi = clamp(f->material_index(), 0, used_shaders.size()-1);
81                 int shader = used_shaders[mi];
82                 bool smooth = f->use_smooth();
83
84                 mesh->add_triangle(vi[0], vi[1], vi[2], shader, smooth);
85
86                 if(n == 4)
87                         mesh->add_triangle(vi[0], vi[2], vi[3], shader, smooth);
88
89                 nverts.push_back(n);
90         }
91
92         /* create generated coordinates. todo: we should actually get the orco
93          * coordinates from modifiers, for now we use texspace loc/size which
94          * is available in the api. */
95         if(mesh->need_attribute(scene, ATTR_STD_GENERATED)) {
96                 Attribute *attr = mesh->attributes.add(ATTR_STD_GENERATED);
97                 float3 loc = get_float3(b_mesh.texspace_location());
98                 float3 size = get_float3(b_mesh.texspace_size());
99
100                 if(size.x != 0.0f) size.x = 0.5f/size.x;
101                 if(size.y != 0.0f) size.y = 0.5f/size.y;
102                 if(size.z != 0.0f) size.z = 0.5f/size.z;
103
104                 loc = loc*size - make_float3(0.5f, 0.5f, 0.5f);
105
106                 float3 *fdata = attr->data_float3();
107                 size_t i = 0;
108
109                 for(b_mesh.vertices.begin(v); v != b_mesh.vertices.end(); ++v)
110                         fdata[i++] = get_float3(v->co())*size - loc;
111         }
112
113         /* create vertex color attributes */
114         {
115                 BL::Mesh::tessface_vertex_colors_iterator l;
116
117                 for(b_mesh.tessface_vertex_colors.begin(l); l != b_mesh.tessface_vertex_colors.end(); ++l) {
118                         if(!mesh->need_attribute(scene, ustring(l->name().c_str())))
119                                 continue;
120
121                         Attribute *attr = mesh->attributes.add(
122                                 ustring(l->name().c_str()), TypeDesc::TypeColor, Attribute::CORNER);
123
124                         BL::MeshColorLayer::data_iterator c;
125                         float3 *fdata = attr->data_float3();
126                         size_t i = 0;
127
128                         for(l->data.begin(c); c != l->data.end(); ++c, ++i) {
129                                 fdata[0] = color_srgb_to_scene_linear(get_float3(c->color1()));
130                                 fdata[1] = color_srgb_to_scene_linear(get_float3(c->color2()));
131                                 fdata[2] = color_srgb_to_scene_linear(get_float3(c->color3()));
132
133                                 if(nverts[i] == 4) {
134                                         fdata[3] = fdata[0];
135                                         fdata[4] = fdata[2];
136                                         fdata[5] = color_srgb_to_scene_linear(get_float3(c->color4()));
137                                         fdata += 6;
138                                 }
139                                 else
140                                         fdata += 3;
141                         }
142                 }
143         }
144
145         /* create uv map attributes */
146         {
147                 BL::Mesh::tessface_uv_textures_iterator l;
148
149                 for(b_mesh.tessface_uv_textures.begin(l); l != b_mesh.tessface_uv_textures.end(); ++l) {
150                         AttributeStandard std = (l->active_render())? ATTR_STD_UV: ATTR_STD_NONE;
151                         ustring name = ustring(l->name().c_str());
152
153                         if(!(mesh->need_attribute(scene, name) || mesh->need_attribute(scene, std)))
154                                 continue;
155
156                         Attribute *attr;
157
158                         if(l->active_render())
159                                 attr = mesh->attributes.add(std, name);
160                         else
161                                 attr = mesh->attributes.add(name, TypeDesc::TypePoint, Attribute::CORNER);
162
163                         BL::MeshTextureFaceLayer::data_iterator t;
164                         float3 *fdata = attr->data_float3();
165                         size_t i = 0;
166
167                         for(l->data.begin(t); t != l->data.end(); ++t, ++i) {
168                                 fdata[0] =  get_float3(t->uv1());
169                                 fdata[1] =  get_float3(t->uv2());
170                                 fdata[2] =  get_float3(t->uv3());
171                                 fdata += 3;
172
173                                 if(nverts[i] == 4) {
174                                         fdata[0] =  get_float3(t->uv1());
175                                         fdata[1] =  get_float3(t->uv3());
176                                         fdata[2] =  get_float3(t->uv4());
177                                         fdata += 3;
178                                 }
179                         }
180                 }
181         }
182
183         /* create texcoord-based tangent attributes */
184         {
185                 BL::Mesh::tessface_uv_textures_iterator l;
186
187                 for(b_mesh.tessface_uv_textures.begin(l); l != b_mesh.tessface_uv_textures.end(); ++l) {
188                         AttributeStandard std = (l->active_render())? ATTR_STD_TANGENT: ATTR_STD_NONE;
189
190                         if(!mesh->need_attribute(scene, std))
191                                 continue;
192
193                         Attribute *attr = mesh->attributes.add(std, ustring("Tangent"));
194
195                         /* compute average tangents per vertex */
196                         float3 *tangents = attr->data_float3();
197                         memset(tangents, 0, sizeof(float3)*mesh->verts.size());
198
199                         BL::MeshTextureFaceLayer::data_iterator t;
200
201                         size_t fi = 0; /* face index */
202                         b_mesh.tessfaces.begin(f);
203                         for(l->data.begin(t); t != l->data.end() && f != b_mesh.tessfaces.end(); ++t, ++fi, ++f) {
204                                 int4 vi = get_int4(f->vertices_raw());
205
206                                 float3 tx0 = get_float3(t->uv1());
207                                 float3 tx1 = get_float3(t->uv2());
208                                 float3 tx2 = get_float3(t->uv3());
209
210                                 float3 v0 = mesh->verts[vi[0]];
211                                 float3 v1 = mesh->verts[vi[1]];
212                                 float3 v2 = mesh->verts[vi[2]];
213
214                                 /* calculate tangent for the triangle;
215                                  * get vertex positions, and find change in position with respect
216                                  * to the texture coords in the first texture coord dimension */
217                                 float3 tangent0 = tri_calc_tangent(v0, v1, v2, tx0, tx1, tx2);
218
219                                 if(nverts[fi] == 4) {
220                                         /* quad tangent */
221                                         float3 tx3 = get_float3(t->uv4());
222                                         float3 v3 = mesh->verts[vi[3]];
223                                         float3 tangent1 = tri_calc_tangent(v0, v2, v3, tx0, tx2, tx3);
224
225                                         tangents[vi[0]] += 0.5f*(tangent0 + tangent1);
226                                         tangents[vi[1]] += tangent0;
227                                         tangents[vi[2]] += 0.5f*(tangent0 + tangent1);
228                                         tangents[vi[3]] += tangent1;
229                                 }
230                                 else {
231                                         /* triangle tangent */
232                                         tangents[vi[0]] += tangent0;
233                                         tangents[vi[1]] += tangent0;
234                                         tangents[vi[2]] += tangent0;
235                                 }
236                         }
237
238                         /* normalize tangent vectors */
239                         for(int i = 0; i < mesh->verts.size(); i++)
240                                 tangents[i] = normalize(tangents[i]);
241                 }
242         }
243 }
244
245 static void create_subd_mesh(Mesh *mesh, BL::Mesh b_mesh, PointerRNA *cmesh, const vector<uint>& used_shaders)
246 {
247         /* create subd mesh */
248         SubdMesh sdmesh;
249
250         /* create vertices */
251         BL::Mesh::vertices_iterator v;
252
253         for(b_mesh.vertices.begin(v); v != b_mesh.vertices.end(); ++v)
254                 sdmesh.add_vert(get_float3(v->co()));
255
256         /* create faces */
257         BL::Mesh::tessfaces_iterator f;
258
259         for(b_mesh.tessfaces.begin(f); f != b_mesh.tessfaces.end(); ++f) {
260                 int4 vi = get_int4(f->vertices_raw());
261                 int n = (vi[3] == 0) ? 3: 4;
262                 //int shader = used_shaders[f->material_index()];
263
264                 if(n == 4)
265                         sdmesh.add_face(vi[0], vi[1], vi[2], vi[3]);
266 #if 0
267                 else
268                         sdmesh.add_face(vi[0], vi[1], vi[2]);
269 #endif
270         }
271
272         /* finalize subd mesh */
273         sdmesh.link_boundary();
274
275         /* subdivide */
276         DiagSplit dsplit;
277         dsplit.camera = NULL;
278         dsplit.dicing_rate = RNA_float_get(cmesh, "dicing_rate");
279
280         sdmesh.tessellate(&dsplit, false, mesh, used_shaders[0], true);
281 }
282
283 /* Sync */
284
285 Mesh *BlenderSync::sync_mesh(BL::Object b_ob, bool object_updated)
286 {
287         /* test if we can instance or if the object is modified */
288         BL::ID b_ob_data = b_ob.data();
289         BL::ID key = (BKE_object_is_modified(b_ob))? b_ob: b_ob_data;
290         BL::Material material_override = render_layer.material_override;
291
292         /* find shader indices */
293         vector<uint> used_shaders;
294
295         BL::Object::material_slots_iterator slot;
296         for(b_ob.material_slots.begin(slot); slot != b_ob.material_slots.end(); ++slot) {
297                 if(material_override)
298                         find_shader(material_override, used_shaders, scene->default_surface);
299                 else
300                         find_shader(slot->material(), used_shaders, scene->default_surface);
301         }
302
303         if(used_shaders.size() == 0) {
304                 if(material_override)
305                         find_shader(material_override, used_shaders, scene->default_surface);
306                 else
307                         used_shaders.push_back(scene->default_surface);
308         }
309         
310         /* test if we need to sync */
311         Mesh *mesh;
312
313         if(!mesh_map.sync(&mesh, key)) {
314                 /* if transform was applied to mesh, need full update */
315                 if(object_updated && mesh->transform_applied);
316                 /* test if shaders changed, these can be object level so mesh
317                  * does not get tagged for recalc */
318                 else if(mesh->used_shaders != used_shaders);
319                 else {
320                         /* even if not tagged for recalc, we may need to sync anyway
321                          * because the shader needs different mesh attributes */
322                         bool attribute_recalc = false;
323
324                         foreach(uint shader, mesh->used_shaders)
325                                 if(scene->shaders[shader]->need_update_attributes)
326                                         attribute_recalc = true;
327
328                         if(!attribute_recalc)
329                                 return mesh;
330                 }
331         }
332
333         /* ensure we only sync instanced meshes once */
334         if(mesh_synced.find(mesh) != mesh_synced.end())
335                 return mesh;
336         
337         mesh_synced.insert(mesh);
338
339         /* create derived mesh */
340         BL::Mesh b_mesh = object_to_mesh(b_ob, b_scene, true, !preview);
341         PointerRNA cmesh = RNA_pointer_get(&b_ob_data.ptr, "cycles");
342
343         vector<Mesh::Triangle> oldtriangle = mesh->triangles;
344
345         mesh->clear();
346         mesh->used_shaders = used_shaders;
347         mesh->name = ustring(b_ob_data.name().c_str());
348
349         if(b_mesh) {
350                 if(cmesh.data && experimental && RNA_boolean_get(&cmesh, "use_subdivision"))
351                         create_subd_mesh(mesh, b_mesh, &cmesh, used_shaders);
352                 else
353                         create_mesh(scene, mesh, b_mesh, used_shaders);
354
355                 /* free derived mesh */
356                 object_remove_mesh(b_data, b_mesh);
357         }
358
359         /* displacement method */
360         if(cmesh.data) {
361                 int method = RNA_enum_get(&cmesh, "displacement_method");
362
363                 if(method == 0 || !experimental)
364                         mesh->displacement_method = Mesh::DISPLACE_BUMP;
365                 else if(method == 1)
366                         mesh->displacement_method = Mesh::DISPLACE_TRUE;
367                 else
368                         mesh->displacement_method = Mesh::DISPLACE_BOTH;
369         }
370
371         /* tag update */
372         bool rebuild = false;
373
374         if(oldtriangle.size() != mesh->triangles.size())
375                 rebuild = true;
376         else if(oldtriangle.size()) {
377                 if(memcmp(&oldtriangle[0], &mesh->triangles[0], sizeof(Mesh::Triangle)*oldtriangle.size()) != 0)
378                         rebuild = true;
379         }
380         
381         mesh->tag_update(scene, rebuild);
382
383         return mesh;
384 }
385
386 void BlenderSync::sync_mesh_motion(BL::Object b_ob, Mesh *mesh, int motion)
387 {
388         /* todo: displacement, subdivision */
389         size_t size = mesh->verts.size();
390
391         /* skip objects without deforming modifiers. this is not a totally reliable,
392          * would need a more extensive check to see which objects are animated */
393         if(!size || !ccl::BKE_object_is_deform_modified(b_ob, b_scene, preview))
394                 return;
395
396         /* get derived mesh */
397         BL::Mesh b_mesh = object_to_mesh(b_ob, b_scene, true, !preview);
398
399         if(b_mesh) {
400                 BL::Mesh::vertices_iterator v;
401                 AttributeStandard std = (motion == -1)? ATTR_STD_MOTION_PRE: ATTR_STD_MOTION_POST;
402                 Attribute *attr_M = mesh->attributes.add(std);
403                 float3 *M = attr_M->data_float3(), *cur_M;
404                 size_t i = 0;
405
406                 for(b_mesh.vertices.begin(v), cur_M = M; v != b_mesh.vertices.end() && i < size; ++v, cur_M++, i++)
407                         *cur_M = get_float3(v->co());
408
409                 /* if number of vertices changed, or if coordinates stayed the same, drop it */
410                 if(i != size || memcmp(M, &mesh->verts[0], sizeof(float3)*size) == 0)
411                         mesh->attributes.remove(std);
412
413                 /* free derived mesh */
414                 object_remove_mesh(b_data, b_mesh);
415         }
416 }
417
418 CCL_NAMESPACE_END
419