Cycles: Pass Blender's C++ RNA structures by reference
[blender-staging.git] / intern / cycles / blender / blender_curves.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "attribute.h"
18 #include "camera.h"
19 #include "curves.h"
20 #include "mesh.h"
21 #include "object.h"
22 #include "scene.h"
23
24 #include "blender_sync.h"
25 #include "blender_util.h"
26
27 #include "util_foreach.h"
28 #include "util_logging.h"
29
30 CCL_NAMESPACE_BEGIN
31
32 /* Utilities */
33
34 /* Hair curve functions */
35
36 void curveinterp_v3_v3v3v3v3(float3 *p, float3 *v1, float3 *v2, float3 *v3, float3 *v4, const float w[4]);
37 void interp_weights(float t, float data[4]);
38 float shaperadius(float shape, float root, float tip, float time);
39 void InterpolateKeySegments(int seg, int segno, int key, int curve, float3 *keyloc, float *time, ParticleCurveData *CData);
40 bool ObtainCacheParticleUV(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background, int uv_num);
41 bool ObtainCacheParticleVcol(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background, int vcol_num);
42 bool ObtainCacheParticleData(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background);
43 void ExportCurveSegments(Scene *scene, Mesh *mesh, ParticleCurveData *CData);
44 void ExportCurveTrianglePlanes(Mesh *mesh, ParticleCurveData *CData,
45                                float3 RotCam, bool is_ortho);
46 void ExportCurveTriangleGeometry(Mesh *mesh, ParticleCurveData *CData, int resolution);
47 void ExportCurveTriangleUV(ParticleCurveData *CData, int vert_offset, int resol, float3 *uvdata);
48 void ExportCurveTriangleVcol(ParticleCurveData *CData, int vert_offset, int resol, uchar4 *cdata);
49
50 ParticleCurveData::ParticleCurveData()
51 {
52 }
53
54 ParticleCurveData::~ParticleCurveData()
55 {
56 }
57
58 void interp_weights(float t, float data[4])
59 {
60         /* Cardinal curve interpolation */
61         float t2 = t * t;
62         float t3 = t2 * t;
63         float fc = 0.71f;
64
65         data[0] = -fc          * t3  + 2.0f * fc          * t2 - fc * t;
66         data[1] =  (2.0f - fc) * t3  + (fc - 3.0f)        * t2 + 1.0f;
67         data[2] =  (fc - 2.0f) * t3  + (3.0f - 2.0f * fc) * t2 + fc * t;
68         data[3] =  fc          * t3  - fc * t2;
69 }
70
71 void curveinterp_v3_v3v3v3v3(float3 *p, float3 *v1, float3 *v2, float3 *v3, float3 *v4, const float w[4])
72 {
73         p->x = v1->x * w[0] + v2->x * w[1] + v3->x * w[2] + v4->x * w[3];
74         p->y = v1->y * w[0] + v2->y * w[1] + v3->y * w[2] + v4->y * w[3];
75         p->z = v1->z * w[0] + v2->z * w[1] + v3->z * w[2] + v4->z * w[3];
76 }
77
78 float shaperadius(float shape, float root, float tip, float time)
79 {
80         float radius = 1.0f - time;
81         
82         if(shape != 0.0f) {
83                 if(shape < 0.0f)
84                         radius = powf(radius, 1.0f + shape);
85                 else
86                         radius = powf(radius, 1.0f / (1.0f - shape));
87         }
88         return (radius * (root - tip)) + tip;
89 }
90
91 /* curve functions */
92
93 void InterpolateKeySegments(int seg, int segno, int key, int curve, float3 *keyloc, float *time, ParticleCurveData *CData)
94 {
95         float3 ckey_loc1 = CData->curvekey_co[key];
96         float3 ckey_loc2 = ckey_loc1;
97         float3 ckey_loc3 = CData->curvekey_co[key+1];
98         float3 ckey_loc4 = ckey_loc3;
99
100         if(key > CData->curve_firstkey[curve])
101                 ckey_loc1 = CData->curvekey_co[key - 1];
102
103         if(key < CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 2)
104                 ckey_loc4 = CData->curvekey_co[key + 2];
105
106         float time1 = CData->curvekey_time[key]/CData->curve_length[curve];
107         float time2 = CData->curvekey_time[key + 1]/CData->curve_length[curve];
108
109         float dfra = (time2 - time1) / (float)segno;
110
111         if(time)
112                 *time = (dfra * seg) + time1;
113
114         float t[4];
115
116         interp_weights((float)seg / (float)segno, t);
117
118         if(keyloc)
119                 curveinterp_v3_v3v3v3v3(keyloc, &ckey_loc1, &ckey_loc2, &ckey_loc3, &ckey_loc4, t);
120 }
121
122 bool ObtainCacheParticleData(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background)
123 {
124         int curvenum = 0;
125         int keyno = 0;
126
127         if(!(mesh && b_mesh && b_ob && CData))
128                 return false;
129
130         Transform tfm = get_transform(b_ob->matrix_world());
131         Transform itfm = transform_quick_inverse(tfm);
132
133         BL::Object::modifiers_iterator b_mod;
134         for(b_ob->modifiers.begin(b_mod); b_mod != b_ob->modifiers.end(); ++b_mod) {
135                 if((b_mod->type() == b_mod->type_PARTICLE_SYSTEM) && (background ? b_mod->show_render() : b_mod->show_viewport())) {
136                         BL::ParticleSystemModifier psmd((const PointerRNA)b_mod->ptr);
137                         BL::ParticleSystem b_psys((const PointerRNA)psmd.particle_system().ptr);
138                         BL::ParticleSettings b_part((const PointerRNA)b_psys.settings().ptr);
139
140                         if((b_part.render_type() == BL::ParticleSettings::render_type_PATH) && (b_part.type() == BL::ParticleSettings::type_HAIR)) {
141                                 int mi = clamp(b_part.material()-1, 0, mesh->used_shaders.size()-1);
142                                 int shader = mesh->used_shaders[mi];
143                                 int draw_step = background ? b_part.render_step() : b_part.draw_step();
144                                 int totparts = b_psys.particles.length();
145                                 int totchild = background ? b_psys.child_particles.length() : (int)((float)b_psys.child_particles.length() * (float)b_part.draw_percentage() / 100.0f);
146                                 int totcurves = totchild;
147                                 
148                                 if(b_part.child_type() == 0 || totchild == 0)
149                                         totcurves += totparts;
150
151                                 if(totcurves == 0)
152                                         continue;
153
154                                 int ren_step = (1 << draw_step) + 1;
155                                 if(b_part.kink() == BL::ParticleSettings::kink_SPIRAL)
156                                         ren_step += b_part.kink_extra_steps();
157
158                                 PointerRNA cpsys = RNA_pointer_get(&b_part.ptr, "cycles");
159
160                                 CData->psys_firstcurve.push_back(curvenum);
161                                 CData->psys_curvenum.push_back(totcurves);
162                                 CData->psys_shader.push_back(shader);
163
164                                 float radius = get_float(cpsys, "radius_scale") * 0.5f;
165         
166                                 CData->psys_rootradius.push_back(radius * get_float(cpsys, "root_width"));
167                                 CData->psys_tipradius.push_back(radius * get_float(cpsys, "tip_width"));
168                                 CData->psys_shape.push_back(get_float(cpsys, "shape"));
169                                 CData->psys_closetip.push_back(get_boolean(cpsys, "use_closetip"));
170
171                                 int pa_no = 0;
172                                 if(!(b_part.child_type() == 0) && totchild != 0)
173                                         pa_no = totparts;
174
175                                 int num_add = (totparts+totchild - pa_no);
176                                 CData->curve_firstkey.reserve(CData->curve_firstkey.size() + num_add);
177                                 CData->curve_keynum.reserve(CData->curve_keynum.size() + num_add);
178                                 CData->curve_length.reserve(CData->curve_length.size() + num_add);
179                                 CData->curvekey_co.reserve(CData->curvekey_co.size() + num_add*ren_step);
180                                 CData->curvekey_time.reserve(CData->curvekey_time.size() + num_add*ren_step);
181
182                                 for(; pa_no < totparts+totchild; pa_no++) {
183                                         int keynum = 0;
184                                         CData->curve_firstkey.push_back(keyno);
185                                         
186                                         float curve_length = 0.0f;
187                                         float3 pcKey;
188                                         for(int step_no = 0; step_no < ren_step; step_no++) {
189                                                 float nco[3];
190                                                 b_psys.co_hair(*b_ob, pa_no, step_no, nco);
191                                                 float3 cKey = make_float3(nco[0], nco[1], nco[2]);
192                                                 cKey = transform_point(&itfm, cKey);
193                                                 if(step_no > 0) {
194                                                         float step_length = len(cKey - pcKey);
195                                                         if(step_length == 0.0f)
196                                                                 continue;
197                                                         curve_length += step_length;
198                                                 }
199                                                 CData->curvekey_co.push_back(cKey);
200                                                 CData->curvekey_time.push_back(curve_length);
201                                                 pcKey = cKey;
202                                                 keynum++;
203                                         }
204                                         keyno += keynum;
205
206                                         CData->curve_keynum.push_back(keynum);
207                                         CData->curve_length.push_back(curve_length);
208                                         curvenum++;
209                                 }
210                         }
211                 }
212         }
213
214         return true;
215 }
216
217 bool ObtainCacheParticleUV(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background, int uv_num)
218 {
219         if(!(mesh && b_mesh && b_ob && CData))
220                 return false;
221
222         CData->curve_uv.clear();
223
224         BL::Object::modifiers_iterator b_mod;
225         for(b_ob->modifiers.begin(b_mod); b_mod != b_ob->modifiers.end(); ++b_mod) {
226                 if((b_mod->type() == b_mod->type_PARTICLE_SYSTEM) && (background ? b_mod->show_render() : b_mod->show_viewport())) {
227                         BL::ParticleSystemModifier psmd((const PointerRNA)b_mod->ptr);
228                         BL::ParticleSystem b_psys((const PointerRNA)psmd.particle_system().ptr);
229                         BL::ParticleSettings b_part((const PointerRNA)b_psys.settings().ptr);
230
231                         if((b_part.render_type() == BL::ParticleSettings::render_type_PATH) && (b_part.type() == BL::ParticleSettings::type_HAIR)) {
232                                 int totparts = b_psys.particles.length();
233                                 int totchild = background ? b_psys.child_particles.length() : (int)((float)b_psys.child_particles.length() * (float)b_part.draw_percentage() / 100.0f);
234                                 int totcurves = totchild;
235                                 
236                                 if(b_part.child_type() == 0 || totchild == 0)
237                                         totcurves += totparts;
238
239                                 if(totcurves == 0)
240                                         continue;
241
242                                 int pa_no = 0;
243                                 if(!(b_part.child_type() == 0) && totchild != 0)
244                                         pa_no = totparts;
245
246                                 int num_add = (totparts+totchild - pa_no);
247                                 CData->curve_uv.reserve(CData->curve_uv.size() + num_add);
248
249                                 BL::ParticleSystem::particles_iterator b_pa;
250                                 b_psys.particles.begin(b_pa);
251                                 for(; pa_no < totparts+totchild; pa_no++) {
252                                         /* Add UVs */
253                                         BL::Mesh::tessface_uv_textures_iterator l;
254                                         b_mesh->tessface_uv_textures.begin(l);
255
256                                         float3 uv = make_float3(0.0f, 0.0f, 0.0f);
257                                         if(b_mesh->tessface_uv_textures.length())
258                                                 b_psys.uv_on_emitter(psmd, *b_pa, pa_no, uv_num, &uv.x);
259                                         CData->curve_uv.push_back(uv);
260
261                                         if(pa_no < totparts && b_pa != b_psys.particles.end())
262                                                 ++b_pa;
263                                 }
264                         }
265                 }
266         }
267
268         return true;
269 }
270
271 bool ObtainCacheParticleVcol(Mesh *mesh, BL::Mesh *b_mesh, BL::Object *b_ob, ParticleCurveData *CData, bool background, int vcol_num)
272 {
273         if(!(mesh && b_mesh && b_ob && CData))
274                 return false;
275
276         CData->curve_vcol.clear();
277
278         BL::Object::modifiers_iterator b_mod;
279         for(b_ob->modifiers.begin(b_mod); b_mod != b_ob->modifiers.end(); ++b_mod) {
280                 if((b_mod->type() == b_mod->type_PARTICLE_SYSTEM) && (background ? b_mod->show_render() : b_mod->show_viewport())) {
281                         BL::ParticleSystemModifier psmd((const PointerRNA)b_mod->ptr);
282                         BL::ParticleSystem b_psys((const PointerRNA)psmd.particle_system().ptr);
283                         BL::ParticleSettings b_part((const PointerRNA)b_psys.settings().ptr);
284
285                         if((b_part.render_type() == BL::ParticleSettings::render_type_PATH) && (b_part.type() == BL::ParticleSettings::type_HAIR)) {
286                                 int totparts = b_psys.particles.length();
287                                 int totchild = background ? b_psys.child_particles.length() : (int)((float)b_psys.child_particles.length() * (float)b_part.draw_percentage() / 100.0f);
288                                 int totcurves = totchild;
289                                 
290                                 if(b_part.child_type() == 0 || totchild == 0)
291                                         totcurves += totparts;
292
293                                 if(totcurves == 0)
294                                         continue;
295
296                                 int pa_no = 0;
297                                 if(!(b_part.child_type() == 0) && totchild != 0)
298                                         pa_no = totparts;
299
300                                 int num_add = (totparts+totchild - pa_no);
301                                 CData->curve_vcol.reserve(CData->curve_vcol.size() + num_add);
302
303                                 BL::ParticleSystem::particles_iterator b_pa;
304                                 b_psys.particles.begin(b_pa);
305                                 for(; pa_no < totparts+totchild; pa_no++) {
306                                         /* Add vertex colors */
307                                         BL::Mesh::tessface_vertex_colors_iterator l;
308                                         b_mesh->tessface_vertex_colors.begin(l);
309
310                                         float3 vcol = make_float3(0.0f, 0.0f, 0.0f);
311                                         if(b_mesh->tessface_vertex_colors.length())
312                                                 b_psys.mcol_on_emitter(psmd, *b_pa, pa_no, vcol_num, &vcol.x);
313                                         CData->curve_vcol.push_back(vcol);
314
315                                         if(pa_no < totparts && b_pa != b_psys.particles.end())
316                                                 ++b_pa;
317                                 }
318                         }
319                 }
320         }
321
322         return true;
323 }
324
325 static void set_resolution(BL::Object *b_ob, BL::Scene *scene, bool render)
326 {
327         BL::Object::modifiers_iterator b_mod;
328         for(b_ob->modifiers.begin(b_mod); b_mod != b_ob->modifiers.end(); ++b_mod) {
329                 if((b_mod->type() == b_mod->type_PARTICLE_SYSTEM) && ((b_mod->show_viewport()) || (b_mod->show_render()))) {
330                         BL::ParticleSystemModifier psmd((const PointerRNA)b_mod->ptr);
331                         BL::ParticleSystem b_psys((const PointerRNA)psmd.particle_system().ptr);
332                         b_psys.set_resolution(*scene, *b_ob, (render)? 2: 1);
333                 }
334         }
335 }
336
337 void ExportCurveTrianglePlanes(Mesh *mesh, ParticleCurveData *CData,
338                                float3 RotCam, bool is_ortho)
339 {
340         int vertexno = mesh->verts.size();
341         int vertexindex = vertexno;
342         int numverts = 0, numtris = 0;
343
344         /* compute and reserve size of arrays */
345         for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
346                 for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
347                         if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
348                                 continue;
349
350                         numverts += 2 + (CData->curve_keynum[curve] - 1)*2;
351                         numtris += (CData->curve_keynum[curve] - 1)*2;
352                 }
353         }
354
355         mesh->verts.reserve(mesh->verts.size() + numverts);
356         mesh->triangles.reserve(mesh->triangles.size() + numtris);
357         mesh->shader.reserve(mesh->shader.size() + numtris);
358         mesh->smooth.reserve(mesh->smooth.size() + numtris);
359
360         /* actually export */
361         for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
362                 for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
363                         if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
364                                 continue;
365
366                         float3 xbasis;
367                         float3 v1;
368                         float time = 0.0f;
369                         float3 ickey_loc = CData->curvekey_co[CData->curve_firstkey[curve]];
370                         float radius = shaperadius(CData->psys_shape[sys], CData->psys_rootradius[sys], CData->psys_tipradius[sys], 0.0f);
371                         v1 = CData->curvekey_co[CData->curve_firstkey[curve] + 1] - CData->curvekey_co[CData->curve_firstkey[curve]];
372                         if(is_ortho)
373                                 xbasis = normalize(cross(RotCam, v1));
374                         else
375                                 xbasis = normalize(cross(RotCam - ickey_loc, v1));
376                         float3 ickey_loc_shfl = ickey_loc - radius * xbasis;
377                         float3 ickey_loc_shfr = ickey_loc + radius * xbasis;
378                         mesh->verts.push_back(ickey_loc_shfl);
379                         mesh->verts.push_back(ickey_loc_shfr);
380                         vertexindex += 2;
381
382                         for(int curvekey = CData->curve_firstkey[curve] + 1; curvekey < CData->curve_firstkey[curve] + CData->curve_keynum[curve]; curvekey++) {
383                                 ickey_loc = CData->curvekey_co[curvekey];
384
385                                 if(curvekey == CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1)
386                                         v1 = CData->curvekey_co[curvekey] - CData->curvekey_co[max(curvekey - 1, CData->curve_firstkey[curve])];
387                                 else 
388                                         v1 = CData->curvekey_co[curvekey + 1] - CData->curvekey_co[curvekey - 1];
389
390                                 time = CData->curvekey_time[curvekey]/CData->curve_length[curve];
391                                 radius = shaperadius(CData->psys_shape[sys], CData->psys_rootradius[sys], CData->psys_tipradius[sys], time);
392
393                                 if(curvekey == CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1)
394                                         radius = shaperadius(CData->psys_shape[sys], CData->psys_rootradius[sys], CData->psys_tipradius[sys], 0.95f);
395
396                                 if(CData->psys_closetip[sys] && (curvekey == CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1))
397                                         radius = shaperadius(CData->psys_shape[sys], CData->psys_rootradius[sys], 0.0f, 0.95f);
398
399                                 if(is_ortho)
400                                         xbasis = normalize(cross(RotCam, v1));
401                                 else
402                                         xbasis = normalize(cross(RotCam - ickey_loc, v1));
403                                 float3 ickey_loc_shfl = ickey_loc - radius * xbasis;
404                                 float3 ickey_loc_shfr = ickey_loc + radius * xbasis;
405                                 mesh->verts.push_back(ickey_loc_shfl);
406                                 mesh->verts.push_back(ickey_loc_shfr);
407                                 mesh->add_triangle(vertexindex-2, vertexindex, vertexindex-1, CData->psys_shader[sys], true);
408                                 mesh->add_triangle(vertexindex+1, vertexindex-1, vertexindex, CData->psys_shader[sys], true);
409                                 vertexindex += 2;
410                         }
411                 }
412         }
413
414         mesh->reserve(mesh->verts.size(), mesh->triangles.size(), 0, 0);
415         mesh->attributes.remove(ATTR_STD_VERTEX_NORMAL);
416         mesh->attributes.remove(ATTR_STD_FACE_NORMAL);
417         mesh->add_face_normals();
418         mesh->add_vertex_normals();
419         mesh->attributes.remove(ATTR_STD_FACE_NORMAL);
420
421         /* texture coords still needed */
422 }
423
424 void ExportCurveTriangleGeometry(Mesh *mesh, ParticleCurveData *CData, int resolution)
425 {
426         int vertexno = mesh->verts.size();
427         int vertexindex = vertexno;
428         int numverts = 0, numtris = 0;
429
430         /* compute and reserve size of arrays */
431         for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
432                 for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
433                         if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
434                                 continue;
435
436                         numverts += (CData->curve_keynum[curve] - 2)*2*resolution + resolution;
437                         numtris += (CData->curve_keynum[curve] - 2)*resolution;
438                 }
439         }
440
441         mesh->verts.reserve(mesh->verts.size() + numverts);
442         mesh->triangles.reserve(mesh->triangles.size() + numtris);
443         mesh->shader.reserve(mesh->shader.size() + numtris);
444         mesh->smooth.reserve(mesh->smooth.size() + numtris);
445
446         /* actually export */
447         for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
448                 for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
449                         if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
450                                 continue;
451
452                         float3 firstxbasis = cross(make_float3(1.0f,0.0f,0.0f),CData->curvekey_co[CData->curve_firstkey[curve]+1] - CData->curvekey_co[CData->curve_firstkey[curve]]);
453                         if(!is_zero(firstxbasis))
454                                 firstxbasis = normalize(firstxbasis);
455                         else
456                                 firstxbasis = normalize(cross(make_float3(0.0f,1.0f,0.0f),CData->curvekey_co[CData->curve_firstkey[curve]+1] - CData->curvekey_co[CData->curve_firstkey[curve]]));
457
458                         for(int curvekey = CData->curve_firstkey[curve]; curvekey < CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1; curvekey++) {
459                                 float3 xbasis = firstxbasis;
460                                 float3 v1;
461                                 float3 v2;
462
463                                 if(curvekey == CData->curve_firstkey[curve]) {
464                                         v1 = CData->curvekey_co[min(curvekey+2,CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1)] - CData->curvekey_co[curvekey+1];
465                                         v2 = CData->curvekey_co[curvekey+1] - CData->curvekey_co[curvekey];
466                                 }
467                                 else if(curvekey == CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1) {
468                                         v1 = CData->curvekey_co[curvekey] - CData->curvekey_co[curvekey-1];
469                                         v2 = CData->curvekey_co[curvekey-1] - CData->curvekey_co[max(curvekey-2,CData->curve_firstkey[curve])];
470                                 }
471                                 else {
472                                         v1 = CData->curvekey_co[curvekey+1] - CData->curvekey_co[curvekey];
473                                         v2 = CData->curvekey_co[curvekey] - CData->curvekey_co[curvekey-1];
474                                 }
475
476                                 xbasis = cross(v1, v2);
477
478                                 if(len_squared(xbasis) >= 0.05f * len_squared(v1) * len_squared(v2)) {
479                                         firstxbasis = normalize(xbasis);
480                                         break;
481                                 }
482                         }
483
484                         for(int curvekey = CData->curve_firstkey[curve]; curvekey < CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1; curvekey++) {
485                                 int subv = 1;
486                                 float3 xbasis;
487                                 float3 ybasis;
488                                 float3 v1;
489                                 float3 v2;
490
491                                 if(curvekey == CData->curve_firstkey[curve]) {
492                                         subv = 0;
493                                         v1 = CData->curvekey_co[min(curvekey+2,CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1)] - CData->curvekey_co[curvekey+1];
494                                         v2 = CData->curvekey_co[curvekey+1] - CData->curvekey_co[curvekey];
495                                 }
496                                 else if(curvekey == CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1) {
497                                         v1 = CData->curvekey_co[curvekey] - CData->curvekey_co[curvekey-1];
498                                         v2 = CData->curvekey_co[curvekey-1] - CData->curvekey_co[max(curvekey-2,CData->curve_firstkey[curve])];
499                                 }
500                                 else {
501                                         v1 = CData->curvekey_co[curvekey+1] - CData->curvekey_co[curvekey];
502                                         v2 = CData->curvekey_co[curvekey] - CData->curvekey_co[curvekey-1];
503                                 }
504
505                                 xbasis = cross(v1, v2);
506
507                                 if(len_squared(xbasis) >= 0.05f * len_squared(v1) * len_squared(v2)) {
508                                         xbasis = normalize(xbasis);
509                                         firstxbasis = xbasis;
510                                 }
511                                 else
512                                         xbasis = firstxbasis;
513
514                                 ybasis = normalize(cross(xbasis, v2));
515
516                                 for(; subv <= 1; subv++) {
517                                         float3 ickey_loc = make_float3(0.0f,0.0f,0.0f);
518                                         float time = 0.0f;
519
520                                         InterpolateKeySegments(subv, 1, curvekey, curve, &ickey_loc, &time, CData);
521
522                                         float radius = shaperadius(CData->psys_shape[sys], CData->psys_rootradius[sys], CData->psys_tipradius[sys], time);
523
524                                         if((curvekey == CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 2) && (subv == 1))
525                                                 radius = shaperadius(CData->psys_shape[sys], CData->psys_rootradius[sys], CData->psys_tipradius[sys], 0.95f);
526
527                                         if(CData->psys_closetip[sys] && (subv == 1) && (curvekey == CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 2))
528                                                 radius = shaperadius(CData->psys_shape[sys], CData->psys_rootradius[sys], 0.0f, 0.95f);
529
530                                         float angle = M_2PI_F / (float)resolution;
531                                         for(int section = 0; section < resolution; section++) {
532                                                 float3 ickey_loc_shf = ickey_loc + radius * (cosf(angle * section) * xbasis + sinf(angle * section) * ybasis);
533                                                 mesh->verts.push_back(ickey_loc_shf);
534                                         }
535
536                                         if(subv != 0) {
537                                                 for(int section = 0; section < resolution - 1; section++) {
538                                                         mesh->add_triangle(vertexindex - resolution + section, vertexindex + section, vertexindex - resolution + section + 1, CData->psys_shader[sys], true);
539                                                         mesh->add_triangle(vertexindex + section + 1, vertexindex - resolution + section + 1, vertexindex + section, CData->psys_shader[sys], true);
540                                                 }
541                                                 mesh->add_triangle(vertexindex-1, vertexindex + resolution - 1, vertexindex - resolution, CData->psys_shader[sys], true);
542                                                 mesh->add_triangle(vertexindex, vertexindex - resolution , vertexindex + resolution - 1, CData->psys_shader[sys], true);
543                                         }
544                                         vertexindex += resolution;
545                                 }
546                         }
547                 }
548         }
549
550         mesh->reserve(mesh->verts.size(), mesh->triangles.size(), 0, 0);
551         mesh->attributes.remove(ATTR_STD_VERTEX_NORMAL);
552         mesh->attributes.remove(ATTR_STD_FACE_NORMAL);
553         mesh->add_face_normals();
554         mesh->add_vertex_normals();
555         mesh->attributes.remove(ATTR_STD_FACE_NORMAL);
556
557         /* texture coords still needed */
558 }
559
560 void ExportCurveSegments(Scene *scene, Mesh *mesh, ParticleCurveData *CData)
561 {
562         int num_keys = 0;
563         int num_curves = 0;
564
565         if(!(mesh->curves.empty() && mesh->curve_keys.empty()))
566                 return;
567
568         Attribute *attr_intercept = NULL;
569         
570         if(mesh->need_attribute(scene, ATTR_STD_CURVE_INTERCEPT))
571                 attr_intercept = mesh->curve_attributes.add(ATTR_STD_CURVE_INTERCEPT);
572
573         /* compute and reserve size of arrays */
574         for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
575                 for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
576                         if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
577                                 continue;
578
579                         num_keys += CData->curve_keynum[curve];
580                         num_curves++;
581                 }
582         }
583
584         if(num_curves > 0) {
585                 VLOG(1) << "Exporting curve segments for mesh " << mesh->name;
586         }
587
588         mesh->curve_keys.reserve(mesh->curve_keys.size() + num_keys);
589         mesh->curves.reserve(mesh->curves.size() + num_curves);
590
591         num_keys = 0;
592         num_curves = 0;
593
594         /* actually export */
595         for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
596                 for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
597                         if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
598                                 continue;
599
600                         size_t num_curve_keys = 0;
601
602                         for(int curvekey = CData->curve_firstkey[curve]; curvekey < CData->curve_firstkey[curve] + CData->curve_keynum[curve]; curvekey++) {
603                                 float3 ickey_loc = CData->curvekey_co[curvekey];
604                                 float time = CData->curvekey_time[curvekey]/CData->curve_length[curve];
605                                 float radius = shaperadius(CData->psys_shape[sys], CData->psys_rootradius[sys], CData->psys_tipradius[sys], time);
606
607                                 if(CData->psys_closetip[sys] && (curvekey == CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1))
608                                         radius = 0.0f;
609
610                                 mesh->add_curve_key(ickey_loc, radius);
611                                 if(attr_intercept)
612                                         attr_intercept->add(time);
613
614                                 num_curve_keys++;
615                         }
616
617                         mesh->add_curve(num_keys, num_curve_keys, CData->psys_shader[sys]);
618                         num_keys += num_curve_keys;
619                         num_curves++;
620                 }
621         }
622
623         /* check allocation */
624         if((mesh->curve_keys.size() != num_keys) || (mesh->curves.size() != num_curves)) {
625                 VLOG(1) << "Allocation failed, clearing data";
626                 mesh->curve_keys.clear();
627                 mesh->curves.clear();
628                 mesh->curve_attributes.clear();
629         }
630 }
631
632 static void ExportCurveSegmentsMotion(Mesh *mesh, ParticleCurveData *CData, int time_index)
633 {
634         VLOG(1) << "Exporting curve motion segments for mesh " << mesh->name
635                 << ", time index " << time_index;
636
637         /* find attribute */
638         Attribute *attr_mP = mesh->curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
639         bool new_attribute = false;
640
641         /* add new attribute if it doesn't exist already */
642         if(!attr_mP) {
643                 VLOG(1) << "Creating new motion vertex position attribute";
644                 attr_mP = mesh->curve_attributes.add(ATTR_STD_MOTION_VERTEX_POSITION);
645                 new_attribute = true;
646         }
647
648         /* export motion vectors for curve keys */
649         size_t numkeys = mesh->curve_keys.size();
650         float4 *mP = attr_mP->data_float4() + time_index*numkeys;
651         bool have_motion = false;
652         int i = 0;
653
654         for(int sys = 0; sys < CData->psys_firstcurve.size(); sys++) {
655                 if(CData->psys_curvenum[sys] == 0)
656                         continue;
657
658                 for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys]; curve++) {
659                         if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
660                                 continue;
661
662                         for(int curvekey = CData->curve_firstkey[curve]; curvekey < CData->curve_firstkey[curve] + CData->curve_keynum[curve]; curvekey++) {
663                                 if(i < mesh->curve_keys.size()) {
664                                         float3 ickey_loc = CData->curvekey_co[curvekey];
665                                         float time = CData->curvekey_time[curvekey]/CData->curve_length[curve];
666                                         float radius = shaperadius(CData->psys_shape[sys], CData->psys_rootradius[sys], CData->psys_tipradius[sys], time);
667
668                                         if(CData->psys_closetip[sys] && (curvekey == CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1))
669                                                 radius = 0.0f;
670
671                                         mP[i] = float3_to_float4(ickey_loc);
672                                         mP[i].w = radius;
673
674                                         /* unlike mesh coordinates, these tend to be slightly different
675                                          * between frames due to particle transforms into/out of object
676                                          * space, so we use an epsilon to detect actual changes */
677                                         if(len_squared(mP[i] - mesh->curve_keys[i]) > 1e-5f*1e-5f)
678                                                 have_motion = true;
679                                 }
680
681                                 i++;
682                         }
683                 }
684         }
685
686         /* in case of new attribute, we verify if there really was any motion */
687         if(new_attribute) {
688                 if(i != numkeys || !have_motion) {
689                         /* no motion, remove attributes again */
690                         VLOG(1) << "No motion, removing attribute";
691                         mesh->curve_attributes.remove(ATTR_STD_MOTION_VERTEX_POSITION);
692                 }
693                 else if(time_index > 0) {
694                         VLOG(1) << "Filling in new motion vertex position for time_index "
695                                 << time_index;
696                         /* motion, fill up previous steps that we might have skipped because
697                          * they had no motion, but we need them anyway now */
698                         for(int step = 0; step < time_index; step++) {
699                                 float4 *mP = attr_mP->data_float4() + step*numkeys;
700
701                                 for(int key = 0; key < numkeys; key++)
702                                         mP[key] = mesh->curve_keys[key];
703                         }
704                 }
705         }
706 }
707
708 void ExportCurveTriangleUV(ParticleCurveData *CData, int vert_offset, int resol, float3 *uvdata)
709 {
710         if(uvdata == NULL)
711                 return;
712
713         float time = 0.0f;
714         float prevtime = 0.0f;
715
716         int vertexindex = vert_offset;
717
718         for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
719                 for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
720                         if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
721                                 continue;
722
723                         for(int curvekey = CData->curve_firstkey[curve]; curvekey < CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1; curvekey++) {
724                                 time = CData->curvekey_time[curvekey]/CData->curve_length[curve];
725
726                                 for(int section = 0; section < resol; section++) {
727                                         uvdata[vertexindex] = CData->curve_uv[curve];
728                                         uvdata[vertexindex].z = prevtime;
729                                         vertexindex++;
730                                         uvdata[vertexindex] = CData->curve_uv[curve];
731                                         uvdata[vertexindex].z = time;
732                                         vertexindex++;
733                                         uvdata[vertexindex] = CData->curve_uv[curve];
734                                         uvdata[vertexindex].z = prevtime;
735                                         vertexindex++;
736                                         uvdata[vertexindex] = CData->curve_uv[curve];
737                                         uvdata[vertexindex].z = time;
738                                         vertexindex++;
739                                         uvdata[vertexindex] = CData->curve_uv[curve];
740                                         uvdata[vertexindex].z = prevtime;
741                                         vertexindex++;
742                                         uvdata[vertexindex] = CData->curve_uv[curve];
743                                         uvdata[vertexindex].z = time;
744                                         vertexindex++;
745                                 }
746
747                                 prevtime = time;
748                         }
749                 }
750         }
751 }
752
753 void ExportCurveTriangleVcol(ParticleCurveData *CData, int vert_offset, int resol, uchar4 *cdata)
754 {
755         if(cdata == NULL)
756                 return;
757
758         int vertexindex = vert_offset;
759
760         for(int sys = 0; sys < CData->psys_firstcurve.size() ; sys++) {
761                 for(int curve = CData->psys_firstcurve[sys]; curve < CData->psys_firstcurve[sys] + CData->psys_curvenum[sys] ; curve++) {
762                         if(CData->curve_keynum[curve] <= 1 || CData->curve_length[curve] == 0.0f)
763                                 continue;
764
765                         for(int curvekey = CData->curve_firstkey[curve]; curvekey < CData->curve_firstkey[curve] + CData->curve_keynum[curve] - 1; curvekey++) {
766                                 for(int section = 0; section < resol; section++) {
767                                         cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
768                                         vertexindex++;
769                                         cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
770                                         vertexindex++;
771                                         cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
772                                         vertexindex++;
773                                         cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
774                                         vertexindex++;
775                                         cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
776                                         vertexindex++;
777                                         cdata[vertexindex] = color_float_to_byte(color_srgb_to_scene_linear(CData->curve_vcol[curve]));
778                                         vertexindex++;
779                                 }
780                         }
781                 }
782         }
783 }
784
785 /* Hair Curve Sync */
786
787 void BlenderSync::sync_curve_settings()
788 {
789         PointerRNA csscene = RNA_pointer_get(&b_scene.ptr, "cycles_curves");
790
791         CurveSystemManager *curve_system_manager = scene->curve_system_manager;
792         CurveSystemManager prev_curve_system_manager = *curve_system_manager;
793
794         curve_system_manager->use_curves = get_boolean(csscene, "use_curves");
795         curve_system_manager->minimum_width = get_float(csscene, "minimum_width");
796         curve_system_manager->maximum_width = get_float(csscene, "maximum_width");
797
798         curve_system_manager->primitive = get_enum(csscene, "primitive");
799         curve_system_manager->curve_shape = get_enum(csscene, "shape");
800         curve_system_manager->resolution = get_int(csscene, "resolution");
801         curve_system_manager->subdivisions = get_int(csscene, "subdivisions");
802         curve_system_manager->use_backfacing = !get_boolean(csscene, "cull_backfacing");
803
804         /* Triangles */
805         if(curve_system_manager->primitive == CURVE_TRIANGLES) {
806                 /* camera facing planes */
807                 if(curve_system_manager->curve_shape == CURVE_RIBBON) {
808                         curve_system_manager->triangle_method = CURVE_CAMERA_TRIANGLES;
809                         curve_system_manager->resolution = 1;
810                 }
811                 else if(curve_system_manager->curve_shape == CURVE_THICK) {
812                         curve_system_manager->triangle_method = CURVE_TESSELATED_TRIANGLES;
813                 }
814         }
815         /* Line Segments */
816         else if(curve_system_manager->primitive == CURVE_LINE_SEGMENTS) {
817                 if(curve_system_manager->curve_shape == CURVE_RIBBON) {
818                         /* tangent shading */
819                         curve_system_manager->line_method = CURVE_UNCORRECTED;
820                         curve_system_manager->use_encasing = true;
821                         curve_system_manager->use_backfacing = false;
822                         curve_system_manager->use_tangent_normal_geometry = true;
823                 }
824                 else if(curve_system_manager->curve_shape == CURVE_THICK) {
825                         curve_system_manager->line_method = CURVE_ACCURATE;
826                         curve_system_manager->use_encasing = false;
827                         curve_system_manager->use_tangent_normal_geometry = false;
828                 }
829         }
830         /* Curve Segments */
831         else if(curve_system_manager->primitive == CURVE_SEGMENTS) {
832                 if(curve_system_manager->curve_shape == CURVE_RIBBON) {
833                         curve_system_manager->primitive = CURVE_RIBBONS;
834                         curve_system_manager->use_backfacing = false;
835                 }
836         }
837
838         if(curve_system_manager->modified_mesh(prev_curve_system_manager)) {
839                 BL::BlendData::objects_iterator b_ob;
840
841                 for(b_data.objects.begin(b_ob); b_ob != b_data.objects.end(); ++b_ob) {
842                         if(object_is_mesh(*b_ob)) {
843                                 BL::Object::particle_systems_iterator b_psys;
844                                 for(b_ob->particle_systems.begin(b_psys); b_psys != b_ob->particle_systems.end(); ++b_psys) {
845                                         if((b_psys->settings().render_type()==BL::ParticleSettings::render_type_PATH)&&(b_psys->settings().type()==BL::ParticleSettings::type_HAIR)) {
846                                                 BL::ID key = BKE_object_is_modified(*b_ob)? *b_ob: b_ob->data();
847                                                 mesh_map.set_recalc(key);
848                                                 object_map.set_recalc(*b_ob);
849                                         }
850                                 }
851                         }
852                 }
853         }
854
855         if(curve_system_manager->modified(prev_curve_system_manager))
856                 curve_system_manager->tag_update(scene);
857 }
858
859 void BlenderSync::sync_curves(Mesh *mesh,
860                               BL::Mesh& b_mesh,
861                               BL::Object& b_ob,
862                               bool motion,
863                               int time_index)
864 {
865         if(!motion) {
866                 /* Clear stored curve data */
867                 mesh->curve_keys.clear();
868                 mesh->curves.clear();
869                 mesh->curve_attributes.clear();
870         }
871
872         /* obtain general settings */
873         bool use_curves = scene->curve_system_manager->use_curves;
874
875         if(!(use_curves && b_ob.mode() != b_ob.mode_PARTICLE_EDIT)) {
876                 if(!motion)
877                         mesh->compute_bounds();
878                 return;
879         }
880
881         int primitive = scene->curve_system_manager->primitive;
882         int triangle_method = scene->curve_system_manager->triangle_method;
883         int resolution = scene->curve_system_manager->resolution;
884         size_t vert_num = mesh->verts.size();
885         size_t tri_num = mesh->triangles.size();
886         int used_res = 1;
887
888         /* extract particle hair data - should be combined with connecting to mesh later*/
889
890         ParticleCurveData CData;
891
892         if(!preview)
893                 set_resolution(&b_ob, &b_scene, true);
894
895         ObtainCacheParticleData(mesh, &b_mesh, &b_ob, &CData, !preview);
896
897         /* add hair geometry to mesh */
898         if(primitive == CURVE_TRIANGLES) {
899                 if(triangle_method == CURVE_CAMERA_TRIANGLES) {
900                         /* obtain camera parameters */
901                         float3 RotCam;
902                         Camera *camera = scene->camera;
903                         Transform &ctfm = camera->matrix;
904                         if(camera->type == CAMERA_ORTHOGRAPHIC) {
905                                 RotCam = -make_float3(ctfm.x.z, ctfm.y.z, ctfm.z.z);
906                         }
907                         else {
908                                 Transform tfm = get_transform(b_ob.matrix_world());
909                                 Transform itfm = transform_quick_inverse(tfm);
910                                 RotCam = transform_point(&itfm, make_float3(ctfm.x.w,
911                                                                             ctfm.y.w,
912                                                                             ctfm.z.w));
913                         }
914                         bool is_ortho = camera->type == CAMERA_ORTHOGRAPHIC;
915                         ExportCurveTrianglePlanes(mesh, &CData, RotCam, is_ortho);
916                 }
917                 else {
918                         ExportCurveTriangleGeometry(mesh, &CData, resolution);
919                         used_res = resolution;
920                 }
921         }
922         else {
923                 if(motion)
924                         ExportCurveSegmentsMotion(mesh, &CData, time_index);
925                 else
926                         ExportCurveSegments(scene, mesh, &CData);
927         }
928
929         /* generated coordinates from first key. we should ideally get this from
930          * blender to handle deforming objects */
931         if(!motion) {
932                 if(mesh->need_attribute(scene, ATTR_STD_GENERATED)) {
933                         float3 loc, size;
934                         mesh_texture_space(b_mesh, loc, size);
935
936                         if(primitive == CURVE_TRIANGLES) {
937                                 Attribute *attr_generated = mesh->attributes.add(ATTR_STD_GENERATED);
938                                 float3 *generated = attr_generated->data_float3();
939
940                                 for(size_t i = vert_num; i < mesh->verts.size(); i++)
941                                         generated[i] = mesh->verts[i]*size - loc;
942                         }
943                         else {
944                                 Attribute *attr_generated = mesh->curve_attributes.add(ATTR_STD_GENERATED);
945                                 float3 *generated = attr_generated->data_float3();
946                                 size_t i = 0;
947
948                                 foreach(Mesh::Curve& curve, mesh->curves) {
949                                         float3 co = float4_to_float3(mesh->curve_keys[curve.first_key]);
950                                         generated[i++] = co*size - loc;
951                                 }
952                         }
953                 }
954         }
955
956         /* create vertex color attributes */
957         if(!motion) {
958                 BL::Mesh::tessface_vertex_colors_iterator l;
959                 int vcol_num = 0;
960
961                 for(b_mesh.tessface_vertex_colors.begin(l); l != b_mesh.tessface_vertex_colors.end(); ++l, vcol_num++) {
962                         if(!mesh->need_attribute(scene, ustring(l->name().c_str())))
963                                 continue;
964
965                         ObtainCacheParticleVcol(mesh, &b_mesh, &b_ob, &CData, !preview, vcol_num);
966
967                         if(primitive == CURVE_TRIANGLES) {
968                                 Attribute *attr_vcol = mesh->attributes.add(
969                                         ustring(l->name().c_str()), TypeDesc::TypeColor, ATTR_ELEMENT_CORNER_BYTE);
970
971                                 uchar4 *cdata = attr_vcol->data_uchar4();
972
973                                 ExportCurveTriangleVcol(&CData, tri_num * 3, used_res, cdata);
974                         }
975                         else {
976                                 Attribute *attr_vcol = mesh->curve_attributes.add(
977                                         ustring(l->name().c_str()), TypeDesc::TypeColor, ATTR_ELEMENT_CURVE);
978
979                                 float3 *fdata = attr_vcol->data_float3();
980
981                                 if(fdata) {
982                                         size_t i = 0;
983
984                                         for(size_t curve = 0; curve < CData.curve_vcol.size(); curve++)
985                                                 if(!(CData.curve_keynum[curve] <= 1 || CData.curve_length[curve] == 0.0f))
986                                                         fdata[i++] = color_srgb_to_scene_linear(CData.curve_vcol[curve]);
987                                 }
988                         }
989                 }
990         }
991
992         /* create UV attributes */
993         if(!motion) {
994                 BL::Mesh::tessface_uv_textures_iterator l;
995                 int uv_num = 0;
996
997                 for(b_mesh.tessface_uv_textures.begin(l); l != b_mesh.tessface_uv_textures.end(); ++l, uv_num++) {
998                         bool active_render = l->active_render();
999                         AttributeStandard std = (active_render)? ATTR_STD_UV: ATTR_STD_NONE;
1000                         ustring name = ustring(l->name().c_str());
1001
1002                         /* UV map */
1003                         if(mesh->need_attribute(scene, name) || mesh->need_attribute(scene, std)) {
1004                                 Attribute *attr_uv;
1005
1006                                 ObtainCacheParticleUV(mesh, &b_mesh, &b_ob, &CData, !preview, uv_num);
1007
1008                                 if(primitive == CURVE_TRIANGLES) {
1009                                         if(active_render)
1010                                                 attr_uv = mesh->attributes.add(std, name);
1011                                         else
1012                                                 attr_uv = mesh->attributes.add(name, TypeDesc::TypePoint, ATTR_ELEMENT_CORNER);
1013
1014                                         float3 *uv = attr_uv->data_float3();
1015
1016                                         ExportCurveTriangleUV(&CData, tri_num * 3, used_res, uv);
1017                                 }
1018                                 else {
1019                                         if(active_render)
1020                                                 attr_uv = mesh->curve_attributes.add(std, name);
1021                                         else
1022                                                 attr_uv = mesh->curve_attributes.add(name, TypeDesc::TypePoint,  ATTR_ELEMENT_CURVE);
1023
1024                                         float3 *uv = attr_uv->data_float3();
1025
1026                                         if(uv) {
1027                                                 size_t i = 0;
1028
1029                                                 for(size_t curve = 0; curve < CData.curve_uv.size(); curve++)
1030                                                         if(!(CData.curve_keynum[curve] <= 1 || CData.curve_length[curve] == 0.0f))
1031                                                                 uv[i++] = CData.curve_uv[curve];
1032                                         }
1033                                 }
1034                         }
1035                 }
1036         }
1037
1038         if(!preview)
1039                 set_resolution(&b_ob, &b_scene, false);
1040
1041         mesh->compute_bounds();
1042 }
1043
1044 CCL_NAMESPACE_END
1045