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