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