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