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