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