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