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