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