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