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