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