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