4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributors: 2004/2005/2006 Blender Foundation, full recode
25 * ***** END GPL LICENSE BLOCK *****
35 #include "MTC_matrixops.h"
37 #include "MEM_guardedalloc.h"
39 #include "BLI_arithb.h"
40 #include "BLI_blenlib.h"
42 #include "BLI_memarena.h"
43 #include "BLI_ghash.h"
45 #include "DNA_armature_types.h"
46 #include "DNA_camera_types.h"
47 #include "DNA_material_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_effect_types.h"
50 #include "DNA_group_types.h"
51 #include "DNA_lamp_types.h"
52 #include "DNA_image_types.h"
53 #include "DNA_lattice_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_meshdata_types.h"
56 #include "DNA_meta_types.h"
57 #include "DNA_modifier_types.h"
58 #include "DNA_node_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_object_force.h"
61 #include "DNA_object_fluidsim.h"
62 #include "DNA_particle_types.h"
63 #include "DNA_scene_types.h"
64 #include "DNA_texture_types.h"
65 #include "DNA_view3d_types.h"
68 #include "BKE_armature.h"
69 #include "BKE_action.h"
70 #include "BKE_curve.h"
71 #include "BKE_customdata.h"
72 #include "BKE_colortools.h"
73 #include "BKE_constraint.h"
74 #include "BKE_displist.h"
75 #include "BKE_deform.h"
76 #include "BKE_DerivedMesh.h"
77 #include "BKE_effect.h"
78 #include "BKE_global.h"
79 #include "BKE_group.h"
82 #include "BKE_image.h"
83 #include "BKE_lattice.h"
84 #include "BKE_library.h"
85 #include "BKE_material.h"
87 #include "BKE_mball.h"
89 #include "BKE_modifier.h"
91 #include "BKE_object.h"
92 #include "BKE_particle.h"
93 #include "BKE_scene.h"
94 #include "BKE_subsurf.h"
95 #include "BKE_texture.h"
96 #include "BKE_utildefines.h"
97 #include "BKE_world.h"
100 #include "IMB_imbuf_types.h"
103 #include "multires.h"
104 #include "occlusion.h"
105 #include "render_types.h"
106 #include "rendercore.h"
107 #include "renderdatabase.h"
108 #include "renderpipeline.h"
119 #ifndef DISABLE_YAFRAY /* disable yafray */
121 #include "YafRay_Api.h"
123 /* yafray: Identity transform 'hack' removed, exporter now transforms vertices back to world.
124 * Same is true for lamp coords & vec.
125 * Duplicated data objects & dupliframe/duplivert objects are only stored once,
126 * only the matrix is stored for all others, in yafray these objects are instances of the original.
127 * The main changes are in RE_Database_FromScene().
130 #endif /* disable yafray */
132 /* 10 times larger than normal epsilon, test it on default nurbs sphere with ray_transp (for quad detection) */
133 /* or for checking vertex normal flips */
134 #define FLT_EPSILON10 1.19209290e-06F
136 /* ------------------------------------------------------------------------- */
138 /* Stuff for stars. This sits here because it uses gl-things. Part of
139 this code may move down to the converter. */
140 /* ------------------------------------------------------------------------- */
141 /* this is a bad beast, since it is misused by the 3d view drawing as well. */
143 static HaloRen *initstar(Render *re, ObjectRen *obr, float *vec, float hasize)
148 projectverto(vec, re->winmat, hoco);
150 har= RE_findOrAddHalo(obr, obr->tothalo++);
152 /* projectvert is done in function zbufvlaggen again, because of parts */
153 VECCOPY(har->co, vec);
161 /* there must be a 'fixed' amount of stars generated between
163 * all stars must by preference lie on the far and solely
164 * differ in clarity/color
167 void RE_make_stars(Render *re, void (*initfunc)(void),
168 void (*vertexfunc)(float*), void (*termfunc)(void))
170 extern unsigned char hash[512];
171 ObjectRen *obr= NULL;
176 double dblrand, hlfrand;
177 float vec[4], fx, fy, fz;
178 float fac, starmindist, clipend;
179 float mat[4][4], stargrid, maxrand, maxjit, force, alpha;
180 int x, y, z, sx, sy, sz, ex, ey, ez, done = 0;
184 wrld= G.scene->world;
191 stargrid = wrld->stardist; /* distance between stars */
192 maxrand = 2.0; /* amount a star can be shifted (in grid units) */
193 maxjit = (wrld->starcolnoise); /* amount a color is being shifted */
196 force = ( wrld->starsize );
198 /* minimal free space (starting at camera) */
199 starmindist= wrld->starmindist;
201 if (stargrid <= 0.10) return;
203 if (re) re->flag |= R_HALO;
204 else stargrid *= 1.0; /* then it draws fewer */
206 if(re) MTC_Mat4Invert(mat, re->viewmat);
207 else MTC_Mat4One(mat);
209 /* BOUNDING BOX CALCULATION
210 * bbox goes from z = loc_near_var | loc_far_var,
215 if(scene->camera==NULL)
217 camera = scene->camera->data;
218 clipend = camera->clipend;
220 /* convert to grid coordinates */
222 sx = ((mat[3][0] - clipend) / stargrid) - maxrand;
223 sy = ((mat[3][1] - clipend) / stargrid) - maxrand;
224 sz = ((mat[3][2] - clipend) / stargrid) - maxrand;
226 ex = ((mat[3][0] + clipend) / stargrid) + maxrand;
227 ey = ((mat[3][1] + clipend) / stargrid) + maxrand;
228 ez = ((mat[3][2] + clipend) / stargrid) + maxrand;
230 dblrand = maxrand * stargrid;
231 hlfrand = 2.0 * dblrand;
237 if(re) /* add render object for stars */
238 obr= RE_addRenderObject(re, NULL, NULL, 0, 0, 0);
240 for (x = sx, fx = sx * stargrid; x <= ex; x++, fx += stargrid) {
241 for (y = sy, fy = sy * stargrid; y <= ey ; y++, fy += stargrid) {
242 for (z = sz, fz = sz * stargrid; z <= ez; z++, fz += stargrid) {
244 BLI_srand((hash[z & 0xff] << 24) + (hash[y & 0xff] << 16) + (hash[x & 0xff] << 8));
245 vec[0] = fx + (hlfrand * BLI_drand()) - dblrand;
246 vec[1] = fy + (hlfrand * BLI_drand()) - dblrand;
247 vec[2] = fz + (hlfrand * BLI_drand()) - dblrand;
251 if(done & 1) vertexfunc(vec);
255 MTC_Mat4MulVecfl(re->viewmat, vec);
257 /* in vec are global coordinates
258 * calculate distance to camera
259 * and using that, define the alpha
269 alpha = sqrt(tx * tx + ty * ty + tz * tz);
271 if (alpha >= clipend) alpha = 0.0;
272 else if (alpha <= starmindist) alpha = 0.0;
273 else if (alpha <= 2.0 * starmindist) {
274 alpha = (alpha - starmindist) / starmindist;
276 alpha -= 2.0 * starmindist;
277 alpha /= (clipend - 2.0 * starmindist);
284 fac = force * BLI_drand();
286 har = initstar(re, obr, vec, fac);
289 har->alfa = sqrt(sqrt(alpha));
291 har->r = har->g = har->b = 1.0;
293 har->r += ((maxjit * BLI_drand()) ) - maxjit;
294 har->g += ((maxjit * BLI_drand()) ) - maxjit;
295 har->b += ((maxjit * BLI_drand()) ) - maxjit;
299 har->type |= HA_ONLYSKY;
305 /* do not call blender_test_break() here, since it is used in UI as well, confusing the callback system */
306 /* main cause is G.afbreek of course, a global again... (ton) */
309 if (termfunc) termfunc();
312 re->tothalo += obr->tothalo;
316 /* ------------------------------------------------------------------------- */
317 /* tool functions/defines for ad hoc simplification and possible future
319 /* ------------------------------------------------------------------------- */
321 #define UVTOINDEX(u,v) (startvlak + (u) * sizev + (v))
324 NOTE THAT U/V COORDINATES ARE SOMETIMES SWAPPED !!
326 ^ ()----p4----p3----()
334 /* ------------------------------------------------------------------------- */
336 static void split_v_renderfaces(ObjectRen *obr, int startvlak, int startvert, int usize, int vsize, int uIndex, int cyclu, int cyclv)
338 int vLen = vsize-1+(!!cyclv);
341 for (v=0; v<vLen; v++) {
342 VlakRen *vlr = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + v);
343 VertRen *vert = RE_vertren_copy(obr, vlr->v2);
349 VlakRen *vlr = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + 0);
352 VlakRen *vlr = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + v+1);
359 VlakRen *vlr = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + v+1);
364 vlr->v1 = RE_vertren_copy(obr, vlr->v1);
370 /* ------------------------------------------------------------------------- */
372 static int check_vnormal(float *n, float *veno)
376 inp=n[0]*veno[0]+n[1]*veno[1]+n[2]*veno[2];
377 if(inp < -FLT_EPSILON10) return 1;
381 /* ------------------------------------------------------------------------- */
382 /* Stress, tangents and normals */
383 /* ------------------------------------------------------------------------- */
385 static void calc_edge_stress_add(float *accum, VertRen *v1, VertRen *v2)
387 float len= VecLenf(v1->co, v2->co)/VecLenf(v1->orco, v2->orco);
390 acc= accum + 2*v1->index;
394 acc= accum + 2*v2->index;
399 static void calc_edge_stress(Render *re, ObjectRen *obr, Mesh *me)
401 float loc[3], size[3], *accum, *acc, *accumoffs, *stress;
404 if(obr->totvert==0) return;
406 mesh_get_texspace(me, loc, NULL, size);
408 accum= MEM_callocN(2*sizeof(float)*obr->totvert, "temp accum for stress");
410 /* de-normalize orco */
411 for(a=0; a<obr->totvert; a++) {
412 VertRen *ver= RE_findOrAddVert(obr, a);
414 ver->orco[0]= ver->orco[0]*size[0] +loc[0];
415 ver->orco[1]= ver->orco[1]*size[1] +loc[1];
416 ver->orco[2]= ver->orco[2]*size[2] +loc[2];
420 /* add stress values */
421 accumoffs= accum; /* so we can use vertex index */
422 for(a=0; a<obr->totvlak; a++) {
423 VlakRen *vlr= RE_findOrAddVlak(obr, a);
425 if(vlr->v1->orco && vlr->v4) {
426 calc_edge_stress_add(accumoffs, vlr->v1, vlr->v2);
427 calc_edge_stress_add(accumoffs, vlr->v2, vlr->v3);
428 calc_edge_stress_add(accumoffs, vlr->v3, vlr->v1);
430 calc_edge_stress_add(accumoffs, vlr->v3, vlr->v4);
431 calc_edge_stress_add(accumoffs, vlr->v4, vlr->v1);
432 calc_edge_stress_add(accumoffs, vlr->v2, vlr->v4);
437 for(a=0; a<obr->totvert; a++) {
438 VertRen *ver= RE_findOrAddVert(obr, a);
440 /* find stress value */
441 acc= accumoffs + 2*ver->index;
444 stress= RE_vertren_get_stress(obr, ver, 1);
448 ver->orco[0] = (ver->orco[0]-loc[0])/size[0];
449 ver->orco[1] = (ver->orco[1]-loc[1])/size[1];
450 ver->orco[2] = (ver->orco[2]-loc[2])/size[2];
457 /* gets tangent from tface or orco */
458 static void calc_tangent_vector(ObjectRen *obr, VertexTangent **vtangents, MemArena *arena, VlakRen *vlr, int do_nmap_tangent, int do_tangent)
460 MTFace *tface= RE_vlakren_get_tface(obr, vlr, obr->actmtface, NULL, 0);
461 VertRen *v1=vlr->v1, *v2=vlr->v2, *v3=vlr->v3, *v4=vlr->v4;
463 float *uv1, *uv2, *uv3, *uv4;
473 uv1= uv[0]; uv2= uv[1]; uv3= uv[2]; uv4= uv[3];
474 spheremap(v1->orco[0], v1->orco[1], v1->orco[2], &uv[0][0], &uv[0][1]);
475 spheremap(v2->orco[0], v2->orco[1], v2->orco[2], &uv[1][0], &uv[1][1]);
476 spheremap(v3->orco[0], v3->orco[1], v3->orco[2], &uv[2][0], &uv[2][1]);
478 spheremap(v4->orco[0], v4->orco[1], v4->orco[2], &uv[3][0], &uv[3][1]);
482 tangent_from_uv(uv1, uv2, uv3, v1->co, v2->co, v3->co, vlr->n, tang);
485 tav= RE_vertren_get_tangent(obr, v1, 1);
486 VECADD(tav, tav, tang);
487 tav= RE_vertren_get_tangent(obr, v2, 1);
488 VECADD(tav, tav, tang);
489 tav= RE_vertren_get_tangent(obr, v3, 1);
490 VECADD(tav, tav, tang);
493 if(do_nmap_tangent) {
494 sum_or_add_vertex_tangent(arena, &vtangents[v1->index], tang, uv1);
495 sum_or_add_vertex_tangent(arena, &vtangents[v2->index], tang, uv2);
496 sum_or_add_vertex_tangent(arena, &vtangents[v3->index], tang, uv3);
500 tangent_from_uv(uv1, uv3, uv4, v1->co, v3->co, v4->co, vlr->n, tang);
503 tav= RE_vertren_get_tangent(obr, v1, 1);
504 VECADD(tav, tav, tang);
505 tav= RE_vertren_get_tangent(obr, v3, 1);
506 VECADD(tav, tav, tang);
507 tav= RE_vertren_get_tangent(obr, v4, 1);
508 VECADD(tav, tav, tang);
511 if(do_nmap_tangent) {
512 sum_or_add_vertex_tangent(arena, &vtangents[v1->index], tang, uv1);
513 sum_or_add_vertex_tangent(arena, &vtangents[v3->index], tang, uv3);
514 sum_or_add_vertex_tangent(arena, &vtangents[v4->index], tang, uv4);
520 static void calc_vertexnormals(Render *re, ObjectRen *obr, int do_tangent, int do_nmap_tangent)
522 MemArena *arena= NULL;
523 VertexTangent **vtangents= NULL;
526 if(do_nmap_tangent) {
527 arena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
528 BLI_memarena_use_calloc(arena);
530 vtangents= MEM_callocN(sizeof(VertexTangent*)*obr->totvert, "VertexTangent");
533 /* clear all vertex normals */
534 for(a=0; a<obr->totvert; a++) {
535 VertRen *ver= RE_findOrAddVert(obr, a);
536 ver->n[0]=ver->n[1]=ver->n[2]= 0.0f;
539 /* calculate cos of angles and point-masses, use as weight factor to
540 add face normal to vertex */
541 for(a=0; a<obr->totvlak; a++) {
542 VlakRen *vlr= RE_findOrAddVlak(obr, a);
543 if(vlr->flag & ME_SMOOTH) {
544 VertRen *v1= vlr->v1;
545 VertRen *v2= vlr->v2;
546 VertRen *v3= vlr->v3;
547 VertRen *v4= vlr->v4;
548 float n1[3], n2[3], n3[3], n4[3];
549 float fac1, fac2, fac3, fac4=0.0f;
551 if(re->flag & R_GLOB_NOPUNOFLIP)
552 vlr->flag |= R_NOPUNOFLIP;
554 VecSubf(n1, v2->co, v1->co);
556 VecSubf(n2, v3->co, v2->co);
559 VecSubf(n3, v1->co, v3->co);
562 fac1= saacos(-n1[0]*n3[0]-n1[1]*n3[1]-n1[2]*n3[2]);
563 fac2= saacos(-n1[0]*n2[0]-n1[1]*n2[1]-n1[2]*n2[2]);
564 fac3= saacos(-n2[0]*n3[0]-n2[1]*n3[1]-n2[2]*n3[2]);
567 VecSubf(n3, v4->co, v3->co);
569 VecSubf(n4, v1->co, v4->co);
572 fac1= saacos(-n4[0]*n1[0]-n4[1]*n1[1]-n4[2]*n1[2]);
573 fac2= saacos(-n1[0]*n2[0]-n1[1]*n2[1]-n1[2]*n2[2]);
574 fac3= saacos(-n2[0]*n3[0]-n2[1]*n3[1]-n2[2]*n3[2]);
575 fac4= saacos(-n3[0]*n4[0]-n3[1]*n4[1]-n3[2]*n4[2]);
577 if(!(vlr->flag & R_NOPUNOFLIP)) {
578 if( check_vnormal(vlr->n, v4->n) ) fac4= -fac4;
581 v4->n[0] +=fac4*vlr->n[0];
582 v4->n[1] +=fac4*vlr->n[1];
583 v4->n[2] +=fac4*vlr->n[2];
586 if(!(vlr->flag & R_NOPUNOFLIP)) {
587 if( check_vnormal(vlr->n, v1->n) ) fac1= -fac1;
588 if( check_vnormal(vlr->n, v2->n) ) fac2= -fac2;
589 if( check_vnormal(vlr->n, v3->n) ) fac3= -fac3;
592 v1->n[0] +=fac1*vlr->n[0];
593 v1->n[1] +=fac1*vlr->n[1];
594 v1->n[2] +=fac1*vlr->n[2];
596 v2->n[0] +=fac2*vlr->n[0];
597 v2->n[1] +=fac2*vlr->n[1];
598 v2->n[2] +=fac2*vlr->n[2];
600 v3->n[0] +=fac3*vlr->n[0];
601 v3->n[1] +=fac3*vlr->n[1];
602 v3->n[2] +=fac3*vlr->n[2];
605 if(do_nmap_tangent || do_tangent) {
606 /* tangents still need to be calculated for flat faces too */
607 /* weighting removed, they are not vertexnormals */
608 calc_tangent_vector(obr, vtangents, arena, vlr, do_nmap_tangent, do_tangent);
613 for(a=0; a<obr->totvlak; a++) {
614 VlakRen *vlr= RE_findOrAddVlak(obr, a);
615 if((vlr->flag & ME_SMOOTH)==0) {
616 float *f1= vlr->v1->n;
617 if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, vlr->n);
619 if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, vlr->n);
621 if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, vlr->n);
624 if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, vlr->n);
628 if(do_nmap_tangent) {
629 VertRen *v1=vlr->v1, *v2=vlr->v2, *v3=vlr->v3, *v4=vlr->v4;
630 MTFace *tface= RE_vlakren_get_tface(obr, vlr, obr->actmtface, NULL, 0);
633 float *vtang, *ftang= RE_vlakren_get_nmap_tangent(obr, vlr, 1);
635 vtang= find_vertex_tangent(vtangents[v1->index], tface->uv[0]);
636 VECCOPY(ftang, vtang);
638 vtang= find_vertex_tangent(vtangents[v2->index], tface->uv[1]);
639 VECCOPY(ftang+3, vtang);
641 vtang= find_vertex_tangent(vtangents[v3->index], tface->uv[2]);
642 VECCOPY(ftang+6, vtang);
645 vtang= find_vertex_tangent(vtangents[v4->index], tface->uv[3]);
646 VECCOPY(ftang+9, vtang);
653 /* normalize vertex normals */
654 for(a=0; a<obr->totvert; a++) {
655 VertRen *ver= RE_findOrAddVert(obr, a);
658 float *tav= RE_vertren_get_tangent(obr, ver, 0);
661 float tdn = tav[0]*ver->n[0] + tav[1]*ver->n[1] + tav[2]*ver->n[2];
662 tav[0] -= ver->n[0]*tdn;
663 tav[1] -= ver->n[1]*tdn;
664 tav[2] -= ver->n[2]*tdn;
672 BLI_memarena_free(arena);
674 MEM_freeN(vtangents);
677 /* ------------------------------------------------------------------------- */
679 /* ------------------------------------------------------------------------- */
681 typedef struct ASvert {
686 typedef struct ASface {
687 struct ASface *next, *prev;
692 static void as_addvert(ASvert *asv, VertRen *v1, VlakRen *vlr)
697 if(v1 == NULL) return;
699 if(asv->faces.first==NULL) {
700 asf= MEM_callocN(sizeof(ASface), "asface");
701 BLI_addtail(&asv->faces, asf);
704 asf= asv->faces.last;
706 if(asf->vlr[a]==NULL) {
713 /* new face struct */
715 asf= MEM_callocN(sizeof(ASface), "asface");
716 BLI_addtail(&asv->faces, asf);
722 static int as_testvertex(VlakRen *vlr, VertRen *ver, ASvert *asv, float thresh)
724 /* return 1: vertex needs a copy */
731 asf= asv->faces.first;
734 if(asf->vlr[a] && asf->vlr[a]!=vlr) {
735 inp= fabs( vlr->n[0]*asf->vlr[a]->n[0] + vlr->n[1]*asf->vlr[a]->n[1] + vlr->n[2]*asf->vlr[a]->n[2] );
736 if(inp < thresh) return 1;
745 static VertRen *as_findvertex(VlakRen *vlr, VertRen *ver, ASvert *asv, float thresh)
747 /* return when new vertex already was made */
752 asf= asv->faces.first;
755 if(asf->vlr[a] && asf->vlr[a]!=vlr) {
756 /* this face already made a copy for this vertex! */
758 inp= fabs( vlr->n[0]*asf->vlr[a]->n[0] + vlr->n[1]*asf->vlr[a]->n[1] + vlr->n[2]*asf->vlr[a]->n[2] );
771 /* note; autosmooth happens in object space still, after applying autosmooth we rotate */
772 /* note2; actually, when original mesh and displist are equal sized, face normals are from original mesh */
773 static void autosmooth(Render *re, ObjectRen *obr, float mat[][4], int degr)
775 ASvert *asv, *asverts;
782 if(obr->totvert==0) return;
783 asverts= MEM_callocN(sizeof(ASvert)*obr->totvert, "all smooth verts");
785 thresh= cos( M_PI*(0.5f+(float)degr)/180.0 );
787 /* step zero: give faces normals of original mesh, if this is provided */
790 /* step one: construct listbase of all vertices and pointers to faces */
791 for(a=0; a<obr->totvlak; a++) {
792 vlr= RE_findOrAddVlak(obr, a);
793 /* skip wire faces */
794 if(vlr->v2 != vlr->v3) {
795 as_addvert(asverts+vlr->v1->index, vlr->v1, vlr);
796 as_addvert(asverts+vlr->v2->index, vlr->v2, vlr);
797 as_addvert(asverts+vlr->v3->index, vlr->v3, vlr);
799 as_addvert(asverts+vlr->v4->index, vlr->v4, vlr);
803 totvert= obr->totvert;
804 /* we now test all vertices, when faces have a normal too much different: they get a new vertex */
805 for(a=0, asv=asverts; a<totvert; a++, asv++) {
806 if(asv && asv->totface>1) {
807 ver= RE_findOrAddVert(obr, a);
809 asf= asv->faces.first;
813 /* is there a reason to make a new vertex? */
815 if( as_testvertex(vlr, ver, asv, thresh) ) {
817 /* already made a new vertex within threshold? */
818 v1= as_findvertex(vlr, ver, asv, thresh);
820 /* make a new vertex */
821 v1= RE_vertren_copy(obr, ver);
824 if(vlr->v1==ver) vlr->v1= v1;
825 if(vlr->v2==ver) vlr->v2= v1;
826 if(vlr->v3==ver) vlr->v3= v1;
827 if(vlr->v4==ver) vlr->v4= v1;
836 for(a=0; a<totvert; a++) {
837 BLI_freelistN(&asverts[a].faces);
841 /* rotate vertices and calculate normal of faces */
842 for(a=0; a<obr->totvert; a++) {
843 ver= RE_findOrAddVert(obr, a);
844 MTC_Mat4MulVecfl(mat, ver->co);
846 for(a=0; a<obr->totvlak; a++) {
847 vlr= RE_findOrAddVlak(obr, a);
849 /* skip wire faces */
850 if(vlr->v2 != vlr->v3) {
852 CalcNormFloat4(vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co, vlr->n);
854 CalcNormFloat(vlr->v3->co, vlr->v2->co, vlr->v1->co, vlr->n);
859 /* ------------------------------------------------------------------------- */
860 /* Orco hash and Materials */
861 /* ------------------------------------------------------------------------- */
863 static float *get_object_orco(Render *re, Object *ob)
868 re->orco_hash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
870 orco = BLI_ghash_lookup(re->orco_hash, ob);
873 if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
874 orco = make_orco_curve(ob);
875 } else if (ob->type==OB_SURF) {
876 orco = make_orco_surf(ob);
877 } else if (ob->type==OB_MBALL) {
878 orco = make_orco_mball(ob);
882 BLI_ghash_insert(re->orco_hash, ob, orco);
888 static void set_object_orco(Render *re, void *ob, float *orco)
891 re->orco_hash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
893 BLI_ghash_insert(re->orco_hash, ob, orco);
896 static void free_mesh_orco_hash(Render *re)
899 BLI_ghash_free(re->orco_hash, NULL, (GHashValFreeFP)MEM_freeN);
900 re->orco_hash = NULL;
904 static void flag_render_node_material(Render *re, bNodeTree *ntree)
908 for(node=ntree->nodes.first; node; node= node->next) {
910 if(GS(node->id->name)==ID_MA) {
911 Material *ma= (Material *)node->id;
913 if(ma->mode & MA_ZTRA)
916 ma->flag |= MA_IS_USED;
918 else if(node->type==NODE_GROUP)
919 flag_render_node_material(re, (bNodeTree *)node->id);
924 static Material *give_render_material(Render *re, Object *ob, int nr)
926 extern Material defmaterial; /* material.c */
929 ma= give_current_material(ob, nr);
933 if(re->r.mode & R_SPEED) ma->texco |= NEED_UV;
935 if(ma->mode & MA_ZTRA)
938 /* for light groups */
939 ma->flag |= MA_IS_USED;
941 if(ma->nodetree && ma->use_nodes)
942 flag_render_node_material(re, ma->nodetree);
947 /* ------------------------------------------------------------------------- */
949 /* ------------------------------------------------------------------------- */
951 /* future thread problem... */
952 static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, float *orco, float *surfnor,
953 float *uvco, int totuv, MCol *mcol, int totcol, float *vec, float *vec1, float ctime,
954 int first, int line, int adapt, float adapt_angle, float adapt_pix, int override_uv)
956 static VertRen *v1= NULL, *v2= NULL;
958 float nor[3], cross[3], crosslen, w, dx, dy, width;
959 static float anor[3], avec[3];
963 VecSubf(nor, vec, vec1);
964 Normalize(nor); // nor needed as tangent
965 Crossf(cross, vec, nor);
967 /* turn cross in pixelsize */
968 w= vec[2]*re->winmat[2][3] + re->winmat[3][3];
969 dx= re->winx*cross[0]*re->winmat[0][0];
970 dy= re->winy*cross[1]*re->winmat[1][1];
971 w= sqrt(dx*dx + dy*dy)/w;
975 if(ma->strand_ease!=0.0f) {
976 if(ma->strand_ease<0.0f)
977 fac= pow(ctime, 1.0+ma->strand_ease);
979 fac= pow(ctime, 1.0/(1.0f-ma->strand_ease));
983 width= ((1.0f-fac)*ma->strand_sta + (fac)*ma->strand_end);
985 /* use actual Blender units for strand width and fall back to minimum width */
986 if(ma->mode & MA_STR_B_UNITS){
987 crosslen= VecLength(cross);
988 w= 2.0f*crosslen*ma->strand_min/w;
993 /*cross is the radius of the strand so we want it to be half of full width */
994 VecMulf(cross,0.5/crosslen);
999 VecMulf(cross, width);
1003 if(ma->mode & MA_TANGENT_STR)
1004 flag= R_SMOOTH|R_NOPUNOFLIP|R_TANGENT;
1008 /* only 1 pixel wide strands filled in as quads now, otherwise zbuf errors */
1009 if(ma->strand_sta==1.0f)
1012 /* single face line */
1014 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1016 vlr->v1= RE_findOrAddVert(obr, obr->totvert++);
1017 vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
1018 vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
1019 vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
1021 VECCOPY(vlr->v1->co, vec);
1022 VecAddf(vlr->v1->co, vlr->v1->co, cross);
1023 VECCOPY(vlr->v1->n, nor);
1024 vlr->v1->orco= orco;
1025 vlr->v1->accum= -1.0f; // accum abuse for strand texco
1027 VECCOPY(vlr->v2->co, vec);
1028 VecSubf(vlr->v2->co, vlr->v2->co, cross);
1029 VECCOPY(vlr->v2->n, nor);
1030 vlr->v2->orco= orco;
1031 vlr->v2->accum= vlr->v1->accum;
1033 VECCOPY(vlr->v4->co, vec1);
1034 VecAddf(vlr->v4->co, vlr->v4->co, cross);
1035 VECCOPY(vlr->v4->n, nor);
1036 vlr->v4->orco= orco;
1037 vlr->v4->accum= 1.0f; // accum abuse for strand texco
1039 VECCOPY(vlr->v3->co, vec1);
1040 VecSubf(vlr->v3->co, vlr->v3->co, cross);
1041 VECCOPY(vlr->v3->n, nor);
1042 vlr->v3->orco= orco;
1043 vlr->v3->accum= vlr->v4->accum;
1045 CalcNormFloat4(vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co, vlr->n);
1051 float *snor= RE_vlakren_get_surfnor(obr, vlr, 1);
1052 VECCOPY(snor, surfnor);
1056 for(i=0; i<totuv; i++){
1058 mtf=RE_vlakren_get_tface(obr,vlr,i,NULL,1);
1059 mtf->uv[0][0]=mtf->uv[1][0]=
1060 mtf->uv[2][0]=mtf->uv[3][0]=(uvco+2*i)[0];
1061 mtf->uv[0][1]=mtf->uv[1][1]=
1062 mtf->uv[2][1]=mtf->uv[3][1]=(uvco+2*i)[1];
1066 mtf=RE_vlakren_get_tface(obr,vlr,override_uv,NULL,0);
1068 mtf->uv[0][0]=mtf->uv[3][0]=0.0f;
1069 mtf->uv[1][0]=mtf->uv[2][0]=1.0f;
1071 mtf->uv[0][1]=mtf->uv[1][1]=0.0f;
1072 mtf->uv[2][1]=mtf->uv[3][1]=1.0f;
1076 for(i=0; i<totcol; i++){
1078 mc=RE_vlakren_get_mcol(obr,vlr,i,NULL,1);
1079 mc[0]=mc[1]=mc[2]=mc[3]=mcol[i];
1080 mc[0]=mc[1]=mc[2]=mc[3]=mcol[i];
1084 /* first two vertices of a strand */
1092 v1= RE_findOrAddVert(obr, obr->totvert++);
1093 v2= RE_findOrAddVert(obr, obr->totvert++);
1095 VECCOPY(v1->co, vec);
1096 VecAddf(v1->co, v1->co, cross);
1097 VECCOPY(v1->n, nor);
1099 v1->accum= -1.0f; // accum abuse for strand texco
1101 VECCOPY(v2->co, vec);
1102 VecSubf(v2->co, v2->co, cross);
1103 VECCOPY(v2->n, nor);
1105 v2->accum= v1->accum;
1107 /* more vertices & faces to strand */
1109 if(adapt==0 || second){
1110 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1114 vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
1115 vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
1117 v1= vlr->v4; // cycle
1118 v2= vlr->v3; // cycle
1129 float dvec[3],pvec[3];
1130 VecSubf(dvec,avec,vec);
1131 Projf(pvec,dvec,vec);
1132 VecSubf(dvec,dvec,pvec);
1134 w= vec[2]*re->winmat[2][3] + re->winmat[3][3];
1135 dx= re->winx*dvec[0]*re->winmat[0][0]/w;
1136 dy= re->winy*dvec[1]*re->winmat[1][1]/w;
1137 w= sqrt(dx*dx + dy*dy);
1138 if(Inpf(anor,nor)<adapt_angle && w>adapt_pix){
1139 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1143 vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
1144 vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
1146 v1= vlr->v4; // cycle
1147 v2= vlr->v3; // cycle
1153 vlr= RE_findOrAddVlak(obr, obr->totvlak-1);
1157 VECCOPY(vlr->v4->co, vec);
1158 VecAddf(vlr->v4->co, vlr->v4->co, cross);
1159 VECCOPY(vlr->v4->n, nor);
1160 vlr->v4->orco= orco;
1161 vlr->v4->accum= -1.0f + 2.0f*ctime; // accum abuse for strand texco
1163 VECCOPY(vlr->v3->co, vec);
1164 VecSubf(vlr->v3->co, vlr->v3->co, cross);
1165 VECCOPY(vlr->v3->n, nor);
1166 vlr->v3->orco= orco;
1167 vlr->v3->accum= vlr->v4->accum;
1169 CalcNormFloat4(vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co, vlr->n);
1175 float *snor= RE_vlakren_get_surfnor(obr, vlr, 1);
1176 VECCOPY(snor, surfnor);
1180 for(i=0; i<totuv; i++){
1182 mtf=RE_vlakren_get_tface(obr,vlr,i,NULL,1);
1183 mtf->uv[0][0]=mtf->uv[1][0]=
1184 mtf->uv[2][0]=mtf->uv[3][0]=(uvco+2*i)[0];
1185 mtf->uv[0][1]=mtf->uv[1][1]=
1186 mtf->uv[2][1]=mtf->uv[3][1]=(uvco+2*i)[1];
1190 mtf=RE_vlakren_get_tface(obr,vlr,override_uv,NULL,0);
1192 mtf->uv[0][0]=mtf->uv[3][0]=0.0f;
1193 mtf->uv[1][0]=mtf->uv[2][0]=1.0f;
1195 mtf->uv[0][1]=mtf->uv[1][1]=(vlr->v1->accum+1.0f)/2.0f;
1196 mtf->uv[2][1]=mtf->uv[3][1]=(vlr->v3->accum+1.0f)/2.0f;
1200 for(i=0; i<totcol; i++){
1202 mc=RE_vlakren_get_mcol(obr,vlr,i,NULL,1);
1203 mc[0]=mc[1]=mc[2]=mc[3]=mcol[i];
1204 mc[0]=mc[1]=mc[2]=mc[3]=mcol[i];
1210 static void static_particle_wire(ObjectRen *obr, Material *ma, float *vec, float *vec1, int first, int line)
1216 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1217 vlr->v1= RE_findOrAddVert(obr, obr->totvert++);
1218 vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
1222 VECCOPY(vlr->v1->co, vec);
1223 VECCOPY(vlr->v2->co, vec1);
1225 VecSubf(vlr->n, vec, vec1);
1227 VECCOPY(vlr->v1->n, vlr->n);
1228 VECCOPY(vlr->v2->n, vlr->n);
1235 v1= RE_findOrAddVert(obr, obr->totvert++);
1236 VECCOPY(v1->co, vec);
1239 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1241 vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
1245 v1= vlr->v2; // cycle
1246 VECCOPY(v1->co, vec);
1248 VecSubf(vlr->n, vec, vec1);
1250 VECCOPY(v1->n, vlr->n);
1257 static void particle_billboard(Render *re, ObjectRen *obr, Material *ma, Object *bb_ob, float *vec, float *vel, float size, float tilt, short align,
1258 int lock, int p, int totpart, short uv_split, short anim, short split_offset, float random, float pa_time, float offset[2], int uv[3])
1262 float xvec[3]={1.0f,0.0f,0.0f}, yvec[3]={0.0f,1.0f,0.0f}, zvec[3];
1263 float onevec[3]={0.0f,0.0f,0.0f}, tvec[3],tvec2[3], bb_center[3];
1264 float uvx=0.0f, uvy=0.0f, uvdx=1.0f, uvdy=1.0f, time=0.0f;
1266 if(align<PART_BB_VIEW)
1269 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1270 vlr->v1= RE_findOrAddVert(obr, obr->totvert++);
1271 vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
1272 vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
1273 vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
1275 if(lock && align==PART_BB_VIEW){
1276 VECCOPY(xvec,bb_ob->obmat[0]);
1278 VECCOPY(yvec,bb_ob->obmat[1]);
1280 VECCOPY(zvec,bb_ob->obmat[2]);
1283 else if(align==PART_BB_VEL){
1287 VECSUB(zvec,bb_ob->obmat[3],vec);
1289 float fac=-Inpf(zvec,temp);
1290 VECADDFAC(zvec,zvec,temp,fac);
1293 Crossf(xvec,temp,zvec);
1295 Crossf(yvec,zvec,xvec);
1298 VECSUB(zvec,bb_ob->obmat[3],vec);
1303 if(align<PART_BB_VIEW)
1304 Crossf(xvec,onevec,zvec);
1306 Crossf(xvec,bb_ob->obmat[1],zvec);
1308 Crossf(yvec,zvec,xvec);
1312 VECCOPY(tvec2,yvec);
1314 VecMulf(xvec,cos(tilt*(float)M_PI));
1315 VecMulf(tvec2,sin(tilt*(float)M_PI));
1316 VECADD(xvec,xvec,tvec2);
1318 VecMulf(yvec,cos(tilt*(float)M_PI));
1319 VecMulf(tvec,-sin(tilt*(float)M_PI));
1320 VECADD(yvec,yvec,tvec);
1325 VECADDFAC(bb_center,vec,xvec,offset[0]);
1326 VECADDFAC(bb_center,bb_center,yvec,offset[1]);
1328 VECADD(vlr->v1->co,bb_center,xvec);
1329 VECADD(vlr->v1->co,vlr->v1->co,yvec);
1330 MTC_Mat4MulVecfl(re->viewmat,vlr->v1->co);
1332 VECSUB(vlr->v2->co,bb_center,xvec);
1333 VECADD(vlr->v2->co,vlr->v2->co,yvec);
1334 MTC_Mat4MulVecfl(re->viewmat,vlr->v2->co);
1336 VECSUB(vlr->v3->co,bb_center,xvec);
1337 VECSUB(vlr->v3->co,vlr->v3->co,yvec);
1338 MTC_Mat4MulVecfl(re->viewmat,vlr->v3->co);
1340 VECADD(vlr->v4->co,bb_center,xvec);
1341 VECSUB(vlr->v4->co,vlr->v4->co,yvec);
1342 MTC_Mat4MulVecfl(re->viewmat,vlr->v4->co);
1344 CalcNormFloat4(vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co, vlr->n);
1345 VECCOPY(vlr->v1->n,vlr->n);
1346 VECCOPY(vlr->v2->n,vlr->n);
1347 VECCOPY(vlr->v3->n,vlr->n);
1348 VECCOPY(vlr->v4->n,vlr->n);
1354 uvdx=uvdy=1.0f/(float)uv_split;
1355 if(anim==PART_BB_ANIM_TIME){
1356 if(split_offset==PART_BB_OFF_NONE)
1358 else if(split_offset==PART_BB_OFF_LINEAR)
1359 time=(float)fmod(pa_time+(float)p/(float)(uv_split*uv_split),1.0f);
1360 else /* split_offset==PART_BB_OFF_RANDOM */
1361 time=(float)fmod(pa_time+random,1.0f);
1364 else if(anim==PART_BB_ANIM_ANGLE){
1365 if(align==PART_BB_VIEW){
1366 time=(float)fmod((tilt+1.0f)/2.0f,1.0);
1369 float axis1[3]={0.0f,0.0f,0.0f};
1370 float axis2[3]={0.0f,0.0f,0.0f};
1371 axis1[(align+1)%3]=1.0f;
1372 axis2[(align+2)%3]=1.0f;
1377 time=saacos(Inpf(zvec,axis1))/(float)M_PI;
1378 if(Inpf(zvec,axis2)<0.0f)
1379 time=1.0f-time/2.0f;
1383 if(split_offset==PART_BB_OFF_LINEAR)
1384 time=(float)fmod(pa_time+(float)p/(float)(uv_split*uv_split),1.0f);
1385 else if(split_offset==PART_BB_OFF_RANDOM)
1386 time=(float)fmod(pa_time+random,1.0f);
1389 if(split_offset==PART_BB_OFF_NONE)
1391 else if(split_offset==PART_BB_OFF_LINEAR)
1392 time=(float)fmod((float)p/(float)(uv_split*uv_split),1.0f);
1393 else /* split_offset==PART_BB_OFF_RANDOM */
1396 uvx=uvdx*floor((float)(uv_split*uv_split)*(float)fmod((double)time,(double)uvdx));
1397 uvy=uvdy*floor((1.0f-time)*(float)uv_split);
1398 if(fmod(time,1.0f/uv_split)==0.0f)
1404 mtf=RE_vlakren_get_tface(obr,vlr,uv[0],NULL,1);
1415 /* time-index UVs */
1417 mtf=RE_vlakren_get_tface(obr,vlr,uv[1],NULL,1);
1418 mtf->uv[0][0]=mtf->uv[1][0]=mtf->uv[2][0]=mtf->uv[3][0]=pa_time;
1419 mtf->uv[0][1]=mtf->uv[1][1]=mtf->uv[2][1]=mtf->uv[3][1]=(float)p/(float)totpart;
1423 if(uv_split>1 && uv[2]>=0){
1424 mtf=RE_vlakren_get_tface(obr,vlr,uv[2],NULL,1);
1425 mtf->uv[0][0]=uvx+uvdx;
1426 mtf->uv[0][1]=uvy+uvdy;
1428 mtf->uv[1][1]=uvy+uvdy;
1431 mtf->uv[3][0]=uvx+uvdx;
1435 static void render_new_particle(Render *re, ObjectRen *obr, DerivedMesh *dm, Material *ma, int path, int first, int line,
1436 float time, float *loc, float *loc1, float *orco, float *surfnor, int totuv, float *uvco,
1437 int totcol, MCol *mcol, float size, int seed, int override_uv,
1438 int adapt, float adapt_angle, float adapt_pix)
1442 if(ma->mode&MA_WIRE)
1443 static_particle_wire(obr, ma, loc, loc1, first, line);
1444 else if(ma->mode & MA_HALO){
1445 har= RE_inithalo_particle(re, obr, dm, ma, loc, loc1, orco, uvco, size, 1.0, seed);
1446 if(har) har->lay= obr->ob->lay;
1449 static_particle_strand(re, obr, ma, orco, surfnor, uvco, totuv, mcol, totcol, loc, loc1, time, first, line, adapt, adapt_angle, adapt_pix, override_uv);
1452 har= RE_inithalo_particle(re, obr, dm, ma, loc, NULL, orco, uvco, size, 0.0, seed);
1453 if(har) har->lay= obr->ob->lay;
1456 static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem *psys, int timeoffset)
1458 Object *ob= obr->ob;
1459 Object *tob=0, *bb_ob=re->scene->camera;
1462 ParticleSystemModifierData *psmd;
1463 ParticleSystem *tpsys=0;
1464 ParticleSettings *part, *tpart=0;
1465 ParticleData *pars, *pa=0,*tpa=0;
1466 ParticleKey *states=0;
1468 ParticleCacheKey *cache=0;
1469 StrandBuffer *strandbuf=0;
1470 StrandVert *svert=0;
1471 StrandBound *sbound= 0;
1472 StrandRen *strand=0;
1475 float loc[3],loc1[3],loc0[3],vel[3],mat[4][4],nmat[3][3],co[3],nor[3],time;
1476 float *orco=0,*surfnor=0,*uvco=0, strandlen=0.0f, curlen=0.0f;
1477 float hasize, pa_size, pa_time, r_tilt, cfra=bsystem_time(ob,(float)CFRA,0.0);
1478 float adapt_angle=0.0, adapt_pix=0.0, random, simplify[2];
1479 int i, a, k, max_k=0, totpart, totuv=0, totcol=0, override_uv=-1, dosimplify = 0, dosurfacecache = 0;
1480 int path_possible=0, keys_possible=0, baked_keys=0, totchild=0;
1481 int seed, path_nbr=0, path=0, orco1=0, adapt=0, uv[3]={0,0,0}, num;
1482 int totface, *origindex = 0;
1485 /* 1. check that everything is ok & updated */
1489 totchild=psys->totchild;
1492 pars=psys->particles;
1494 if(part==NULL || pars==NULL || !psys_check_enabled(ob, psys))
1497 if(part->draw_as==PART_DRAW_OB || part->draw_as==PART_DRAW_GR || part->draw_as==PART_DRAW_NOT)
1500 /* 2. start initialising things */
1501 if(part->phystype==PART_PHYS_KEYED){
1502 if(psys->flag & PSYS_FIRST_KEYED)
1503 psys_count_keyed_targets(ob,psys);
1508 psmd= psys_get_modifier(ob,psys);
1509 if(!(psmd->modifier.mode & eModifierMode_Render))
1512 if(G.rendering == 0) { /* preview render */
1513 totchild = (int)((float)totchild * (float)part->disp / 100.0f);
1516 psys->flag|=PSYS_DRAWING;
1518 rng= rng_new(psys->seed);
1520 ma= give_render_material(re, ob, part->omat);
1526 calc_ipo(ma->ipo, cfra);
1527 execute_ipo((ID *)ma, ma->ipo);
1530 RE_set_customdata_names(obr, &psmd->dm->faceData);
1531 totuv=CustomData_number_of_layers(&psmd->dm->faceData,CD_MTFACE);
1532 totcol=CustomData_number_of_layers(&psmd->dm->faceData,CD_MCOL);
1534 if(ma->texco & TEXCO_UV && totuv) {
1535 uvco = MEM_callocN(totuv*2*sizeof(float),"particle_uvs");
1537 if(ma->strand_uvname[0]) {
1538 override_uv= CustomData_get_named_layer_index(&psmd->dm->faceData,CD_MTFACE,ma->strand_uvname);
1539 override_uv-= CustomData_get_layer_index(&psmd->dm->faceData,CD_MTFACE);
1544 mcol = MEM_callocN(totcol*sizeof(MCol),"particle_mcols");
1546 if(part->draw_as==PART_DRAW_BB){
1547 int first_uv=CustomData_get_layer_index(&psmd->dm->faceData,CD_MTFACE);
1549 uv[0]=CustomData_get_named_layer_index(&psmd->dm->faceData,CD_MTFACE,psys->bb_uvname[0]);
1551 uv[0]=CustomData_get_active_layer_index(&psmd->dm->faceData,CD_MTFACE);
1553 uv[1]=CustomData_get_named_layer_index(&psmd->dm->faceData,CD_MTFACE,psys->bb_uvname[1]);
1555 // uv[1]=CustomData_get_active_layer_index(&psmd->dm->faceData,CD_MTFACE);
1557 uv[2]=CustomData_get_named_layer_index(&psmd->dm->faceData,CD_MTFACE,psys->bb_uvname[2]);
1559 // uv[2]=CustomData_get_active_layer_index(&psmd->dm->faceData,CD_MTFACE);
1568 if(part->flag&PART_ABS_TIME && part->ipo){
1569 calc_ipo(part->ipo, cfra);
1570 execute_ipo((ID *)part, part->ipo);
1573 if(part->flag&PART_GLOB_TIME)
1574 cfra=bsystem_time(0,(float)CFRA,0.0);
1576 if(part->type==PART_REACTOR){
1577 psys_get_reactor_target(ob, psys, &tob, &tpsys);
1578 if(tpsys && (part->from==PART_FROM_PARTICLE || part->phystype==PART_PHYS_NO)){
1579 psmd=psys_get_modifier(tob,tpsys);
1584 hasize = ma->hasize;
1589 MTC_Mat4MulMat4(mat, ob->obmat, re->viewmat);
1590 MTC_Mat4Invert(ob->imat, mat); /* need to be that way, for imat texture */
1591 Mat3CpyMat4(nmat, ob->imat);
1594 totpart=psys->totpart;
1596 if(psys->pathcache){
1600 if(part->draw_as==PART_DRAW_PATH){
1602 path_nbr=(int)pow(2.0,(double) part->ren_step);
1603 //if(part->phystype==PART_PHYS_KEYED && (psys->flag&PSYS_BAKED)==0)
1604 // path_nbr*=psys->totkeyed;
1607 if((ma->mode & (MA_HALO|MA_WIRE))==0) {
1608 orco= MEM_mallocN(3*sizeof(float)*(totpart+totchild), "particle orcos");
1609 set_object_orco(re, psys, orco);
1614 if(part->draw&PART_DRAW_REN_ADAPT) {
1616 adapt_pix=(float)part->adapt_pix;
1617 adapt_angle=cos((float)part->adapt_angle*(float)(M_PI/180.0));
1620 if(re->r.renderer==R_INTERN && part->draw&PART_DRAW_REN_STRAND) {
1621 strandbuf= RE_addStrandBuffer(obr, (totpart+totchild)*(path_nbr+1));
1623 strandbuf->lay= ob->lay;
1624 Mat4CpyMat4(strandbuf->winmat, re->winmat);
1625 strandbuf->winx= re->winx;
1626 strandbuf->winy= re->winy;
1627 strandbuf->maxdepth= 2;
1628 strandbuf->adaptcos= cos((float)part->adapt_angle*(float)(M_PI/180.0));
1629 strandbuf->overrideuv= override_uv;
1630 strandbuf->minwidth= ma->strand_min;
1632 if(ma->strand_widthfade == 0.0f)
1633 strandbuf->widthfade= 0.0f;
1634 else if(ma->strand_widthfade >= 1.0f)
1635 strandbuf->widthfade= 2.0f - ma->strand_widthfade;
1637 strandbuf->widthfade= 1.0f/MAX2(ma->strand_widthfade, 1e-5f);
1639 if(part->flag & PART_HAIR_BSPLINE)
1640 strandbuf->flag |= R_STRAND_BSPLINE;
1641 if(ma->mode & MA_STR_B_UNITS)
1642 strandbuf->flag |= R_STRAND_B_UNITS;
1644 svert= strandbuf->vert;
1646 if(re->r.mode & R_SPEED)
1648 else if((re->wrld.mode & WO_AMB_OCC) && (re->wrld.ao_gather_method == WO_AOGATHER_APPROX))
1652 totface= psmd->dm->getNumFaces(psmd->dm);
1653 origindex= psmd->dm->getFaceDataArray(psmd->dm, CD_ORIGINDEX);
1655 for(a=0; a<totface; a++)
1656 strandbuf->totbound= MAX2(strandbuf->totbound, origindex[a]);
1657 strandbuf->totbound++;
1659 strandbuf->totbound++;
1660 strandbuf->bound= MEM_callocN(sizeof(StrandBound)*strandbuf->totbound, "StrandBound");
1661 sbound= strandbuf->bound;
1662 sbound->start= sbound->end= 0;
1666 else if(keys_possible && part->draw&PART_DRAW_KEYS){
1667 path_nbr=part->keys_step;
1673 orco=MEM_mallocN(3*sizeof(float),"particle orco");
1678 psys->lattice=psys_get_lattice(ob,psys);
1680 /* 3. start creating renderable things */
1681 for(a=0,pa=pars; a<totpart+totchild; a++, pa++, seed++) {
1682 random = rng_getFloat(rng);
1685 if(pa->flag & PARS_UNEXIST) continue;
1687 pa_time=(cfra-pa->time)/pa->lifetime;
1688 if((part->flag&PART_ABS_TIME)==0){
1690 /* correction for lifetime */
1691 calc_ipo(ma->ipo, 100.0f*pa_time);
1692 execute_ipo((ID *)ma, ma->ipo);
1695 /* correction for lifetime */
1696 calc_ipo(part->ipo, 100.0f*pa_time);
1697 execute_ipo((ID *)part, part->ipo);
1701 hasize = ma->hasize;
1704 if(tpsys && (part->from==PART_FROM_PARTICLE || part->phystype==PART_PHYS_NO)){
1705 tpa=tpsys->particles+pa->num;
1706 psys_particle_on_emitter(ob, psmd,tpart->from,tpa->num,pa->num_dmcache,tpa->fuv,tpa->foffset,co,nor,0,0,orco,0);
1709 psys_particle_on_emitter(ob, psmd,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,nor,0,0,orco,0);
1711 num= pa->num_dmcache;
1713 if(num == DMCACHE_NOTFOUND)
1714 if(pa->num < psmd->dm->getNumFaces(psmd->dm))
1717 if(uvco && ELEM(part->from,PART_FROM_FACE,PART_FROM_VOLUME)){
1718 for(i=0; i<totuv; i++){
1719 if(num != DMCACHE_NOTFOUND) {
1720 MFace *mface=psmd->dm->getFaceData(psmd->dm,num,CD_MFACE);
1721 mtface=(MTFace*)CustomData_get_layer_n(&psmd->dm->faceData,CD_MTFACE,i);
1724 psys_interpolate_uvs(mtface,mface->v4,pa->fuv,uvco+2*i);
1728 uvco[2*i + 1]= 0.0f;
1732 if(mcol && ELEM(part->from,PART_FROM_FACE,PART_FROM_VOLUME)){
1733 for(i=0; i<totcol; i++){
1734 if(num != DMCACHE_NOTFOUND) {
1735 MFace *mface=psmd->dm->getFaceData(psmd->dm,num,CD_MFACE);
1736 MCol *mc=(MCol*)CustomData_get_layer_n(&psmd->dm->faceData,CD_MCOL,i);
1739 psys_interpolate_mcol(mc,mface->v4,pa->fuv,mcol+i);
1742 memset(&mcol[i], 0, sizeof(MCol));
1748 r_tilt=1.0f+pa->r_ave[0];
1751 cache = psys->pathcache[a];
1752 max_k = (int)cache->steps;
1755 if(totchild && (part->draw&PART_DRAW_PARENT)==0) continue;
1758 ChildParticle *cpa= psys->child+a-totpart;
1760 pa_time=psys_get_child_time(psys, cpa, cfra);
1762 if((part->flag&PART_ABS_TIME)==0){
1764 /* correction for lifetime */
1765 calc_ipo(ma->ipo, 100.0f*pa_time);
1766 execute_ipo((ID *)ma, ma->ipo);
1769 /* correction for lifetime */
1770 calc_ipo(part->ipo, 100.0f*pa_time);
1771 execute_ipo((ID *)part, part->ipo);
1775 pa_size=psys_get_child_size(psys, cpa, cfra, &pa_time);
1777 r_tilt=2.0f*cpa->rand[2];
1782 if(part->childtype == PART_CHILD_FACES) {
1783 psys_particle_on_emitter(ob, psmd,
1784 PART_FROM_FACE, cpa->num,DMCACHE_ISCHILD,
1785 cpa->fuv,cpa->foffset,co,nor,0,0,orco,0);
1788 ParticleData *par = psys->particles + cpa->parent;
1789 psys_particle_on_emitter(ob, psmd, part->from,
1790 par->num,DMCACHE_ISCHILD,par->fuv,
1791 par->foffset,co,nor,0,0,orco,0);
1795 if(part->from!=PART_FROM_PARTICLE && part->childtype==PART_CHILD_FACES){
1796 for(i=0; i<totuv; i++){
1797 if(part->childtype==PART_CHILD_FACES){
1798 MFace *mface=psmd->dm->getFaceData(psmd->dm,cpa->num,CD_MFACE);
1800 mtface=(MTFace*)CustomData_get_layer_n(&psmd->dm->faceData,CD_MTFACE,i);
1803 psys_interpolate_uvs(mtface,mface->v4,cpa->fuv,uvco+2*i);
1806 uvco[2*i]=uvco[2*i+1]=0.0f;
1810 else if(ELEM(part->from,PART_FROM_FACE,PART_FROM_VOLUME)){
1811 ParticleData *parent = psys->particles + cpa->parent;
1812 num= parent->num_dmcache;
1814 if(num == DMCACHE_NOTFOUND)
1815 if(parent->num < psmd->dm->getNumFaces(psmd->dm))
1818 for(i=0; i<totuv; i++) {
1819 if(num != DMCACHE_NOTFOUND) {
1820 MFace *mface=psmd->dm->getFaceData(psmd->dm,num,CD_MFACE);
1821 mtface=(MTFace*)CustomData_get_layer_n(&psmd->dm->faceData,CD_MTFACE,i);
1823 psys_interpolate_uvs(mtface,mface->v4,parent->fuv,uvco+2*i);
1827 uvco[2*i + 1]= 0.0f;
1834 if(part->from!=PART_FROM_PARTICLE && part->childtype==PART_CHILD_FACES){
1835 for(i=0; i<totcol; i++){
1836 if(part->childtype==PART_CHILD_FACES){
1837 MFace *mface=psmd->dm->getFaceData(psmd->dm,cpa->num,CD_MFACE);
1838 MCol *mc=(MCol*)CustomData_get_layer_n(&psmd->dm->faceData,CD_MCOL,i);
1841 psys_interpolate_mcol(mc,mface->v4,cpa->fuv,mcol+i);
1844 memset(&mcol[i], 0, sizeof(MCol));
1847 else if(ELEM(part->from,PART_FROM_FACE,PART_FROM_VOLUME)){
1848 ParticleData *parent = psys->particles + cpa->parent;
1849 num= parent->num_dmcache;
1851 if(num == DMCACHE_NOTFOUND)
1852 if(parent->num < psmd->dm->getNumFaces(psmd->dm))
1855 for(i=0; i<totcol; i++){
1856 if(num != DMCACHE_NOTFOUND) {
1857 MFace *mface=psmd->dm->getFaceData(psmd->dm,num,CD_MFACE);
1858 MCol *mc=(MCol*)CustomData_get_layer_n(&psmd->dm->faceData,CD_MCOL,i);
1861 psys_interpolate_mcol(mc,mface->v4,parent->fuv,mcol+i);
1864 memset(&mcol[i], 0, sizeof(MCol));
1869 dosimplify= psys_render_simplify_params(psys, cpa, simplify);
1872 cache = psys->childcache[a-totpart];
1873 max_k = (int)cache->steps;
1877 if(origindex[cpa->num]+1 > sbound - strandbuf->bound) {
1878 sbound= strandbuf->bound + origindex[cpa->num]+1;
1879 sbound->start= sbound->end= obr->totstrand;
1884 /* surface normal shading setup */
1885 if(ma->mode_l & MA_STR_SURFDIFF) {
1886 Mat3MulVecfl(nmat, nor);
1892 /* strand render setup */
1894 strand= RE_findOrAddStrand(obr, obr->totstrand++);
1895 strand->buffer= strandbuf;
1896 strand->vert= svert;
1897 VECCOPY(strand->orco, orco);
1900 float *ssimplify= RE_strandren_get_simplify(obr, strand, 1);
1901 ssimplify[0]= simplify[0];
1902 ssimplify[1]= simplify[1];
1906 float *snor= RE_strandren_get_surfnor(obr, strand, 1);
1907 VECCOPY(snor, surfnor);
1910 if(dosurfacecache && num >= 0) {
1911 int *facenum= RE_strandren_get_face(obr, strand, 1);
1916 for(i=0; i<totuv; i++) {
1917 if(i != override_uv) {
1918 float *uv= RE_strandren_get_uv(obr, strand, i, NULL, 1);
1926 for(i=0; i<totcol; i++) {
1927 MCol *mc= RE_strandren_get_mcol(obr, strand, i, NULL, 1);
1935 /* strandco computation setup */
1939 for(k=1; k<=path_nbr; k++)
1941 strandlen += VecLenf((cache+k-1)->co, (cache+k)->co);
1944 for(k=0; k<=path_nbr; k++){
1947 //bti->convert_bake_key(bsys,cache+k,0,(void*)&state);
1948 //copy_particle_key(&state,cache+k,0);
1949 VECCOPY(state.co,(cache+k)->co);
1950 VECCOPY(state.vel,(cache+k)->vel);
1956 curlen += VecLenf((cache+k-1)->co, (cache+k)->co);
1957 time= curlen/strandlen;
1962 if(psys_get_particle_state(ob,psys,a,&state,0)==0)
1966 VECCOPY(loc,state.co);
1967 if(part->draw_as!=PART_DRAW_BB)
1968 MTC_Mat4MulVecfl(re->viewmat,loc);
1970 if(part->draw_as==PART_DRAW_LINE) {
1971 VECCOPY(vel,state.vel);
1972 //VECADD(vel,vel,state.co);
1973 MTC_Mat4Mul3Vecfl(re->viewmat,vel);
1974 //VECSUB(vel,vel,loc);
1976 if(part->draw & PART_DRAW_VEL_LENGTH)
1977 VecMulf(vel,VecLength(state.vel));
1978 VECADDFAC(loc0,loc,vel,-part->draw_line[0]);
1979 VECADDFAC(loc1,loc,vel,part->draw_line[1]);
1981 render_new_particle(re,obr,psmd->dm,ma,1,0,1,0.0f,loc0,loc1,
1982 orco,surfnor,totuv,uvco,totcol,mcol,hasize,seed,override_uv,0,0,0);
1984 else if(part->draw_as==PART_DRAW_BB) {
1985 VECCOPY(vel,state.vel);
1986 //MTC_Mat4Mul3Vecfl(re->viewmat,vel);
1987 particle_billboard(re,obr,ma,bb_ob,loc,vel,pa_size,part->bb_tilt*(1.0f-part->bb_rand_tilt*r_tilt),
1988 part->bb_align,part->draw&PART_DRAW_BB_LOCK,
1989 a,totpart+totchild,part->bb_uv_split,part->bb_anim,part->bb_split_offset,random,pa_time,part->bb_offset,uv);
1991 else if(strandbuf) {
1992 VECCOPY(svert->co, loc);
1993 svert->strandco= -1.0f + 2.0f*time;
1999 VECSUB(loc0,loc1,loc);
2000 VECADD(loc0,loc1,loc0);
2001 render_new_particle(re,obr,psmd->dm,ma,path,1,0,0.0f,loc1,loc0,
2002 orco,surfnor,totuv,uvco,totcol,mcol,hasize,seed,override_uv,
2003 adapt,adapt_angle,adapt_pix);
2006 if(path_nbr==0 || k)
2007 render_new_particle(re,obr,psmd->dm,ma,path,0,0,time,loc,loc1,
2008 orco,surfnor,totuv,uvco,totcol,mcol,hasize,seed,override_uv,
2009 adapt,adapt_angle,adapt_pix);
2018 if(re->test_break())
2023 strandbuf->surface= cache_strand_surface(re, obr, psmd->dm, mat, timeoffset);
2026 if(ma) do_mat_ipo(ma);
2045 psys->flag &= ~PSYS_DRAWING;
2052 if(path && (ma->mode_l & MA_TANGENT_STR)==0)
2053 calc_vertexnormals(re, obr, 0, 0);
2058 /* ------------------------------------------------------------------------- */
2060 /* ------------------------------------------------------------------------- */
2062 static void make_render_halos(Render *re, ObjectRen *obr, Mesh *me, int totvert, MVert *mvert, Material *ma, float *orco)
2064 Object *ob= obr->ob;
2066 float xn, yn, zn, nor[3], view[3];
2067 float vec[3], hasize, mat[4][4], imat[3][3];
2068 int a, ok, seed= ma->seed1;
2070 MTC_Mat4MulMat4(mat, ob->obmat, re->viewmat);
2071 MTC_Mat3CpyMat4(imat, ob->imat);
2075 for(a=0; a<totvert; a++, mvert++) {
2081 VECCOPY(vec, mvert->co);
2082 MTC_Mat4MulVecfl(mat, vec);
2084 if(ma->mode & MA_HALOPUNO) {
2090 nor[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
2091 nor[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
2092 nor[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
2098 zn= nor[0]*view[0]+nor[1]*view[1]+nor[2]*view[2];
2099 if(zn>=0.0) hasize= 0.0;
2100 else hasize*= zn*zn*zn*zn;
2103 if(orco) har= RE_inithalo(re, obr, ma, vec, NULL, orco, hasize, 0.0, seed);
2104 else har= RE_inithalo(re, obr, ma, vec, NULL, mvert->co, hasize, 0.0, seed);
2105 if(har) har->lay= ob->lay;
2112 static int verghalo(const void *a1, const void *a2)
2114 const HaloRen *har1= *(const HaloRen**)a1;
2115 const HaloRen *har2= *(const HaloRen**)a2;
2117 if(har1->zs < har2->zs) return 1;
2118 else if(har1->zs > har2->zs) return -1;
2122 static void sort_halos(Render *re, int totsort)
2125 HaloRen *har= NULL, **haso;
2128 if(re->tothalo==0) return;
2130 re->sortedhalos= MEM_callocN(sizeof(HaloRen*)*re->tothalo, "sorthalos");
2131 haso= re->sortedhalos;
2133 for(obr=re->objecttable.first; obr; obr=obr->next) {
2134 for(a=0; a<obr->tothalo; a++) {
2135 if((a & 255)==0) har= obr->bloha[a>>8];
2142 qsort(re->sortedhalos, totsort, sizeof(HaloRen*), verghalo);
2145 /* ------------------------------------------------------------------------- */
2146 /* Displacement Mapping */
2147 /* ------------------------------------------------------------------------- */
2149 static short test_for_displace(Render *re, Object *ob)
2151 /* return 1 when this object uses displacement textures. */
2155 for (i=1; i<=ob->totcol; i++) {
2156 ma=give_render_material(re, ob, i);
2157 /* ma->mapto is ORed total of all mapto channels */
2158 if(ma && (ma->mapto & MAP_DISPLACE)) return 1;
2163 static void displace_render_vert(Render *re, ObjectRen *obr, ShadeInput *shi, VertRen *vr, int vindex, float *scale, float mat[][4], float imat[][3])
2166 short texco= shi->mat->texco;
2167 float sample=0, displace[3];
2171 /* shi->co is current render coord, just make sure at least some vector is here */
2172 VECCOPY(shi->co, vr->co);
2173 /* vertex normal is used for textures type 'col' and 'var' */
2174 VECCOPY(shi->vn, vr->n);
2177 Mat4MulVecfl(mat, shi->co);
2180 shi->vn[0]= imat[0][0]*vr->n[0]+imat[0][1]*vr->n[1]+imat[0][2]*vr->n[2];
2181 shi->vn[1]= imat[1][0]*vr->n[0]+imat[1][1]*vr->n[1]+imat[1][2]*vr->n[2];
2182 shi->vn[2]= imat[2][0]*vr->n[0]+imat[2][1]*vr->n[1]+imat[2][2]*vr->n[2];
2185 if (texco & TEXCO_UV) {
2187 shi->actuv= obr->actmtface;
2189 for (i=0; (tface=RE_vlakren_get_tface(obr, shi->vlr, i, &name, 0)); i++) {
2190 ShadeInputUV *suv= &shi->uv[i];
2192 /* shi.uv needs scale correction from tface uv */
2193 suv->uv[0]= 2*tface->uv[vindex][0]-1.0f;
2194 suv->uv[1]= 2*tface->uv[vindex][1]-1.0f;
2201 /* set all rendercoords, 'texco' is an ORed value for all textures needed */
2202 if ((texco & TEXCO_ORCO) && (vr->orco)) {
2203 VECCOPY(shi->lo, vr->orco);
2205 if (texco & TEXCO_STICKY) {
2206 float *sticky= RE_vertren_get_sticky(obr, vr, 0);
2208 shi->sticky[0]= sticky[0];
2209 shi->sticky[1]= sticky[1];
2210 shi->sticky[2]= 0.0f;
2213 if (texco & TEXCO_GLOB) {
2214 VECCOPY(shi->gl, shi->co);
2215 MTC_Mat4MulVecfl(re->viewinv, shi->gl);
2217 if (texco & TEXCO_NORM) {
2218 VECCOPY(shi->orn, shi->vn);
2220 if(texco & TEXCO_REFL) {
2224 shi->displace[0]= shi->displace[1]= shi->displace[2]= 0.0;
2226 do_material_tex(shi);
2228 //printf("no=%f, %f, %f\nbefore co=%f, %f, %f\n", vr->n[0], vr->n[1], vr->n[2],
2229 //vr->co[0], vr->co[1], vr->co[2]);
2231 displace[0]= shi->displace[0] * scale[0];
2232 displace[1]= shi->displace[1] * scale[1];
2233 displace[2]= shi->displace[2] * scale[2];
2236 Mat3MulVecfl(imat, displace);
2238 /* 0.5 could become button once? */
2239 vr->co[0] += displace[0];
2240 vr->co[1] += displace[1];
2241 vr->co[2] += displace[2];
2243 //printf("after co=%f, %f, %f\n", vr->co[0], vr->co[1], vr->co[2]);
2245 /* we just don't do this vertex again, bad luck for other face using same vertex with
2246 different material... */
2249 /* Pass sample back so displace_face can decide which way to split the quad */
2250 sample = shi->displace[0]*shi->displace[0];
2251 sample += shi->displace[1]*shi->displace[1];
2252 sample += shi->displace[2]*shi->displace[2];
2255 /* Should be sqrt(sample), but I'm only looking for "bigger". Save the cycles. */
2259 static void displace_render_face(Render *re, ObjectRen *obr, VlakRen *vlr, float *scale, float mat[][4], float imat[][3])
2263 /* Warning, This is not that nice, and possibly a bit slow,
2264 however some variables were not initialized properly in, unless using shade_input_initialize(...), we need to do a memset */
2265 memset(&shi, 0, sizeof(ShadeInput));
2266 /* end warning! - Campbell */
2268 /* set up shadeinput struct for multitex() */
2270 /* memset above means we dont need this */
2271 /*shi.osatex= 0;*/ /* signal not to use dx[] and dy[] texture AA vectors */
2273 shi.vlr= vlr; /* current render face */
2274 shi.mat= vlr->mat; /* current input material */
2276 /* Displace the verts, flag is set when done */
2278 displace_render_vert(re, obr, &shi, vlr->v1,0, scale, mat, imat);
2281 displace_render_vert(re, obr, &shi, vlr->v2, 1, scale, mat, imat);
2284 displace_render_vert(re, obr, &shi, vlr->v3, 2, scale, mat, imat);
2288 displace_render_vert(re, obr, &shi, vlr->v4, 3, scale, mat, imat);
2290 /* closest in displace value. This will help smooth edges. */
2291 if ( fabs(vlr->v1->accum - vlr->v3->accum) > fabs(vlr->v2->accum - vlr->v4->accum))
2292 vlr->flag |= R_DIVIDE_24;
2293 else vlr->flag &= ~R_DIVIDE_24;
2296 /* Recalculate the face normal - if flipped before, flip now */
2298 CalcNormFloat4(vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co, vlr->n);
2301 CalcNormFloat(vlr->v3->co, vlr->v2->co, vlr->v1->co, vlr->n);
2305 static void do_displacement(Render *re, ObjectRen *obr, float mat[][4], float imat[][3])
2309 // float min[3]={1e30, 1e30, 1e30}, max[3]={-1e30, -1e30, -1e30};
2310 float scale[3]={1.0f, 1.0f, 1.0f}, temp[3];//, xn
2311 int i; //, texflag=0;
2314 /* Object Size with parenting */
2317 VecAddf(temp, obt->size, obt->dsize);
2318 scale[0]*=temp[0]; scale[1]*=temp[1]; scale[2]*=temp[2];
2322 /* Clear all flags */
2323 for(i=0; i<obr->totvert; i++){
2324 vr= RE_findOrAddVert(obr, i);
2328 for(i=0; i<obr->totvlak; i++){
2329 vlr=RE_findOrAddVlak(obr, i);
2330 displace_render_face(re, obr, vlr, scale, mat, imat);
2333 /* Recalc vertex normals */
2334 calc_vertexnormals(re, obr, 0, 0);
2337 /* ------------------------------------------------------------------------- */
2339 /* ------------------------------------------------------------------------- */
2341 static void init_render_mball(Render *re, ObjectRen *obr)
2343 Object *ob= obr->ob;
2346 VlakRen *vlr, *vlr1;
2348 float *data, *nors, *orco, mat[4][4], imat[3][3], xn, yn, zn;
2349 int a, need_orco, vlakindex, *index;
2351 if (ob!=find_basis_mball(ob))
2354 MTC_Mat4MulMat4(mat, ob->obmat, re->viewmat);
2355 MTC_Mat4Invert(ob->imat, mat);
2356 MTC_Mat3CpyMat4(imat, ob->imat);
2358 ma= give_render_material(re, ob, 1);
2361 if(ma->texco & TEXCO_ORCO) {
2365 makeDispListMBall(ob);
2371 orco= get_object_orco(re, ob);
2373 for(a=0; a<dl->nr; a++, data+=3, nors+=3, orco+=3) {
2375 ver= RE_findOrAddVert(obr, obr->totvert++);
2376 VECCOPY(ver->co, data);
2377 MTC_Mat4MulVecfl(mat, ver->co);
2379 /* render normals are inverted */
2385 ver->n[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
2386 ver->n[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
2387 ver->n[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
2389 //if(ob->transflag & OB_NEG_SCALE) VecMulf(ver->n. -1.0);
2391 if(need_orco) ver->orco= orco;
2395 for(a=0; a<dl->parts; a++, index+=4) {
2397 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2398 vlr->v1= RE_findOrAddVert(obr, index[0]);
2399 vlr->v2= RE_findOrAddVert(obr, index[1]);
2400 vlr->v3= RE_findOrAddVert(obr, index[2]);
2403 if(ob->transflag & OB_NEG_SCALE)
2404 CalcNormFloat(vlr->v1->co, vlr->v2->co, vlr->v3->co, vlr->n);
2406 CalcNormFloat(vlr->v3->co, vlr->v2->co, vlr->v1->co, vlr->n);
2409 vlr->flag= ME_SMOOTH+R_NOPUNOFLIP;
2412 /* mball -too bad- always has triangles, because quads can be non-planar */
2413 if(index[3] && index[3]!=index[2]) {
2414 vlr1= RE_findOrAddVlak(obr, obr->totvlak++);
2415 vlakindex= vlr1->index;
2417 vlr1->index= vlakindex;
2419 vlr1->v3= RE_findOrAddVert(obr, index[3]);
2420 if(ob->transflag & OB_NEG_SCALE)
2421 CalcNormFloat(vlr1->v1->co, vlr1->v2->co, vlr1->v3->co, vlr1->n);
2423 CalcNormFloat(vlr1->v3->co, vlr1->v2->co, vlr1->v1->co, vlr1->n);
2427 /* enforce display lists remade */
2428 freedisplist(&ob->disp);
2430 /* this enforces remake for real, orco displist is small (in scale) */
2431 ob->recalc |= OB_RECALC_DATA;
2434 /* ------------------------------------------------------------------------- */
2435 /* Surfaces and Curves */
2436 /* ------------------------------------------------------------------------- */
2438 /* returns amount of vertices added for orco */
2439 static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar, float *orco, float mat[4][4])
2441 Object *ob= obr->ob;
2442 VertRen *v1, *v2, *v3, *v4, *ver;
2443 VlakRen *vlr, *vlr1, *vlr2, *vlr3;
2444 Curve *cu= ob->data;
2445 float *data, n1[3], flen;
2446 int u, v, orcoret= 0;
2447 int p1, p2, p3, p4, a;
2448 int sizeu, nsizeu, sizev, nsizev;
2449 int startvert, startvlak;
2451 startvert= obr->totvert;
2452 nsizeu = sizeu = dl->parts; nsizev = sizev = dl->nr;
2455 for (u = 0; u < sizeu; u++) {
2456 v1 = RE_findOrAddVert(obr, obr->totvert++); /* save this for possible V wrapping */
2457 VECCOPY(v1->co, data); data += 3;
2459 v1->orco= orco; orco+= 3; orcoret++;
2461 MTC_Mat4MulVecfl(mat, v1->co);
2463 for (v = 1; v < sizev; v++) {
2464 ver= RE_findOrAddVert(obr, obr->totvert++);
2465 VECCOPY(ver->co, data); data += 3;
2467 ver->orco= orco; orco+= 3; orcoret++;
2469 MTC_Mat4MulVecfl(mat, ver->co);
2471 /* if V-cyclic, add extra vertices at end of the row */
2472 if (dl->flag & DL_CYCL_U) {
2473 ver= RE_findOrAddVert(obr, obr->totvert++);
2474 VECCOPY(ver->co, v1->co);
2476 ver->orco= orco; orco+=3; orcoret++; //orcobase + 3*(u*sizev + 0);
2481 /* Done before next loop to get corner vert */
2482 if (dl->flag & DL_CYCL_U) nsizev++;
2483 if (dl->flag & DL_CYCL_V) nsizeu++;
2485 /* if U cyclic, add extra row at end of column */
2486 if (dl->flag & DL_CYCL_V) {
2487 for (v = 0; v < nsizev; v++) {
2488 v1= RE_findOrAddVert(obr, startvert + v);
2489 ver= RE_findOrAddVert(obr, obr->totvert++);
2490 VECCOPY(ver->co, v1->co);
2492 ver->orco= orco; orco+=3; orcoret++; //ver->orco= orcobase + 3*(0*sizev + v);
2500 startvlak= obr->totvlak;
2502 for(u = 0; u < sizeu - 1; u++) {
2503 p1 = startvert + u * sizev; /* walk through face list */
2508 for(v = 0; v < sizev - 1; v++) {
2509 v1= RE_findOrAddVert(obr, p1);
2510 v2= RE_findOrAddVert(obr, p2);
2511 v3= RE_findOrAddVert(obr, p3);
2512 v4= RE_findOrAddVert(obr, p4);
2514 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2515 vlr->v1= v1; vlr->v2= v2; vlr->v3= v3; vlr->v4= v4;
2517 flen= CalcNormFloat4(vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co, n1);
2518 VECCOPY(vlr->n, n1);
2520 vlr->mat= matar[ dl->col];
2521 vlr->ec= ME_V1V2+ME_V2V3;
2523 if( (cu->flag & CU_NOPUNOFLIP) ) {
2524 vlr->flag |= R_NOPUNOFLIP;
2527 VecAddf(v1->n, v1->n, n1);
2528 VecAddf(v2->n, v2->n, n1);
2529 VecAddf(v3->n, v3->n, n1);
2530 VecAddf(v4->n, v4->n, n1);
2532 p1++; p2++; p3++; p4++;
2535 /* fix normals for U resp. V cyclic faces */
2536 sizeu--; sizev--; /* dec size for face array */
2537 if (dl->flag & DL_CYCL_V) {
2539 for (v = 0; v < sizev; v++)
2542 vlr= RE_findOrAddVlak(obr, UVTOINDEX(sizeu - 1, v));
2543 vlr1= RE_findOrAddVlak(obr, UVTOINDEX(0, v));
2544 VecAddf(vlr1->v1->n, vlr1->v1->n, vlr->n);
2545 VecAddf(vlr1->v2->n, vlr1->v2->n, vlr->n);
2546 VecAddf(vlr->v3->n, vlr->v3->n, vlr1->n);
2547 VecAddf(vlr->v4->n, vlr->v4->n, vlr1->n);
2550 if (dl->flag & DL_CYCL_U) {
2552 for (u = 0; u < sizeu; u++)
2555 vlr= RE_findOrAddVlak(obr, UVTOINDEX(u, 0));
2556 vlr1= RE_findOrAddVlak(obr, UVTOINDEX(u, sizev-1));
2557 VecAddf(vlr1->v2->n, vlr1->v2->n, vlr->n);
2558 VecAddf(vlr1->v3->n, vlr1->v3->n, vlr->n);
2559 VecAddf(vlr->v1->n, vlr->v1->n, vlr1->n);
2560 VecAddf(vlr->v4->n, vlr->v4->n, vlr1->n);
2563 /* last vertex is an extra case:
2565 ^ ()----()----()----()
2569 ()====()====[]====()
2573 ()----()----()----()
2576 vertex [] is no longer shared, therefore distribute
2577 normals of the surrounding faces to all of the duplicates of []
2580 if ((dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U))
2582 vlr= RE_findOrAddVlak(obr, UVTOINDEX(sizeu - 1, sizev - 1)); /* (m,n) */
2583 vlr1= RE_findOrAddVlak(obr, UVTOINDEX(0,0)); /* (0,0) */
2584 VecAddf(n1, vlr->n, vlr1->n);
2585 vlr2= RE_findOrAddVlak(obr, UVTOINDEX(0, sizev-1)); /* (0,n) */
2586 VecAddf(n1, n1, vlr2->n);
2587 vlr3= RE_findOrAddVlak(obr, UVTOINDEX(sizeu-1, 0)); /* (m,0) */
2588 VecAddf(n1, n1, vlr3->n);
2589 VECCOPY(vlr->v3->n, n1);
2590 VECCOPY(vlr1->v1->n, n1);
2591 VECCOPY(vlr2->v2->n, n1);
2592 VECCOPY(vlr3->v4->n, n1);
2594 for(a = startvert; a < obr->totvert; a++) {
2595 ver= RE_findOrAddVert(obr, a);
2603 static void init_render_surf(Render *re, ObjectRen *obr)
2605 Object *ob= obr->ob;
2610 Material *matar[32];
2611 float *orco=NULL, *orcobase=NULL, mat[4][4];
2618 MTC_Mat4MulMat4(mat, ob->obmat, re->viewmat);
2619 MTC_Mat4Invert(ob->imat, mat);
2621 /* material array */
2622 memset(matar, 0, 4*32);
2623 matar[0]= give_render_material(re, ob, 0);
2624 for(a=0; a<ob->totcol; a++) {
2625 matar[a]= give_render_material(re, ob, a+1);
2626 if(matar[a] && matar[a]->texco & TEXCO_ORCO) {
2631 if(ob->parent && (ob->parent->type==OB_LATTICE)) need_orco= 1;
2633 if(need_orco) orcobase= orco= get_object_orco(re, ob);
2635 displist.first= displist.last= 0;
2636 makeDispListSurf(ob, &displist, 1);
2639 /* walk along displaylist and create rendervertices/-faces */
2641 /* watch out: u ^= y, v ^= x !! */
2642 if(dl->type==DL_SURF) {
2643 orco+= 3*dl_surf_to_renderdata(obr, dl, matar, orco, mat);
2648 freedisplist(&displist);
2651 static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
2653 Object *ob= obr->ob;
2658 ListBase olddl={NULL, NULL};
2659 Material *matar[32];
2660 float len, *data, *fp, *orco=NULL, *orcobase= NULL;
2661 float n[3], mat[4][4];
2662 int nr, startvert, startvlak, a, b;
2663 int frontside, need_orco=0;
2666 if(ob->type==OB_FONT && cu->str==NULL) return;
2667 else if(ob->type==OB_CURVE && cu->nurb.first==NULL) return;
2669 /* no modifier call here, is in makedisp */
2672 SWAP(ListBase, olddl, cu->disp);
2675 if(cu->disp.first==NULL)
2676 makeDispListCurveTypes(ob, 0);
2678 if(cu->disp.first==NULL) return;
2680 MTC_Mat4MulMat4(mat, ob->obmat, re->viewmat);
2681 MTC_Mat4Invert(ob->imat, mat);
2683 /* material array */
2684 memset(matar, 0, 4*32);
2685 matar[0]= give_render_material(re, ob, 0);
2686 for(a=0; a<ob->totcol; a++) {
2687 matar[a]= give_render_material(re, ob, a+1);
2688 if(matar[a]->texco & TEXCO_ORCO) {
2693 if(need_orco) orcobase=orco= get_object_orco(re, ob);
2697 if(dl->type==DL_INDEX3) {
2700 startvert= obr->totvert;
2703 n[0]= ob->imat[0][2];
2704 n[1]= ob->imat[1][2];
2705 n[2]= ob->imat[2][2];
2708 for(a=0; a<dl->nr; a++, data+=3) {
2709 ver= RE_findOrAddVert(obr, obr->totvert++);
2710 VECCOPY(ver->co, data);
2712 /* flip normal if face is backfacing, also used in face loop below */
2713 if(ver->co[2] < 0.0) {
2718 ver->n[0]= -n[0]; ver->n[1]= -n[1]; ver->n[2]= -n[2];
2722 MTC_Mat4MulVecfl(mat, ver->co);
2731 startvlak= obr->totvlak;
2733 for(a=0; a<dl->parts; a++, index+=3) {
2735 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2736 vlr->v1= RE_findOrAddVert(obr, startvert+index[0]);
2737 vlr->v2= RE_findOrAddVert(obr, startvert+index[1]);
2738 vlr->v3= RE_findOrAddVert(obr, startvert+index[2]);
2745 vlr->n[0]= -n[0]; vlr->n[1]= -n[1]; vlr->n[2]= -n[2];
2748 vlr->mat= matar[ dl->col ];
2750 if( (cu->flag & CU_NOPUNOFLIP) ) {
2751 vlr->flag |= R_NOPUNOFLIP;
2757 else if (dl->type==DL_SURF) {
2759 /* cyclic U means an extruded full circular curve, we skip bevel splitting then */
2760 if (dl->flag & DL_CYCL_U) {
2761 orco+= 3*dl_surf_to_renderdata(obr, dl, matar, orco, mat);
2767 startvert= obr->totvert;
2768 nr= dl->nr*dl->parts;
2771 ver= RE_findOrAddVert(obr, obr->totvert++);
2773 VECCOPY(ver->co, fp);
2774 MTC_Mat4MulVecfl(mat, ver->co);
2783 if(dl->bevelSplitFlag || timeoffset==0) {
2784 startvlak= obr->totvlak;
2786 for(a=0; a<dl->parts; a++) {
2788 frontside= (a >= dl->nr/2);
2790 DL_SURFINDEX(dl->flag & DL_CYCL_U, dl->flag & DL_CYCL_V, dl->nr, dl->parts);
2796 for(; b<dl->nr; b++) {
2797 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2798 vlr->v1= RE_findOrAddVert(obr, p2);
2799 vlr->v2= RE_findOrAddVert(obr, p1);
2800 vlr->v3= RE_findOrAddVert(obr, p3);
2801 vlr->v4= RE_findOrAddVert(obr, p4);
2802 vlr->ec= ME_V2V3+ME_V3V4;
2803 if(a==0) vlr->ec+= ME_V1V2;
2807 /* this is not really scientific: the vertices
2808 * 2, 3 en 4 seem to give better vertexnormals than 1 2 3:
2809 * front and backside treated different!!