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