Depsgraph: remove EvaluationContext, pass Depsgraph instead.
[blender.git] / source / blender / render / intern / source / convertblender.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributors: 2004/2005/2006 Blender Foundation, full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/render/intern/source/convertblender.c
27  *  \ingroup render
28  */
29
30 #include <math.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <limits.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_math.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_utildefines.h"
41 #include "BLI_rand.h"
42 #include "BLI_memarena.h"
43 #ifdef WITH_FREESTYLE
44 #  include "BLI_edgehash.h"
45 #endif
46
47 #include "BLT_translation.h"
48
49 #include "DNA_material_types.h"
50 #include "DNA_curve_types.h"
51 #include "DNA_group_types.h"
52 #include "DNA_lamp_types.h"
53 #include "DNA_image_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_meshdata_types.h"
56 #include "DNA_modifier_types.h"
57 #include "DNA_node_types.h"
58 #include "DNA_object_types.h"
59 #include "DNA_object_fluidsim_types.h"
60 #include "DNA_particle_types.h"
61 #include "DNA_scene_types.h"
62 #include "DNA_texture_types.h"
63
64 #include "BKE_anim.h"
65 #include "BKE_curve.h"
66 #include "BKE_customdata.h"
67 #include "BKE_colortools.h"
68 #include "BKE_displist.h"
69 #include "BKE_DerivedMesh.h"
70 #include "BKE_global.h"
71 #include "BKE_group.h"
72 #include "BKE_key.h"
73 #include "BKE_image.h"
74 #include "BKE_lattice.h"
75 #include "BKE_layer.h"
76 #include "BKE_material.h"
77 #include "BKE_main.h"
78 #include "BKE_mball.h"
79 #include "BKE_mesh.h"
80 #include "BKE_modifier.h"
81 #include "BKE_node.h"
82 #include "BKE_object.h"
83 #include "BKE_particle.h"
84 #include "BKE_scene.h"
85
86 #include "DEG_depsgraph.h"
87 #include "DEG_depsgraph_build.h"
88
89 #include "PIL_time.h"
90
91 #include "envmap.h"
92 #include "occlusion.h"
93 #include "pointdensity.h"
94 #include "voxeldata.h"
95 #include "render_types.h"
96 #include "rendercore.h"
97 #include "renderdatabase.h"
98 #include "renderpipeline.h"
99 #include "shadbuf.h"
100 #include "shading.h"
101 #include "strand.h"
102 #include "texture.h"
103 #include "volume_precache.h"
104 #include "sss.h"
105 #include "zbuf.h"
106 #include "sunsky.h"
107
108 /* 10 times larger than normal epsilon, test it on default nurbs sphere with ray_transp (for quad detection) */
109 /* or for checking vertex normal flips */
110 #define FLT_EPSILON10 1.19209290e-06F
111
112 /* could enable at some point but for now there are far too many conversions */
113 #ifdef __GNUC__
114 #  pragma GCC diagnostic ignored "-Wdouble-promotion"
115 #endif
116
117 /* ------------------------------------------------------------------------- */
118 /* tool functions/defines for ad hoc simplification and possible future 
119  * cleanup      */
120 /* ------------------------------------------------------------------------- */
121
122 #define UVTOINDEX(u, v) (startvlak + (u) * sizev + (v))
123 /*
124  *
125  * NOTE THAT U/V COORDINATES ARE SOMETIMES SWAPPED !!
126  *
127  * ^   ()----p4----p3----()
128  * |   |     |     |     |
129  * u   |     |  F1 |  F2 |
130  *     |     |     |     |
131  *     ()----p1----p2----()
132  *            v ->
133  */
134
135 /* ------------------------------------------------------------------------- */
136
137 #define CD_MASK_RENDER_INTERNAL \
138     (CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL)
139
140 static void split_v_renderfaces(ObjectRen *obr, int startvlak, int UNUSED(startvert), int UNUSED(usize), int vsize, int uIndex, int UNUSED(cyclu), int cyclv)
141 {
142         int vLen = vsize-1+(!!cyclv);
143         int v;
144
145         for (v=0; v<vLen; v++) {
146                 VlakRen *vlr = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + v);
147                 VlakRen *vlr_other;
148                 VertRen *vert = RE_vertren_copy(obr, vlr->v2);
149
150                 if (cyclv) {
151                         vlr->v2 = vert;
152
153                         if (v == vLen - 1) {
154                                 vlr_other = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + 0);
155                                 vlr_other->v1 = vert;
156                         }
157                         else {
158                                 vlr_other = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + v+1);
159                                 vlr_other->v1 = vert;
160                         }
161                 }
162                 else {
163                         vlr->v2 = vert;
164
165                         if (v < vLen - 1) {
166                                 vlr_other = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + v+1);
167                                 vlr_other->v1 = vert;
168                         }
169
170                         if (v == 0) {
171                                 vlr->v1 = RE_vertren_copy(obr, vlr->v1);
172                         }
173                 }
174         }
175 }
176
177 /* ------------------------------------------------------------------------- */
178 /* Stress, tangents and normals                                              */
179 /* ------------------------------------------------------------------------- */
180
181 static void calc_edge_stress_add(float *accum, VertRen *v1, VertRen *v2)
182 {
183         float len= len_v3v3(v1->co, v2->co)/len_v3v3(v1->orco, v2->orco);
184         float *acc;
185         
186         acc= accum + 2*v1->index;
187         acc[0]+= len;
188         acc[1]+= 1.0f;
189         
190         acc= accum + 2*v2->index;
191         acc[0]+= len;
192         acc[1]+= 1.0f;
193 }
194
195 static void calc_edge_stress(Render *UNUSED(re), ObjectRen *obr, Mesh *me)
196 {
197         float loc[3], size[3], *accum, *acc, *accumoffs, *stress;
198         int a;
199         
200         if (obr->totvert==0) return;
201         
202         BKE_mesh_texspace_get(me, loc, NULL, size);
203         
204         accum= MEM_callocN(2*sizeof(float)*obr->totvert, "temp accum for stress");
205         
206         /* de-normalize orco */
207         for (a=0; a<obr->totvert; a++) {
208                 VertRen *ver= RE_findOrAddVert(obr, a);
209                 if (ver->orco) {
210                         ver->orco[0]= ver->orco[0]*size[0] +loc[0];
211                         ver->orco[1]= ver->orco[1]*size[1] +loc[1];
212                         ver->orco[2]= ver->orco[2]*size[2] +loc[2];
213                 }
214         }
215         
216         /* add stress values */
217         accumoffs= accum;       /* so we can use vertex index */
218         for (a=0; a<obr->totvlak; a++) {
219                 VlakRen *vlr= RE_findOrAddVlak(obr, a);
220
221                 if (vlr->v1->orco && vlr->v4) {
222                         calc_edge_stress_add(accumoffs, vlr->v1, vlr->v2);
223                         calc_edge_stress_add(accumoffs, vlr->v2, vlr->v3);
224                         calc_edge_stress_add(accumoffs, vlr->v3, vlr->v1);
225                         if (vlr->v4) {
226                                 calc_edge_stress_add(accumoffs, vlr->v3, vlr->v4);
227                                 calc_edge_stress_add(accumoffs, vlr->v4, vlr->v1);
228                                 calc_edge_stress_add(accumoffs, vlr->v2, vlr->v4);
229                         }
230                 }
231         }
232         
233         for (a=0; a<obr->totvert; a++) {
234                 VertRen *ver= RE_findOrAddVert(obr, a);
235                 if (ver->orco) {
236                         /* find stress value */
237                         acc= accumoffs + 2*ver->index;
238                         if (acc[1]!=0.0f)
239                                 acc[0]/= acc[1];
240                         stress= RE_vertren_get_stress(obr, ver, 1);
241                         *stress= *acc;
242                         
243                         /* restore orcos */
244                         ver->orco[0] = (ver->orco[0]-loc[0])/size[0];
245                         ver->orco[1] = (ver->orco[1]-loc[1])/size[1];
246                         ver->orco[2] = (ver->orco[2]-loc[2])/size[2];
247                 }
248         }
249         
250         MEM_freeN(accum);
251 }
252
253 /* gets tangent from tface or orco */
254 static void calc_tangent_vector(ObjectRen *obr, VlakRen *vlr, int do_tangent)
255 {
256         MTFace *tface= RE_vlakren_get_tface(obr, vlr, obr->actmtface, NULL, 0);
257         VertRen *v1=vlr->v1, *v2=vlr->v2, *v3=vlr->v3, *v4=vlr->v4;
258         float tang[3], *tav;
259         float *uv1, *uv2, *uv3, *uv4;
260         float uv[4][2];
261         
262         if (tface) {
263                 uv1= tface->uv[0];
264                 uv2= tface->uv[1];
265                 uv3= tface->uv[2];
266                 uv4= tface->uv[3];
267         }
268         else if (v1->orco) {
269                 uv1= uv[0]; uv2= uv[1]; uv3= uv[2]; uv4= uv[3];
270                 map_to_sphere(&uv[0][0], &uv[0][1], v1->orco[0], v1->orco[1], v1->orco[2]);
271                 map_to_sphere(&uv[1][0], &uv[1][1], v2->orco[0], v2->orco[1], v2->orco[2]);
272                 map_to_sphere(&uv[2][0], &uv[2][1], v3->orco[0], v3->orco[1], v3->orco[2]);
273                 if (v4)
274                         map_to_sphere(&uv[3][0], &uv[3][1], v4->orco[0], v4->orco[1], v4->orco[2]);
275         }
276         else return;
277
278         tangent_from_uv_v3(uv1, uv2, uv3, v1->co, v2->co, v3->co, vlr->n, tang);
279         
280         if (do_tangent) {
281                 tav= RE_vertren_get_tangent(obr, v1, 1);
282                 add_v3_v3(tav, tang);
283                 tav= RE_vertren_get_tangent(obr, v2, 1);
284                 add_v3_v3(tav, tang);
285                 tav= RE_vertren_get_tangent(obr, v3, 1);
286                 add_v3_v3(tav, tang);
287         }
288         
289         if (v4) {
290                 tangent_from_uv_v3(uv1, uv3, uv4, v1->co, v3->co, v4->co, vlr->n, tang);
291                 
292                 if (do_tangent) {
293                         tav= RE_vertren_get_tangent(obr, v1, 1);
294                         add_v3_v3(tav, tang);
295                         tav= RE_vertren_get_tangent(obr, v3, 1);
296                         add_v3_v3(tav, tang);
297                         tav= RE_vertren_get_tangent(obr, v4, 1);
298                         add_v3_v3(tav, tang);
299                 }
300         }
301 }
302
303
304
305 /****************************************************************
306  ************ tangent space generation interface ****************
307  ****************************************************************/
308
309 typedef struct {
310         ObjectRen *obr;
311         int mtface_index;
312 } SRenderMeshToTangent;
313
314 /* interface */
315 #include "mikktspace.h"
316
317 static int GetNumFaces(const SMikkTSpaceContext *pContext)
318 {
319         SRenderMeshToTangent *pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
320         return pMesh->obr->totvlak;
321 }
322
323 static int GetNumVertsOfFace(const SMikkTSpaceContext *pContext, const int face_num)
324 {
325         SRenderMeshToTangent *pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
326         VlakRen *vlr= RE_findOrAddVlak(pMesh->obr, face_num);
327         return vlr->v4!=NULL ? 4 : 3;
328 }
329
330 static void GetPosition(const SMikkTSpaceContext *pContext, float r_co[3], const int face_num, const int vert_index)
331 {
332         //assert(vert_index>=0 && vert_index<4);
333         SRenderMeshToTangent *pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
334         VlakRen *vlr= RE_findOrAddVlak(pMesh->obr, face_num);
335         const float *co = (&vlr->v1)[vert_index]->co;
336         copy_v3_v3(r_co, co);
337 }
338
339 static void GetTextureCoordinate(const SMikkTSpaceContext *pContext, float r_uv[2], const int face_num, const int vert_index)
340 {
341         //assert(vert_index>=0 && vert_index<4);
342         SRenderMeshToTangent *pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
343         VlakRen *vlr= RE_findOrAddVlak(pMesh->obr, face_num);
344         MTFace *tface= RE_vlakren_get_tface(pMesh->obr, vlr, pMesh->mtface_index, NULL, 0);
345         const float *coord;
346         
347         if (tface  != NULL) {
348                 coord= tface->uv[vert_index];
349                 copy_v2_v2(r_uv, coord);
350         }
351         else if ((coord = (&vlr->v1)[vert_index]->orco)) {
352                 map_to_sphere(&r_uv[0], &r_uv[1], coord[0], coord[1], coord[2]);
353         }
354         else { /* else we get un-initialized value, 0.0 ok default? */
355                 zero_v2(r_uv);
356         }
357 }
358
359 static void GetNormal(const SMikkTSpaceContext *pContext, float r_no[3], const int face_num, const int vert_index)
360 {
361         //assert(vert_index>=0 && vert_index<4);
362         SRenderMeshToTangent *pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
363         VlakRen *vlr= RE_findOrAddVlak(pMesh->obr, face_num);
364
365         if (vlr->flag & ME_SMOOTH) {
366                 const float *n = (&vlr->v1)[vert_index]->n;
367                 copy_v3_v3(r_no, n);
368         }
369         else {
370                 negate_v3_v3(r_no, vlr->n);
371         }
372 }
373 static void SetTSpace(const SMikkTSpaceContext *pContext, const float fvTangent[3], const float fSign, const int face_num, const int iVert)
374 {
375         //assert(vert_index>=0 && vert_index<4);
376         SRenderMeshToTangent *pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
377         VlakRen *vlr = RE_findOrAddVlak(pMesh->obr, face_num);
378         float *ftang = RE_vlakren_get_nmap_tangent(pMesh->obr, vlr, pMesh->mtface_index, true);
379         if (ftang!=NULL) {
380                 copy_v3_v3(&ftang[iVert*4+0], fvTangent);
381                 ftang[iVert*4+3]=fSign;
382         }
383 }
384
385 static void calc_vertexnormals(Render *UNUSED(re), ObjectRen *obr, bool do_vertex_normal, bool do_tangent, bool do_nmap_tangent)
386 {
387         int a;
388
389         /* clear all vertex normals */
390         if (do_vertex_normal) {
391                 for (a=0; a<obr->totvert; a++) {
392                         VertRen *ver= RE_findOrAddVert(obr, a);
393                         ver->n[0]=ver->n[1]=ver->n[2]= 0.0f;
394                 }
395         }
396
397         /* calculate cos of angles and point-masses, use as weight factor to
398          * add face normal to vertex */
399         for (a=0; a<obr->totvlak; a++) {
400                 VlakRen *vlr= RE_findOrAddVlak(obr, a);
401                 if (do_vertex_normal && vlr->flag & ME_SMOOTH) {
402                         float *n4= (vlr->v4)? vlr->v4->n: NULL;
403                         const float *c4= (vlr->v4)? vlr->v4->co: NULL;
404
405                         accumulate_vertex_normals_v3(vlr->v1->n, vlr->v2->n, vlr->v3->n, n4,
406                                 vlr->n, vlr->v1->co, vlr->v2->co, vlr->v3->co, c4);
407                 }
408                 if (do_tangent) {
409                         /* tangents still need to be calculated for flat faces too */
410                         /* weighting removed, they are not vertexnormals */
411                         calc_tangent_vector(obr, vlr, do_tangent);
412                 }
413         }
414
415         /* do solid faces */
416         for (a=0; a<obr->totvlak; a++) {
417                 VlakRen *vlr= RE_findOrAddVlak(obr, a);
418
419                 if (do_vertex_normal && (vlr->flag & ME_SMOOTH)==0) {
420                         if (is_zero_v3(vlr->v1->n)) copy_v3_v3(vlr->v1->n, vlr->n);
421                         if (is_zero_v3(vlr->v2->n)) copy_v3_v3(vlr->v2->n, vlr->n);
422                         if (is_zero_v3(vlr->v3->n)) copy_v3_v3(vlr->v3->n, vlr->n);
423                         if (vlr->v4 && is_zero_v3(vlr->v4->n)) copy_v3_v3(vlr->v4->n, vlr->n);
424                 }
425         }
426         
427         /* normalize vertex normals */
428         for (a=0; a<obr->totvert; a++) {
429                 VertRen *ver= RE_findOrAddVert(obr, a);
430                 normalize_v3(ver->n);
431                 if (do_tangent) {
432                         float *tav= RE_vertren_get_tangent(obr, ver, 0);
433                         if (tav) {
434                                 /* orthonorm. */
435                                 const float tdn = dot_v3v3(tav, ver->n);
436                                 tav[0] -= ver->n[0]*tdn;
437                                 tav[1] -= ver->n[1]*tdn;
438                                 tav[2] -= ver->n[2]*tdn;
439                                 normalize_v3(tav);
440                         }
441                 }
442         }
443
444         /* normal mapping tangent with mikktspace */
445         if (do_nmap_tangent != false) {
446                 SRenderMeshToTangent mesh2tangent;
447                 SMikkTSpaceContext sContext;
448                 SMikkTSpaceInterface sInterface;
449                 memset(&mesh2tangent, 0, sizeof(SRenderMeshToTangent));
450                 memset(&sContext, 0, sizeof(SMikkTSpaceContext));
451                 memset(&sInterface, 0, sizeof(SMikkTSpaceInterface));
452
453                 mesh2tangent.obr = obr;
454
455                 sContext.m_pUserData = &mesh2tangent;
456                 sContext.m_pInterface = &sInterface;
457                 sInterface.m_getNumFaces = GetNumFaces;
458                 sInterface.m_getNumVerticesOfFace = GetNumVertsOfFace;
459                 sInterface.m_getPosition = GetPosition;
460                 sInterface.m_getTexCoord = GetTextureCoordinate;
461                 sInterface.m_getNormal = GetNormal;
462                 sInterface.m_setTSpaceBasic = SetTSpace;
463
464                 for (a = 0; a < MAX_MTFACE; a++) {
465                         if (obr->tangent_mask & 1 << a) {
466                                 mesh2tangent.mtface_index = a;
467                                 genTangSpaceDefault(&sContext);
468                         }
469                 }
470         }
471 }
472
473 /* ------------------------------------------------------------------------- */
474 /* Autosmoothing:                                                            */
475 /* ------------------------------------------------------------------------- */
476
477 typedef struct ASvert {
478         int totface;
479         ListBase faces;
480 } ASvert;
481
482 typedef struct ASface {
483         struct ASface *next, *prev;
484         VlakRen *vlr[4];
485         VertRen *nver[4];
486 } ASface;
487
488 static int as_addvert(ASvert *asv, VertRen *v1, VlakRen *vlr)
489 {
490         ASface *asf;
491         int a = -1;
492
493         if (v1 == NULL)
494                 return a;
495
496         asf = asv->faces.last;
497         if (asf) {
498                 for (a = 0; a < 4 && asf->vlr[a]; a++) {
499                 }
500         }
501         else {
502                 a = 4;
503         }
504
505         /* new face struct */
506         if (a == 4) {
507                 a = 0;
508                 asf = MEM_callocN(sizeof(ASface), "asface");
509                 BLI_addtail(&asv->faces, asf);
510         }
511
512         asf->vlr[a] = vlr;
513         asv->totface++;
514
515         return a;
516 }
517
518 static VertRen *as_findvertex_lnor(VlakRen *vlr, VertRen *ver, ASvert *asv, const float lnor[3])
519 {
520         /* return when new vertex already was made, or existing one is OK */
521         ASface *asf;
522         int a;
523
524         /* First face, we can use existing vert and assign it current lnor! */
525         if (asv->totface == 1) {
526                 copy_v3_v3(ver->n, lnor);
527                 return ver;
528         }
529
530         /* In case existing ver has same normal as current lnor, we can simply use it! */
531         if (equals_v3v3(lnor, ver->n)) {
532                 return ver;
533         }
534
535         asf = asv->faces.first;
536         while (asf) {
537                 for (a = 0; a < 4; a++) {
538                         if (asf->vlr[a] && asf->vlr[a] != vlr) {
539                                 /* this face already made a copy for this vertex! */
540                                 if (asf->nver[a]) {
541                                         if (equals_v3v3(lnor, asf->nver[a]->n)) {
542                                                 return asf->nver[a];
543                                         }
544                                 }
545                         }
546                 }
547                 asf = asf->next;
548         }
549
550         return NULL;
551 }
552
553 static void as_addvert_lnor(ObjectRen *obr, ASvert *asv, VertRen *ver, VlakRen *vlr, const short _lnor[3])
554 {
555         VertRen *v1;
556         ASface *asf;
557         int asf_idx;
558         float lnor[3];
559
560         normal_short_to_float_v3(lnor, _lnor);
561
562         asf_idx = as_addvert(asv, ver, vlr);
563         if (asf_idx < 0) {
564                 return;
565         }
566         asf = asv->faces.last;
567
568         /* already made a new vertex within threshold? */
569         v1 = as_findvertex_lnor(vlr, ver, asv, lnor);
570         if (v1 == NULL) {
571                 /* make a new vertex */
572                 v1 = RE_vertren_copy(obr, ver);
573                 copy_v3_v3(v1->n, lnor);
574         }
575         if (v1 != ver) {
576                 asf->nver[asf_idx] = v1;
577                 if (vlr->v1 == ver) vlr->v1 = v1;
578                 if (vlr->v2 == ver) vlr->v2 = v1;
579                 if (vlr->v3 == ver) vlr->v3 = v1;
580                 if (vlr->v4 == ver) vlr->v4 = v1;
581         }
582 }
583
584 /* note; autosmooth happens in object space still, after applying autosmooth we rotate */
585 /* note2; actually, when original mesh and displist are equal sized, face normals are from original mesh */
586 static void autosmooth(Render *UNUSED(re), ObjectRen *obr, float mat[4][4], short (*lnors)[4][3])
587 {
588         ASvert *asverts;
589         VertRen *ver;
590         VlakRen *vlr;
591         int a, totvert;
592
593         float rot[3][3];
594
595         /* Note: For normals, we only want rotation, not scaling component.
596          *       Negative scales (aka mirroring) give wrong results, see T44102. */
597         if (lnors) {
598                 float mat3[3][3], size[3];
599
600                 copy_m3_m4(mat3, mat);
601                 mat3_to_rot_size(rot, size, mat3);
602         }
603
604         if (obr->totvert == 0)
605                 return;
606
607         totvert = obr->totvert;
608         asverts = MEM_callocN(sizeof(ASvert) * totvert, "all smooth verts");
609
610         if (lnors) {
611                 /* We construct listbase of all vertices and pointers to faces, and add new verts when needed
612                  * (i.e. when existing ones do not share the same (loop)normal).
613                  */
614                 for (a = 0; a < obr->totvlak; a++, lnors++) {
615                         vlr = RE_findOrAddVlak(obr, a);
616                         /* skip wire faces */
617                         if (vlr->v2 != vlr->v3) {
618                                 as_addvert_lnor(obr, asverts+vlr->v1->index, vlr->v1, vlr, (const short*)lnors[0][0]);
619                                 as_addvert_lnor(obr, asverts+vlr->v2->index, vlr->v2, vlr, (const short*)lnors[0][1]);
620                                 as_addvert_lnor(obr, asverts+vlr->v3->index, vlr->v3, vlr, (const short*)lnors[0][2]);
621                                 if (vlr->v4)
622                                         as_addvert_lnor(obr, asverts+vlr->v4->index, vlr->v4, vlr, (const short*)lnors[0][3]);
623                         }
624                 }
625         }
626
627         /* free */
628         for (a = 0; a < totvert; a++) {
629                 BLI_freelistN(&asverts[a].faces);
630         }
631         MEM_freeN(asverts);
632
633         /* rotate vertices and calculate normal of faces */
634         for (a = 0; a < obr->totvert; a++) {
635                 ver = RE_findOrAddVert(obr, a);
636                 mul_m4_v3(mat, ver->co);
637                 if (lnors) {
638                         mul_m3_v3(rot, ver->n);
639                         negate_v3(ver->n);
640                 }
641         }
642         for (a = 0; a < obr->totvlak; a++) {
643                 vlr = RE_findOrAddVlak(obr, a);
644
645                 /* skip wire faces */
646                 if (vlr->v2 != vlr->v3) {
647                         if (vlr->v4)
648                                 normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
649                         else 
650                                 normal_tri_v3(vlr->n, vlr->v3->co, vlr->v2->co, vlr->v1->co);
651                 }
652         }
653 }
654
655 /* ------------------------------------------------------------------------- */
656 /* Orco hash and Materials                                                   */
657 /* ------------------------------------------------------------------------- */
658
659 static float *get_object_orco(Render *re, void *ob)
660 {
661         if (!re->orco_hash) {
662                 return NULL;
663         }
664
665         return BLI_ghash_lookup(re->orco_hash, ob);
666 }
667
668 static void set_object_orco(Render *re, void *ob, float *orco)
669 {
670         if (!re->orco_hash)
671                 re->orco_hash = BLI_ghash_ptr_new("set_object_orco gh");
672         
673         BLI_ghash_insert(re->orco_hash, ob, orco);
674 }
675
676 static void free_mesh_orco_hash(Render *re) 
677 {
678         if (re->orco_hash) {
679                 BLI_ghash_free(re->orco_hash, NULL, MEM_freeN);
680                 re->orco_hash = NULL;
681         }
682 }
683
684 static void check_material_mapto(Material *ma)
685 {
686         int a;
687         ma->mapto_textured = 0;
688         
689         /* cache which inputs are actually textured.
690          * this can avoid a bit of time spent iterating through all the texture slots, map inputs and map tos
691          * every time a property which may or may not be textured is accessed */
692         
693         for (a=0; a<MAX_MTEX; a++) {
694                 if (ma->mtex[a] && ma->mtex[a]->tex) {
695                         /* currently used only in volume render, so we'll check for those flags */
696                         if (ma->mtex[a]->mapto & MAP_DENSITY) ma->mapto_textured |= MAP_DENSITY;
697                         if (ma->mtex[a]->mapto & MAP_EMISSION) ma->mapto_textured |= MAP_EMISSION;
698                         if (ma->mtex[a]->mapto & MAP_EMISSION_COL) ma->mapto_textured |= MAP_EMISSION_COL;
699                         if (ma->mtex[a]->mapto & MAP_SCATTERING) ma->mapto_textured |= MAP_SCATTERING;
700                         if (ma->mtex[a]->mapto & MAP_TRANSMISSION_COL) ma->mapto_textured |= MAP_TRANSMISSION_COL;
701                         if (ma->mtex[a]->mapto & MAP_REFLECTION) ma->mapto_textured |= MAP_REFLECTION;
702                         if (ma->mtex[a]->mapto & MAP_REFLECTION_COL) ma->mapto_textured |= MAP_REFLECTION_COL;
703                 }
704         }
705 }
706 static void flag_render_node_material(Render *re, bNodeTree *ntree)
707 {
708         bNode *node;
709
710         for (node = ntree->nodes.first; node; node = node->next) {
711                 if (node->id) {
712                         if (GS(node->id->name)==ID_MA) {
713                                 Material *ma= (Material *)node->id;
714
715                                 if ((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))
716                                         re->flag |= R_ZTRA;
717
718                                 ma->flag |= MA_IS_USED;
719                         }
720                         else if (node->type==NODE_GROUP)
721                                 flag_render_node_material(re, (bNodeTree *)node->id);
722                 }
723         }
724 }
725
726 static Material *give_render_material(Render *re, Object *ob, short nr)
727 {
728         extern Material defmaterial;    /* material.c */
729         Material *ma;
730         
731         ma= give_current_material(ob, nr);
732         if (ma==NULL)
733                 ma= &defmaterial;
734         
735         if (re->r.mode & R_SPEED) ma->texco |= NEED_UV;
736         
737         if (ma->material_type == MA_TYPE_VOLUME) {
738                 ma->mode |= MA_TRANSP;
739                 ma->mode &= ~MA_SHADBUF;
740         }
741         if ((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))
742                 re->flag |= R_ZTRA;
743         
744         /* for light groups and SSS */
745         ma->flag |= MA_IS_USED;
746
747         if (ma->nodetree && ma->use_nodes)
748                 flag_render_node_material(re, ma->nodetree);
749         
750         check_material_mapto(ma);
751         
752         return ma;
753 }
754
755 /* ------------------------------------------------------------------------- */
756 /* Particles                                                                 */
757 /* ------------------------------------------------------------------------- */
758 typedef struct ParticleStrandData {
759         struct MCol *mcol;
760         float *orco, *uvco, *surfnor;
761         float time, adapt_angle, adapt_pix, size;
762         int totuv, totcol;
763         int first, line, adapt, override_uv;
764 }
765 ParticleStrandData;
766 /* future thread problem... */
767 static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, ParticleStrandData *sd, const float vec[3], const float vec1[3])
768 {
769         static VertRen *v1= NULL, *v2= NULL;
770         VlakRen *vlr= NULL;
771         float nor[3], cross[3], crosslen, w, dx, dy, width;
772         static float anor[3], avec[3];
773         int flag, i;
774         static int second=0;
775         
776         sub_v3_v3v3(nor, vec, vec1);
777         normalize_v3(nor);  /* nor needed as tangent */
778         cross_v3_v3v3(cross, vec, nor);
779
780         /* turn cross in pixelsize */
781         w= vec[2]*re->winmat[2][3] + re->winmat[3][3];
782         dx= re->winx*cross[0]*re->winmat[0][0];
783         dy= re->winy*cross[1]*re->winmat[1][1];
784         w = sqrtf(dx * dx + dy * dy) / w;
785         
786         if (w!=0.0f) {
787                 float fac;
788                 if (ma->strand_ease!=0.0f) {
789                         if (ma->strand_ease<0.0f)
790                                 fac= pow(sd->time, 1.0f+ma->strand_ease);
791                         else
792                                 fac= pow(sd->time, 1.0f/(1.0f-ma->strand_ease));
793                 }
794                 else fac= sd->time;
795
796                 width= ((1.0f-fac)*ma->strand_sta + (fac)*ma->strand_end);
797
798                 /* use actual Blender units for strand width and fall back to minimum width */
799                 if (ma->mode & MA_STR_B_UNITS) {
800                         crosslen= len_v3(cross);
801                         w= 2.0f*crosslen*ma->strand_min/w;
802
803                         if (width < w)
804                                 width= w;
805
806                         /*cross is the radius of the strand so we want it to be half of full width */
807                         mul_v3_fl(cross, 0.5f/crosslen);
808                 }
809                 else
810                         width/=w;
811
812                 mul_v3_fl(cross, width);
813         }
814         
815         if (ma->mode & MA_TANGENT_STR)
816                 flag= R_SMOOTH|R_TANGENT;
817         else
818                 flag= R_SMOOTH;
819         
820         /* only 1 pixel wide strands filled in as quads now, otherwise zbuf errors */
821         if (ma->strand_sta==1.0f)
822                 flag |= R_STRAND;
823         
824         /* single face line */
825         if (sd->line) {
826                 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
827                 vlr->flag= flag;
828                 vlr->v1= RE_findOrAddVert(obr, obr->totvert++);
829                 vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
830                 vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
831                 vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
832                 
833                 copy_v3_v3(vlr->v1->co, vec);
834                 add_v3_v3(vlr->v1->co, cross);
835                 copy_v3_v3(vlr->v1->n, nor);
836                 vlr->v1->orco= sd->orco;
837                 vlr->v1->accum = -1.0f;  /* accum abuse for strand texco */
838                 
839                 copy_v3_v3(vlr->v2->co, vec);
840                 sub_v3_v3v3(vlr->v2->co, vlr->v2->co, cross);
841                 copy_v3_v3(vlr->v2->n, nor);
842                 vlr->v2->orco= sd->orco;
843                 vlr->v2->accum= vlr->v1->accum;
844
845                 copy_v3_v3(vlr->v4->co, vec1);
846                 add_v3_v3(vlr->v4->co, cross);
847                 copy_v3_v3(vlr->v4->n, nor);
848                 vlr->v4->orco= sd->orco;
849                 vlr->v4->accum = 1.0f;  /* accum abuse for strand texco */
850
851                 copy_v3_v3(vlr->v3->co, vec1);
852                 sub_v3_v3v3(vlr->v3->co, vlr->v3->co, cross);
853                 copy_v3_v3(vlr->v3->n, nor);
854                 vlr->v3->orco= sd->orco;
855                 vlr->v3->accum= vlr->v4->accum;
856
857                 normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
858                 
859                 vlr->mat= ma;
860                 vlr->ec= ME_V2V3;
861
862                 if (sd->surfnor) {
863                         float *snor= RE_vlakren_get_surfnor(obr, vlr, 1);
864                         copy_v3_v3(snor, sd->surfnor);
865                 }
866
867                 if (sd->uvco) {
868                         for (i=0; i<sd->totuv; i++) {
869                                 MTFace *mtf;
870                                 mtf=RE_vlakren_get_tface(obr, vlr, i, NULL, 1);
871                                 mtf->uv[0][0]=mtf->uv[1][0]=
872                                 mtf->uv[2][0]=mtf->uv[3][0]=(sd->uvco+2*i)[0];
873                                 mtf->uv[0][1]=mtf->uv[1][1]=
874                                 mtf->uv[2][1]=mtf->uv[3][1]=(sd->uvco+2*i)[1];
875                         }
876                         if (sd->override_uv>=0) {
877                                 MTFace *mtf;
878                                 mtf=RE_vlakren_get_tface(obr, vlr, sd->override_uv, NULL, 0);
879                                 
880                                 mtf->uv[0][0]=mtf->uv[3][0]=0.0f;
881                                 mtf->uv[1][0]=mtf->uv[2][0]=1.0f;
882
883                                 mtf->uv[0][1]=mtf->uv[1][1]=0.0f;
884                                 mtf->uv[2][1]=mtf->uv[3][1]=1.0f;
885                         }
886                 }
887                 if (sd->mcol) {
888                         for (i=0; i<sd->totcol; i++) {
889                                 MCol *mc;
890                                 mc=RE_vlakren_get_mcol(obr, vlr, i, NULL, 1);
891                                 mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
892                                 mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
893                         }
894                 }
895         }
896         /* first two vertices of a strand */
897         else if (sd->first) {
898                 if (sd->adapt) {
899                         copy_v3_v3(anor, nor);
900                         copy_v3_v3(avec, vec);
901                         second=1;
902                 }
903
904                 v1= RE_findOrAddVert(obr, obr->totvert++);
905                 v2= RE_findOrAddVert(obr, obr->totvert++);
906                 
907                 copy_v3_v3(v1->co, vec);
908                 add_v3_v3(v1->co, cross);
909                 copy_v3_v3(v1->n, nor);
910                 v1->orco= sd->orco;
911                 v1->accum = -1.0f;  /* accum abuse for strand texco */
912                 
913                 copy_v3_v3(v2->co, vec);
914                 sub_v3_v3v3(v2->co, v2->co, cross);
915                 copy_v3_v3(v2->n, nor);
916                 v2->orco= sd->orco;
917                 v2->accum= v1->accum;
918         }
919         /* more vertices & faces to strand */
920         else {
921                 if (sd->adapt==0 || second) {
922                         vlr= RE_findOrAddVlak(obr, obr->totvlak++);
923                         vlr->flag= flag;
924                         vlr->v1= v1;
925                         vlr->v2= v2;
926                         vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
927                         vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
928
929                         v1= vlr->v4; /* cycle */
930                         v2= vlr->v3; /* cycle */
931
932
933                         if (sd->adapt) {
934                                 second=0;
935                                 copy_v3_v3(anor, nor);
936                                 copy_v3_v3(avec, vec);
937                         }
938
939                 }
940                 else if (sd->adapt) {
941                         float dvec[3], pvec[3];
942                         sub_v3_v3v3(dvec, avec, vec);
943                         project_v3_v3v3(pvec, dvec, vec);
944                         sub_v3_v3v3(dvec, dvec, pvec);
945
946                         w= vec[2]*re->winmat[2][3] + re->winmat[3][3];
947                         dx= re->winx*dvec[0]*re->winmat[0][0]/w;
948                         dy= re->winy*dvec[1]*re->winmat[1][1]/w;
949                         w = sqrtf(dx * dx + dy * dy);
950                         if (dot_v3v3(anor, nor)<sd->adapt_angle && w>sd->adapt_pix) {
951                                 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
952                                 vlr->flag= flag;
953                                 vlr->v1= v1;
954                                 vlr->v2= v2;
955                                 vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
956                                 vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
957
958                                 v1= vlr->v4; /* cycle */
959                                 v2= vlr->v3; /* cycle */
960
961                                 copy_v3_v3(anor, nor);
962                                 copy_v3_v3(avec, vec);
963                         }
964                         else {
965                                 vlr= RE_findOrAddVlak(obr, obr->totvlak-1);
966                         }
967                 }
968         
969                 copy_v3_v3(vlr->v4->co, vec);
970                 add_v3_v3(vlr->v4->co, cross);
971                 copy_v3_v3(vlr->v4->n, nor);
972                 vlr->v4->orco= sd->orco;
973                 vlr->v4->accum= -1.0f + 2.0f * sd->time;  /* accum abuse for strand texco */
974
975                 copy_v3_v3(vlr->v3->co, vec);
976                 sub_v3_v3v3(vlr->v3->co, vlr->v3->co, cross);
977                 copy_v3_v3(vlr->v3->n, nor);
978                 vlr->v3->orco= sd->orco;
979                 vlr->v3->accum= vlr->v4->accum;
980                 
981                 normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
982                 
983                 vlr->mat= ma;
984                 vlr->ec= ME_V2V3;
985
986                 if (sd->surfnor) {
987                         float *snor= RE_vlakren_get_surfnor(obr, vlr, 1);
988                         copy_v3_v3(snor, sd->surfnor);
989                 }
990
991                 if (sd->uvco) {
992                         for (i=0; i<sd->totuv; i++) {
993                                 MTFace *mtf;
994                                 mtf=RE_vlakren_get_tface(obr, vlr, i, NULL, 1);
995                                 mtf->uv[0][0]=mtf->uv[1][0]=
996                                 mtf->uv[2][0]=mtf->uv[3][0]=(sd->uvco+2*i)[0];
997                                 mtf->uv[0][1]=mtf->uv[1][1]=
998                                 mtf->uv[2][1]=mtf->uv[3][1]=(sd->uvco+2*i)[1];
999                         }
1000                         if (sd->override_uv>=0) {
1001                                 MTFace *mtf;
1002                                 mtf=RE_vlakren_get_tface(obr, vlr, sd->override_uv, NULL, 0);
1003                                 
1004                                 mtf->uv[0][0]=mtf->uv[3][0]=0.0f;
1005                                 mtf->uv[1][0]=mtf->uv[2][0]=1.0f;
1006
1007                                 mtf->uv[0][1]=mtf->uv[1][1]=(vlr->v1->accum+1.0f)/2.0f;
1008                                 mtf->uv[2][1]=mtf->uv[3][1]=(vlr->v3->accum+1.0f)/2.0f;
1009                         }
1010                 }
1011                 if (sd->mcol) {
1012                         for (i=0; i<sd->totcol; i++) {
1013                                 MCol *mc;
1014                                 mc=RE_vlakren_get_mcol(obr, vlr, i, NULL, 1);
1015                                 mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
1016                                 mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
1017                         }
1018                 }
1019         }
1020 }
1021
1022 static void static_particle_wire(ObjectRen *obr, Material *ma, const float vec[3], const float vec1[3], int first, int line)
1023 {
1024         VlakRen *vlr;
1025         static VertRen *v1;
1026
1027         if (line) {
1028                 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1029                 vlr->v1= RE_findOrAddVert(obr, obr->totvert++);
1030                 vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
1031                 vlr->v3= vlr->v2;
1032                 vlr->v4= NULL;
1033                 
1034                 copy_v3_v3(vlr->v1->co, vec);
1035                 copy_v3_v3(vlr->v2->co, vec1);
1036                 
1037                 sub_v3_v3v3(vlr->n, vec, vec1);
1038                 normalize_v3(vlr->n);
1039                 copy_v3_v3(vlr->v1->n, vlr->n);
1040                 copy_v3_v3(vlr->v2->n, vlr->n);
1041                 
1042                 vlr->mat= ma;
1043                 vlr->ec= ME_V1V2;
1044
1045         }
1046         else if (first) {
1047                 v1= RE_findOrAddVert(obr, obr->totvert++);
1048                 copy_v3_v3(v1->co, vec);
1049         }
1050         else {
1051                 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1052                 vlr->v1= v1;
1053                 vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
1054                 vlr->v3= vlr->v2;
1055                 vlr->v4= NULL;
1056                 
1057                 v1= vlr->v2; /* cycle */
1058                 copy_v3_v3(v1->co, vec);
1059                 
1060                 sub_v3_v3v3(vlr->n, vec, vec1);
1061                 normalize_v3(vlr->n);
1062                 copy_v3_v3(v1->n, vlr->n);
1063                 
1064                 vlr->mat= ma;
1065                 vlr->ec= ME_V1V2;
1066         }
1067
1068 }
1069
1070 static void particle_curve(Render *re, ObjectRen *obr, DerivedMesh *dm, Material *ma, ParticleStrandData *sd,
1071                            const float loc[3], const float loc1[3], int seed, float *pa_co)
1072 {
1073         HaloRen *har = NULL;
1074
1075         if (ma->material_type == MA_TYPE_WIRE)
1076                 static_particle_wire(obr, ma, loc, loc1, sd->first, sd->line);
1077         else if (ma->material_type == MA_TYPE_HALO) {
1078                 har= RE_inithalo_particle(re, obr, dm, ma, loc, loc1, sd->orco, sd->uvco, sd->size, 1.0, seed, pa_co);
1079                 if (har) har->lay= obr->ob->lay;
1080         }
1081         else
1082                 static_particle_strand(re, obr, ma, sd, loc, loc1);
1083 }
1084 static void particle_billboard(Render *re, ObjectRen *obr, Material *ma, ParticleBillboardData *bb)
1085 {
1086         VlakRen *vlr;
1087         MTFace *mtf;
1088         float xvec[3], yvec[3], zvec[3], bb_center[3];
1089         /* Number of tiles */
1090         int totsplit = bb->uv_split * bb->uv_split;
1091         int tile, x, y;
1092         /* Tile offsets */
1093         float uvx = 0.0f, uvy = 0.0f, uvdx = 1.0f, uvdy = 1.0f, time = 0.0f;
1094
1095         vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1096         vlr->v1= RE_findOrAddVert(obr, obr->totvert++);
1097         vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
1098         vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
1099         vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
1100
1101         psys_make_billboard(bb, xvec, yvec, zvec, bb_center);
1102
1103         add_v3_v3v3(vlr->v1->co, bb_center, xvec);
1104         add_v3_v3(vlr->v1->co, yvec);
1105         mul_m4_v3(re->viewmat, vlr->v1->co);
1106
1107         sub_v3_v3v3(vlr->v2->co, bb_center, xvec);
1108         add_v3_v3(vlr->v2->co, yvec);
1109         mul_m4_v3(re->viewmat, vlr->v2->co);
1110
1111         sub_v3_v3v3(vlr->v3->co, bb_center, xvec);
1112         sub_v3_v3v3(vlr->v3->co, vlr->v3->co, yvec);
1113         mul_m4_v3(re->viewmat, vlr->v3->co);
1114
1115         add_v3_v3v3(vlr->v4->co, bb_center, xvec);
1116         sub_v3_v3(vlr->v4->co, yvec);
1117         mul_m4_v3(re->viewmat, vlr->v4->co);
1118
1119         normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
1120         copy_v3_v3(vlr->v1->n, vlr->n);
1121         copy_v3_v3(vlr->v2->n, vlr->n);
1122         copy_v3_v3(vlr->v3->n, vlr->n);
1123         copy_v3_v3(vlr->v4->n, vlr->n);
1124         
1125         vlr->mat= ma;
1126         vlr->ec= ME_V2V3;
1127
1128         if (bb->uv_split > 1) {
1129                 uvdx = uvdy = 1.0f / (float)bb->uv_split;
1130
1131                 if (ELEM(bb->anim, PART_BB_ANIM_AGE, PART_BB_ANIM_FRAME)) {
1132                         if (bb->anim == PART_BB_ANIM_FRAME)
1133                                 time = ((int)(bb->time * bb->lifetime) % totsplit)/(float)totsplit;
1134                         else
1135                                 time = bb->time;
1136                 }
1137                 else if (bb->anim == PART_BB_ANIM_ANGLE) {
1138                         if (bb->align == PART_BB_VIEW) {
1139                                 time = (float)fmod((bb->tilt + 1.0f) / 2.0f, 1.0);
1140                         }
1141                         else {
1142                                 float axis1[3] = {0.0f, 0.0f, 0.0f};
1143                                 float axis2[3] = {0.0f, 0.0f, 0.0f};
1144
1145                                 axis1[(bb->align + 1) % 3] = 1.0f;
1146                                 axis2[(bb->align + 2) % 3] = 1.0f;
1147
1148                                 if (bb->lock == 0) {
1149                                         zvec[bb->align] = 0.0f;
1150                                         normalize_v3(zvec);
1151                                 }
1152                                 
1153                                 time = saacos(dot_v3v3(zvec, axis1)) / (float)M_PI;
1154                                 
1155                                 if (dot_v3v3(zvec, axis2) < 0.0f)
1156                                         time = 1.0f - time / 2.0f;
1157                                 else
1158                                         time /= 2.0f;
1159                         }
1160                 }
1161
1162                 if (bb->split_offset == PART_BB_OFF_LINEAR)
1163                         time = (float)fmod(time + (float)bb->num / (float)totsplit, 1.0f);
1164                 else if (bb->split_offset==PART_BB_OFF_RANDOM)
1165                         time = (float)fmod(time + bb->random, 1.0f);
1166
1167                 /* Find the coordinates in tile space (integer), then convert to UV
1168                  * space (float). Note that Y is flipped. */
1169                 tile = (int)((time + FLT_EPSILON10) * totsplit);
1170                 x = tile % bb->uv_split;
1171                 y = tile / bb->uv_split;
1172                 y = (bb->uv_split - 1) - y;
1173                 uvx = uvdx * x;
1174                 uvy = uvdy * y;
1175         }
1176
1177         /* normal UVs */
1178         if (bb->uv[0] >= 0) {
1179                 mtf = RE_vlakren_get_tface(obr, vlr, bb->uv[0], NULL, 1);
1180                 mtf->uv[0][0] = 1.0f;
1181                 mtf->uv[0][1] = 1.0f;
1182                 mtf->uv[1][0] = 0.0f;
1183                 mtf->uv[1][1] = 1.0f;
1184                 mtf->uv[2][0] = 0.0f;
1185                 mtf->uv[2][1] = 0.0f;
1186                 mtf->uv[3][0] = 1.0f;
1187                 mtf->uv[3][1] = 0.0f;
1188         }
1189
1190         /* time-index UVs */
1191         if (bb->uv[1] >= 0) {
1192                 mtf = RE_vlakren_get_tface(obr, vlr, bb->uv[1], NULL, 1);
1193                 mtf->uv[0][0] = mtf->uv[1][0] = mtf->uv[2][0] = mtf->uv[3][0] = bb->time;
1194                 mtf->uv[0][1] = mtf->uv[1][1] = mtf->uv[2][1] = mtf->uv[3][1] = (float)bb->num/(float)bb->totnum;
1195         }
1196
1197         /* split UVs */
1198         if (bb->uv_split > 1 && bb->uv[2] >= 0) {
1199                 mtf = RE_vlakren_get_tface(obr, vlr, bb->uv[2], NULL, 1);
1200                 mtf->uv[0][0] = uvx + uvdx;
1201                 mtf->uv[0][1] = uvy + uvdy;
1202                 mtf->uv[1][0] = uvx;
1203                 mtf->uv[1][1] = uvy + uvdy;
1204                 mtf->uv[2][0] = uvx;
1205                 mtf->uv[2][1] = uvy;
1206                 mtf->uv[3][0] = uvx + uvdx;
1207                 mtf->uv[3][1] = uvy;
1208         }
1209 }
1210 static void particle_normal_ren(short ren_as, ParticleSettings *part, Render *re, ObjectRen *obr, DerivedMesh *dm, Material *ma, ParticleStrandData *sd, ParticleBillboardData *bb, ParticleKey *state, int seed, float hasize, float *pa_co)
1211 {
1212         float loc[3], loc0[3], loc1[3], vel[3];
1213         
1214         copy_v3_v3(loc, state->co);
1215
1216         if (ren_as != PART_DRAW_BB)
1217                 mul_m4_v3(re->viewmat, loc);
1218
1219         switch (ren_as) {
1220                 case PART_DRAW_LINE:
1221                         sd->line = 1;
1222                         sd->time = 0.0f;
1223                         sd->size = hasize;
1224
1225                         mul_v3_mat3_m4v3(vel, re->viewmat, state->vel);
1226                         normalize_v3(vel);
1227
1228                         if (part->draw & PART_DRAW_VEL_LENGTH)
1229                                 mul_v3_fl(vel, len_v3(state->vel));
1230
1231                         madd_v3_v3v3fl(loc0, loc, vel, -part->draw_line[0]);
1232                         madd_v3_v3v3fl(loc1, loc, vel, part->draw_line[1]);
1233
1234                         particle_curve(re, obr, dm, ma, sd, loc0, loc1, seed, pa_co);
1235
1236                         break;
1237
1238                 case PART_DRAW_BB:
1239
1240                         copy_v3_v3(bb->vec, loc);
1241                         copy_v3_v3(bb->vel, state->vel);
1242
1243                         particle_billboard(re, obr, ma, bb);
1244
1245                         break;
1246
1247                 default:
1248                 {
1249                         HaloRen *har = NULL;
1250
1251                         har = RE_inithalo_particle(re, obr, dm, ma, loc, NULL, sd->orco, sd->uvco, hasize, 0.0, seed, pa_co);
1252                         
1253                         if (har) har->lay= obr->ob->lay;
1254
1255                         break;
1256                 }
1257         }
1258 }
1259 static void get_particle_uvco_mcol(short from, DerivedMesh *dm, float *fuv, int num, ParticleStrandData *sd)
1260 {
1261         int i;
1262
1263         /* get uvco */
1264         if (sd->uvco && ELEM(from, PART_FROM_FACE, PART_FROM_VOLUME)) {
1265                 for (i=0; i<sd->totuv; i++) {
1266                         if (!ELEM(num, DMCACHE_NOTFOUND, DMCACHE_ISCHILD)) {
1267                                 MFace *mface = dm->getTessFaceData(dm, num, CD_MFACE);
1268                                 MTFace *mtface = (MTFace*)CustomData_get_layer_n(&dm->faceData, CD_MTFACE, i);
1269                                 mtface += num;
1270                                 
1271                                 psys_interpolate_uvs(mtface, mface->v4, fuv, sd->uvco + 2 * i);
1272                         }
1273                         else {
1274                                 sd->uvco[2*i] = 0.0f;
1275                                 sd->uvco[2*i + 1] = 0.0f;
1276                         }
1277                 }
1278         }
1279
1280         /* get mcol */
1281         if (sd->mcol && ELEM(from, PART_FROM_FACE, PART_FROM_VOLUME)) {
1282                 for (i=0; i<sd->totcol; i++) {
1283                         if (!ELEM(num, DMCACHE_NOTFOUND, DMCACHE_ISCHILD)) {
1284                                 MFace *mface = dm->getTessFaceData(dm, num, CD_MFACE);
1285                                 MCol *mc = (MCol*)CustomData_get_layer_n(&dm->faceData, CD_MCOL, i);
1286                                 mc += num * 4;
1287
1288                                 psys_interpolate_mcol(mc, mface->v4, fuv, sd->mcol + i);
1289                         }
1290                         else
1291                                 memset(&sd->mcol[i], 0, sizeof(MCol));
1292                 }
1293         }
1294 }
1295 static int render_new_particle_system(Depsgraph *depsgraph, Render *re,
1296                                       ObjectRen *obr, ParticleSystem *psys, int timeoffset)
1297 {
1298         Object *ob= obr->ob;
1299 //      Object *tob=0;
1300         Material *ma = NULL;
1301         ParticleSystemModifierData *psmd;
1302         ParticleSystem *tpsys = NULL;
1303         ParticleSettings *part, *tpart = NULL;
1304         ParticleData *pars, *pa = NULL, *tpa = NULL;
1305         ParticleKey *states = NULL;
1306         ParticleKey state;
1307         ParticleCacheKey *cache = NULL;
1308         ParticleBillboardData bb;
1309         ParticleSimulationData sim = {NULL};
1310         ParticleStrandData sd;
1311         StrandBuffer *strandbuf = NULL;
1312         StrandVert *svert = NULL;
1313         StrandBound *sbound = NULL;
1314         StrandRen *strand = NULL;
1315         RNG *rng = NULL;
1316         float loc[3], loc1[3], loc0[3], mat[4][4], nmat[3][3], co[3], nor[3], duplimat[4][4];
1317         float strandlen=0.0f, curlen=0.0f;
1318         float hasize, pa_size, r_tilt, r_length;
1319         float pa_time, pa_birthtime, pa_dietime;
1320         float random, pa_co[3];
1321         const float cfra= BKE_scene_frame_get(re->scene);
1322         int i, a, k, max_k=0, totpart;
1323         bool do_surfacecache = false, use_duplimat = false;
1324         int totchild=0, step_nbr;
1325         int seed, path_nbr=0, orco1=0, num;
1326         int totface;
1327
1328         const int *index_mf_to_mpoly = NULL;
1329         const int *index_mp_to_orig = NULL;
1330
1331 /* 1. check that everything is ok & updated */
1332         if (psys==NULL)
1333                 return 0;
1334
1335         part=psys->part;
1336         pars=psys->particles;
1337
1338         if (part==NULL || pars==NULL || !psys_check_enabled(ob, psys, G.is_rendering))
1339                 return 0;
1340         
1341         if (part->ren_as==PART_DRAW_OB || part->ren_as==PART_DRAW_GR || part->ren_as==PART_DRAW_NOT)
1342                 return 1;
1343
1344         if ((re->r.scemode & R_VIEWPORT_PREVIEW) && (ob->mode & OB_MODE_PARTICLE_EDIT))
1345                 return 0;
1346
1347         if (part->ren_as == PART_DRAW_BB && part->bb_ob == NULL && RE_GetCamera(re) == NULL)
1348                 return 0;
1349
1350 /* 2. start initializing things */
1351
1352         /* last possibility to bail out! */
1353         psmd = psys_get_modifier(ob, psys);
1354         if (!(psmd->modifier.mode & eModifierMode_Render))
1355                 return 0;
1356
1357         sim.depsgraph = depsgraph;
1358         sim.scene = re->scene;
1359         sim.ob = ob;
1360         sim.psys = psys;
1361         sim.psmd = psmd;
1362
1363         if (part->phystype==PART_PHYS_KEYED)
1364                 psys_count_keyed_targets(&sim);
1365
1366         totchild=psys->totchild;
1367
1368         /* can happen for disconnected/global hair */
1369         if (part->type==PART_HAIR && !psys->childcache)
1370                 totchild= 0;
1371
1372         if (re->r.scemode & R_VIEWPORT_PREVIEW) { /* preview render */
1373                 totchild = (int)((float)totchild * (float)part->disp / 100.0f);
1374                 step_nbr = 1 << part->draw_step;
1375         }
1376         else {
1377                 step_nbr = 1 << part->ren_step;
1378         }
1379         if (ELEM(part->kink, PART_KINK_SPIRAL))
1380                 step_nbr += part->kink_extra_steps;
1381
1382         psys->flag |= PSYS_DRAWING;
1383
1384         rng= BLI_rng_new(psys->seed);
1385
1386         totpart=psys->totpart;
1387
1388         memset(&sd, 0, sizeof(ParticleStrandData));
1389         sd.override_uv = -1;
1390
1391 /* 2.1 setup material stff */
1392         ma= give_render_material(re, ob, part->omat);
1393         
1394 #if 0  /* XXX old animation system */
1395         if (ma->ipo) {
1396                 calc_ipo(ma->ipo, cfra);
1397                 execute_ipo((ID *)ma, ma->ipo);
1398         }
1399 #endif  /* XXX old animation system */
1400
1401         hasize = ma->hasize;
1402         seed = ma->seed1;
1403
1404         re->flag |= R_HALO;
1405
1406         RE_set_customdata_names(obr, &psmd->dm_final->faceData);
1407         sd.totuv = CustomData_number_of_layers(&psmd->dm_final->faceData, CD_MTFACE);
1408         sd.totcol = CustomData_number_of_layers(&psmd->dm_final->faceData, CD_MCOL);
1409
1410         if (ma->texco & TEXCO_UV && sd.totuv) {
1411                 sd.uvco = MEM_callocN(sd.totuv * 2 * sizeof(float), "particle_uvs");
1412
1413                 if (ma->strand_uvname[0]) {
1414                         sd.override_uv = CustomData_get_named_layer_index(&psmd->dm_final->faceData, CD_MTFACE, ma->strand_uvname);
1415                         sd.override_uv -= CustomData_get_layer_index(&psmd->dm_final->faceData, CD_MTFACE);
1416                 }
1417         }
1418         else
1419                 sd.uvco = NULL;
1420
1421         if (sd.totcol)
1422                 sd.mcol = MEM_callocN(sd.totcol * sizeof(MCol), "particle_mcols");
1423
1424 /* 2.2 setup billboards */
1425         if (part->ren_as == PART_DRAW_BB) {
1426                 int first_uv = CustomData_get_layer_index(&psmd->dm_final->faceData, CD_MTFACE);
1427
1428                 bb.uv[0] = CustomData_get_named_layer_index(&psmd->dm_final->faceData, CD_MTFACE, psys->bb_uvname[0]);
1429                 if (bb.uv[0] < 0)
1430                         bb.uv[0] = CustomData_get_active_layer_index(&psmd->dm_final->faceData, CD_MTFACE);
1431
1432                 bb.uv[1] = CustomData_get_named_layer_index(&psmd->dm_final->faceData, CD_MTFACE, psys->bb_uvname[1]);
1433
1434                 bb.uv[2] = CustomData_get_named_layer_index(&psmd->dm_final->faceData, CD_MTFACE, psys->bb_uvname[2]);
1435
1436                 if (first_uv >= 0) {
1437                         bb.uv[0] -= first_uv;
1438                         bb.uv[1] -= first_uv;
1439                         bb.uv[2] -= first_uv;
1440                 }
1441
1442                 bb.align = part->bb_align;
1443                 bb.anim = part->bb_anim;
1444                 bb.lock = part->draw & PART_DRAW_BB_LOCK;
1445                 bb.ob = (part->bb_ob ? part->bb_ob : RE_GetCamera(re));
1446                 bb.split_offset = part->bb_split_offset;
1447                 bb.totnum = totpart+totchild;
1448                 bb.uv_split = part->bb_uv_split;
1449         }
1450         
1451 /* 2.5 setup matrices */
1452         mul_m4_m4m4(mat, re->viewmat, ob->obmat);
1453         invert_m4_m4(ob->imat, mat);    /* need to be that way, for imat texture */
1454         transpose_m3_m4(nmat, ob->imat);
1455
1456         if (psys->flag & PSYS_USE_IMAT) {
1457                 /* psys->imat is the original emitter's inverse matrix, ob->obmat is the duplicated object's matrix */
1458                 mul_m4_m4m4(duplimat, ob->obmat, psys->imat);
1459                 use_duplimat = true;
1460         }
1461
1462 /* 2.6 setup strand rendering */
1463         if (part->ren_as == PART_DRAW_PATH && psys->pathcache) {
1464                 path_nbr = step_nbr;
1465
1466                 if (path_nbr) {
1467                         if (!ELEM(ma->material_type, MA_TYPE_HALO, MA_TYPE_WIRE)) {
1468                                 sd.orco = get_object_orco(re, psys);
1469                                 if (!sd.orco) {
1470                                         sd.orco = MEM_mallocN(3*sizeof(float)*(totpart+totchild), "particle orcos");
1471                                         set_object_orco(re, psys, sd.orco);
1472                                 }
1473                         }
1474                 }
1475
1476                 if (part->draw & PART_DRAW_REN_ADAPT) {
1477                         sd.adapt = 1;
1478                         sd.adapt_pix = (float)part->adapt_pix;
1479                         sd.adapt_angle = cosf(DEG2RADF((float)part->adapt_angle));
1480                 }
1481
1482                 if (part->draw & PART_DRAW_REN_STRAND) {
1483                         strandbuf= RE_addStrandBuffer(obr, (totpart+totchild)*(path_nbr+1));
1484                         strandbuf->ma= ma;
1485                         strandbuf->lay= ob->lay;
1486                         copy_m4_m4(strandbuf->winmat, re->winmat);
1487                         strandbuf->winx= re->winx;
1488                         strandbuf->winy= re->winy;
1489                         strandbuf->maxdepth= 2;
1490                         strandbuf->adaptcos= cosf(DEG2RADF((float)part->adapt_angle));
1491                         strandbuf->overrideuv= sd.override_uv;
1492                         strandbuf->minwidth= ma->strand_min;
1493
1494                         if (ma->strand_widthfade == 0.0f)
1495                                 strandbuf->widthfade= -1.0f;
1496                         else if (ma->strand_widthfade >= 1.0f)
1497                                 strandbuf->widthfade= 2.0f - ma->strand_widthfade;
1498                         else
1499                                 strandbuf->widthfade= 1.0f/MAX2(ma->strand_widthfade, 1e-5f);
1500
1501                         if (part->flag & PART_HAIR_BSPLINE)
1502                                 strandbuf->flag |= R_STRAND_BSPLINE;
1503                         if (ma->mode & MA_STR_B_UNITS)
1504                                 strandbuf->flag |= R_STRAND_B_UNITS;
1505
1506                         svert= strandbuf->vert;
1507
1508                         if (re->r.mode & R_SPEED)
1509                                 do_surfacecache = true;
1510                         else if ((re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) && (re->wrld.ao_gather_method == WO_AOGATHER_APPROX))
1511                                 if (ma->amb != 0.0f)
1512                                         do_surfacecache = true;
1513
1514                         totface= psmd->dm_final->getNumTessFaces(psmd->dm_final);
1515                         index_mf_to_mpoly = psmd->dm_final->getTessFaceDataArray(psmd->dm_final, CD_ORIGINDEX);
1516                         index_mp_to_orig = psmd->dm_final->getPolyDataArray(psmd->dm_final, CD_ORIGINDEX);
1517                         if (index_mf_to_mpoly == NULL) {
1518                                 index_mp_to_orig = NULL;
1519                         }
1520                         for (a=0; a<totface; a++)
1521                                 strandbuf->totbound = max_ii(strandbuf->totbound, (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, a): a);
1522
1523                         strandbuf->totbound++;
1524                         strandbuf->bound= MEM_callocN(sizeof(StrandBound)*strandbuf->totbound, "StrandBound");
1525                         sbound= strandbuf->bound;
1526                         sbound->start= sbound->end= 0;
1527                 }
1528         }
1529
1530         if (sd.orco == NULL) {
1531                 sd.orco = MEM_mallocN(3 * sizeof(float), "particle orco");
1532                 orco1 = 1;
1533         }
1534
1535         if (path_nbr == 0)
1536                 psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
1537
1538 /* 3. start creating renderable things */
1539         for (a=0, pa=pars; a<totpart+totchild; a++, pa++, seed++) {
1540                 random = BLI_rng_get_float(rng);
1541                 /* setup per particle individual stuff */
1542                 if (a<totpart) {
1543                         if (pa->flag & PARS_UNEXIST) continue;
1544
1545                         pa_time=(cfra-pa->time)/pa->lifetime;
1546                         pa_birthtime = pa->time;
1547                         pa_dietime = pa->dietime;
1548
1549                         hasize = ma->hasize;
1550
1551                         /* XXX 'tpsys' is alwyas NULL, this code won't run! */
1552                         /* get orco */
1553                         if (tpsys && part->phystype == PART_PHYS_NO) {
1554                                 tpa = tpsys->particles + pa->num;
1555                                 psys_particle_on_emitter(
1556                                         psmd,
1557                                         tpart->from, tpa->num, pa->num_dmcache, tpa->fuv,
1558                                         tpa->foffset, co, nor, NULL, NULL, sd.orco, NULL);
1559                         }
1560                         else {
1561                                 psys_particle_on_emitter(
1562                                         psmd,
1563                                         part->from, pa->num, pa->num_dmcache,
1564                                         pa->fuv, pa->foffset, co, nor, NULL, NULL, sd.orco, NULL);
1565                         }
1566
1567                         /* get uvco & mcol */
1568                         num= pa->num_dmcache;
1569
1570                         if (num == DMCACHE_NOTFOUND)
1571                                 if (pa->num < psmd->dm_final->getNumTessFaces(psmd->dm_final))
1572                                         num= pa->num;
1573
1574                         get_particle_uvco_mcol(part->from, psmd->dm_final, pa->fuv, num, &sd);
1575
1576                         pa_size = pa->size;
1577
1578                         r_tilt = 2.0f*(psys_frand(psys, a) - 0.5f);
1579                         r_length = psys_frand(psys, a+1);
1580
1581                         if (path_nbr) {
1582                                 cache = psys->pathcache[a];
1583                                 max_k = (int)cache->segments;
1584                         }
1585
1586                         if (totchild && (part->draw&PART_DRAW_PARENT)==0) continue;
1587                 }
1588                 else {
1589                         ChildParticle *cpa= psys->child+a-totpart;
1590
1591                         if (path_nbr) {
1592                                 cache = psys->childcache[a-totpart];
1593
1594                                 if (cache->segments < 0)
1595                                         continue;
1596
1597                                 max_k = (int)cache->segments;
1598                         }
1599                         
1600                         pa_time = psys_get_child_time(psys, cpa, cfra, &pa_birthtime, &pa_dietime);
1601                         pa_size = psys_get_child_size(psys, cpa, cfra, &pa_time);
1602
1603                         r_tilt = 2.0f*(psys_frand(psys, a + 21) - 0.5f);
1604                         r_length = psys_frand(psys, a + 22);
1605
1606                         num = cpa->num;
1607
1608                         /* get orco */
1609                         if (part->childtype == PART_CHILD_FACES) {
1610                                 psys_particle_on_emitter(
1611                                         psmd,
1612                                         PART_FROM_FACE, cpa->num, DMCACHE_ISCHILD,
1613                                         cpa->fuv, cpa->foffset, co, nor, NULL, NULL, sd.orco, NULL);
1614                         }
1615                         else {
1616                                 ParticleData *par = psys->particles + cpa->parent;
1617                                 psys_particle_on_emitter(
1618                                         psmd,
1619                                         part->from, par->num, DMCACHE_ISCHILD, par->fuv,
1620                                         par->foffset, co, nor, NULL, NULL, sd.orco, NULL);
1621                         }
1622
1623                         /* get uvco & mcol */
1624                         if (part->childtype==PART_CHILD_FACES) {
1625                                 get_particle_uvco_mcol(PART_FROM_FACE, psmd->dm_final, cpa->fuv, cpa->num, &sd);
1626                         }
1627                         else {
1628                                 ParticleData *parent = psys->particles + cpa->parent;
1629                                 num = parent->num_dmcache;
1630
1631                                 if (num == DMCACHE_NOTFOUND)
1632                                         if (parent->num < psmd->dm_final->getNumTessFaces(psmd->dm_final))
1633                                                 num = parent->num;
1634
1635                                 get_particle_uvco_mcol(part->from, psmd->dm_final, parent->fuv, num, &sd);
1636                         }
1637
1638                         if (strandbuf) {
1639                                 int orignum = (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, cpa->num) : cpa->num;
1640
1641                                 if ((orignum > sbound - strandbuf->bound) &&
1642                                     (orignum < strandbuf->totbound))
1643                                 {
1644                                         sbound = &strandbuf->bound[orignum];
1645                                         sbound->start = sbound->end = obr->totstrand;
1646                                 }
1647                         }
1648                 }
1649
1650                 /* TEXCO_PARTICLE */
1651                 pa_co[0] = pa_time;
1652                 pa_co[1] = 0.f;
1653                 pa_co[2] = 0.f;
1654
1655                 /* surface normal shading setup */
1656                 if (ma->mode_l & MA_STR_SURFDIFF) {
1657                         mul_m3_v3(nmat, nor);
1658                         sd.surfnor= nor;
1659                 }
1660                 else
1661                         sd.surfnor= NULL;
1662
1663                 /* strand render setup */
1664                 if (strandbuf) {
1665                         strand= RE_findOrAddStrand(obr, obr->totstrand++);
1666                         strand->buffer= strandbuf;
1667                         strand->vert= svert;
1668                         copy_v3_v3(strand->orco, sd.orco);
1669
1670                         if (sd.surfnor) {
1671                                 float *snor= RE_strandren_get_surfnor(obr, strand, 1);
1672                                 copy_v3_v3(snor, sd.surfnor);
1673                         }
1674
1675                         if (do_surfacecache && num >= 0) {
1676                                 int *facenum= RE_strandren_get_face(obr, strand, 1);
1677                                 *facenum= num;
1678                         }
1679
1680                         if (sd.uvco) {
1681                                 for (i=0; i<sd.totuv; i++) {
1682                                         if (i != sd.override_uv) {
1683                                                 float *uv= RE_strandren_get_uv(obr, strand, i, NULL, 1);
1684
1685                                                 uv[0]= sd.uvco[2*i];
1686                                                 uv[1]= sd.uvco[2*i+1];
1687                                         }
1688                                 }
1689                         }
1690                         if (sd.mcol) {
1691                                 for (i=0; i<sd.totcol; i++) {
1692                                         MCol *mc= RE_strandren_get_mcol(obr, strand, i, NULL, 1);
1693                                         *mc = sd.mcol[i];
1694                                 }
1695                         }
1696
1697                         sbound->end++;
1698                 }
1699
1700                 /* strandco computation setup */
1701                 if (path_nbr) {
1702                         strandlen= 0.0f;
1703                         curlen= 0.0f;
1704                         for (k=1; k<=path_nbr; k++)
1705                                 if (k<=max_k)
1706                                         strandlen += len_v3v3((cache+k-1)->co, (cache+k)->co);
1707                 }
1708
1709                 if (path_nbr) {
1710                         /* render strands */
1711                         for (k=0; k<=path_nbr; k++) {
1712                                 float time;
1713
1714                                 if (k<=max_k) {
1715                                         copy_v3_v3(state.co, (cache+k)->co);
1716                                         copy_v3_v3(state.vel, (cache+k)->vel);
1717                                 }
1718                                 else
1719                                         continue;
1720
1721                                 if (k > 0)
1722                                         curlen += len_v3v3((cache+k-1)->co, (cache+k)->co);
1723                                 time= curlen/strandlen;
1724
1725                                 copy_v3_v3(loc, state.co);
1726                                 mul_m4_v3(re->viewmat, loc);
1727
1728                                 if (strandbuf) {
1729                                         copy_v3_v3(svert->co, loc);
1730                                         svert->strandco= -1.0f + 2.0f*time;
1731                                         svert++;
1732                                         strand->totvert++;
1733                                 }
1734                                 else {
1735                                         sd.size = hasize;
1736
1737                                         if (k==1) {
1738                                                 sd.first = 1;
1739                                                 sd.time = 0.0f;
1740                                                 sub_v3_v3v3(loc0, loc1, loc);
1741                                                 add_v3_v3v3(loc0, loc1, loc0);
1742
1743                                                 particle_curve(re, obr, psmd->dm_final, ma, &sd, loc1, loc0, seed, pa_co);
1744                                         }
1745
1746                                         sd.first = 0;
1747                                         sd.time = time;
1748
1749                                         if (k)
1750                                                 particle_curve(re, obr, psmd->dm_final, ma, &sd, loc, loc1, seed, pa_co);
1751
1752                                         copy_v3_v3(loc1, loc);
1753                                 }
1754                         }
1755
1756                 }
1757                 else {
1758                         /* render normal particles */
1759                         if (part->trail_count > 1) {
1760                                 float length = part->path_end * (1.0f - part->randlength * r_length);
1761                                 int trail_count = part->trail_count * (1.0f - part->randlength * r_length);
1762                                 float ct = (part->draw & PART_ABS_PATH_TIME) ? cfra : pa_time;
1763                                 float dt = length / (trail_count ? (float)trail_count : 1.0f);
1764
1765                                 /* make sure we have pointcache in memory before getting particle on path */
1766                                 psys_make_temp_pointcache(ob, psys);
1767
1768                                 for (i=0; i < trail_count; i++, ct -= dt) {
1769                                         if (part->draw & PART_ABS_PATH_TIME) {
1770                                                 if (ct < pa_birthtime || ct > pa_dietime)
1771                                                         continue;
1772                                         }
1773                                         else if (ct < 0.0f || ct > 1.0f)
1774                                                 continue;
1775
1776                                         state.time = (part->draw & PART_ABS_PATH_TIME) ? -ct : ct;
1777                                         psys_get_particle_on_path(&sim, a, &state, 1);
1778
1779                                         if (psys->parent)
1780                                                 mul_m4_v3(psys->parent->obmat, state.co);
1781
1782                                         if (use_duplimat)
1783                                                 mul_m4_v4(duplimat, state.co);
1784
1785                                         if (part->ren_as == PART_DRAW_BB) {
1786                                                 bb.random = random;
1787                                                 bb.offset[0] = part->bb_offset[0];
1788                                                 bb.offset[1] = part->bb_offset[1];
1789                                                 bb.size[0] = part->bb_size[0] * pa_size;
1790                                                 if (part->bb_align==PART_BB_VEL) {
1791                                                         float pa_vel = len_v3(state.vel);
1792                                                         float head = part->bb_vel_head*pa_vel;
1793                                                         float tail = part->bb_vel_tail*pa_vel;
1794                                                         bb.size[1] = part->bb_size[1]*pa_size + head + tail;
1795                                                         /* use offset to adjust the particle center. this is relative to size, so need to divide! */
1796                                                         if (bb.size[1] > 0.0f)
1797                                                                 bb.offset[1] += (head-tail) / bb.size[1];
1798                                                 }
1799                                                 else
1800                                                         bb.size[1] = part->bb_size[1] * pa_size;
1801                                                 bb.tilt = part->bb_tilt * (1.0f - part->bb_rand_tilt * r_tilt);
1802                                                 bb.time = ct;
1803                                                 bb.num = a;
1804                                         }
1805
1806                                         pa_co[0] = (part->draw & PART_ABS_PATH_TIME) ? (ct-pa_birthtime)/(pa_dietime-pa_birthtime) : ct;
1807                                         pa_co[1] = (float)i/(float)(trail_count-1);
1808
1809                                         particle_normal_ren(part->ren_as, part, re, obr, psmd->dm_final, ma, &sd, &bb, &state, seed, hasize, pa_co);
1810                                 }
1811                         }
1812                         else {
1813                                 state.time=cfra;
1814                                 if (psys_get_particle_state(&sim, a, &state, 0)==0)
1815                                         continue;
1816
1817                                 if (psys->parent)
1818                                         mul_m4_v3(psys->parent->obmat, state.co);
1819
1820                                 if (use_duplimat)
1821                                         mul_m4_v3(duplimat, state.co);
1822
1823                                 if (part->ren_as == PART_DRAW_BB) {
1824                                         bb.random = random;
1825                                         bb.offset[0] = part->bb_offset[0];
1826                                         bb.offset[1] = part->bb_offset[1];
1827                                         bb.size[0] = part->bb_size[0] * pa_size;
1828                                         if (part->bb_align==PART_BB_VEL) {
1829                                                 float pa_vel = len_v3(state.vel);
1830                                                 float head = part->bb_vel_head*pa_vel;
1831                                                 float tail = part->bb_vel_tail*pa_vel;
1832                                                 bb.size[1] = part->bb_size[1]*pa_size + head + tail;
1833                                                 /* use offset to adjust the particle center. this is relative to size, so need to divide! */
1834                                                 if (bb.size[1] > 0.0f)
1835                                                         bb.offset[1] += (head-tail) / bb.size[1];
1836                                         }
1837                                         else
1838                                                 bb.size[1] = part->bb_size[1] * pa_size;
1839                                         bb.tilt = part->bb_tilt * (1.0f - part->bb_rand_tilt * r_tilt);
1840                                         bb.time = pa_time;
1841                                         bb.num = a;
1842                                         bb.lifetime = pa_dietime-pa_birthtime;
1843                                 }
1844
1845                                 particle_normal_ren(part->ren_as, part, re, obr, psmd->dm_final, ma, &sd, &bb, &state, seed, hasize, pa_co);
1846                         }
1847                 }
1848
1849                 if (orco1==0)
1850                         sd.orco+=3;
1851
1852                 if (re->test_break(re->tbh))
1853                         break;
1854         }
1855
1856         if (do_surfacecache)
1857                 strandbuf->surface= cache_strand_surface(re, obr, psmd->dm_final, mat, timeoffset);
1858
1859 /* 4. clean up */
1860 #if 0  /* XXX old animation system */
1861         if (ma) do_mat_ipo(re->scene, ma);
1862 #endif  /* XXX old animation system */
1863
1864         if (orco1)
1865                 MEM_freeN(sd.orco);
1866
1867         if (sd.uvco)
1868                 MEM_freeN(sd.uvco);
1869         
1870         if (sd.mcol)
1871                 MEM_freeN(sd.mcol);
1872
1873         if (states)
1874                 MEM_freeN(states);
1875         
1876         BLI_rng_free(rng);
1877
1878         psys->flag &= ~PSYS_DRAWING;
1879
1880         if (psys->lattice_deform_data) {
1881                 end_latt_deform(psys->lattice_deform_data);
1882                 psys->lattice_deform_data = NULL;
1883         }
1884
1885         if (path_nbr && (ma->mode_l & MA_TANGENT_STR)==0)
1886                 calc_vertexnormals(re, obr, 1, 0, 0);
1887
1888         return 1;
1889 }
1890
1891 /* ------------------------------------------------------------------------- */
1892 /* Halo's                                                                                                                                */
1893 /* ------------------------------------------------------------------------- */
1894
1895 static void make_render_halos(Render *re, ObjectRen *obr, Mesh *UNUSED(me), int totvert, MVert *mvert, Material *ma, float *orco)
1896 {
1897         Object *ob= obr->ob;
1898         HaloRen *har;
1899         float xn, yn, zn, nor[3], view[3];
1900         float vec[3], hasize, mat[4][4], imat[3][3];
1901         int a, ok, seed= ma->seed1;
1902
1903         mul_m4_m4m4(mat, re->viewmat, ob->obmat);
1904         copy_m3_m4(imat, ob->imat);
1905
1906         re->flag |= R_HALO;
1907
1908         for (a=0; a<totvert; a++, mvert++) {
1909                 ok= 1;
1910
1911                 if (ok) {
1912                         hasize= ma->hasize;
1913
1914                         copy_v3_v3(vec, mvert->co);
1915                         mul_m4_v3(mat, vec);
1916
1917                         if (ma->mode & MA_HALOPUNO) {
1918                                 xn= mvert->no[0];
1919                                 yn= mvert->no[1];
1920                                 zn= mvert->no[2];
1921
1922                                 /* transpose ! */
1923                                 nor[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
1924                                 nor[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
1925                                 nor[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
1926                                 normalize_v3(nor);
1927
1928                                 copy_v3_v3(view, vec);
1929                                 normalize_v3(view);
1930
1931                                 zn = dot_v3v3(nor, view);
1932                                 if (zn>=0.0f) hasize= 0.0f;
1933                                 else hasize*= zn*zn*zn*zn;
1934                         }
1935
1936                         if (orco) har= RE_inithalo(re, obr, ma, vec, NULL, orco, hasize, 0.0, seed);
1937                         else har= RE_inithalo(re, obr, ma, vec, NULL, mvert->co, hasize, 0.0, seed);
1938                         if (har) har->lay= ob->lay;
1939                 }
1940                 if (orco) orco+= 3;
1941                 seed++;
1942         }
1943 }
1944
1945 static int verghalo(const void *a1, const void *a2)
1946 {
1947         const HaloRen *har1= *(const HaloRen**)a1;
1948         const HaloRen *har2= *(const HaloRen**)a2;
1949         
1950         if (har1->zs < har2->zs) return 1;
1951         else if (har1->zs > har2->zs) return -1;
1952         return 0;
1953 }
1954
1955 static void sort_halos(Render *re, int totsort)
1956 {
1957         ObjectRen *obr;
1958         HaloRen *har= NULL, **haso;
1959         int a;
1960
1961         if (re->tothalo==0) return;
1962
1963         re->sortedhalos= MEM_callocN(sizeof(HaloRen*)*re->tothalo, "sorthalos");
1964         haso= re->sortedhalos;
1965
1966         for (obr=re->objecttable.first; obr; obr=obr->next) {
1967                 for (a=0; a<obr->tothalo; a++) {
1968                         if ((a & 255)==0) har= obr->bloha[a>>8];
1969                         else har++;
1970
1971                         *(haso++)= har;
1972                 }
1973         }
1974
1975         qsort(re->sortedhalos, totsort, sizeof(HaloRen*), verghalo);
1976 }
1977
1978 /* ------------------------------------------------------------------------- */
1979 /* Displacement Mapping                                                                                                          */
1980 /* ------------------------------------------------------------------------- */
1981
1982 static short test_for_displace(Render *re, Object *ob)
1983 {
1984         /* return 1 when this object uses displacement textures. */
1985         Material *ma;
1986         int i;
1987         
1988         for (i=1; i<=ob->totcol; i++) {
1989                 ma=give_render_material(re, ob, i);
1990                 /* ma->mapto is ORed total of all mapto channels */
1991                 if (ma && (ma->mapto & MAP_DISPLACE)) return 1;
1992         }
1993         return 0;
1994 }
1995
1996 static void displace_render_vert(Render *re, ObjectRen *obr, ShadeInput *shi, VertRen *vr, int vindex, float *scale)
1997 {
1998         MTFace *tface;
1999         short texco= shi->mat->texco;
2000         float sample=0, displace[3];
2001         char *name;
2002         int i;
2003
2004         /* shi->co is current render coord, just make sure at least some vector is here */
2005         copy_v3_v3(shi->co, vr->co);
2006         /* vertex normal is used for textures type 'col' and 'var' */
2007         copy_v3_v3(shi->vn, vr->n);
2008
2009         if (texco & TEXCO_UV) {
2010                 shi->totuv= 0;
2011                 shi->actuv= obr->actmtface;
2012
2013                 for (i=0; (tface=RE_vlakren_get_tface(obr, shi->vlr, i, &name, 0)); i++) {
2014                         ShadeInputUV *suv= &shi->uv[i];
2015
2016                         /* shi.uv needs scale correction from tface uv */
2017                         suv->uv[0]= 2*tface->uv[vindex][0]-1.0f;
2018                         suv->uv[1]= 2*tface->uv[vindex][1]-1.0f;
2019                         suv->uv[2]= 0.0f;
2020                         suv->name= name;
2021                         shi->totuv++;
2022                 }
2023         }
2024
2025         /* set all rendercoords, 'texco' is an ORed value for all textures needed */
2026         if ((texco & TEXCO_ORCO) && (vr->orco)) {
2027                 copy_v3_v3(shi->lo, vr->orco);
2028         }
2029         if (texco & TEXCO_GLOB) {
2030                 copy_v3_v3(shi->gl, shi->co);
2031                 mul_m4_v3(re->viewinv, shi->gl);
2032         }
2033         if (texco & TEXCO_NORM) {
2034                 copy_v3_v3(shi->orn, shi->vn);
2035         }
2036         if (texco & TEXCO_REFL) {
2037                 /* not (yet?) */
2038         }
2039         if (texco & TEXCO_STRESS) {
2040                 const float *s= RE_vertren_get_stress(obr, vr, 0);
2041
2042                 if (s) {
2043                         shi->stress= *s;
2044                         if (shi->stress<1.0f) shi->stress-= 1.0f;
2045                         else shi->stress= (shi->stress-1.0f)/shi->stress;
2046                 }
2047                 else
2048                         shi->stress= 0.0f;
2049         }
2050
2051         shi->displace[0]= shi->displace[1]= shi->displace[2]= 0.0;
2052         
2053         do_material_tex(shi, re);
2054         
2055         //printf("no=%f, %f, %f\nbefore co=%f, %f, %f\n", vr->n[0], vr->n[1], vr->n[2], 
2056         //vr->co[0], vr->co[1], vr->co[2]);
2057
2058         displace[0]= shi->displace[0] * scale[0];
2059         displace[1]= shi->displace[1] * scale[1];
2060         displace[2]= shi->displace[2] * scale[2];
2061
2062         /* 0.5 could become button once?  */
2063         vr->co[0] += displace[0]; 
2064         vr->co[1] += displace[1];
2065         vr->co[2] += displace[2];
2066         
2067         //printf("after co=%f, %f, %f\n", vr->co[0], vr->co[1], vr->co[2]); 
2068         
2069         /* we just don't do this vertex again, bad luck for other face using same vertex with
2070          * different material... */
2071         vr->flag |= 1;
2072         
2073         /* Pass sample back so displace_face can decide which way to split the quad */
2074         sample  = shi->displace[0]*shi->displace[0];
2075         sample += shi->displace[1]*shi->displace[1];
2076         sample += shi->displace[2]*shi->displace[2];
2077         
2078         vr->accum=sample; 
2079         /* Should be sqrt(sample), but I'm only looking for "bigger".  Save the cycles. */
2080         return;
2081 }
2082
2083 static void displace_render_face(Render *re, ObjectRen *obr, VlakRen *vlr, float *scale)
2084 {
2085         ShadeInput shi;
2086
2087         /* Warning, This is not that nice, and possibly a bit slow,
2088          * however some variables were not initialized properly in, unless using shade_input_initialize(...), we need to do a memset */
2089         memset(&shi, 0, sizeof(ShadeInput)); 
2090         /* end warning! - Campbell */
2091         
2092         /* set up shadeinput struct for multitex() */
2093         
2094         /* memset above means we don't need this */
2095         /*shi.osatex= 0;*/              /* signal not to use dx[] and dy[] texture AA vectors */
2096
2097         shi.obr= obr;
2098         shi.vlr= vlr;           /* current render face */
2099         shi.mat= vlr->mat;              /* current input material */
2100         shi.thread= 0;
2101         
2102         /* TODO, assign these, displacement with new bumpmap is skipped without - campbell */
2103 #if 0
2104         /* order is not known ? */
2105         shi.v1= vlr->v1;
2106         shi.v2= vlr->v2;
2107         shi.v3= vlr->v3;
2108 #endif
2109
2110         /* Displace the verts, flag is set when done */
2111         if (!vlr->v1->flag)
2112                 displace_render_vert(re, obr, &shi, vlr->v1, 0,  scale);
2113         
2114         if (!vlr->v2->flag)
2115                 displace_render_vert(re, obr, &shi, vlr->v2, 1, scale);
2116
2117         if (!vlr->v3->flag)
2118                 displace_render_vert(re, obr, &shi, vlr->v3, 2, scale);
2119
2120         if (vlr->v4) {
2121                 if (!vlr->v4->flag)
2122                         displace_render_vert(re, obr, &shi, vlr->v4, 3, scale);
2123
2124                 /*      closest in displace value.  This will help smooth edges.   */ 
2125                 if (fabsf(vlr->v1->accum - vlr->v3->accum) > fabsf(vlr->v2->accum - vlr->v4->accum)) vlr->flag |=  R_DIVIDE_24;
2126                 else                                                                                 vlr->flag &= ~R_DIVIDE_24;
2127         }
2128         
2129         /* Recalculate the face normal  - if flipped before, flip now */
2130         if (vlr->v4) {
2131                 normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
2132         }
2133         else {
2134                 normal_tri_v3(vlr->n, vlr->v3->co, vlr->v2->co, vlr->v1->co);
2135         }
2136 }
2137
2138 static void displace(Render *re, ObjectRen *obr)
2139 {
2140         VertRen *vr;
2141         VlakRen *vlr;
2142 //      float min[3]={1e30, 1e30, 1e30}, max[3]={-1e30, -1e30, -1e30};
2143         float scale[3]={1.0f, 1.0f, 1.0f}, temp[3];//, xn
2144         int i; //, texflag=0;
2145         Object *obt;
2146                 
2147         /* Object Size with parenting */
2148         obt=obr->ob;
2149         while (obt) {
2150                 mul_v3_v3v3(temp, obt->size, obt->dscale);
2151                 scale[0]*=temp[0]; scale[1]*=temp[1]; scale[2]*=temp[2];
2152                 obt=obt->parent;
2153         }
2154         
2155         /* Clear all flags */
2156         for (i=0; i<obr->totvert; i++) {
2157                 vr= RE_findOrAddVert(obr, i);
2158                 vr->flag= 0;
2159         }
2160
2161         for (i=0; i<obr->totvlak; i++) {
2162                 vlr=RE_findOrAddVlak(obr, i);
2163                 displace_render_face(re, obr, vlr, scale);
2164         }
2165         
2166         /* Recalc vertex normals */
2167         calc_vertexnormals(re, obr, 1, 0, 0);
2168 }
2169
2170 /* ------------------------------------------------------------------------- */
2171 /* Metaball                                                                                                                              */
2172 /* ------------------------------------------------------------------------- */
2173
2174 static void init_render_mball(Depsgraph *depsgraph, Render *re, ObjectRen *obr)
2175 {
2176         Object *ob= obr->ob;
2177         DispList *dl;
2178         VertRen *ver;
2179         VlakRen *vlr, *vlr1;
2180         Material *ma;
2181         float *data, *nors, *orco=NULL, mat[4][4], imat[3][3], xn, yn, zn;
2182         int a, need_orco, vlakindex, *index, negative_scale;
2183         ListBase dispbase= {NULL, NULL};
2184
2185         if (ob!=BKE_mball_basis_find(re->scene, ob))
2186                 return;
2187
2188         mul_m4_m4m4(mat, re->viewmat, ob->obmat);
2189         invert_m4_m4(ob->imat, mat);
2190         copy_m3_m4(imat, ob->imat);
2191         negative_scale = is_negative_m4(mat);
2192
2193         ma= give_render_material(re, ob, 1);
2194
2195         need_orco= 0;
2196         if (ma->texco & TEXCO_ORCO) {
2197                 need_orco= 1;
2198         }
2199
2200         BKE_displist_make_mball_forRender(depsgraph, re->scene, ob, &dispbase);
2201         dl= dispbase.first;
2202         if (dl == NULL) return;
2203
2204         data= dl->verts;
2205         nors= dl->nors;
2206         if (need_orco) {
2207                 orco= get_object_orco(re, ob);
2208
2209                 if (!orco) {
2210                         /* orco hasn't been found in cache - create new one and add to cache */
2211                         orco= BKE_mball_make_orco(ob, &dispbase);
2212                         set_object_orco(re, ob, orco);
2213                 }
2214         }
2215
2216         for (a=0; a<dl->nr; a++, data+=3, nors+=3) {
2217
2218                 ver= RE_findOrAddVert(obr, obr->totvert++);
2219                 copy_v3_v3(ver->co, data);
2220                 mul_m4_v3(mat, ver->co);
2221
2222                 /* render normals are inverted */
2223                 xn= -nors[0];
2224                 yn= -nors[1];
2225                 zn= -nors[2];
2226
2227                 /* transpose ! */
2228                 ver->n[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
2229                 ver->n[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
2230                 ver->n[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
2231                 normalize_v3(ver->n);
2232                 //if (ob->transflag & OB_NEG_SCALE) negate_v3(ver->n);
2233                 
2234                 if (need_orco) {
2235                         ver->orco= orco;
2236                         orco+=3;
2237                 }
2238         }
2239
2240         index= dl->index;
2241         for (a=0; a<dl->parts; a++, index+=4) {
2242
2243                 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2244                 vlr->v1= RE_findOrAddVert(obr, index[0]);
2245                 vlr->v2= RE_findOrAddVert(obr, index[1]);
2246                 vlr->v3= RE_findOrAddVert(obr, index[2]);
2247                 vlr->v4 = NULL;
2248
2249                 if (negative_scale)
2250                         normal_tri_v3(vlr->n, vlr->v1->co, vlr->v2->co, vlr->v3->co);
2251                 else
2252                         normal_tri_v3(vlr->n, vlr->v3->co, vlr->v2->co, vlr->v1->co);
2253
2254                 vlr->mat= ma;
2255                 vlr->flag= ME_SMOOTH;
2256                 vlr->ec= 0;
2257
2258                 /* mball -too bad- always has triangles, because quads can be non-planar */
2259                 if (index[3] && index[3]!=index[2]) {
2260                         vlr1= RE_findOrAddVlak(obr, obr->totvlak++);
2261                         vlakindex= vlr1->index;
2262                         *vlr1= *vlr;
2263                         vlr1->index= vlakindex;
2264                         vlr1->v2= vlr1->v3;
2265                         vlr1->v3= RE_findOrAddVert(obr, index[3]);
2266                         if (negative_scale)
2267                                 normal_tri_v3(vlr1->n, vlr1->v1->co, vlr1->v2->co, vlr1->v3->co);
2268                         else
2269                                 normal_tri_v3(vlr1->n, vlr1->v3->co, vlr1->v2->co, vlr1->v1->co);
2270                 }
2271         }
2272
2273         /* enforce display lists remade */
2274         BKE_displist_free(&dispbase);
2275 }
2276
2277 /* ------------------------------------------------------------------------- */
2278 /* Surfaces and Curves                                                                                                           */
2279 /* ------------------------------------------------------------------------- */
2280
2281 /* returns amount of vertices added for orco */
2282 static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar, float *orco, float mat[4][4])
2283 {
2284         VertRen *v1, *v2, *v3, *v4, *ver;
2285         VlakRen *vlr, *vlr1, *vlr2, *vlr3;
2286         float *data, n1[3];
2287         int u, v, orcoret= 0;
2288         int p1, p2, p3, p4, a;
2289         int sizeu, nsizeu, sizev, nsizev;
2290         int startvert, startvlak;
2291         
2292         startvert= obr->totvert;
2293         nsizeu = sizeu = dl->parts; nsizev = sizev = dl->nr; 
2294         
2295         data= dl->verts;
2296         for (u = 0; u < sizeu; u++) {
2297                 v1 = RE_findOrAddVert(obr, obr->totvert++); /* save this for possible V wrapping */
2298                 copy_v3_v3(v1->co, data); data += 3;
2299                 if (orco) {
2300                         v1->orco= orco; orco+= 3; orcoret++;
2301                 }
2302                 mul_m4_v3(mat, v1->co);
2303                 
2304                 for (v = 1; v < sizev; v++) {
2305                         ver= RE_findOrAddVert(obr, obr->totvert++);
2306                         copy_v3_v3(ver->co, data); data += 3;
2307                         if (orco) {
2308                                 ver->orco= orco; orco+= 3; orcoret++;
2309                         }
2310                         mul_m4_v3(mat, ver->co);
2311                 }
2312                 /* if V-cyclic, add extra vertices at end of the row */
2313                 if (dl->flag & DL_CYCL_U) {
2314                         ver= RE_findOrAddVert(obr, obr->totvert++);
2315                         copy_v3_v3(ver->co, v1->co);
2316                         if (orco) {
2317                                 ver->orco= orco; orco+=3; orcoret++; //orcobase + 3*(u*sizev + 0);
2318                         }
2319                 }
2320         }
2321         
2322         /* Done before next loop to get corner vert */
2323         if (dl->flag & DL_CYCL_U) nsizev++;
2324         if (dl->flag & DL_CYCL_V) nsizeu++;
2325         
2326         /* if U cyclic, add extra row at end of column */
2327         if (dl->flag & DL_CYCL_V) {
2328                 for (v = 0; v < nsizev; v++) {
2329                         v1= RE_findOrAddVert(obr, startvert + v);
2330                         ver= RE_findOrAddVert(obr, obr->totvert++);
2331                         copy_v3_v3(ver->co, v1->co);
2332                         if (orco) {
2333                                 ver->orco= orco; orco+=3; orcoret++; //ver->orco= orcobase + 3*(0*sizev + v);
2334                         }
2335                 }
2336         }
2337         
2338         sizeu = nsizeu;
2339         sizev = nsizev;
2340         
2341         startvlak= obr->totvlak;
2342         
2343         for (u = 0; u < sizeu - 1; u++) {
2344                 p1 = startvert + u * sizev; /* walk through face list */
2345                 p2 = p1 + 1;
2346                 p3 = p2 + sizev;
2347                 p4 = p3 - 1;
2348                 
2349                 for (v = 0; v < sizev - 1; v++) {
2350                         v1= RE_findOrAddVert(obr, p1);
2351                         v2= RE_findOrAddVert(obr, p2);
2352                         v3= RE_findOrAddVert(obr, p3);
2353                         v4= RE_findOrAddVert(obr, p4);
2354                         
2355                         vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2356                         vlr->v1= v1; vlr->v2= v2; vlr->v3= v3; vlr->v4= v4;
2357                         
2358                         normal_quad_v3(n1, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
2359                         
2360                         copy_v3_v3(vlr->n, n1);
2361                         
2362                         vlr->mat= matar[ dl->col];
2363                         vlr->ec= ME_V1V2+ME_V2V3;
2364                         vlr->flag= dl->rt;
2365                         
2366                         add_v3_v3(v1->n, n1);
2367                         add_v3_v3(v2->n, n1);
2368                         add_v3_v3(v3->n, n1);
2369                         add_v3_v3(v4->n, n1);
2370                         
2371                         p1++; p2++; p3++; p4++;
2372                 }
2373         }
2374         /* fix normals for U resp. V cyclic faces */
2375         sizeu--; sizev--;  /* dec size for face array */
2376         if (dl->flag & DL_CYCL_V) {
2377                 
2378                 for (v = 0; v < sizev; v++) {
2379                         /* optimize! :*/
2380                         vlr= RE_findOrAddVlak(obr, UVTOINDEX(sizeu - 1, v));
2381                         vlr1= RE_findOrAddVlak(obr, UVTOINDEX(0, v));
2382                         add_v3_v3(vlr1->v1->n, vlr->n);
2383                         add_v3_v3(vlr1->v2->n, vlr->n);
2384                         add_v3_v3(vlr->v3->n, vlr1->n);
2385                         add_v3_v3(vlr->v4->n, vlr1->n);
2386                 }
2387         }
2388         if (dl->flag & DL_CYCL_U) {
2389                 
2390                 for (u = 0; u < sizeu; u++) {
2391                         /* optimize! :*/
2392                         vlr= RE_findOrAddVlak(obr, UVTOINDEX(u, 0));
2393                         vlr1= RE_findOrAddVlak(obr, UVTOINDEX(u, sizev-1));
2394                         add_v3_v3(vlr1->v2->n, vlr->n);
2395                         add_v3_v3(vlr1->v3->n, vlr->n);
2396                         add_v3_v3(vlr->v1->n, vlr1->n);
2397                         add_v3_v3(vlr->v4->n, vlr1->n);
2398                 }
2399         }
2400
2401         /* last vertex is an extra case: 
2402          *
2403          *     ^     ()----()----()----()
2404          *     |     |     |     ||     |
2405          *     u     |     |(0,n)||(0,0)|
2406          *     |     |     ||     |
2407          *     ()====()====[]====()
2408          *     |     |     ||     |
2409          *     |     |(m,n)||(m,0)|
2410          *     |     |     ||     |
2411          *     ()----()----()----()
2412          *     v ->
2413          *  
2414          *  vertex [] is no longer shared, therefore distribute
2415          *  normals of the surrounding faces to all of the duplicates of []
2416          */
2417
2418         if ((dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U)) {
2419                 vlr= RE_findOrAddVlak(obr, UVTOINDEX(sizeu - 1, sizev - 1)); /* (m, n) */
2420                 vlr1= RE_findOrAddVlak(obr, UVTOINDEX(0, 0));  /* (0, 0) */
2421                 add_v3_v3v3(n1, vlr->n, vlr1->n);
2422                 vlr2= RE_findOrAddVlak(obr, UVTOINDEX(0, sizev-1)); /* (0, n) */
2423                 add_v3_v3(n1, vlr2->n);
2424                 vlr3= RE_findOrAddVlak(obr, UVTOINDEX(sizeu-1, 0)); /* (m, 0) */
2425                 add_v3_v3(n1, vlr3->n);
2426                 copy_v3_v3(vlr->v3->n, n1);
2427                 copy_v3_v3(vlr1->v1->n, n1);
2428                 copy_v3_v3(vlr2->v2->n, n1);
2429                 copy_v3_v3(vlr3->v4->n, n1);
2430         }
2431         for (a = startvert; a < obr->totvert; a++) {
2432                 ver= RE_findOrAddVert(obr, a);
2433                 normalize_v3(ver->n);
2434         }
2435         
2436         
2437         return orcoret;
2438 }
2439
2440 static void init_render_dm(DerivedMesh *dm, Render *re, ObjectRen *obr,
2441         int timeoffset, float *orco, float mat[4][4])
2442 {
2443         Object *ob= obr->ob;
2444         int a, end, totvert, vertofs;
2445         short mat_iter;
2446         VertRen *ver;
2447         VlakRen *vlr;
2448         MVert *mvert = NULL;
2449         MFace *mface;
2450         Material *ma;
2451 #ifdef WITH_FREESTYLE
2452         const int *index_mf_to_mpoly = NULL;
2453         const int *index_mp_to_orig = NULL;
2454         FreestyleFace *ffa = NULL;
2455 #endif
2456         /* Curve *cu= ELEM(ob->type, OB_FONT, OB_CURVE) ? ob->data : NULL; */
2457
2458         mvert= dm->getVertArray(dm);
2459         totvert= dm->getNumVerts(dm);
2460
2461         for (a=0; a<totvert; a++, mvert++) {
2462                 ver= RE_findOrAddVert(obr, obr->totvert++);
2463                 copy_v3_v3(ver->co, mvert->co);
2464                 mul_m4_v3(mat, ver->co);
2465
2466                 if (orco) {
2467                         ver->orco= orco;
2468                         orco+=3;
2469                 }
2470         }
2471
2472         if (!timeoffset) {
2473                 /* store customdata names, because DerivedMesh is freed */
2474                 RE_set_customdata_names(obr, &dm->faceData);
2475
2476                 /* still to do for keys: the correct local texture coordinate */
2477
2478                 /* faces in order of color blocks */
2479                 vertofs= obr->totvert - totvert;
2480                 for (mat_iter= 0; (mat_iter < ob->totcol || (mat_iter==0 && ob->totcol==0)); mat_iter++) {
2481
2482                         ma= give_render_material(re, ob, mat_iter+1);
2483                         end= dm->getNumTessFaces(dm);
2484                         mface= dm->getTessFaceArray(dm);
2485
2486 #ifdef WITH_FREESTYLE
2487                         if (ob->type == OB_MESH) {
2488                                 Mesh *me= ob->data;
2489                                 index_mf_to_mpoly= dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
2490                                 index_mp_to_orig= dm->getPolyDataArray(dm, CD_ORIGINDEX);
2491                                 ffa= CustomData_get_layer(&me->pdata, CD_FREESTYLE_FACE);
2492                         }
2493 #endif
2494
2495                         for (a=0; a<end; a++, mface++) {
2496                                 int v1, v2, v3, v4, flag;
2497
2498                                 if (mface->mat_nr == mat_iter) {
2499                                         float len;
2500
2501                                         v1= mface->v1;
2502                                         v2= mface->v2;
2503                                         v3= mface->v3;
2504                                         v4= mface->v4;
2505                                         flag= mface->flag & ME_SMOOTH;
2506
2507                                         vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2508                                         vlr->v1= RE_findOrAddVert(obr, vertofs+v1);
2509                                         vlr->v2= RE_findOrAddVert(obr, vertofs+v2);
2510                                         vlr->v3= RE_findOrAddVert(obr, vertofs+v3);
2511                                         if (v4) vlr->v4= RE_findOrAddVert(obr, vertofs+v4);
2512                                         else vlr->v4 = NULL;
2513
2514                                         /* render normals are inverted in render */
2515                                         if (vlr->v4)
2516                                                 len= normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
2517                                         else
2518                                                 len= normal_tri_v3(vlr->n, vlr->v3->co, vlr->v2->co, vlr->v1->co);
2519
2520                                         vlr->mat= ma;
2521                                         vlr->flag= flag;
2522                                         vlr->ec= 0; /* mesh edges rendered separately */
2523 #ifdef WITH_FREESTYLE
2524                                         if (ffa) {
2525                                                 int index = (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, a) : a;
2526                                                 vlr->freestyle_face_mark= (ffa[index].flag & FREESTYLE_FACE_MARK) ? 1 : 0;
2527                                         }
2528                                         else {
2529                                                 vlr->freestyle_face_mark= 0;
2530                                         }
2531 #endif
2532
2533                                         if (len==0) obr->totvlak--;
2534                                         else {
2535                                                 CustomDataLayer *layer;
2536                                                 MTFace *mtface, *mtf;
2537                                                 MCol *mcol, *mc;
2538                                                 int index, mtfn= 0, mcn= 0;
2539                                                 char *name;
2540
2541                                                 for (index=0; index<dm->faceData.totlayer; index++) {
2542                                                         layer= &dm->faceData.layers[index];
2543                                                         name= layer->name;
2544
2545                                                         if (layer->type == CD_MTFACE && mtfn < MAX_MTFACE) {
2546                                                                 mtf= RE_vlakren_get_tface(obr, vlr, mtfn++, &name, 1);
2547                                                                 mtface= (MTFace*)layer->data;
2548                                                                 *mtf= mtface[a];
2549                                                         }
2550                                                         else if (layer->type == CD_MCOL && mcn < MAX_MCOL) {
2551                                                                 mc= RE_vlakren_get_mcol(obr, vlr, mcn++, &name, 1);
2552                                                                 mcol= (MCol*)layer->data;
2553                                                                 memcpy(mc, &mcol[a*4], sizeof(MCol)*4);
2554                                                         }
2555                                                 }
2556                                         }
2557                                 }
2558                         }
2559                 }
2560
2561                 /* Normals */
2562                 calc_vertexnormals(re, obr, 1, 0, 0);
2563         }
2564
2565 }
2566
2567 static void init_render_surf(Depsgraph *depsgraph, Render *re, ObjectRen *obr, int timeoffset)
2568 {
2569         Object *ob= obr->ob;
2570         Nurb *nu = NULL;
2571         Curve *cu;
2572         ListBase displist= {NULL, NULL};
2573         DispList *dl;
2574         Material **matar;
2575         float *orco=NULL, mat[4][4];
2576         int a, totmat;
2577         bool need_orco = false;
2578         DerivedMesh *dm= NULL;
2579
2580         cu= ob->data;
2581         nu= cu->nurb.first;
2582         if (nu == NULL) return;
2583
2584         mul_m4_m4m4(mat, re->viewmat, ob->obmat);
2585         invert_m4_m4(ob->imat, mat);
2586
2587         /* material array */
2588         totmat= ob->totcol+1;
2589         matar= MEM_callocN(sizeof(Material*)*totmat, "init_render_surf matar");
2590
2591         for (a=0; a<totmat; a++) {
2592                 matar[a]= give_render_material(re, ob, a+1);
2593
2594                 if (matar[a] && matar[a]->texco & TEXCO_ORCO)
2595                         need_orco= 1;
2596         }
2597
2598         if (ob->parent && (ob->parent->type==OB_LATTICE)) need_orco= 1;
2599
2600         BKE_displist_make_surf(depsgraph, re->scene, ob, &displist, &dm, 1, 0, 1);
2601
2602         if (dm) {
2603                 if (need_orco) {
2604                         orco = get_object_orco(re, ob);
2605                         if (!orco) {
2606                                 orco= BKE_displist_make_orco(depsgraph, re->scene, ob, dm, true, true);
2607                                 if (orco) {
2608                                         set_object_orco(re, ob, orco);
2609                                 }
2610                         }
2611                 }
2612
2613                 init_render_dm(dm, re, obr, timeoffset, orco, mat);
2614                 dm->release(dm);
2615         }
2616         else {
2617                 if (need_orco) {
2618                         orco = get_object_orco(re, ob);
2619                         if (!orco) {
2620                                 orco = BKE_curve_surf_make_orco(ob);
2621                                 set_object_orco(re, ob, orco);
2622                         }
2623                 }
2624
2625                 /* walk along displaylist and create rendervertices/-faces */
2626                 for (dl=displist.first; dl; dl=dl->next) {
2627                         /* watch out: u ^= y, v ^= x !! */
2628                         if (dl->type==DL_SURF)
2629                                 orco+= 3*dl_surf_to_renderdata(obr, dl, matar, orco, mat);
2630                 }
2631         }
2632
2633         BKE_displist_free(&displist);
2634
2635         MEM_freeN(matar);
2636 }
2637
2638 static void init_render_curve(Depsgraph *depsgraph, Render *re, ObjectRen *obr, int timeoffset)
2639 {
2640         Object *ob= obr->ob;
2641         Curve *cu;
2642         VertRen *ver;
2643         VlakRen *vlr;
2644         DispList *dl;
2645         DerivedMesh *dm = NULL;
2646         ListBase disp={NULL, NULL};
2647         Material **matar;
2648         float *data, *fp, *orco=NULL;
2649         float n[3], mat[4][4], nmat[4][4];
2650         int nr, startvert, a, b, negative_scale;
2651         bool need_orco = false;
2652         int totmat;
2653
2654         cu= ob->data;
2655         if (ob->type==OB_FONT && cu->str==NULL) return;
2656         else if (ob->type==OB_CURVE && cu->nurb.first==NULL) return;
2657
2658         BKE_displist_make_curveTypes_forRender(depsgraph, re->scene, ob, &disp, &dm, false, true);
2659         dl= disp.first;
2660         if (dl==NULL) return;
2661         
2662         mul_m4_m4m4(mat, re->viewmat, ob->obmat);
2663         invert_m4_m4(ob->imat, mat);
2664         negative_scale = is_negative_m4(mat);
2665
2666         /* local object -> world space transform for normals */
2667         transpose_m4_m4(nmat, mat);
2668         invert_m4(nmat);
2669
2670         /* material array */
2671         totmat= ob->totcol+1;
2672         matar= MEM_callocN(sizeof(Material*)*totmat, "init_render_surf matar");
2673
2674         for (a=0; a<totmat; a++) {
2675                 matar[a]= give_render_material(re, ob, a+1);
2676
2677                 if (matar[a] && matar[a]->texco & TEXCO_ORCO)
2678                         need_orco= 1;
2679         }
2680
2681         if (dm) {
2682                 if (need_orco) {
2683                         orco = get_object_orco(re, ob);
2684                         if (!orco) {
2685                                 orco = BKE_displist_make_orco(depsgraph, re->scene, ob, dm, true, true);
2686                                 if (orco) {
2687                                         set_object_orco(re, ob, orco);
2688                                 }
2689                         }
2690                 }
2691
2692                 init_render_dm(dm, re, obr, timeoffset, orco, mat);
2693                 dm->release(dm);
2694         }
2695         else {
2696                 if (need_orco) {
2697                         orco = get_object_orco(re, ob);
2698                         if (!orco) {
2699                                 orco = BKE_curve_make_orco(depsgraph, re->scene, ob, NULL);
2700                                 set_object_orco(re, ob, orco);
2701                         }
2702                 }
2703
2704                 while (dl) {
2705                         if (dl->col > ob->totcol) {
2706                                 /* pass */
2707                         }
2708                         else if (dl->type==DL_INDEX3) {
2709                                 const int *index;
2710
2711                                 startvert= obr->totvert;
2712                                 data= dl->verts;
2713
2714                                 for (a=0; a<dl->nr; a++, data+=3) {
2715                                         ver= RE_findOrAddVert(obr, obr->totvert++);
2716                                         copy_v3_v3(ver->co, data);
2717
2718                                         mul_m4_v3(mat, ver->co);
2719
2720                                         if (orco) {
2721                                                 ver->orco = orco;
2722                                                 orco += 3;
2723                                         }
2724                                 }
2725
2726                                 if (timeoffset==0) {
2727                                         float tmp[3];
2728                                         const int startvlak= obr->totvlak;
2729
2730                                         zero_v3(n);
2731                                         index= dl->index;
2732                                         for (a=0; a<dl->parts; a++, index+=3) {
2733                                                 int v1 = index[0], v2 = index[2], v3 = index[1];
2734                                                 float *co1 = &dl->verts[v1 * 3],
2735                                                       *co2 = &dl->verts[v2 * 3],
2736                                                       *co3 = &dl->verts[v3 * 3];
2737
2738                                                 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2739                                                 vlr->v1= RE_findOrAddVert(obr, startvert + v1);
2740                                                 vlr->v2= RE_findOrAddVert(obr, startvert + v2);
2741                                                 vlr->v3= RE_findOrAddVert(obr, startvert + v3);
2742                                                 vlr->v4= NULL;
2743
2744                                                 /* to prevent float accuracy issues, we calculate normal in local object space (not world) */
2745                                                 if (normal_tri_v3(tmp, co1, co2, co3) > FLT_EPSILON) {
2746                                                         if (negative_scale == false) {
2747                                                                 add_v3_v3(n, tmp);
2748                                                         }
2749                                                         else {
2750                                                                 sub_v3_v3(n, tmp);
2751                                                         }
2752                                                 }
2753
2754                                                 vlr->mat= matar[ dl->col ];
2755                                                 vlr->flag= 0;
2756                                                 vlr->ec= 0;
2757                                         }
2758
2759                                         /* transform normal to world space */
2760                                         mul_m4_v3(nmat, n);
2761                                         normalize_v3(n);
2762
2763                                         /* vertex normals */
2764                                         for (a= startvlak; a<obr->totvlak; a++) {
2765                                                 vlr= RE_findOrAddVlak(obr, a);
2766
2767                                                 copy_v3_v3(vlr->n, n);
2768                                                 add_v3_v3(vlr->v1->n, vlr->n);
2769                                                 add_v3_v3(vlr->v3->n, vlr->n);
2770                                                 add_v3_v3(vlr->v2->n, vlr->n);
2771                                         }
2772                                         for (a=startvert; a<obr->totvert; a++) {
2773                                                 ver= RE_findOrAddVert(obr, a);
2774                                                 normalize_v3(ver->n);
2775                                         }
2776                                 }
2777                         }
2778                         else if (dl->type==DL_SURF) {
2779
2780                                 /* cyclic U means an extruded full circular curve, we skip bevel splitting then */
2781                                 if (dl->flag & DL_CYCL_U) {
2782                                         orco+= 3*dl_surf_to_renderdata(obr, dl, matar, orco, mat);
2783                                 }
2784                                 else {
2785                                         int p1, p2, p3, p4;
2786
2787                                         fp= dl->verts;
2788                                         startvert= obr->totvert;
2789                                         nr= dl->nr*dl->parts;
2790
2791                                         while (nr--) {
2792                                                 ver= RE_findOrAddVert(obr, obr->totvert++);
2793
2794                                                 copy_v3_v3(ver->co, fp);
2795                                                 mul_m4_v3(mat, ver->co);
2796                                                 fp+= 3;
2797
2798                                                 if (orco) {
2799                                                         ver->orco = orco;
2800                                                         orco += 3;
2801                                                 }
2802                                         }
2803
2804                                         if (dl->flag & DL_CYCL_V && orco) {
2805                                                 fp = dl->verts;
2806                                                 nr = dl->nr;
2807                                                 while (nr--) {
2808                                                         ver = RE_findOrAddVert(obr, obr->totvert++);
2809                                                         copy_v3_v3(ver->co, fp);
2810                                                         mul_m4_v3(mat, ver->co);
2811                                                         ver->orco = orco;
2812                                                         fp += 3;
2813                                                         orco += 3;
2814                                                 }
2815                                         }
2816
2817                                         if (dl->bevel_split || timeoffset == 0) {
2818                                                 const int startvlak= obr->totvlak;
2819
2820                                                 for (a=0; a<dl->parts; a++) {
2821
2822                                                         if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4)==0)
2823                                                                 break;
2824
2825                                                         p1+= startvert;
2826                                                         p2+= startvert;
2827                                                         p3+= startvert;
2828                                                         p4+= startvert;
2829
2830                                                         if (dl->flag & DL_CYCL_V && orco && a == dl->parts - 1) {
2831                                                                 p3 = p1 + dl->nr;
2832                                                                 p4 = p2 + dl->nr;
2833                                                         }
2834
2835                                                         for (; b<dl->nr; b++) {
2836                                                                 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2837                                                                 /* important 1 offset in order is kept [#24913] */
2838                                                                 vlr->v1= RE_findOrAddVert(obr, p2);
2839                                                                 vlr->v2= RE_findOrAddVert(obr, p1);
2840                                                                 vlr->v3= RE_findOrAddVert(obr, p3);
2841                                                                 vlr->v4= RE_findOrAddVert(obr, p4);
2842                                                                 vlr->ec= ME_V2V3+ME_V3V4;
2843                                                                 if (a==0) vlr->ec+= ME_V1V2;
2844
2845                                                                 vlr->flag= dl->rt;
2846
2847                                                                 normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
2848                                                                 vlr->mat= matar[ dl->col ];
2849
2850                                                                 p4= p3;
2851                                                                 p3++;
2852                                                                 p2= p1;
2853                                                                 p1++;
2854                                                         }
2855                                                 }
2856
2857                                                 if (dl->bevel_split) {
2858                                                         for (a = 0; a < dl->parts - 1 + !!(dl->flag & DL_CYCL_V); a++) {
2859                                                                 if (BLI_BITMAP_TEST(dl->bevel_split, a)) {
2860                                                                         split_v_renderfaces(
2861                                                                                 obr, startvlak, startvert, dl->parts, dl->nr, a,
2862                                                                                 /* intentionally swap (v, u) --> (u, v) */
2863                                                                                 dl->flag & DL_CYCL_V, dl->flag & DL_CYCL_U);
2864                                                                 }
2865                                                         }
2866                                                 }
2867
2868                                                 /* vertex normals */
2869                                                 for (a= startvlak; a<obr->totvlak; a++) {
2870                                                         vlr= RE_findOrAddVlak(obr, a);
2871
2872                                                         add_v3_v3(vlr->v1->n, vlr->n);
2873                                                         add_v3_v3(vlr->v3->n, vlr->n);
2874                                                         add_v3_v3(vlr->v2->n, vlr->n);
2875                                                         add_v3_v3(vlr->v4->n, vlr->n);
2876                                                 }
2877                                                 for (a=startvert; a<obr->totvert; a++) {
2878                                                         ver= RE_findOrAddVert(obr, a);
2879                                                         normalize_v3(ver->n);
2880                                                 }
2881                                         }
2882