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