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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 *****
28 /** \file blender/render/intern/source/convertblender.c
39 #include "MEM_guardedalloc.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
45 #include "BLI_memarena.h"
46 #include "BLI_ghash.h"
48 #include "DNA_armature_types.h"
49 #include "DNA_camera_types.h"
50 #include "DNA_material_types.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_effect_types.h"
53 #include "DNA_group_types.h"
54 #include "DNA_lamp_types.h"
55 #include "DNA_image_types.h"
56 #include "DNA_lattice_types.h"
57 #include "DNA_mesh_types.h"
58 #include "DNA_meshdata_types.h"
59 #include "DNA_meta_types.h"
60 #include "DNA_modifier_types.h"
61 #include "DNA_node_types.h"
62 #include "DNA_object_types.h"
63 #include "DNA_object_force.h"
64 #include "DNA_object_fluidsim.h"
65 #include "DNA_particle_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_texture_types.h"
68 #include "DNA_view3d_types.h"
71 #include "BKE_armature.h"
72 #include "BKE_action.h"
73 #include "BKE_curve.h"
74 #include "BKE_customdata.h"
75 #include "BKE_colortools.h"
76 #include "BKE_constraint.h"
77 #include "BKE_displist.h"
78 #include "BKE_deform.h"
79 #include "BKE_DerivedMesh.h"
80 #include "BKE_effect.h"
81 #include "BKE_global.h"
82 #include "BKE_group.h"
85 #include "BKE_image.h"
86 #include "BKE_lattice.h"
87 #include "BKE_library.h"
88 #include "BKE_material.h"
90 #include "BKE_mball.h"
92 #include "BKE_modifier.h"
94 #include "BKE_object.h"
95 #include "BKE_particle.h"
96 #include "BKE_scene.h"
97 #include "BKE_subsurf.h"
98 #include "BKE_texture.h"
100 #include "BKE_world.h"
102 #include "PIL_time.h"
103 #include "IMB_imbuf_types.h"
106 #include "occlusion.h"
107 #include "pointdensity.h"
108 #include "voxeldata.h"
109 #include "render_types.h"
110 #include "rendercore.h"
111 #include "renderdatabase.h"
112 #include "renderpipeline.h"
117 #include "volume_precache.h"
124 /* 10 times larger than normal epsilon, test it on default nurbs sphere with ray_transp (for quad detection) */
125 /* or for checking vertex normal flips */
126 #define FLT_EPSILON10 1.19209290e-06F
128 /* ------------------------------------------------------------------------- */
130 /* Stuff for stars. This sits here because it uses gl-things. Part of
131 this code may move down to the converter. */
132 /* ------------------------------------------------------------------------- */
133 /* this is a bad beast, since it is misused by the 3d view drawing as well. */
135 static HaloRen *initstar(Render *re, ObjectRen *obr, float *vec, float hasize)
140 projectverto(vec, re->winmat, hoco);
142 har= RE_findOrAddHalo(obr, obr->tothalo++);
144 /* projectvert is done in function zbufvlaggen again, because of parts */
145 copy_v3_v3(har->co, vec);
153 /* there must be a 'fixed' amount of stars generated between
155 * all stars must by preference lie on the far and solely
156 * differ in clarity/color
159 void RE_make_stars(Render *re, Scene *scenev3d, void (*initfunc)(void),
160 void (*vertexfunc)(float*), void (*termfunc)(void))
162 extern unsigned char hash[512];
163 ObjectRen *obr= NULL;
169 double dblrand, hlfrand;
170 float vec[4], fx, fy, fz;
171 float fac, starmindist, clipend;
172 float mat[4][4], stargrid, maxrand, maxjit, force, alpha;
173 int x, y, z, sx, sy, sz, ex, ey, ez, done = 0;
174 unsigned int totstar= 0;
185 stargrid = wrld->stardist; /* distance between stars */
186 maxrand = 2.0; /* amount a star can be shifted (in grid units) */
187 maxjit = (wrld->starcolnoise); /* amount a color is being shifted */
190 force = ( wrld->starsize );
192 /* minimal free space (starting at camera) */
193 starmindist= wrld->starmindist;
195 if (stargrid <= 0.10f) return;
197 if (re) re->flag |= R_HALO;
198 else stargrid *= 1.0f; /* then it draws fewer */
200 if(re) invert_m4_m4(mat, re->viewmat);
203 /* BOUNDING BOX CALCULATION
204 * bbox goes from z = loc_near_var | loc_far_var,
209 camera= re ? RE_GetCamera(re) : scene->camera;
211 if(camera==NULL || camera->type != OB_CAMERA)
215 clipend = cam->clipend;
217 /* convert to grid coordinates */
219 sx = ((mat[3][0] - clipend) / stargrid) - maxrand;
220 sy = ((mat[3][1] - clipend) / stargrid) - maxrand;
221 sz = ((mat[3][2] - clipend) / stargrid) - maxrand;
223 ex = ((mat[3][0] + clipend) / stargrid) + maxrand;
224 ey = ((mat[3][1] + clipend) / stargrid) + maxrand;
225 ez = ((mat[3][2] + clipend) / stargrid) + maxrand;
227 dblrand = maxrand * stargrid;
228 hlfrand = 2.0 * dblrand;
234 if(re) /* add render object for stars */
235 obr= RE_addRenderObject(re, NULL, NULL, 0, 0, 0);
237 for (x = sx, fx = sx * stargrid; x <= ex; x++, fx += stargrid) {
238 for (y = sy, fy = sy * stargrid; y <= ey ; y++, fy += stargrid) {
239 for (z = sz, fz = sz * stargrid; z <= ez; z++, fz += stargrid) {
241 BLI_srand((hash[z & 0xff] << 24) + (hash[y & 0xff] << 16) + (hash[x & 0xff] << 8));
242 vec[0] = fx + (hlfrand * BLI_drand()) - dblrand;
243 vec[1] = fy + (hlfrand * BLI_drand()) - dblrand;
244 vec[2] = fz + (hlfrand * BLI_drand()) - dblrand;
248 if(done & 1) vertexfunc(vec);
252 mul_m4_v3(re->viewmat, vec);
254 /* in vec are global coordinates
255 * calculate distance to camera
256 * and using that, define the alpha
266 alpha = sqrt(tx * tx + ty * ty + tz * tz);
268 if (alpha >= clipend) alpha = 0.0;
269 else if (alpha <= starmindist) alpha = 0.0;
270 else if (alpha <= 2.0f * starmindist) {
271 alpha = (alpha - starmindist) / starmindist;
273 alpha -= 2.0f * starmindist;
274 alpha /= (clipend - 2.0f * starmindist);
275 alpha = 1.0f - alpha;
281 fac = force * BLI_drand();
283 har = initstar(re, obr, vec, fac);
286 har->alfa = sqrt(sqrt(alpha));
288 har->r = har->g = har->b = 1.0;
290 har->r += ((maxjit * BLI_drand()) ) - maxjit;
291 har->g += ((maxjit * BLI_drand()) ) - maxjit;
292 har->b += ((maxjit * BLI_drand()) ) - maxjit;
296 har->type |= HA_ONLYSKY;
302 /* break out of the loop if generating stars takes too long */
303 if(re && !(totstar % 1000000)) {
304 if(re->test_break(re->tbh)) {
313 /* do not call blender_test_break() here, since it is used in UI as well, confusing the callback system */
314 /* main cause is G.afbreek of course, a global again... (ton) */
317 if (termfunc) termfunc();
320 re->tothalo += obr->tothalo;
324 /* ------------------------------------------------------------------------- */
325 /* tool functions/defines for ad hoc simplification and possible future
327 /* ------------------------------------------------------------------------- */
329 #define UVTOINDEX(u,v) (startvlak + (u) * sizev + (v))
332 NOTE THAT U/V COORDINATES ARE SOMETIMES SWAPPED !!
334 ^ ()----p4----p3----()
342 /* ------------------------------------------------------------------------- */
344 static void split_v_renderfaces(ObjectRen *obr, int startvlak, int UNUSED(startvert), int UNUSED(usize), int vsize, int uIndex, int UNUSED(cyclu), int cyclv)
346 int vLen = vsize-1+(!!cyclv);
349 for (v=0; v<vLen; v++) {
350 VlakRen *vlr = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + v);
351 VertRen *vert = RE_vertren_copy(obr, vlr->v2);
357 VlakRen *vlr = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + 0);
360 VlakRen *vlr = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + v+1);
367 VlakRen *vlr = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + v+1);
372 vlr->v1 = RE_vertren_copy(obr, vlr->v1);
378 /* ------------------------------------------------------------------------- */
379 /* Stress, tangents and normals */
380 /* ------------------------------------------------------------------------- */
382 static void calc_edge_stress_add(float *accum, VertRen *v1, VertRen *v2)
384 float len= len_v3v3(v1->co, v2->co)/len_v3v3(v1->orco, v2->orco);
387 acc= accum + 2*v1->index;
391 acc= accum + 2*v2->index;
396 static void calc_edge_stress(Render *UNUSED(re), ObjectRen *obr, Mesh *me)
398 float loc[3], size[3], *accum, *acc, *accumoffs, *stress;
401 if(obr->totvert==0) return;
403 mesh_get_texspace(me, loc, NULL, size);
405 accum= MEM_callocN(2*sizeof(float)*obr->totvert, "temp accum for stress");
407 /* de-normalize orco */
408 for(a=0; a<obr->totvert; a++) {
409 VertRen *ver= RE_findOrAddVert(obr, a);
411 ver->orco[0]= ver->orco[0]*size[0] +loc[0];
412 ver->orco[1]= ver->orco[1]*size[1] +loc[1];
413 ver->orco[2]= ver->orco[2]*size[2] +loc[2];
417 /* add stress values */
418 accumoffs= accum; /* so we can use vertex index */
419 for(a=0; a<obr->totvlak; a++) {
420 VlakRen *vlr= RE_findOrAddVlak(obr, a);
422 if(vlr->v1->orco && vlr->v4) {
423 calc_edge_stress_add(accumoffs, vlr->v1, vlr->v2);
424 calc_edge_stress_add(accumoffs, vlr->v2, vlr->v3);
425 calc_edge_stress_add(accumoffs, vlr->v3, vlr->v1);
427 calc_edge_stress_add(accumoffs, vlr->v3, vlr->v4);
428 calc_edge_stress_add(accumoffs, vlr->v4, vlr->v1);
429 calc_edge_stress_add(accumoffs, vlr->v2, vlr->v4);
434 for(a=0; a<obr->totvert; a++) {
435 VertRen *ver= RE_findOrAddVert(obr, a);
437 /* find stress value */
438 acc= accumoffs + 2*ver->index;
441 stress= RE_vertren_get_stress(obr, ver, 1);
445 ver->orco[0] = (ver->orco[0]-loc[0])/size[0];
446 ver->orco[1] = (ver->orco[1]-loc[1])/size[1];
447 ver->orco[2] = (ver->orco[2]-loc[2])/size[2];
454 /* gets tangent from tface or orco */
455 static void calc_tangent_vector(ObjectRen *obr, VertexTangent **vtangents, MemArena *arena, VlakRen *vlr, int do_nmap_tangent, int do_tangent)
457 MTFace *tface= RE_vlakren_get_tface(obr, vlr, obr->actmtface, NULL, 0);
458 VertRen *v1=vlr->v1, *v2=vlr->v2, *v3=vlr->v3, *v4=vlr->v4;
460 float *uv1, *uv2, *uv3, *uv4;
470 uv1= uv[0]; uv2= uv[1]; uv3= uv[2]; uv4= uv[3];
471 map_to_sphere( &uv[0][0], &uv[0][1],v1->orco[0], v1->orco[1], v1->orco[2]);
472 map_to_sphere( &uv[1][0], &uv[1][1],v2->orco[0], v2->orco[1], v2->orco[2]);
473 map_to_sphere( &uv[2][0], &uv[2][1],v3->orco[0], v3->orco[1], v3->orco[2]);
475 map_to_sphere( &uv[3][0], &uv[3][1],v4->orco[0], v4->orco[1], v4->orco[2]);
479 tangent_from_uv(uv1, uv2, uv3, v1->co, v2->co, v3->co, vlr->n, tang);
482 tav= RE_vertren_get_tangent(obr, v1, 1);
483 VECADD(tav, tav, tang);
484 tav= RE_vertren_get_tangent(obr, v2, 1);
485 VECADD(tav, tav, tang);
486 tav= RE_vertren_get_tangent(obr, v3, 1);
487 VECADD(tav, tav, tang);
490 if(do_nmap_tangent) {
491 sum_or_add_vertex_tangent(arena, &vtangents[v1->index], tang, uv1);
492 sum_or_add_vertex_tangent(arena, &vtangents[v2->index], tang, uv2);
493 sum_or_add_vertex_tangent(arena, &vtangents[v3->index], tang, uv3);
497 tangent_from_uv(uv1, uv3, uv4, v1->co, v3->co, v4->co, vlr->n, tang);
500 tav= RE_vertren_get_tangent(obr, v1, 1);
501 VECADD(tav, tav, tang);
502 tav= RE_vertren_get_tangent(obr, v3, 1);
503 VECADD(tav, tav, tang);
504 tav= RE_vertren_get_tangent(obr, v4, 1);
505 VECADD(tav, tav, tang);
508 if(do_nmap_tangent) {
509 sum_or_add_vertex_tangent(arena, &vtangents[v1->index], tang, uv1);
510 sum_or_add_vertex_tangent(arena, &vtangents[v3->index], tang, uv3);
511 sum_or_add_vertex_tangent(arena, &vtangents[v4->index], tang, uv4);
518 /****************************************************************
519 ************ tangent space generation interface *****************
520 ****************************************************************/
526 } SRenderMeshToTangent;
529 #include "mikktspace.h"
531 static int GetNumFaces(const SMikkTSpaceContext * pContext)
533 SRenderMeshToTangent * pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
534 return pMesh->obr->totvlak;
537 static int GetNumVertsOfFace(const SMikkTSpaceContext * pContext, const int face_num)
539 SRenderMeshToTangent * pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
540 VlakRen *vlr= RE_findOrAddVlak(pMesh->obr, face_num);
541 return vlr->v4!=NULL ? 4 : 3;
544 static void GetPosition(const SMikkTSpaceContext * pContext, float fPos[], const int face_num, const int vert_index)
546 //assert(vert_index>=0 && vert_index<4);
547 SRenderMeshToTangent * pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
548 VlakRen *vlr= RE_findOrAddVlak(pMesh->obr, face_num);
549 const float *co= (&vlr->v1)[vert_index]->co;
550 copy_v3_v3(fPos, co);
553 static void GetTextureCoordinate(const SMikkTSpaceContext * pContext, float fUV[], const int face_num, const int vert_index)
555 //assert(vert_index>=0 && vert_index<4);
556 SRenderMeshToTangent * pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
557 VlakRen *vlr= RE_findOrAddVlak(pMesh->obr, face_num);
558 MTFace *tface= RE_vlakren_get_tface(pMesh->obr, vlr, pMesh->obr->actmtface, NULL, 0);
562 coord= tface->uv[vert_index];
563 fUV[0]= coord[0]; fUV[1]= coord[1];
565 else if((coord= (&vlr->v1)[vert_index]->orco)) {
566 map_to_sphere(&fUV[0], &fUV[1], coord[0], coord[1], coord[2]);
568 else { /* else we get un-initialized value, 0.0 ok default? */
569 fUV[0]= fUV[1]= 0.0f;
573 static void GetNormal(const SMikkTSpaceContext * pContext, float fNorm[], const int face_num, const int vert_index)
575 //assert(vert_index>=0 && vert_index<4);
576 SRenderMeshToTangent * pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
577 VlakRen *vlr= RE_findOrAddVlak(pMesh->obr, face_num);
578 const float *n= (&vlr->v1)[vert_index]->n;
579 copy_v3_v3(fNorm, n);
581 static void SetTSpace(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fSign, const int face_num, const int iVert)
583 //assert(vert_index>=0 && vert_index<4);
584 SRenderMeshToTangent * pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
585 VlakRen *vlr= RE_findOrAddVlak(pMesh->obr, face_num);
586 float * ftang= RE_vlakren_get_nmap_tangent(pMesh->obr, vlr, 1);
588 copy_v3_v3(&ftang[iVert*4+0], fvTangent);
589 ftang[iVert*4+3]=fSign;
593 static void calc_vertexnormals(Render *UNUSED(re), ObjectRen *obr, int do_tangent, int do_nmap_tangent)
595 MemArena *arena= NULL;
596 VertexTangent **vtangents= NULL;
597 int a, iCalcNewMethod;
599 if(do_nmap_tangent) {
600 arena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "nmap tangent arena");
601 BLI_memarena_use_calloc(arena);
603 vtangents= MEM_callocN(sizeof(VertexTangent*)*obr->totvert, "VertexTangent");
606 /* clear all vertex normals */
607 for(a=0; a<obr->totvert; a++) {
608 VertRen *ver= RE_findOrAddVert(obr, a);
609 ver->n[0]=ver->n[1]=ver->n[2]= 0.0f;
612 /* calculate cos of angles and point-masses, use as weight factor to
613 add face normal to vertex */
614 for(a=0; a<obr->totvlak; a++) {
615 VlakRen *vlr= RE_findOrAddVlak(obr, a);
616 if(vlr->flag & ME_SMOOTH) {
617 float *n4= (vlr->v4)? vlr->v4->n: NULL;
618 float *c4= (vlr->v4)? vlr->v4->co: NULL;
620 accumulate_vertex_normals(vlr->v1->n, vlr->v2->n, vlr->v3->n, n4,
621 vlr->n, vlr->v1->co, vlr->v2->co, vlr->v3->co, c4);
623 if(do_nmap_tangent || do_tangent) {
624 /* tangents still need to be calculated for flat faces too */
625 /* weighting removed, they are not vertexnormals */
626 calc_tangent_vector(obr, vtangents, arena, vlr, do_nmap_tangent, do_tangent);
631 for(a=0; a<obr->totvlak; a++) {
632 VlakRen *vlr= RE_findOrAddVlak(obr, a);
634 if((vlr->flag & ME_SMOOTH)==0) {
635 if(is_zero_v3(vlr->v1->n)) copy_v3_v3(vlr->v1->n, vlr->n);
636 if(is_zero_v3(vlr->v2->n)) copy_v3_v3(vlr->v2->n, vlr->n);
637 if(is_zero_v3(vlr->v3->n)) copy_v3_v3(vlr->v3->n, vlr->n);
638 if(vlr->v4 && is_zero_v3(vlr->v4->n)) copy_v3_v3(vlr->v4->n, vlr->n);
641 if(do_nmap_tangent) {
642 VertRen *v1=vlr->v1, *v2=vlr->v2, *v3=vlr->v3, *v4=vlr->v4;
643 MTFace *tface= RE_vlakren_get_tface(obr, vlr, obr->actmtface, NULL, 0);
647 float *vtang, *ftang= RE_vlakren_get_nmap_tangent(obr, vlr, 1);
649 vtang= find_vertex_tangent(vtangents[v1->index], tface->uv[0]);
650 copy_v3_v3(ftang, vtang);
652 vtang= find_vertex_tangent(vtangents[v2->index], tface->uv[1]);
653 copy_v3_v3(ftang+4, vtang);
654 normalize_v3(ftang+4);
655 vtang= find_vertex_tangent(vtangents[v3->index], tface->uv[2]);
656 copy_v3_v3(ftang+8, vtang);
657 normalize_v3(ftang+8);
659 vtang= find_vertex_tangent(vtangents[v4->index], tface->uv[3]);
660 copy_v3_v3(ftang+12, vtang);
661 normalize_v3(ftang+12);
663 for(k=0; k<4; k++) ftang[4*k+3]=1;
668 /* normalize vertex normals */
669 for(a=0; a<obr->totvert; a++) {
670 VertRen *ver= RE_findOrAddVert(obr, a);
671 normalize_v3(ver->n);
673 float *tav= RE_vertren_get_tangent(obr, ver, 0);
676 float tdn = tav[0]*ver->n[0] + tav[1]*ver->n[1] + tav[2]*ver->n[2];
677 tav[0] -= ver->n[0]*tdn;
678 tav[1] -= ver->n[1]*tdn;
679 tav[2] -= ver->n[2]*tdn;
686 if(iCalcNewMethod!=0 && do_nmap_tangent!=0)
688 SRenderMeshToTangent mesh2tangent;
689 SMikkTSpaceContext sContext;
690 SMikkTSpaceInterface sInterface;
691 memset(&mesh2tangent, 0, sizeof(SRenderMeshToTangent));
692 memset(&sContext, 0, sizeof(SMikkTSpaceContext));
693 memset(&sInterface, 0, sizeof(SMikkTSpaceInterface));
695 mesh2tangent.obr = obr;
697 sContext.m_pUserData = &mesh2tangent;
698 sContext.m_pInterface = &sInterface;
699 sInterface.m_getNumFaces = GetNumFaces;
700 sInterface.m_getNumVerticesOfFace = GetNumVertsOfFace;
701 sInterface.m_getPosition = GetPosition;
702 sInterface.m_getTexCoord = GetTextureCoordinate;
703 sInterface.m_getNormal = GetNormal;
704 sInterface.m_setTSpaceBasic = SetTSpace;
707 iCalcNewMethod = genTangSpaceDefault(&sContext);
712 BLI_memarena_free(arena);
714 MEM_freeN(vtangents);
717 /* ------------------------------------------------------------------------- */
719 /* ------------------------------------------------------------------------- */
721 typedef struct ASvert {
726 typedef struct ASface {
727 struct ASface *next, *prev;
732 static void as_addvert(ASvert *asv, VertRen *v1, VlakRen *vlr)
737 if(v1 == NULL) return;
739 if(asv->faces.first==NULL) {
740 asf= MEM_callocN(sizeof(ASface), "asface");
741 BLI_addtail(&asv->faces, asf);
744 asf= asv->faces.last;
746 if(asf->vlr[a]==NULL) {
753 /* new face struct */
755 asf= MEM_callocN(sizeof(ASface), "asface");
756 BLI_addtail(&asv->faces, asf);
762 static int as_testvertex(VlakRen *vlr, VertRen *UNUSED(ver), ASvert *asv, float thresh)
764 /* return 1: vertex needs a copy */
771 asf= asv->faces.first;
774 if(asf->vlr[a] && asf->vlr[a]!=vlr) {
775 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] );
776 if(inp < thresh) return 1;
785 static VertRen *as_findvertex(VlakRen *vlr, VertRen *UNUSED(ver), ASvert *asv, float thresh)
787 /* return when new vertex already was made */
792 asf= asv->faces.first;
795 if(asf->vlr[a] && asf->vlr[a]!=vlr) {
796 /* this face already made a copy for this vertex! */
798 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] );
811 /* note; autosmooth happens in object space still, after applying autosmooth we rotate */
812 /* note2; actually, when original mesh and displist are equal sized, face normals are from original mesh */
813 static void autosmooth(Render *UNUSED(re), ObjectRen *obr, float mat[][4], int degr)
815 ASvert *asv, *asverts;
822 if(obr->totvert==0) return;
823 asverts= MEM_callocN(sizeof(ASvert)*obr->totvert, "all smooth verts");
825 thresh= cosf((float)M_PI*(0.5f+(float)degr)/180.0f );
827 /* step zero: give faces normals of original mesh, if this is provided */
830 /* step one: construct listbase of all vertices and pointers to faces */
831 for(a=0; a<obr->totvlak; a++) {
832 vlr= RE_findOrAddVlak(obr, a);
833 /* skip wire faces */
834 if(vlr->v2 != vlr->v3) {
835 as_addvert(asverts+vlr->v1->index, vlr->v1, vlr);
836 as_addvert(asverts+vlr->v2->index, vlr->v2, vlr);
837 as_addvert(asverts+vlr->v3->index, vlr->v3, vlr);
839 as_addvert(asverts+vlr->v4->index, vlr->v4, vlr);
843 totvert= obr->totvert;
844 /* we now test all vertices, when faces have a normal too much different: they get a new vertex */
845 for(a=0, asv=asverts; a<totvert; a++, asv++) {
846 if(asv && asv->totface>1) {
847 ver= RE_findOrAddVert(obr, a);
849 asf= asv->faces.first;
853 /* is there a reason to make a new vertex? */
855 if( as_testvertex(vlr, ver, asv, thresh) ) {
857 /* already made a new vertex within threshold? */
858 v1= as_findvertex(vlr, ver, asv, thresh);
860 /* make a new vertex */
861 v1= RE_vertren_copy(obr, ver);
864 if(vlr->v1==ver) vlr->v1= v1;
865 if(vlr->v2==ver) vlr->v2= v1;
866 if(vlr->v3==ver) vlr->v3= v1;
867 if(vlr->v4==ver) vlr->v4= v1;
876 for(a=0; a<totvert; a++) {
877 BLI_freelistN(&asverts[a].faces);
881 /* rotate vertices and calculate normal of faces */
882 for(a=0; a<obr->totvert; a++) {
883 ver= RE_findOrAddVert(obr, a);
884 mul_m4_v3(mat, ver->co);
886 for(a=0; a<obr->totvlak; a++) {
887 vlr= RE_findOrAddVlak(obr, a);
889 /* skip wire faces */
890 if(vlr->v2 != vlr->v3) {
892 normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
894 normal_tri_v3( vlr->n,vlr->v3->co, vlr->v2->co, vlr->v1->co);
899 /* ------------------------------------------------------------------------- */
900 /* Orco hash and Materials */
901 /* ------------------------------------------------------------------------- */
903 static float *get_object_orco(Render *re, Object *ob)
908 re->orco_hash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "get_object_orco gh");
910 orco = BLI_ghash_lookup(re->orco_hash, ob);
913 if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
914 orco = make_orco_curve(re->scene, ob);
915 } else if (ob->type==OB_SURF) {
916 orco = make_orco_surf(ob);
920 BLI_ghash_insert(re->orco_hash, ob, orco);
926 static void set_object_orco(Render *re, void *ob, float *orco)
929 re->orco_hash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "set_object_orco gh");
931 BLI_ghash_insert(re->orco_hash, ob, orco);
934 static void free_mesh_orco_hash(Render *re)
937 BLI_ghash_free(re->orco_hash, NULL, (GHashValFreeFP)MEM_freeN);
938 re->orco_hash = NULL;
942 static void check_material_mapto(Material *ma)
945 ma->mapto_textured = 0;
947 /* cache which inputs are actually textured.
948 * this can avoid a bit of time spent iterating through all the texture slots, map inputs and map tos
949 * every time a property which may or may not be textured is accessed */
951 for(a=0; a<MAX_MTEX; a++) {
952 if(ma->mtex[a] && ma->mtex[a]->tex) {
953 /* currently used only in volume render, so we'll check for those flags */
954 if(ma->mtex[a]->mapto & MAP_DENSITY) ma->mapto_textured |= MAP_DENSITY;
955 if(ma->mtex[a]->mapto & MAP_EMISSION) ma->mapto_textured |= MAP_EMISSION;
956 if(ma->mtex[a]->mapto & MAP_EMISSION_COL) ma->mapto_textured |= MAP_EMISSION_COL;
957 if(ma->mtex[a]->mapto & MAP_SCATTERING) ma->mapto_textured |= MAP_SCATTERING;
958 if(ma->mtex[a]->mapto & MAP_TRANSMISSION_COL) ma->mapto_textured |= MAP_TRANSMISSION_COL;
959 if(ma->mtex[a]->mapto & MAP_REFLECTION) ma->mapto_textured |= MAP_REFLECTION;
960 if(ma->mtex[a]->mapto & MAP_REFLECTION_COL) ma->mapto_textured |= MAP_REFLECTION_COL;
964 static void flag_render_node_material(Render *re, bNodeTree *ntree)
968 for(node=ntree->nodes.first; node; node= node->next) {
970 if(GS(node->id->name)==ID_MA) {
971 Material *ma= (Material *)node->id;
973 if((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))
976 ma->flag |= MA_IS_USED;
978 else if(node->type==NODE_GROUP)
979 flag_render_node_material(re, (bNodeTree *)node->id);
984 static Material *give_render_material(Render *re, Object *ob, int nr)
986 extern Material defmaterial; /* material.c */
989 ma= give_current_material(ob, nr);
993 if(re->r.mode & R_SPEED) ma->texco |= NEED_UV;
995 if(ma->material_type == MA_TYPE_VOLUME) {
996 ma->mode |= MA_TRANSP;
997 ma->mode &= ~MA_SHADBUF;
999 if((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))
1002 /* for light groups and SSS */
1003 ma->flag |= MA_IS_USED;
1005 if(ma->nodetree && ma->use_nodes)
1006 flag_render_node_material(re, ma->nodetree);
1008 check_material_mapto(ma);
1013 /* ------------------------------------------------------------------------- */
1015 /* ------------------------------------------------------------------------- */
1016 typedef struct ParticleStrandData
1019 float *orco, *uvco, *surfnor;
1020 float time, adapt_angle, adapt_pix, size;
1022 int first, line, adapt, override_uv;
1025 /* future thread problem... */
1026 static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, ParticleStrandData *sd, const float vec[3], const float vec1[3])
1028 static VertRen *v1= NULL, *v2= NULL;
1030 float nor[3], cross[3], crosslen, w, dx, dy, width;
1031 static float anor[3], avec[3];
1033 static int second=0;
1035 sub_v3_v3v3(nor, vec, vec1);
1036 normalize_v3(nor); // nor needed as tangent
1037 cross_v3_v3v3(cross, vec, nor);
1039 /* turn cross in pixelsize */
1040 w= vec[2]*re->winmat[2][3] + re->winmat[3][3];
1041 dx= re->winx*cross[0]*re->winmat[0][0];
1042 dy= re->winy*cross[1]*re->winmat[1][1];
1043 w= sqrt(dx*dx + dy*dy)/w;
1047 if(ma->strand_ease!=0.0f) {
1048 if(ma->strand_ease<0.0f)
1049 fac= pow(sd->time, 1.0f+ma->strand_ease);
1051 fac= pow(sd->time, 1.0f/(1.0f-ma->strand_ease));
1055 width= ((1.0f-fac)*ma->strand_sta + (fac)*ma->strand_end);
1057 /* use actual Blender units for strand width and fall back to minimum width */
1058 if(ma->mode & MA_STR_B_UNITS){
1059 crosslen= len_v3(cross);
1060 w= 2.0f*crosslen*ma->strand_min/w;
1065 /*cross is the radius of the strand so we want it to be half of full width */
1066 mul_v3_fl(cross,0.5f/crosslen);
1071 mul_v3_fl(cross, width);
1075 if(ma->mode & MA_TANGENT_STR)
1076 flag= R_SMOOTH|R_TANGENT;
1080 /* only 1 pixel wide strands filled in as quads now, otherwise zbuf errors */
1081 if(ma->strand_sta==1.0f)
1084 /* single face line */
1086 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1088 vlr->v1= RE_findOrAddVert(obr, obr->totvert++);
1089 vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
1090 vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
1091 vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
1093 copy_v3_v3(vlr->v1->co, vec);
1094 add_v3_v3(vlr->v1->co, cross);
1095 copy_v3_v3(vlr->v1->n, nor);
1096 vlr->v1->orco= sd->orco;
1097 vlr->v1->accum= -1.0f; // accum abuse for strand texco
1099 copy_v3_v3(vlr->v2->co, vec);
1100 sub_v3_v3v3(vlr->v2->co, vlr->v2->co, cross);
1101 copy_v3_v3(vlr->v2->n, nor);
1102 vlr->v2->orco= sd->orco;
1103 vlr->v2->accum= vlr->v1->accum;
1105 copy_v3_v3(vlr->v4->co, vec1);
1106 add_v3_v3(vlr->v4->co, cross);
1107 copy_v3_v3(vlr->v4->n, nor);
1108 vlr->v4->orco= sd->orco;
1109 vlr->v4->accum= 1.0f; // accum abuse for strand texco
1111 copy_v3_v3(vlr->v3->co, vec1);
1112 sub_v3_v3v3(vlr->v3->co, vlr->v3->co, cross);
1113 copy_v3_v3(vlr->v3->n, nor);
1114 vlr->v3->orco= sd->orco;
1115 vlr->v3->accum= vlr->v4->accum;
1117 normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
1123 float *snor= RE_vlakren_get_surfnor(obr, vlr, 1);
1124 copy_v3_v3(snor, sd->surfnor);
1128 for(i=0; i<sd->totuv; i++){
1130 mtf=RE_vlakren_get_tface(obr,vlr,i,NULL,1);
1131 mtf->uv[0][0]=mtf->uv[1][0]=
1132 mtf->uv[2][0]=mtf->uv[3][0]=(sd->uvco+2*i)[0];
1133 mtf->uv[0][1]=mtf->uv[1][1]=
1134 mtf->uv[2][1]=mtf->uv[3][1]=(sd->uvco+2*i)[1];
1136 if(sd->override_uv>=0){
1138 mtf=RE_vlakren_get_tface(obr,vlr,sd->override_uv,NULL,0);
1140 mtf->uv[0][0]=mtf->uv[3][0]=0.0f;
1141 mtf->uv[1][0]=mtf->uv[2][0]=1.0f;
1143 mtf->uv[0][1]=mtf->uv[1][1]=0.0f;
1144 mtf->uv[2][1]=mtf->uv[3][1]=1.0f;
1148 for(i=0; i<sd->totcol; i++){
1150 mc=RE_vlakren_get_mcol(obr,vlr,i,NULL,1);
1151 mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
1152 mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
1156 /* first two vertices of a strand */
1157 else if(sd->first) {
1159 copy_v3_v3(anor, nor);
1160 copy_v3_v3(avec, vec);
1164 v1= RE_findOrAddVert(obr, obr->totvert++);
1165 v2= RE_findOrAddVert(obr, obr->totvert++);
1167 copy_v3_v3(v1->co, vec);
1168 add_v3_v3(v1->co, cross);
1169 copy_v3_v3(v1->n, nor);
1171 v1->accum= -1.0f; // accum abuse for strand texco
1173 copy_v3_v3(v2->co, vec);
1174 sub_v3_v3v3(v2->co, v2->co, cross);
1175 copy_v3_v3(v2->n, nor);
1177 v2->accum= v1->accum;
1179 /* more vertices & faces to strand */
1181 if(sd->adapt==0 || second){
1182 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1186 vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
1187 vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
1189 v1= vlr->v4; // cycle
1190 v2= vlr->v3; // cycle
1195 copy_v3_v3(anor,nor);
1196 copy_v3_v3(avec,vec);
1201 float dvec[3],pvec[3];
1202 sub_v3_v3v3(dvec,avec,vec);
1203 project_v3_v3v3(pvec,dvec,vec);
1204 sub_v3_v3v3(dvec,dvec,pvec);
1206 w= vec[2]*re->winmat[2][3] + re->winmat[3][3];
1207 dx= re->winx*dvec[0]*re->winmat[0][0]/w;
1208 dy= re->winy*dvec[1]*re->winmat[1][1]/w;
1209 w= sqrt(dx*dx + dy*dy);
1210 if(dot_v3v3(anor,nor)<sd->adapt_angle && w>sd->adapt_pix){
1211 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1215 vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
1216 vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
1218 v1= vlr->v4; // cycle
1219 v2= vlr->v3; // cycle
1221 copy_v3_v3(anor,nor);
1222 copy_v3_v3(avec,vec);
1225 vlr= RE_findOrAddVlak(obr, obr->totvlak-1);
1229 copy_v3_v3(vlr->v4->co, vec);
1230 add_v3_v3(vlr->v4->co, cross);
1231 copy_v3_v3(vlr->v4->n, nor);
1232 vlr->v4->orco= sd->orco;
1233 vlr->v4->accum= -1.0f + 2.0f*sd->time; // accum abuse for strand texco
1235 copy_v3_v3(vlr->v3->co, vec);
1236 sub_v3_v3v3(vlr->v3->co, vlr->v3->co, cross);
1237 copy_v3_v3(vlr->v3->n, nor);
1238 vlr->v3->orco= sd->orco;
1239 vlr->v3->accum= vlr->v4->accum;
1241 normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
1247 float *snor= RE_vlakren_get_surfnor(obr, vlr, 1);
1248 copy_v3_v3(snor, sd->surfnor);
1252 for(i=0; i<sd->totuv; i++){
1254 mtf=RE_vlakren_get_tface(obr,vlr,i,NULL,1);
1255 mtf->uv[0][0]=mtf->uv[1][0]=
1256 mtf->uv[2][0]=mtf->uv[3][0]=(sd->uvco+2*i)[0];
1257 mtf->uv[0][1]=mtf->uv[1][1]=
1258 mtf->uv[2][1]=mtf->uv[3][1]=(sd->uvco+2*i)[1];
1260 if(sd->override_uv>=0){
1262 mtf=RE_vlakren_get_tface(obr,vlr,sd->override_uv,NULL,0);
1264 mtf->uv[0][0]=mtf->uv[3][0]=0.0f;
1265 mtf->uv[1][0]=mtf->uv[2][0]=1.0f;
1267 mtf->uv[0][1]=mtf->uv[1][1]=(vlr->v1->accum+1.0f)/2.0f;
1268 mtf->uv[2][1]=mtf->uv[3][1]=(vlr->v3->accum+1.0f)/2.0f;
1272 for(i=0; i<sd->totcol; i++){
1274 mc=RE_vlakren_get_mcol(obr,vlr,i,NULL,1);
1275 mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
1276 mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
1282 static void static_particle_wire(ObjectRen *obr, Material *ma, const float vec[3], const float vec1[3], int first, int line)
1288 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1289 vlr->v1= RE_findOrAddVert(obr, obr->totvert++);
1290 vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
1294 copy_v3_v3(vlr->v1->co, vec);
1295 copy_v3_v3(vlr->v2->co, vec1);
1297 sub_v3_v3v3(vlr->n, vec, vec1);
1298 normalize_v3(vlr->n);
1299 copy_v3_v3(vlr->v1->n, vlr->n);
1300 copy_v3_v3(vlr->v2->n, vlr->n);
1307 v1= RE_findOrAddVert(obr, obr->totvert++);
1308 copy_v3_v3(v1->co, vec);
1311 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1313 vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
1317 v1= vlr->v2; // cycle
1318 copy_v3_v3(v1->co, vec);
1320 sub_v3_v3v3(vlr->n, vec, vec1);
1321 normalize_v3(vlr->n);
1322 copy_v3_v3(v1->n, vlr->n);
1330 static void particle_curve(Render *re, ObjectRen *obr, DerivedMesh *dm, Material *ma, ParticleStrandData *sd, float *loc, float *loc1, int seed, float *pa_co)
1334 if(ma->material_type == MA_TYPE_WIRE)
1335 static_particle_wire(obr, ma, loc, loc1, sd->first, sd->line);
1336 else if(ma->material_type == MA_TYPE_HALO) {
1337 har= RE_inithalo_particle(re, obr, dm, ma, loc, loc1, sd->orco, sd->uvco, sd->size, 1.0, seed, pa_co);
1338 if(har) har->lay= obr->ob->lay;
1341 static_particle_strand(re, obr, ma, sd, loc, loc1);
1343 static void particle_billboard(Render *re, ObjectRen *obr, Material *ma, ParticleBillboardData *bb)
1347 float xvec[3], yvec[3], zvec[3], bb_center[3];
1348 /* Number of tiles */
1349 int totsplit = bb->uv_split * bb->uv_split;
1352 float uvx = 0.0f, uvy = 0.0f, uvdx = 1.0f, uvdy = 1.0f, time = 0.0f;
1354 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
1355 vlr->v1= RE_findOrAddVert(obr, obr->totvert++);
1356 vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
1357 vlr->v3= RE_findOrAddVert(obr, obr->totvert++);
1358 vlr->v4= RE_findOrAddVert(obr, obr->totvert++);
1360 psys_make_billboard(bb, xvec, yvec, zvec, bb_center);
1362 VECADD(vlr->v1->co, bb_center, xvec);
1363 VECADD(vlr->v1->co, vlr->v1->co, yvec);
1364 mul_m4_v3(re->viewmat, vlr->v1->co);
1366 VECSUB(vlr->v2->co, bb_center, xvec);
1367 VECADD(vlr->v2->co, vlr->v2->co, yvec);
1368 mul_m4_v3(re->viewmat, vlr->v2->co);
1370 VECSUB(vlr->v3->co, bb_center, xvec);
1371 VECSUB(vlr->v3->co, vlr->v3->co, yvec);
1372 mul_m4_v3(re->viewmat, vlr->v3->co);
1374 VECADD(vlr->v4->co, bb_center, xvec);
1375 VECSUB(vlr->v4->co, vlr->v4->co, yvec);
1376 mul_m4_v3(re->viewmat, vlr->v4->co);
1378 normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
1379 copy_v3_v3(vlr->v1->n,vlr->n);
1380 copy_v3_v3(vlr->v2->n,vlr->n);
1381 copy_v3_v3(vlr->v3->n,vlr->n);
1382 copy_v3_v3(vlr->v4->n,vlr->n);
1387 if(bb->uv_split > 1){
1388 uvdx = uvdy = 1.0f / (float)bb->uv_split;
1390 if(ELEM(bb->anim, PART_BB_ANIM_AGE, PART_BB_ANIM_FRAME)) {
1391 if(bb->anim == PART_BB_ANIM_FRAME)
1392 time = ((int)(bb->time * bb->lifetime) % totsplit)/(float)totsplit;
1396 else if(bb->anim == PART_BB_ANIM_ANGLE) {
1397 if(bb->align == PART_BB_VIEW) {
1398 time = (float)fmod((bb->tilt + 1.0f) / 2.0f, 1.0);
1401 float axis1[3] = {0.0f,0.0f,0.0f};
1402 float axis2[3] = {0.0f,0.0f,0.0f};
1404 axis1[(bb->align + 1) % 3] = 1.0f;
1405 axis2[(bb->align + 2) % 3] = 1.0f;
1408 zvec[bb->align] = 0.0f;
1412 time = saacos(dot_v3v3(zvec, axis1)) / (float)M_PI;
1414 if(dot_v3v3(zvec, axis2) < 0.0f)
1415 time = 1.0f - time / 2.0f;
1421 if(bb->split_offset == PART_BB_OFF_LINEAR)
1422 time = (float)fmod(time + (float)bb->num / (float)totsplit, 1.0f);
1423 else if(bb->split_offset==PART_BB_OFF_RANDOM)
1424 time = (float)fmod(time + bb->random, 1.0f);
1426 /* Find the coordinates in tile space (integer), then convert to UV
1427 * space (float). Note that Y is flipped. */
1428 tile = (int)((time + FLT_EPSILON10) * totsplit);
1429 x = tile % bb->uv_split;
1430 y = tile / bb->uv_split;
1431 y = (bb->uv_split - 1) - y;
1438 mtf = RE_vlakren_get_tface(obr, vlr, bb->uv[0], NULL, 1);
1439 mtf->uv[0][0] = 1.0f;
1440 mtf->uv[0][1] = 1.0f;
1441 mtf->uv[1][0] = 0.0f;
1442 mtf->uv[1][1] = 1.0f;
1443 mtf->uv[2][0] = 0.0f;
1444 mtf->uv[2][1] = 0.0f;
1445 mtf->uv[3][0] = 1.0f;
1446 mtf->uv[3][1] = 0.0f;
1449 /* time-index UVs */
1451 mtf = RE_vlakren_get_tface(obr, vlr, bb->uv[1], NULL, 1);
1452 mtf->uv[0][0] = mtf->uv[1][0] = mtf->uv[2][0] = mtf->uv[3][0] = bb->time;
1453 mtf->uv[0][1] = mtf->uv[1][1] = mtf->uv[2][1] = mtf->uv[3][1] = (float)bb->num/(float)bb->totnum;
1457 if(bb->uv_split > 1 && bb->uv[2] >= 0){
1458 mtf = RE_vlakren_get_tface(obr, vlr, bb->uv[2], NULL, 1);
1459 mtf->uv[0][0] = uvx + uvdx;
1460 mtf->uv[0][1] = uvy + uvdy;
1461 mtf->uv[1][0] = uvx;
1462 mtf->uv[1][1] = uvy + uvdy;
1463 mtf->uv[2][0] = uvx;
1464 mtf->uv[2][1] = uvy;
1465 mtf->uv[3][0] = uvx + uvdx;
1466 mtf->uv[3][1] = uvy;
1469 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)
1471 float loc[3], loc0[3], loc1[3], vel[3];
1473 copy_v3_v3(loc, state->co);
1475 if(ren_as != PART_DRAW_BB)
1476 mul_m4_v3(re->viewmat, loc);
1479 case PART_DRAW_LINE:
1484 copy_v3_v3(vel, state->vel);
1485 mul_mat3_m4_v3(re->viewmat, vel);
1488 if(part->draw & PART_DRAW_VEL_LENGTH)
1489 mul_v3_fl(vel, len_v3(state->vel));
1491 VECADDFAC(loc0, loc, vel, -part->draw_line[0]);
1492 VECADDFAC(loc1, loc, vel, part->draw_line[1]);
1494 particle_curve(re, obr, dm, ma, sd, loc0, loc1, seed, pa_co);
1500 copy_v3_v3(bb->vec, loc);
1501 copy_v3_v3(bb->vel, state->vel);
1503 particle_billboard(re, obr, ma, bb);
1511 har = RE_inithalo_particle(re, obr, dm, ma, loc, NULL, sd->orco, sd->uvco, hasize, 0.0, seed, pa_co);
1513 if(har) har->lay= obr->ob->lay;
1519 static void get_particle_uvco_mcol(short from, DerivedMesh *dm, float *fuv, int num, ParticleStrandData *sd)
1524 if(sd->uvco && ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
1525 for(i=0; i<sd->totuv; i++) {
1526 if(num != DMCACHE_NOTFOUND) {
1527 MFace *mface = dm->getFaceData(dm, num, CD_MFACE);
1528 MTFace *mtface = (MTFace*)CustomData_get_layer_n(&dm->faceData, CD_MTFACE, i);
1531 psys_interpolate_uvs(mtface, mface->v4, fuv, sd->uvco + 2 * i);
1534 sd->uvco[2*i] = 0.0f;
1535 sd->uvco[2*i + 1] = 0.0f;
1541 if(sd->mcol && ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
1542 for(i=0; i<sd->totcol; i++) {
1543 if(num != DMCACHE_NOTFOUND) {
1544 MFace *mface = dm->getFaceData(dm, num, CD_MFACE);
1545 MCol *mc = (MCol*)CustomData_get_layer_n(&dm->faceData, CD_MCOL, i);
1548 psys_interpolate_mcol(mc, mface->v4, fuv, sd->mcol + i);
1551 memset(&sd->mcol[i], 0, sizeof(MCol));
1555 static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem *psys, int timeoffset)
1557 Object *ob= obr->ob;
1560 ParticleSystemModifierData *psmd;
1561 ParticleSystem *tpsys=0;
1562 ParticleSettings *part, *tpart=0;
1563 ParticleData *pars, *pa=0,*tpa=0;
1564 ParticleKey *states=0;
1566 ParticleCacheKey *cache=0;
1567 ParticleBillboardData bb;
1568 ParticleSimulationData sim = {0};
1569 ParticleStrandData sd;
1570 StrandBuffer *strandbuf=0;
1571 StrandVert *svert=0;
1572 StrandBound *sbound= 0;
1573 StrandRen *strand=0;
1575 float loc[3],loc1[3],loc0[3],mat[4][4],nmat[3][3],co[3],nor[3],duplimat[4][4];
1576 float strandlen=0.0f, curlen=0.0f;
1577 float hasize, pa_size, r_tilt, r_length;
1578 float pa_time, pa_birthtime, pa_dietime;
1579 float random, simplify[2], pa_co[3];
1580 const float cfra= BKE_curframe(re->scene);
1581 int i, a, k, max_k=0, totpart, dosimplify = 0, dosurfacecache = 0, use_duplimat = 0;
1583 int seed, path_nbr=0, orco1=0, num;
1584 int totface, *origindex = 0;
1587 /* 1. check that everything is ok & updated */
1592 pars=psys->particles;
1594 if(part==NULL || pars==NULL || !psys_check_enabled(ob, psys))
1597 if(part->ren_as==PART_DRAW_OB || part->ren_as==PART_DRAW_GR || part->ren_as==PART_DRAW_NOT)
1600 /* 2. start initialising things */
1602 /* last possibility to bail out! */
1603 psmd = psys_get_modifier(ob,psys);
1604 if(!(psmd->modifier.mode & eModifierMode_Render))
1607 sim.scene= re->scene;
1612 if(part->phystype==PART_PHYS_KEYED)
1613 psys_count_keyed_targets(&sim);
1615 totchild=psys->totchild;
1617 /* can happen for disconnected/global hair */
1618 if(part->type==PART_HAIR && !psys->childcache)
1621 if(G.rendering == 0) { /* preview render */
1622 totchild = (int)((float)totchild * (float)part->disp / 100.0f);
1625 psys->flag |= PSYS_DRAWING;
1627 rng= rng_new(psys->seed);
1629 totpart=psys->totpart;
1631 memset(&sd, 0, sizeof(ParticleStrandData));
1632 sd.override_uv = -1;
1634 /* 2.1 setup material stff */
1635 ma= give_render_material(re, ob, part->omat);
1637 #if 0 // XXX old animation system
1639 calc_ipo(ma->ipo, cfra);
1640 execute_ipo((ID *)ma, ma->ipo);
1642 #endif // XXX old animation system
1644 hasize = ma->hasize;
1649 RE_set_customdata_names(obr, &psmd->dm->faceData);
1650 sd.totuv = CustomData_number_of_layers(&psmd->dm->faceData, CD_MTFACE);
1651 sd.totcol = CustomData_number_of_layers(&psmd->dm->faceData, CD_MCOL);
1653 if(ma->texco & TEXCO_UV && sd.totuv) {
1654 sd.uvco = MEM_callocN(sd.totuv * 2 * sizeof(float), "particle_uvs");
1656 if(ma->strand_uvname[0]) {
1657 sd.override_uv = CustomData_get_named_layer_index(&psmd->dm->faceData, CD_MTFACE, ma->strand_uvname);
1658 sd.override_uv -= CustomData_get_layer_index(&psmd->dm->faceData, CD_MTFACE);
1665 sd.mcol = MEM_callocN(sd.totcol * sizeof(MCol), "particle_mcols");
1667 /* 2.2 setup billboards */
1668 if(part->ren_as == PART_DRAW_BB) {
1669 int first_uv = CustomData_get_layer_index(&psmd->dm->faceData, CD_MTFACE);
1671 bb.uv[0] = CustomData_get_named_layer_index(&psmd->dm->faceData, CD_MTFACE, psys->bb_uvname[0]);
1673 bb.uv[0] = CustomData_get_active_layer_index(&psmd->dm->faceData, CD_MTFACE);
1675 bb.uv[1] = CustomData_get_named_layer_index(&psmd->dm->faceData, CD_MTFACE, psys->bb_uvname[1]);
1677 bb.uv[2] = CustomData_get_named_layer_index(&psmd->dm->faceData, CD_MTFACE, psys->bb_uvname[2]);
1680 bb.uv[0] -= first_uv;
1681 bb.uv[1] -= first_uv;
1682 bb.uv[2] -= first_uv;
1685 bb.align = part->bb_align;
1686 bb.anim = part->bb_anim;
1687 bb.lock = part->draw & PART_DRAW_BB_LOCK;
1688 bb.ob = (part->bb_ob ? part->bb_ob : RE_GetCamera(re));
1689 bb.split_offset = part->bb_split_offset;
1690 bb.totnum = totpart+totchild;
1691 bb.uv_split = part->bb_uv_split;
1694 /* 2.5 setup matrices */
1695 mul_m4_m4m4(mat, ob->obmat, re->viewmat);
1696 invert_m4_m4(ob->imat, mat); /* need to be that way, for imat texture */
1697 copy_m3_m4(nmat, ob->imat);
1700 if(psys->flag & PSYS_USE_IMAT) {
1701 /* psys->imat is the original emitter's inverse matrix, ob->obmat is the duplicated object's matrix */
1702 mul_m4_m4m4(duplimat, psys->imat, ob->obmat);
1706 /* 2.6 setup strand rendering */
1707 if(part->ren_as == PART_DRAW_PATH && psys->pathcache){
1708 path_nbr=(int)pow(2.0,(double) part->ren_step);
1711 if(!ELEM(ma->material_type, MA_TYPE_HALO, MA_TYPE_WIRE)) {
1712 sd.orco = MEM_mallocN(3*sizeof(float)*(totpart+totchild), "particle orcos");
1713 set_object_orco(re, psys, sd.orco);
1717 if(part->draw & PART_DRAW_REN_ADAPT) {
1719 sd.adapt_pix = (float)part->adapt_pix;
1720 sd.adapt_angle = cos((float)part->adapt_angle * (float)(M_PI / 180.0));
1723 if(re->r.renderer==R_INTERN && part->draw&PART_DRAW_REN_STRAND) {
1724 strandbuf= RE_addStrandBuffer(obr, (totpart+totchild)*(path_nbr+1));
1726 strandbuf->lay= ob->lay;
1727 copy_m4_m4(strandbuf->winmat, re->winmat);
1728 strandbuf->winx= re->winx;
1729 strandbuf->winy= re->winy;
1730 strandbuf->maxdepth= 2;
1731 strandbuf->adaptcos= cos((float)part->adapt_angle*(float)(M_PI/180.0));
1732 strandbuf->overrideuv= sd.override_uv;
1733 strandbuf->minwidth= ma->strand_min;
1735 if(ma->strand_widthfade == 0.0f)
1736 strandbuf->widthfade= 0.0f;
1737 else if(ma->strand_widthfade >= 1.0f)
1738 strandbuf->widthfade= 2.0f - ma->strand_widthfade;
1740 strandbuf->widthfade= 1.0f/MAX2(ma->strand_widthfade, 1e-5f);
1742 if(part->flag & PART_HAIR_BSPLINE)
1743 strandbuf->flag |= R_STRAND_BSPLINE;
1744 if(ma->mode & MA_STR_B_UNITS)
1745 strandbuf->flag |= R_STRAND_B_UNITS;
1747 svert= strandbuf->vert;
1749 if(re->r.mode & R_SPEED)
1751 else if((re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) && (re->wrld.ao_gather_method == WO_AOGATHER_APPROX))
1755 totface= psmd->dm->getNumFaces(psmd->dm);
1756 origindex= psmd->dm->getFaceDataArray(psmd->dm, CD_ORIGINDEX);
1757 for(a=0; a<totface; a++)
1758 strandbuf->totbound= MAX2(strandbuf->totbound, (origindex)? origindex[a]: a);
1760 strandbuf->totbound++;
1761 strandbuf->bound= MEM_callocN(sizeof(StrandBound)*strandbuf->totbound, "StrandBound");
1762 sbound= strandbuf->bound;
1763 sbound->start= sbound->end= 0;
1768 sd.orco = MEM_mallocN(3 * sizeof(float), "particle orco");
1773 psys->lattice = psys_get_lattice(&sim);
1775 /* 3. start creating renderable things */
1776 for(a=0,pa=pars; a<totpart+totchild; a++, pa++, seed++) {
1777 random = rng_getFloat(rng);
1778 /* setup per particle individual stuff */
1780 if(pa->flag & PARS_UNEXIST) continue;
1782 pa_time=(cfra-pa->time)/pa->lifetime;
1783 pa_birthtime = pa->time;
1784 pa_dietime = pa->dietime;
1786 hasize = ma->hasize;
1789 if(tpsys && part->phystype==PART_PHYS_NO){
1790 tpa=tpsys->particles+pa->num;
1791 psys_particle_on_emitter(psmd,tpart->from,tpa->num,pa->num_dmcache,tpa->fuv,tpa->foffset,co,nor,0,0,sd.orco,0);
1794 psys_particle_on_emitter(psmd,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,nor,0,0,sd.orco,0);
1796 /* get uvco & mcol */
1797 num= pa->num_dmcache;
1799 if(num == DMCACHE_NOTFOUND)
1800 if(pa->num < psmd->dm->getNumFaces(psmd->dm))
1803 get_particle_uvco_mcol(part->from, psmd->dm, pa->fuv, num, &sd);
1807 r_tilt = 2.0f*(PSYS_FRAND(a) - 0.5f);
1808 r_length = PSYS_FRAND(a+1);
1811 cache = psys->pathcache[a];
1812 max_k = (int)cache->steps;
1815 if(totchild && (part->draw&PART_DRAW_PARENT)==0) continue;
1818 ChildParticle *cpa= psys->child+a-totpart;
1821 cache = psys->childcache[a-totpart];
1823 if(cache->steps < 0)
1826 max_k = (int)cache->steps;
1829 pa_time = psys_get_child_time(psys, cpa, cfra, &pa_birthtime, &pa_dietime);
1830 pa_size = psys_get_child_size(psys, cpa, cfra, &pa_time);
1832 r_tilt = 2.0f*(PSYS_FRAND(a + 21) - 0.5f);
1833 r_length = PSYS_FRAND(a + 22);
1838 if(part->childtype == PART_CHILD_FACES) {
1839 psys_particle_on_emitter(psmd,
1840 PART_FROM_FACE, cpa->num,DMCACHE_ISCHILD,
1841 cpa->fuv,cpa->foffset,co,nor,0,0,sd.orco,0);
1844 ParticleData *par = psys->particles + cpa->parent;
1845 psys_particle_on_emitter(psmd, part->from,
1846 par->num,DMCACHE_ISCHILD,par->fuv,
1847 par->foffset,co,nor,0,0,sd.orco,0);
1850 /* get uvco & mcol */
1851 if(part->childtype==PART_CHILD_FACES) {
1852 get_particle_uvco_mcol(PART_FROM_FACE, psmd->dm, cpa->fuv, cpa->num, &sd);
1855 ParticleData *parent = psys->particles + cpa->parent;
1856 num = parent->num_dmcache;
1858 if(num == DMCACHE_NOTFOUND)
1859 if(parent->num < psmd->dm->getNumFaces(psmd->dm))
1862 get_particle_uvco_mcol(part->from, psmd->dm, parent->fuv, num, &sd);
1865 dosimplify = psys_render_simplify_params(psys, cpa, simplify);
1868 int orignum= (origindex)? origindex[cpa->num]: cpa->num;
1870 if(orignum > sbound - strandbuf->bound) {
1871 sbound= strandbuf->bound + orignum;
1872 sbound->start= sbound->end= obr->totstrand;
1877 /* TEXCO_PARTICLE */
1882 /* surface normal shading setup */
1883 if(ma->mode_l & MA_STR_SURFDIFF) {
1884 mul_m3_v3(nmat, nor);
1890 /* strand render setup */
1892 strand= RE_findOrAddStrand(obr, obr->totstrand++);
1893 strand->buffer= strandbuf;
1894 strand->vert= svert;
1895 copy_v3_v3(strand->orco, sd.orco);
1898 float *ssimplify= RE_strandren_get_simplify(obr, strand, 1);
1899 ssimplify[0]= simplify[0];
1900 ssimplify[1]= simplify[1];
1904 float *snor= RE_strandren_get_surfnor(obr, strand, 1);
1905 copy_v3_v3(snor, sd.surfnor);
1908 if(dosurfacecache && num >= 0) {
1909 int *facenum= RE_strandren_get_face(obr, strand, 1);
1914 for(i=0; i<sd.totuv; i++) {
1915 if(i != sd.override_uv) {
1916 float *uv= RE_strandren_get_uv(obr, strand, i, NULL, 1);
1918 uv[0]= sd.uvco[2*i];
1919 uv[1]= sd.uvco[2*i+1];
1924 for(i=0; i<sd.totcol; i++) {
1925 MCol *mc= RE_strandren_get_mcol(obr, strand, i, NULL, 1);
1933 /* strandco computation setup */
1937 for(k=1; k<=path_nbr; k++)
1939 strandlen += len_v3v3((cache+k-1)->co, (cache+k)->co);
1943 /* render strands */
1944 for(k=0; k<=path_nbr; k++){
1948 copy_v3_v3(state.co,(cache+k)->co);
1949 copy_v3_v3(state.vel,(cache+k)->vel);
1955 curlen += len_v3v3((cache+k-1)->co, (cache+k)->co);
1956 time= curlen/strandlen;
1958 copy_v3_v3(loc,state.co);
1959 mul_m4_v3(re->viewmat,loc);
1962 copy_v3_v3(svert->co, loc);
1963 svert->strandco= -1.0f + 2.0f*time;
1973 VECSUB(loc0,loc1,loc);
1974 VECADD(loc0,loc1,loc0);
1976 particle_curve(re, obr, psmd->dm, ma, &sd, loc1, loc0, seed, pa_co);
1983 particle_curve(re, obr, psmd->dm, ma, &sd, loc, loc1, seed, pa_co);
1985 copy_v3_v3(loc1,loc);
1991 /* render normal particles */
1992 if(part->trail_count > 1) {
1993 float length = part->path_end * (1.0f - part->randlength * r_length);
1994 int trail_count = part->trail_count * (1.0f - part->randlength * r_length);
1995 float ct = (part->draw & PART_ABS_PATH_TIME) ? cfra : pa_time;
1996 float dt = length / (trail_count ? (float)trail_count : 1.0f);
1998 /* make sure we have pointcache in memory before getting particle on path */
1999 psys_make_temp_pointcache(ob, psys);
2001 for(i=0; i < trail_count; i++, ct -= dt) {
2002 if(part->draw & PART_ABS_PATH_TIME) {
2003 if(ct < pa_birthtime || ct > pa_dietime)
2006 else if(ct < 0.0f || ct > 1.0f)
2009 state.time = (part->draw & PART_ABS_PATH_TIME) ? -ct : ct;
2010 psys_get_particle_on_path(&sim,a,&state,1);
2013 mul_m4_v3(psys->parent->obmat, state.co);
2016 mul_m4_v4(duplimat, state.co);
2018 if(part->ren_as == PART_DRAW_BB) {
2020 bb.offset[0] = part->bb_offset[0];
2021 bb.offset[1] = part->bb_offset[1];
2022 bb.size[0] = part->bb_size[0] * pa_size;
2023 if (part->bb_align==PART_BB_VEL) {
2024 float pa_vel = len_v3(state.vel);
2025 float head = part->bb_vel_head*pa_vel;
2026 float tail = part->bb_vel_tail*pa_vel;
2027 bb.size[1] = part->bb_size[1]*pa_size + head + tail;
2028 /* use offset to adjust the particle center. this is relative to size, so need to divide! */
2029 if (bb.size[1] > 0.0f)
2030 bb.offset[1] += (head-tail) / bb.size[1];
2033 bb.size[1] = part->bb_size[1] * pa_size;
2034 bb.tilt = part->bb_tilt * (1.0f - part->bb_rand_tilt * r_tilt);
2039 pa_co[0] = (part->draw & PART_ABS_PATH_TIME) ? (ct-pa_birthtime)/(pa_dietime-pa_birthtime) : ct;
2040 pa_co[1] = (float)i/(float)(trail_count-1);
2042 particle_normal_ren(part->ren_as, part, re, obr, psmd->dm, ma, &sd, &bb, &state, seed, hasize, pa_co);
2047 if(psys_get_particle_state(&sim,a,&state,0)==0)
2051 mul_m4_v3(psys->parent->obmat, state.co);
2054 mul_m4_v3(duplimat, state.co);
2056 if(part->ren_as == PART_DRAW_BB) {
2058 bb.offset[0] = part->bb_offset[0];
2059 bb.offset[1] = part->bb_offset[1];
2060 bb.size[0] = part->bb_size[0] * pa_size;
2061 if (part->bb_align==PART_BB_VEL) {
2062 float pa_vel = len_v3(state.vel);
2063 float head = part->bb_vel_head*pa_vel;
2064 float tail = part->bb_vel_tail*pa_vel;
2065 bb.size[1] = part->bb_size[1]*pa_size + head + tail;
2066 /* use offset to adjust the particle center. this is relative to size, so need to divide! */
2067 if (bb.size[1] > 0.0f)
2068 bb.offset[1] += (head-tail) / bb.size[1];
2071 bb.size[1] = part->bb_size[1] * pa_size;
2072 bb.tilt = part->bb_tilt * (1.0f - part->bb_rand_tilt * r_tilt);
2075 bb.lifetime = pa_dietime-pa_birthtime;
2078 particle_normal_ren(part->ren_as, part, re, obr, psmd->dm, ma, &sd, &bb, &state, seed, hasize, pa_co);
2085 if(re->test_break(re->tbh))
2090 strandbuf->surface= cache_strand_surface(re, obr, psmd->dm, mat, timeoffset);
2093 #if 0 // XXX old animation system
2094 if(ma) do_mat_ipo(re->scene, ma);
2095 #endif // XXX old animation system
2114 psys->flag &= ~PSYS_DRAWING;
2117 end_latt_deform(psys->lattice);
2118 psys->lattice= NULL;
2121 if(path_nbr && (ma->mode_l & MA_TANGENT_STR)==0)
2122 calc_vertexnormals(re, obr, 0, 0);
2127 /* ------------------------------------------------------------------------- */
2129 /* ------------------------------------------------------------------------- */
2131 static void make_render_halos(Render *re, ObjectRen *obr, Mesh *UNUSED(me), int totvert, MVert *mvert, Material *ma, float *orco)
2133 Object *ob= obr->ob;
2135 float xn, yn, zn, nor[3], view[3];
2136 float vec[3], hasize, mat[4][4], imat[3][3];
2137 int a, ok, seed= ma->seed1;
2139 mul_m4_m4m4(mat, ob->obmat, re->viewmat);
2140 copy_m3_m4(imat, ob->imat);
2144 for(a=0; a<totvert; a++, mvert++) {
2150 copy_v3_v3(vec, mvert->co);
2151 mul_m4_v3(mat, vec);
2153 if(ma->mode & MA_HALOPUNO) {
2159 nor[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
2160 nor[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
2161 nor[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
2164 copy_v3_v3(view, vec);
2167 zn= nor[0]*view[0]+nor[1]*view[1]+nor[2]*view[2];
2168 if(zn>=0.0f) hasize= 0.0f;
2169 else hasize*= zn*zn*zn*zn;
2172 if(orco) har= RE_inithalo(re, obr, ma, vec, NULL, orco, hasize, 0.0, seed);
2173 else har= RE_inithalo(re, obr, ma, vec, NULL, mvert->co, hasize, 0.0, seed);
2174 if(har) har->lay= ob->lay;
2181 static int verghalo(const void *a1, const void *a2)
2183 const HaloRen *har1= *(const HaloRen**)a1;
2184 const HaloRen *har2= *(const HaloRen**)a2;
2186 if(har1->zs < har2->zs) return 1;
2187 else if(har1->zs > har2->zs) return -1;
2191 static void sort_halos(Render *re, int totsort)
2194 HaloRen *har= NULL, **haso;
2197 if(re->tothalo==0) return;
2199 re->sortedhalos= MEM_callocN(sizeof(HaloRen*)*re->tothalo, "sorthalos");
2200 haso= re->sortedhalos;
2202 for(obr=re->objecttable.first; obr; obr=obr->next) {
2203 for(a=0; a<obr->tothalo; a++) {
2204 if((a & 255)==0) har= obr->bloha[a>>8];
2211 qsort(re->sortedhalos, totsort, sizeof(HaloRen*), verghalo);
2214 /* ------------------------------------------------------------------------- */
2215 /* Displacement Mapping */
2216 /* ------------------------------------------------------------------------- */
2218 static short test_for_displace(Render *re, Object *ob)
2220 /* return 1 when this object uses displacement textures. */
2224 for (i=1; i<=ob->totcol; i++) {
2225 ma=give_render_material(re, ob, i);
2226 /* ma->mapto is ORed total of all mapto channels */
2227 if(ma && (ma->mapto & MAP_DISPLACE)) return 1;
2232 static void displace_render_vert(Render *re, ObjectRen *obr, ShadeInput *shi, VertRen *vr, int vindex, float *scale, float mat[][4], float imat[][3])
2235 short texco= shi->mat->texco;
2236 float sample=0, displace[3];
2240 /* shi->co is current render coord, just make sure at least some vector is here */
2241 copy_v3_v3(shi->co, vr->co);
2242 /* vertex normal is used for textures type 'col' and 'var' */
2243 copy_v3_v3(shi->vn, vr->n);
2246 mul_m4_v3(mat, shi->co);
2249 shi->vn[0]= imat[0][0]*vr->n[0]+imat[0][1]*vr->n[1]+imat[0][2]*vr->n[2];
2250 shi->vn[1]= imat[1][0]*vr->n[0]+imat[1][1]*vr->n[1]+imat[1][2]*vr->n[2];
2251 shi->vn[2]= imat[2][0]*vr->n[0]+imat[2][1]*vr->n[1]+imat[2][2]*vr->n[2];
2254 if (texco & TEXCO_UV) {
2256 shi->actuv= obr->actmtface;
2258 for (i=0; (tface=RE_vlakren_get_tface(obr, shi->vlr, i, &name, 0)); i++) {
2259 ShadeInputUV *suv= &shi->uv[i];
2261 /* shi.uv needs scale correction from tface uv */
2262 suv->uv[0]= 2*tface->uv[vindex][0]-1.0f;
2263 suv->uv[1]= 2*tface->uv[vindex][1]-1.0f;
2270 /* set all rendercoords, 'texco' is an ORed value for all textures needed */
2271 if ((texco & TEXCO_ORCO) && (vr->orco)) {
2272 copy_v3_v3(shi->lo, vr->orco);
2274 if (texco & TEXCO_STICKY) {
2275 float *sticky= RE_vertren_get_sticky(obr, vr, 0);
2277 shi->sticky[0]= sticky[0];
2278 shi->sticky[1]= sticky[1];
2279 shi->sticky[2]= 0.0f;
2282 if (texco & TEXCO_GLOB) {
2283 copy_v3_v3(shi->gl, shi->co);
2284 mul_m4_v3(re->viewinv, shi->gl);
2286 if (texco & TEXCO_NORM) {
2287 copy_v3_v3(shi->orn, shi->vn);
2289 if(texco & TEXCO_REFL) {
2293 shi->displace[0]= shi->displace[1]= shi->displace[2]= 0.0;
2295 do_material_tex(shi);
2297 //printf("no=%f, %f, %f\nbefore co=%f, %f, %f\n", vr->n[0], vr->n[1], vr->n[2],
2298 //vr->co[0], vr->co[1], vr->co[2]);
2300 displace[0]= shi->displace[0] * scale[0];
2301 displace[1]= shi->displace[1] * scale[1];
2302 displace[2]= shi->displace[2] * scale[2];
2305 mul_m3_v3(imat, displace);
2307 /* 0.5 could become button once? */
2308 vr->co[0] += displace[0];
2309 vr->co[1] += displace[1];
2310 vr->co[2] += displace[2];
2312 //printf("after co=%f, %f, %f\n", vr->co[0], vr->co[1], vr->co[2]);
2314 /* we just don't do this vertex again, bad luck for other face using same vertex with
2315 different material... */
2318 /* Pass sample back so displace_face can decide which way to split the quad */
2319 sample = shi->displace[0]*shi->displace[0];
2320 sample += shi->displace[1]*shi->displace[1];
2321 sample += shi->displace[2]*shi->displace[2];
2324 /* Should be sqrt(sample), but I'm only looking for "bigger". Save the cycles. */
2328 static void displace_render_face(Render *re, ObjectRen *obr, VlakRen *vlr, float *scale, float mat[][4], float imat[][3])
2332 /* Warning, This is not that nice, and possibly a bit slow,
2333 however some variables were not initialized properly in, unless using shade_input_initialize(...), we need to do a memset */
2334 memset(&shi, 0, sizeof(ShadeInput));
2335 /* end warning! - Campbell */
2337 /* set up shadeinput struct for multitex() */
2339 /* memset above means we dont need this */
2340 /*shi.osatex= 0;*/ /* signal not to use dx[] and dy[] texture AA vectors */
2343 shi.vlr= vlr; /* current render face */
2344 shi.mat= vlr->mat; /* current input material */
2347 /* TODO, assign these, displacement with new bumpmap is skipped without - campbell */
2349 /* order is not known ? */
2355 /* Displace the verts, flag is set when done */
2357 displace_render_vert(re, obr, &shi, vlr->v1,0, scale, mat, imat);
2360 displace_render_vert(re, obr, &shi, vlr->v2, 1, scale, mat, imat);
2363 displace_render_vert(re, obr, &shi, vlr->v3, 2, scale, mat, imat);
2367 displace_render_vert(re, obr, &shi, vlr->v4, 3, scale, mat, imat);
2369 /* closest in displace value. This will help smooth edges. */
2370 if ( fabs(vlr->v1->accum - vlr->v3->accum) > fabs(vlr->v2->accum - vlr->v4->accum))
2371 vlr->flag |= R_DIVIDE_24;
2372 else vlr->flag &= ~R_DIVIDE_24;
2375 /* Recalculate the face normal - if flipped before, flip now */
2377 normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
2380 normal_tri_v3( vlr->n,vlr->v3->co, vlr->v2->co, vlr->v1->co);
2384 static void do_displacement(Render *re, ObjectRen *obr, float mat[][4], float imat[][3])
2388 // float min[3]={1e30, 1e30, 1e30}, max[3]={-1e30, -1e30, -1e30};
2389 float scale[3]={1.0f, 1.0f, 1.0f}, temp[3];//, xn
2390 int i; //, texflag=0;
2393 /* Object Size with parenting */
2396 add_v3_v3v3(temp, obt->size, obt->dsize);
2397 scale[0]*=temp[0]; scale[1]*=temp[1]; scale[2]*=temp[2];
2401 /* Clear all flags */
2402 for(i=0; i<obr->totvert; i++){
2403 vr= RE_findOrAddVert(obr, i);
2407 for(i=0; i<obr->totvlak; i++){
2408 vlr=RE_findOrAddVlak(obr, i);
2409 displace_render_face(re, obr, vlr, scale, mat, imat);
2412 /* Recalc vertex normals */
2413 calc_vertexnormals(re, obr, 0, 0);
2416 /* ------------------------------------------------------------------------- */
2418 /* ------------------------------------------------------------------------- */
2420 static void init_render_mball(Render *re, ObjectRen *obr)
2422 Object *ob= obr->ob;
2425 VlakRen *vlr, *vlr1;
2427 float *data, *nors, *orco=NULL, mat[4][4], imat[3][3], xn, yn, zn;
2428 int a, need_orco, vlakindex, *index;
2429 ListBase dispbase= {NULL, NULL};
2431 if (ob!=find_basis_mball(re->scene, ob))
2434 mul_m4_m4m4(mat, ob->obmat, re->viewmat);
2435 invert_m4_m4(ob->imat, mat);
2436 copy_m3_m4(imat, ob->imat);
2438 ma= give_render_material(re, ob, 1);
2441 if(ma->texco & TEXCO_ORCO) {
2445 makeDispListMBall_forRender(re->scene, ob, &dispbase);
2452 orco= get_object_orco(re, ob);
2455 /* orco hasn't been found in cache - create new one and add to cache */
2456 orco= make_orco_mball(ob, &dispbase);
2457 set_object_orco(re, ob, orco);
2461 for(a=0; a<dl->nr; a++, data+=3, nors+=3) {
2463 ver= RE_findOrAddVert(obr, obr->totvert++);
2464 copy_v3_v3(ver->co, data);
2465 mul_m4_v3(mat, ver->co);
2467 /* render normals are inverted */
2473 ver->n[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
2474 ver->n[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
2475 ver->n[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
2476 normalize_v3(ver->n);
2477 //if(ob->transflag & OB_NEG_SCALE) negate_v3(ver->n);
2486 for(a=0; a<dl->parts; a++, index+=4) {
2488 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2489 vlr->v1= RE_findOrAddVert(obr, index[0]);
2490 vlr->v2= RE_findOrAddVert(obr, index[1]);
2491 vlr->v3= RE_findOrAddVert(obr, index[2]);
2494 if(ob->transflag & OB_NEG_SCALE)
2495 normal_tri_v3( vlr->n,vlr->v1->co, vlr->v2->co, vlr->v3->co);
2497 normal_tri_v3( vlr->n,vlr->v3->co, vlr->v2->co, vlr->v1->co);
2500 vlr->flag= ME_SMOOTH;
2503 /* mball -too bad- always has triangles, because quads can be non-planar */
2504 if(index[3] && index[3]!=index[2]) {
2505 vlr1= RE_findOrAddVlak(obr, obr->totvlak++);
2506 vlakindex= vlr1->index;
2508 vlr1->index= vlakindex;
2510 vlr1->v3= RE_findOrAddVert(obr, index[3]);
2511 if(ob->transflag & OB_NEG_SCALE)
2512 normal_tri_v3( vlr1->n,vlr1->v1->co, vlr1->v2->co, vlr1->v3->co);
2514 normal_tri_v3( vlr1->n,vlr1->v3->co, vlr1->v2->co, vlr1->v1->co);
2518 /* enforce display lists remade */
2519 freedisplist(&dispbase);
2522 /* ------------------------------------------------------------------------- */
2523 /* Surfaces and Curves */
2524 /* ------------------------------------------------------------------------- */
2526 /* returns amount of vertices added for orco */
2527 static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar, float *orco, float mat[4][4])
2529 VertRen *v1, *v2, *v3, *v4, *ver;
2530 VlakRen *vlr, *vlr1, *vlr2, *vlr3;
2532 int u, v, orcoret= 0;
2533 int p1, p2, p3, p4, a;
2534 int sizeu, nsizeu, sizev, nsizev;
2535 int startvert, startvlak;
2537 startvert= obr->totvert;
2538 nsizeu = sizeu = dl->parts; nsizev = sizev = dl->nr;
2541 for (u = 0; u < sizeu; u++) {
2542 v1 = RE_findOrAddVert(obr, obr->totvert++); /* save this for possible V wrapping */
2543 copy_v3_v3(v1->co, data); data += 3;
2545 v1->orco= orco; orco+= 3; orcoret++;
2547 mul_m4_v3(mat, v1->co);
2549 for (v = 1; v < sizev; v++) {
2550 ver= RE_findOrAddVert(obr, obr->totvert++);
2551 copy_v3_v3(ver->co, data); data += 3;
2553 ver->orco= orco; orco+= 3; orcoret++;
2555 mul_m4_v3(mat, ver->co);
2557 /* if V-cyclic, add extra vertices at end of the row */
2558 if (dl->flag & DL_CYCL_U) {
2559 ver= RE_findOrAddVert(obr, obr->totvert++);
2560 copy_v3_v3(ver->co, v1->co);
2562 ver->orco= orco; orco+=3; orcoret++; //orcobase + 3*(u*sizev + 0);
2567 /* Done before next loop to get corner vert */
2568 if (dl->flag & DL_CYCL_U) nsizev++;
2569 if (dl->flag & DL_CYCL_V) nsizeu++;
2571 /* if U cyclic, add extra row at end of column */
2572 if (dl->flag & DL_CYCL_V) {
2573 for (v = 0; v < nsizev; v++) {
2574 v1= RE_findOrAddVert(obr, startvert + v);
2575 ver= RE_findOrAddVert(obr, obr->totvert++);
2576 copy_v3_v3(ver->co, v1->co);
2578 ver->orco= orco; orco+=3; orcoret++; //ver->orco= orcobase + 3*(0*sizev + v);
2586 startvlak= obr->totvlak;
2588 for(u = 0; u < sizeu - 1; u++) {
2589 p1 = startvert + u * sizev; /* walk through face list */
2594 for(v = 0; v < sizev - 1; v++) {
2595 v1= RE_findOrAddVert(obr, p1);
2596 v2= RE_findOrAddVert(obr, p2);
2597 v3= RE_findOrAddVert(obr, p3);
2598 v4= RE_findOrAddVert(obr, p4);
2600 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2601 vlr->v1= v1; vlr->v2= v2; vlr->v3= v3; vlr->v4= v4;
2603 normal_quad_v3( n1,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
2605 copy_v3_v3(vlr->n, n1);
2607 vlr->mat= matar[ dl->col];
2608 vlr->ec= ME_V1V2+ME_V2V3;
2611 add_v3_v3(v1->n, n1);
2612 add_v3_v3(v2->n, n1);
2613 add_v3_v3(v3->n, n1);
2614 add_v3_v3(v4->n, n1);
2616 p1++; p2++; p3++; p4++;
2619 /* fix normals for U resp. V cyclic faces */
2620 sizeu--; sizev--; /* dec size for face array */
2621 if (dl->flag & DL_CYCL_V) {
2623 for (v = 0; v < sizev; v++)
2626 vlr= RE_findOrAddVlak(obr, UVTOINDEX(sizeu - 1, v));
2627 vlr1= RE_findOrAddVlak(obr, UVTOINDEX(0, v));
2628 add_v3_v3(vlr1->v1->n, vlr->n);
2629 add_v3_v3(vlr1->v2->n, vlr->n);
2630 add_v3_v3(vlr->v3->n, vlr1->n);
2631 add_v3_v3(vlr->v4->n, vlr1->n);
2634 if (dl->flag & DL_CYCL_U) {
2636 for (u = 0; u < sizeu; u++)
2639 vlr= RE_findOrAddVlak(obr, UVTOINDEX(u, 0));
2640 vlr1= RE_findOrAddVlak(obr, UVTOINDEX(u, sizev-1));
2641 add_v3_v3(vlr1->v2->n, vlr->n);
2642 add_v3_v3(vlr1->v3->n, vlr->n);
2643 add_v3_v3(vlr->v1->n, vlr1->n);
2644 add_v3_v3(vlr->v4->n, vlr1->n);
2647 /* last vertex is an extra case:
2649 ^ ()----()----()----()
2653 ()====()====[]====()
2657 ()----()----()----()
2660 vertex [] is no longer shared, therefore distribute
2661 normals of the surrounding faces to all of the duplicates of []
2664 if ((dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U))
2666 vlr= RE_findOrAddVlak(obr, UVTOINDEX(sizeu - 1, sizev - 1)); /* (m,n) */
2667 vlr1= RE_findOrAddVlak(obr, UVTOINDEX(0,0)); /* (0,0) */
2668 add_v3_v3v3(n1, vlr->n, vlr1->n);
2669 vlr2= RE_findOrAddVlak(obr, UVTOINDEX(0, sizev-1)); /* (0,n) */
2670 add_v3_v3(n1, vlr2->n);
2671 vlr3= RE_findOrAddVlak(obr, UVTOINDEX(sizeu-1, 0)); /* (m,0) */
2672 add_v3_v3(n1, vlr3->n);
2673 copy_v3_v3(vlr->v3->n, n1);
2674 copy_v3_v3(vlr1->v1->n, n1);
2675 copy_v3_v3(vlr2->v2->n, n1);
2676 copy_v3_v3(vlr3->v4->n, n1);
2678 for(a = startvert; a < obr->totvert; a++) {
2679 ver= RE_findOrAddVert(obr, a);
2680 normalize_v3(ver->n);
2687 static void init_render_dm(DerivedMesh *dm, Render *re, ObjectRen *obr,
2688 int timeoffset, float *orco, float mat[4][4])
2690 Object *ob= obr->ob;
2691 int a, a1, end, totvert, vertofs;
2694 MVert *mvert = NULL;
2697 /* Curve *cu= ELEM(ob->type, OB_FONT, OB_CURVE) ? ob->data : NULL; */
2699 mvert= dm->getVertArray(dm);
2700 totvert= dm->getNumVerts(dm);
2702 for(a=0; a<totvert; a++, mvert++) {
2703 ver= RE_findOrAddVert(obr, obr->totvert++);
2704 copy_v3_v3(ver->co, mvert->co);
2705 mul_m4_v3(mat, ver->co);
2714 /* store customdata names, because DerivedMesh is freed */
2715 RE_set_customdata_names(obr, &dm->faceData);
2717 /* still to do for keys: the correct local texture coordinate */
2719 /* faces in order of color blocks */
2720 vertofs= obr->totvert - totvert;
2721 for(a1=0; (a1<ob->totcol || (a1==0 && ob->totcol==0)); a1++) {
2723 ma= give_render_material(re, ob, a1+1);
2724 end= dm->getNumFaces(dm);
2725 mface= dm->getFaceArray(dm);
2727 for(a=0; a<end; a++, mface++) {
2728 int v1, v2, v3, v4, flag;
2730 if( mface->mat_nr==a1 ) {
2737 flag= mface->flag & ME_SMOOTH;
2739 vlr= RE_findOrAddVlak(obr, obr->totvlak++);
2740 vlr->v1= RE_findOrAddVert(obr, vertofs+v1);
2741 vlr->v2= RE_findOrAddVert(obr, vertofs+v2);
2742 vlr->v3= RE_findOrAddVert(obr, vertofs+v3);
2743 if(v4) vlr->v4= RE_findOrAddVert(obr, vertofs+v4);
2746 /* render normals are inverted in render */
2748 len= normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
2750 len= normal_tri_v3( vlr->n,vlr->v3->co, vlr->v2->co, vlr->v1->co);
2754 vlr->ec= 0; /* mesh edges rendered separately */
2756 if(len==0) obr->totvlak--;
2758 CustomDataLayer *layer;
2759 MTFace *mtface, *mtf;
2761 int index, mtfn= 0, mcn= 0;
2764 for(index=0; index<dm->faceData.totlayer; index++) {
2765 layer= &dm->faceData.layers[index];
2768 if(layer->type == CD_MTFACE && mtfn < MAX_MTFACE) {
2769 mtf= RE_vlakren_get_tface(obr, vlr, mtfn++, &name, 1);
2770 mtface= (MTFace*)layer->data;
2773 else if(layer->type == CD_MCOL && mcn < MAX_MCOL) {
2774 mc= RE_vlakren_get_mcol(obr, vlr, mcn++, &name, 1);
2775 mcol= (MCol*)layer->data;
2776 memcpy(mc, &mcol[a*4], sizeof(MCol)*4);
2785 calc_vertexnormals(re, obr, 0, 0);
2790 static void init_render_surf(Render *re, ObjectRen *obr, int timeoffset)
2792 Object *ob= obr->ob;
2795 ListBase displist= {NULL, NULL};
2798 float *orco=NULL, mat[4][4];
2799 int a, totmat, need_orco=0;
2800 DerivedMesh *dm= NULL;
2806 mul_m4_m4m4(mat, ob->obmat, re->viewmat);
2807 invert_m4_m4(ob->imat, mat);
2809 /* material array */
2810 totmat= ob->totcol+1;
2811 matar= MEM_callocN(sizeof(Material*)*totmat, "init_render_surf matar");
2813 for(a=0; a<totmat; a++) {
2814 matar[a]= give_render_material(re, ob, a+1);
2816 if(matar[a] && matar[a]->texco & TEXCO_ORCO)
2820 if(ob->parent && (ob->parent->type==OB_LATTICE)) need_orco= 1;
2822 makeDispListSurf(re->scene, ob, &displist, &dm, 1, 0);
2826 orco= makeOrcoDispList(re->scene, ob, dm, 1);
2828 set_object_orco(re, ob, orco);
2832 init_render_dm(dm, re, obr, timeoffset, orco, mat);
2836 orco= get_object_orco(re, ob);
2839 /* walk along displaylist and create rendervertices/-faces */
2840 for(dl=displist.first; dl; dl=dl->next) {
2841 /* watch out: u ^= y, v ^= x !! */
2842 if(dl->type==DL_SURF)
2843 orco+= 3*dl_surf_to_renderdata(obr, dl, matar, orco, mat);
2847 freedisplist(&displist);
2852 static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
2854 Object *ob= obr->ob;
2859 DerivedMesh *dm = NULL;
2860 ListBase disp={NULL, NULL};
2862 float *data, *fp, *orco=NULL;
2863 float n[3], mat[4][4];
2864 int nr, startvert, a, b;
2865 int need_orco=0, totmat;
2868 if(ob->type==OB_FONT && cu->str==NULL) return;
2869 else if(ob->type==OB_CURVE && cu->nurb.first==NULL) return;