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