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