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