2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: none of this file.
23 * Contributors: Brecht Van Lommel.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/render/intern/source/strand.c
37 #include "MEM_guardedalloc.h"
39 #include "DNA_key_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_meshdata_types.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_utildefines.h"
46 #include "BLI_ghash.h"
47 #include "BLI_memarena.h"
50 #include "BKE_DerivedMesh.h"
54 #include "render_types.h"
55 #include "initrender.h"
56 #include "rendercore.h"
57 #include "renderdatabase.h"
58 #include "renderpipeline.h"
59 #include "pixelblending.h"
65 void hoco_to_zco(ZSpan *zspan, float *zco, float *hoco);
66 void zspan_scanconvert_strand(ZSpan *zspan, void *handle, float *v1, float *v2, float *v3, void (*func)(void *, int, int, float, float, float) );
67 void zbufsinglewire(ZSpan *zspan, int obi, int zvlnr, float *ho1, float *ho2);
71 static float strand_eval_width(Material *ma, float strandco)
75 strandco= 0.5f*(strandco + 1.0f);
77 if (ma->strand_ease!=0.0f) {
78 if (ma->strand_ease<0.0f)
79 fac= pow(strandco, 1.0f+ma->strand_ease);
81 fac= pow(strandco, 1.0f/(1.0f-ma->strand_ease));
85 return ((1.0f-fac)*ma->strand_sta + (fac)*ma->strand_end);
88 void strand_eval_point(StrandSegment *sseg, StrandPoint *spoint)
91 StrandBuffer *strandbuf;
93 float p[4][3], data[4], cross[3], w, dx, dy, t;
96 strandbuf= sseg->buffer;
99 type= (strandbuf->flag & R_STRAND_BSPLINE)? KEY_BSPLINE: KEY_CARDINAL;
101 copy_v3_v3(p[0], sseg->v[0]->co);
102 copy_v3_v3(p[1], sseg->v[1]->co);
103 copy_v3_v3(p[2], sseg->v[2]->co);
104 copy_v3_v3(p[3], sseg->v[3]->co);
106 if (sseg->obi->flag & R_TRANSFORMED) {
107 mul_m4_v3(sseg->obi->mat, p[0]);
108 mul_m4_v3(sseg->obi->mat, p[1]);
109 mul_m4_v3(sseg->obi->mat, p[2]);
110 mul_m4_v3(sseg->obi->mat, p[3]);
114 copy_v3_v3(spoint->co, p[1]);
115 spoint->strandco= sseg->v[1]->strandco;
117 spoint->dtstrandco= (sseg->v[2]->strandco - sseg->v[0]->strandco);
118 if (sseg->v[0] != sseg->v[1])
119 spoint->dtstrandco *= 0.5f;
121 else if (t == 1.0f) {
122 copy_v3_v3(spoint->co, p[2]);
123 spoint->strandco= sseg->v[2]->strandco;
125 spoint->dtstrandco= (sseg->v[3]->strandco - sseg->v[1]->strandco);
126 if (sseg->v[3] != sseg->v[2])
127 spoint->dtstrandco *= 0.5f;
130 key_curve_position_weights(t, data, type);
131 spoint->co[0]= data[0]*p[0][0] + data[1]*p[1][0] + data[2]*p[2][0] + data[3]*p[3][0];
132 spoint->co[1]= data[0]*p[0][1] + data[1]*p[1][1] + data[2]*p[2][1] + data[3]*p[3][1];
133 spoint->co[2]= data[0]*p[0][2] + data[1]*p[1][2] + data[2]*p[2][2] + data[3]*p[3][2];
134 spoint->strandco= (1.0f-t)*sseg->v[1]->strandco + t*sseg->v[2]->strandco;
137 key_curve_tangent_weights(t, data, type);
138 spoint->dtco[0]= data[0]*p[0][0] + data[1]*p[1][0] + data[2]*p[2][0] + data[3]*p[3][0];
139 spoint->dtco[1]= data[0]*p[0][1] + data[1]*p[1][1] + data[2]*p[2][1] + data[3]*p[3][1];
140 spoint->dtco[2]= data[0]*p[0][2] + data[1]*p[1][2] + data[2]*p[2][2] + data[3]*p[3][2];
142 normalize_v3_v3(spoint->tan, spoint->dtco);
143 normalize_v3_v3(spoint->nor, spoint->co);
144 negate_v3(spoint->nor);
146 spoint->width= strand_eval_width(ma, spoint->strandco);
149 simplify= RE_strandren_get_simplify(strandbuf->obr, sseg->strand, 0);
150 spoint->alpha= (simplify)? simplify[1]: 1.0f;
153 cross_v3_v3v3(cross, spoint->co, spoint->tan);
155 w= spoint->co[2]*strandbuf->winmat[2][3] + strandbuf->winmat[3][3];
156 dx= strandbuf->winx*cross[0]*strandbuf->winmat[0][0]/w;
157 dy= strandbuf->winy*cross[1]*strandbuf->winmat[1][1]/w;
158 w= sqrt(dx*dx + dy*dy);
161 if (strandbuf->flag & R_STRAND_B_UNITS) {
162 const float crosslen= len_v3(cross);
163 w= 2.0f*crosslen*strandbuf->minwidth/w;
165 if (spoint->width < w) {
166 spoint->alpha= spoint->width/w;
171 /* squared because we only change width, not length */
172 spoint->width *= simplify[0]*simplify[0];
174 mul_v3_fl(cross, spoint->width*0.5f/crosslen);
177 mul_v3_fl(cross, spoint->width/w);
180 sub_v3_v3v3(spoint->co1, spoint->co, cross);
181 add_v3_v3v3(spoint->co2, spoint->co, cross);
183 copy_v3_v3(spoint->dsco, cross);
186 /* *************** */
188 static void interpolate_vec1(float *v1, float *v2, float t, float negt, float *v)
190 v[0]= negt*v1[0] + t*v2[0];
193 static void interpolate_vec3(float *v1, float *v2, float t, float negt, float *v)
195 v[0]= negt*v1[0] + t*v2[0];
196 v[1]= negt*v1[1] + t*v2[1];
197 v[2]= negt*v1[2] + t*v2[2];
200 static void interpolate_vec4(float *v1, float *v2, float t, float negt, float *v)
202 v[0]= negt*v1[0] + t*v2[0];
203 v[1]= negt*v1[1] + t*v2[1];
204 v[2]= negt*v1[2] + t*v2[2];
205 v[3]= negt*v1[3] + t*v2[3];
208 static void interpolate_shade_result(ShadeResult *shr1, ShadeResult *shr2, float t, ShadeResult *shr, int addpassflag)
210 float negt= 1.0f - t;
212 interpolate_vec4(shr1->combined, shr2->combined, t, negt, shr->combined);
214 if (addpassflag & SCE_PASS_VECTOR) {
215 interpolate_vec4(shr1->winspeed, shr2->winspeed, t, negt, shr->winspeed);
218 if (addpassflag & ~(SCE_PASS_VECTOR)) {
219 if (addpassflag & SCE_PASS_Z)
220 interpolate_vec1(&shr1->z, &shr2->z, t, negt, &shr->z);
221 if (addpassflag & SCE_PASS_RGBA)
222 interpolate_vec4(shr1->col, shr2->col, t, negt, shr->col);
223 if (addpassflag & SCE_PASS_NORMAL) {
224 interpolate_vec3(shr1->nor, shr2->nor, t, negt, shr->nor);
225 normalize_v3(shr->nor);
227 if (addpassflag & SCE_PASS_EMIT)
228 interpolate_vec3(shr1->emit, shr2->emit, t, negt, shr->emit);
229 if (addpassflag & SCE_PASS_DIFFUSE)
230 interpolate_vec3(shr1->diff, shr2->diff, t, negt, shr->diff);
231 if (addpassflag & SCE_PASS_SPEC)
232 interpolate_vec3(shr1->spec, shr2->spec, t, negt, shr->spec);
233 if (addpassflag & SCE_PASS_SHADOW)
234 interpolate_vec3(shr1->shad, shr2->shad, t, negt, shr->shad);
235 if (addpassflag & SCE_PASS_AO)
236 interpolate_vec3(shr1->ao, shr2->ao, t, negt, shr->ao);
237 if (addpassflag & SCE_PASS_ENVIRONMENT)
238 interpolate_vec3(shr1->env, shr2->env, t, negt, shr->env);
239 if (addpassflag & SCE_PASS_INDIRECT)
240 interpolate_vec3(shr1->indirect, shr2->indirect, t, negt, shr->indirect);
241 if (addpassflag & SCE_PASS_REFLECT)
242 interpolate_vec3(shr1->refl, shr2->refl, t, negt, shr->refl);
243 if (addpassflag & SCE_PASS_REFRACT)
244 interpolate_vec3(shr1->refr, shr2->refr, t, negt, shr->refr);
245 if (addpassflag & SCE_PASS_MIST)
246 interpolate_vec1(&shr1->mist, &shr2->mist, t, negt, &shr->mist);
250 static void strand_apply_shaderesult_alpha(ShadeResult *shr, float alpha)
253 shr->combined[0] *= alpha;
254 shr->combined[1] *= alpha;
255 shr->combined[2] *= alpha;
256 shr->combined[3] *= alpha;
258 shr->col[0] *= alpha;
259 shr->col[1] *= alpha;
260 shr->col[2] *= alpha;
261 shr->col[3] *= alpha;
267 static void strand_shade_point(Render *re, ShadeSample *ssamp, StrandSegment *sseg, StrandVert *svert, StrandPoint *spoint)
269 ShadeInput *shi= ssamp->shi;
270 ShadeResult *shr= ssamp->shr;
274 memset(&vlr, 0, sizeof(vlr));
276 if (sseg->buffer->ma->mode & MA_TANGENT_STR)
277 vlr.flag |= R_TANGENT;
283 shi->strand= sseg->strand;
285 shi->obr= sseg->obi->obr;
287 /* cache for shadow */
288 shi->samplenr= re->shadowsamplenr[shi->thread]++;
293 /* seed RNG for consistent results across tiles */
294 seed = shi->strand->index + (svert - shi->strand->vert);
295 BLI_thread_srandom(shi->thread, seed);
297 shade_input_set_strand(shi, sseg->strand, spoint);
298 shade_input_set_strand_texco(shi, sseg->strand, sseg->v[1], spoint);
300 /* init material vars */
301 shade_input_init_material(shi);
304 shade_samples_do_AO(ssamp);
305 shade_input_do_shade(shi, shr);
307 /* apply simplification */
308 strand_apply_shaderesult_alpha(shr, spoint->alpha);
310 /* include lamphalos for strand, since halo layer was added already */
311 if (re->flag & R_LAMPHALO)
312 if (shi->layflag & SCE_LAY_HALO)
313 renderspothalo(shi, shr->combined, shr->combined[3]);
318 /* *************** */
320 struct StrandShadeCache {
326 typedef struct StrandCacheEntry {
331 StrandShadeCache *strand_shade_cache_create(void)
333 StrandShadeCache *cache;
335 cache= MEM_callocN(sizeof(StrandShadeCache), "StrandShadeCache");
336 cache->resulthash= BLI_ghash_pair_new("strand_shade_cache_create1 gh");
337 cache->refcounthash= BLI_ghash_pair_new("strand_shade_cache_create2 gh");
338 cache->memarena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "strand shade cache arena");
343 void strand_shade_cache_free(StrandShadeCache *cache)
345 BLI_ghash_free(cache->refcounthash, NULL, NULL);
346 BLI_ghash_free(cache->resulthash, (GHashKeyFreeFP)MEM_freeN, NULL);
347 BLI_memarena_free(cache->memarena);
351 static GHashPair strand_shade_hash_pair(ObjectInstanceRen *obi, StrandVert *svert)
353 GHashPair pair = {obi, svert};
357 static void strand_shade_get(Render *re, StrandShadeCache *cache, ShadeSample *ssamp, StrandSegment *sseg, StrandVert *svert)
359 StrandCacheEntry *entry;
362 GHashPair pair = strand_shade_hash_pair(sseg->obi, svert);
364 entry= BLI_ghash_lookup(cache->resulthash, &pair);
365 refcount= BLI_ghash_lookup(cache->refcounthash, &pair);
368 /* not shaded yet, shade and insert into hash */
369 p.t= (sseg->v[1] == svert)? 0.0f: 1.0f;
370 strand_eval_point(sseg, &p);
371 strand_shade_point(re, ssamp, sseg, svert, &p);
373 entry= MEM_callocN(sizeof(StrandCacheEntry), "StrandCacheEntry");
375 entry->shr = ssamp->shr[0];
376 BLI_ghash_insert(cache->resulthash, entry, entry);
379 /* already shaded, just copy previous result from hash */
380 ssamp->shr[0]= entry->shr;
382 /* lower reference count and remove if not needed anymore by any samples */
384 if (*refcount == 0) {
385 BLI_ghash_remove(cache->resulthash, &pair, (GHashKeyFreeFP)MEM_freeN, NULL);
386 BLI_ghash_remove(cache->refcounthash, &pair, NULL, NULL);
390 void strand_shade_segment(Render *re, StrandShadeCache *cache, StrandSegment *sseg, ShadeSample *ssamp, float t, float s, int addpassflag)
392 ShadeResult shr1, shr2;
394 /* get shading for two endpoints and interpolate */
395 strand_shade_get(re, cache, ssamp, sseg, sseg->v[1]);
397 strand_shade_get(re, cache, ssamp, sseg, sseg->v[2]);
400 interpolate_shade_result(&shr1, &shr2, t, ssamp->shr, addpassflag);
402 /* apply alpha along width */
403 if (sseg->buffer->widthfade != -1.0f) {
404 s = 1.0f - powf(fabsf(s), sseg->buffer->widthfade);
406 strand_apply_shaderesult_alpha(ssamp->shr, s);
410 void strand_shade_unref(StrandShadeCache *cache, ObjectInstanceRen *obi, StrandVert *svert)
412 GHashPair pair = strand_shade_hash_pair(obi, svert);
415 /* lower reference count and remove if not needed anymore by any samples */
416 refcount= BLI_ghash_lookup(cache->refcounthash, &pair);
419 if (*refcount == 0) {
420 BLI_ghash_remove(cache->resulthash, &pair, (GHashKeyFreeFP)MEM_freeN, NULL);
421 BLI_ghash_remove(cache->refcounthash, &pair, NULL, NULL);
425 static void strand_shade_refcount(StrandShadeCache *cache, StrandSegment *sseg, StrandVert *svert)
427 GHashPair pair = strand_shade_hash_pair(sseg->obi, svert);
429 int *refcount= BLI_ghash_lookup(cache->refcounthash, &pair);
432 key= BLI_memarena_alloc(cache->memarena, sizeof(GHashPair));
434 refcount= BLI_memarena_alloc(cache->memarena, sizeof(int));
436 BLI_ghash_insert(cache->refcounthash, key, refcount);
442 /* *************** */
444 typedef struct StrandPart {
458 StrandSegment *segment;
461 StrandShadeCache *cache;
464 typedef struct StrandSortSegment {
465 struct StrandSortSegment *next;
466 int obi, strand, segment;
470 static int compare_strand_segment(const void *poin1, const void *poin2)
472 const StrandSortSegment *seg1= (const StrandSortSegment*)poin1;
473 const StrandSortSegment *seg2= (const StrandSortSegment*)poin2;
475 if (seg1->z < seg2->z)
477 else if (seg1->z == seg2->z)
483 static void do_strand_point_project(float winmat[][4], ZSpan *zspan, float *co, float *hoco, float *zco)
485 projectvert(co, winmat, hoco);
486 hoco_to_zco(zspan, zco, hoco);
489 static void strand_project_point(float winmat[][4], float winx, float winy, StrandPoint *spoint)
493 projectvert(spoint->co, winmat, spoint->hoco);
495 div= 1.0f/spoint->hoco[3];
496 spoint->x= spoint->hoco[0]*div*winx*0.5f;
497 spoint->y= spoint->hoco[1]*div*winy*0.5f;
500 static APixstrand *addpsmainAstrand(ListBase *lb)
504 psm= MEM_mallocN(sizeof(APixstrMain), "addpsmainA");
505 BLI_addtail(lb, psm);
506 psm->ps = MEM_callocN(4096 * sizeof(APixstrand), "pixstr");
511 static APixstrand *addpsAstrand(ZSpan *zspan)
514 if (zspan->apstrandmcounter==0) {
515 zspan->curpstrand= addpsmainAstrand(zspan->apsmbase);
516 zspan->apstrandmcounter= 4095;
520 zspan->apstrandmcounter--;
522 return zspan->curpstrand;
525 #define MAX_ZROW 2000
527 static void do_strand_fillac(void *handle, int x, int y, float u, float v, float z)
529 StrandPart *spart= (StrandPart*)handle;
530 StrandShadeCache *cache= spart->cache;
531 StrandSegment *sseg= spart->segment;
532 APixstrand *apn, *apnew;
534 int offset, mask, obi, strnr, seg, zverg, bufferz, maskz=0;
536 offset = y*spart->rectx + x;
537 obi= sseg->obi - spart->re->objectinstance;
538 strnr= sseg->strand->index + 1;
539 seg= sseg->v[1] - sseg->strand->vert;
540 mask= (1<<spart->sample);
542 /* check against solid z-buffer */
545 if (spart->rectdaps) {
546 /* find the z of the sample */
548 intptr_t *rd= spart->rectdaps + offset;
551 if (spart->rectmask) maskz= 0x7FFFFFFF;
554 for (ps= (PixStr *)(*rd); ps; ps= ps->next) {
555 if (mask & ps->mask) {
565 bufferz= (spart->rectz)? spart->rectz[offset]: 0x7FFFFFFF;
567 maskz= spart->rectmask[offset];
570 #define CHECK_ADD(n) \
571 if (apn->p[n]==strnr && apn->obi[n]==obi && apn->seg[n]==seg) \
572 { if (!(apn->mask[n] & mask)) { apn->mask[n] |= mask; apn->v[n] += t; apn->u[n] += s; } break; } (void)0
573 #define CHECK_ASSIGN(n) \
575 {apn->obi[n]= obi; apn->p[n]= strnr; apn->z[n]= zverg; apn->mask[n]= mask; apn->v[n]= t; apn->u[n]= s; apn->seg[n]= seg; break; } (void)0
577 /* add to pixel list */
578 if (zverg < bufferz && (spart->totapixbuf[offset] < MAX_ZROW)) {
579 if (!spart->rectmask || zverg > maskz) {
580 t = u*spart->t[0] + v*spart->t[1] + (1.0f-u-v)*spart->t[2];
581 s = fabs(u*spart->s[0] + v*spart->s[1] + (1.0f-u-v)*spart->s[2]);
583 apn= spart->apixbuf + offset;
594 apnew= addpsAstrand(spart->zspan);
595 SWAP(APixstrand, *apnew, *apn);
601 strand_shade_refcount(cache, sseg, sseg->v[1]);
602 strand_shade_refcount(cache, sseg, sseg->v[2]);
604 spart->totapixbuf[offset]++;
609 /* width is calculated in hoco space, to ensure strands are visible */
610 static int strand_test_clip(float winmat[][4], ZSpan *UNUSED(zspan), float *bounds, float *co, float *zcomp, float widthx, float widthy)
615 projectvert(co, winmat, hoco);
617 /* we compare z without perspective division for segment sorting */
620 if (hoco[0]+widthx < bounds[0]*hoco[3]) clipflag |= 1;
621 else if (hoco[0]-widthx > bounds[1]*hoco[3]) clipflag |= 2;
623 if (hoco[1]-widthy > bounds[3]*hoco[3]) clipflag |= 4;
624 else if (hoco[1]+widthy < bounds[2]*hoco[3]) clipflag |= 8;
626 clipflag |= testclip(hoco);
631 static void do_scanconvert_strand(Render *UNUSED(re), StrandPart *spart, ZSpan *zspan, float t, float dt, float *co1, float *co2, float *co3, float *co4, int sample)
633 float jco1[3], jco2[3], jco3[3], jco4[3], jx, jy;
635 copy_v3_v3(jco1, co1);
636 copy_v3_v3(jco2, co2);
637 copy_v3_v3(jco3, co3);
638 copy_v3_v3(jco4, co4);
641 jx= -spart->jit[sample][0];
642 jy= -spart->jit[sample][1];
644 jco1[0] += jx; jco1[1] += jy;
645 jco2[0] += jx; jco2[1] += jy;
646 jco3[0] += jx; jco3[1] += jy;
647 jco4[0] += jx; jco4[1] += jy;
652 spart->sample= sample;
660 zspan_scanconvert_strand(zspan, spart, jco1, jco2, jco3, do_strand_fillac);
667 zspan_scanconvert_strand(zspan, spart, jco1, jco3, jco4, do_strand_fillac);
670 static void strand_render(Render *re, StrandSegment *sseg, float winmat[][4], StrandPart *spart, ZSpan *zspan, int totzspan, StrandPoint *p1, StrandPoint *p2)
674 float dt= p2->t - p1->t;
678 for (a=0; a<re->osa; a++)
679 do_scanconvert_strand(re, spart, zspan, t, dt, p1->zco2, p1->zco1, p2->zco1, p2->zco2, a);
682 do_scanconvert_strand(re, spart, zspan, t, dt, p1->zco2, p1->zco1, p2->zco1, p2->zco2, 0);
685 float hoco1[4], hoco2[4];
688 obi= sseg->obi - re->objectinstance;
689 index= sseg->strand->index;
691 projectvert(p1->co, winmat, hoco1);
692 projectvert(p2->co, winmat, hoco2);
695 for (a=0; a<totzspan; a++) {
697 /* render both strand and single pixel wire to counter aliasing */
698 zbufclip4(re, &zspan[a], obi, index, p1->hoco2, p1->hoco1, p2->hoco1, p2->hoco2, p1->clip2, p1->clip1, p2->clip1, p2->clip2);
700 /* only render a line for now, which makes the shadow map more
701 * similar across frames, and so reduces flicker */
702 zbufsinglewire(&zspan[a], obi, index, hoco1, hoco2);
707 static int strand_segment_recursive(Render *re, float winmat[][4], StrandPart *spart, ZSpan *zspan, int totzspan, StrandSegment *sseg, StrandPoint *p1, StrandPoint *p2, int depth)
710 StrandBuffer *buffer= sseg->buffer;
711 float dot, d1[2], d2[2], len1, len2;
713 if (depth == buffer->maxdepth)
716 p.t= (p1->t + p2->t)*0.5f;
717 strand_eval_point(sseg, &p);
718 strand_project_point(buffer->winmat, buffer->winx, buffer->winy, &p);
720 d1[0]= (p.x - p1->x);
721 d1[1]= (p.y - p1->y);
722 len1= d1[0]*d1[0] + d1[1]*d1[1];
724 d2[0]= (p2->x - p.x);
725 d2[1]= (p2->y - p.y);
726 len2= d2[0]*d2[0] + d2[1]*d2[1];
728 if (len1 == 0.0f || len2 == 0.0f)
731 dot= d1[0]*d2[0] + d1[1]*d2[1];
732 if (dot*dot > sseg->sqadaptcos*len1*len2)
736 do_strand_point_project(winmat, zspan, p.co1, p.hoco1, p.zco1);
737 do_strand_point_project(winmat, zspan, p.co2, p.hoco2, p.zco2);
741 projectvert(p.co1, winmat, p.hoco1);
742 projectvert(p.co2, winmat, p.hoco2);
743 p.clip1= testclip(p.hoco1);
744 p.clip2= testclip(p.hoco2);
748 if (!strand_segment_recursive(re, winmat, spart, zspan, totzspan, sseg, p1, &p, depth+1))
749 strand_render(re, sseg, winmat, spart, zspan, totzspan, p1, &p);
750 if (!strand_segment_recursive(re, winmat, spart, zspan, totzspan, sseg, &p, p2, depth+1))
751 strand_render(re, sseg, winmat, spart, zspan, totzspan, &p, p2);
756 void render_strand_segment(Render *re, float winmat[][4], StrandPart *spart, ZSpan *zspan, int totzspan, StrandSegment *sseg)
758 StrandBuffer *buffer= sseg->buffer;
759 StrandPoint *p1= &sseg->point1;
760 StrandPoint *p2= &sseg->point2;
765 strand_eval_point(sseg, p1);
766 strand_project_point(buffer->winmat, buffer->winx, buffer->winy, p1);
767 strand_eval_point(sseg, p2);
768 strand_project_point(buffer->winmat, buffer->winx, buffer->winy, p2);
771 do_strand_point_project(winmat, zspan, p1->co1, p1->hoco1, p1->zco1);
772 do_strand_point_project(winmat, zspan, p1->co2, p1->hoco2, p1->zco2);
773 do_strand_point_project(winmat, zspan, p2->co1, p2->hoco1, p2->zco1);
774 do_strand_point_project(winmat, zspan, p2->co2, p2->hoco2, p2->zco2);
778 projectvert(p1->co1, winmat, p1->hoco1);
779 projectvert(p1->co2, winmat, p1->hoco2);
780 projectvert(p2->co1, winmat, p2->hoco1);
781 projectvert(p2->co2, winmat, p2->hoco2);
782 p1->clip1= testclip(p1->hoco1);
783 p1->clip2= testclip(p1->hoco2);
784 p2->clip1= testclip(p2->hoco1);
785 p2->clip2= testclip(p2->hoco2);
789 if (!strand_segment_recursive(re, winmat, spart, zspan, totzspan, sseg, p1, p2, 0))
790 strand_render(re, sseg, winmat, spart, zspan, totzspan, p1, p2);
793 /* render call to fill in strands */
794 int zbuffer_strands_abuf(Render *re, RenderPart *pa, APixstrand *apixbuf, ListBase *apsmbase, unsigned int lay, int UNUSED(negzmask), float winmat[][4], int winx, int winy, int UNUSED(sample), float (*jit)[2], float clipcrop, int shadow, StrandShadeCache *cache)
797 ObjectInstanceRen *obi;
804 StrandSortSegment *sortsegments = NULL, *sortseg, *firstseg;
806 float z[4], bounds[4], obwinmat[4][4];
807 int a, b, c, i, totsegment, clip[4];
809 if (re->test_break(re->tbh))
811 if (re->totstrand == 0)
814 /* setup StrandPart */
815 memset(&spart, 0, sizeof(spart));
818 spart.rectx= pa->rectx;
819 spart.recty= pa->recty;
820 spart.apixbuf= apixbuf;
822 spart.rectdaps= pa->rectdaps;
823 spart.rectz= pa->rectz;
824 spart.rectmask= pa->rectmask;
826 spart.shadow= shadow;
829 zbuf_alloc_span(&zspan, pa->rectx, pa->recty, clipcrop);
831 /* needed for transform from hoco to zbuffer co */
832 zspan.zmulx= ((float)winx)/2.0f;
833 zspan.zmuly= ((float)winy)/2.0f;
835 zspan.zofsx= -pa->disprect.xmin;
836 zspan.zofsy= -pa->disprect.ymin;
838 /* to center the sample position */
844 zspan.apsmbase= apsmbase;
847 bounds[0]= (2*pa->disprect.xmin - winx-1)/(float)winx;
848 bounds[1]= (2*pa->disprect.xmax - winx+1)/(float)winx;
849 bounds[2]= (2*pa->disprect.ymin - winy-1)/(float)winy;
850 bounds[3]= (2*pa->disprect.ymax - winy+1)/(float)winy;
852 memarena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "strand sort arena");
856 /* for all object instances */
857 for (obi=re->instancetable.first, i=0; obi; obi=obi->next, i++) {
859 float widthx, widthy;
863 if (!obr->strandbuf || !(obr->strandbuf->lay & lay))
866 /* compute matrix and try clipping whole object */
867 if (obi->flag & R_TRANSFORMED)
868 mult_m4_m4m4(obwinmat, winmat, obi->mat);
870 copy_m4_m4(obwinmat, winmat);
872 /* test if we should skip it */
873 ma = obr->strandbuf->ma;
875 if (shadow && !(ma->mode & MA_SHADBUF))
877 else if (!shadow && (ma->mode & MA_ONLYCAST))
880 if (clip_render_object(obi->obr->boundbox, bounds, obwinmat))
883 widthx= obr->strandbuf->maxwidth*obwinmat[0][0];
884 widthy= obr->strandbuf->maxwidth*obwinmat[1][1];
886 /* for each bounding box containing a number of strands */
887 sbound= obr->strandbuf->bound;
888 for (c=0; c<obr->strandbuf->totbound; c++, sbound++) {
889 if (clip_render_object(sbound->boundbox, bounds, obwinmat))
892 /* for each strand in this bounding box */
893 for (a=sbound->start; a<sbound->end; a++) {
894 strand= RE_findOrAddStrand(obr, a);
897 /* keep clipping and z depth for 4 control points */
898 clip[1]= strand_test_clip(obwinmat, &zspan, bounds, svert->co, &z[1], widthx, widthy);
899 clip[2]= strand_test_clip(obwinmat, &zspan, bounds, (svert+1)->co, &z[2], widthx, widthy);
900 clip[0]= clip[1]; z[0]= z[1];
902 for (b=0; b<strand->totvert-1; b++, svert++) {
903 /* compute 4th point clipping and z depth */
904 if (b < strand->totvert-2) {
905 clip[3]= strand_test_clip(obwinmat, &zspan, bounds, (svert+2)->co, &z[3], widthx, widthy);
908 clip[3]= clip[2]; z[3]= z[2];
911 /* check clipping and add to sortsegments buffer */
912 if (!(clip[0] & clip[1] & clip[2] & clip[3])) {
913 sortseg= BLI_memarena_alloc(memarena, sizeof(StrandSortSegment));
915 sortseg->strand= strand->index;
918 sortseg->z= 0.5f*(z[1] + z[2]);
920 sortseg->next= firstseg;
925 /* shift clipping and z depth */
926 clip[0]= clip[1]; z[0]= z[1];
927 clip[1]= clip[2]; z[1]= z[2];
928 clip[2]= clip[3]; z[2]= z[3];
934 if (!re->test_break(re->tbh)) {
935 /* convert list to array and sort */
936 sortsegments= MEM_mallocN(sizeof(StrandSortSegment)*totsegment, "StrandSortSegment");
937 for (a=0, sortseg=firstseg; a<totsegment; a++, sortseg=sortseg->next)
938 sortsegments[a]= *sortseg;
939 qsort(sortsegments, totsegment, sizeof(StrandSortSegment), compare_strand_segment);
942 BLI_memarena_free(memarena);
944 spart.totapixbuf= MEM_callocN(sizeof(int)*pa->rectx*pa->recty, "totapixbuf");
946 if (!re->test_break(re->tbh)) {
947 /* render segments in sorted order */
948 sortseg= sortsegments;
949 for (a=0; a<totsegment; a++, sortseg++) {
950 if (re->test_break(re->tbh))
953 obi= &re->objectinstance[sortseg->obi];
957 sseg.strand= RE_findOrAddStrand(obr, sortseg->strand);
958 sseg.buffer= sseg.strand->buffer;
959 sseg.sqadaptcos= sseg.buffer->adaptcos;
960 sseg.sqadaptcos *= sseg.sqadaptcos;
962 svert= sseg.strand->vert + sortseg->segment;
963 sseg.v[0]= (sortseg->segment > 0)? (svert-1): svert;
966 sseg.v[3]= (sortseg->segment < sseg.strand->totvert-2)? svert+2: svert+1;
969 spart.segment= &sseg;
971 render_strand_segment(re, winmat, &spart, &zspan, 1, &sseg);
976 MEM_freeN(sortsegments);
977 MEM_freeN(spart.totapixbuf);
979 zbuf_free_span(&zspan);
984 /* *************** */
986 StrandSurface *cache_strand_surface(Render *re, ObjectRen *obr, DerivedMesh *dm, float mat[][4], int timeoffset)
992 int a, totvert, totface;
994 totvert= dm->getNumVerts(dm);
995 totface= dm->getNumTessFaces(dm);
997 for (mesh = re->strandsurface.first; mesh; mesh = mesh->next) {
998 if ((mesh->obr.ob == obr->ob) &&
999 (mesh->obr.par == obr->par) &&
1000 (mesh->obr.index == obr->index) &&
1001 (mesh->totvert == totvert) &&
1002 (mesh->totface == totface))
1009 mesh= MEM_callocN(sizeof(StrandSurface), "StrandSurface");
1011 mesh->totvert= totvert;
1012 mesh->totface= totface;
1013 mesh->face= MEM_callocN(sizeof(int)*4*mesh->totface, "StrandSurfFaces");
1014 mesh->ao= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfAO");
1015 mesh->env= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfEnv");
1016 mesh->indirect= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfIndirect");
1017 BLI_addtail(&re->strandsurface, mesh);
1020 if (timeoffset == -1 && !mesh->prevco)
1021 mesh->prevco= co= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfCo");
1022 else if (timeoffset == 0 && !mesh->co)
1023 mesh->co= co= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfCo");
1024 else if (timeoffset == 1 && !mesh->nextco)
1025 mesh->nextco= co= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfCo");
1029 mvert= dm->getVertArray(dm);
1030 for (a=0; a<mesh->totvert; a++, mvert++) {
1031 copy_v3_v3(co[a], mvert->co);
1032 mul_m4_v3(mat, co[a]);
1035 mface= dm->getTessFaceArray(dm);
1036 for (a=0; a<mesh->totface; a++, mface++) {
1037 mesh->face[a][0]= mface->v1;
1038 mesh->face[a][1]= mface->v2;
1039 mesh->face[a][2]= mface->v3;
1040 mesh->face[a][3]= mface->v4;
1046 void free_strand_surface(Render *re)
1048 StrandSurface *mesh;
1050 for (mesh=re->strandsurface.first; mesh; mesh=mesh->next) {
1051 if (mesh->co) MEM_freeN(mesh->co);
1052 if (mesh->prevco) MEM_freeN(mesh->prevco);
1053 if (mesh->nextco) MEM_freeN(mesh->nextco);
1054 if (mesh->ao) MEM_freeN(mesh->ao);
1055 if (mesh->env) MEM_freeN(mesh->env);
1056 if (mesh->indirect) MEM_freeN(mesh->indirect);
1057 if (mesh->face) MEM_freeN(mesh->face);
1060 BLI_freelistN(&re->strandsurface);
1063 void strand_minmax(StrandRen *strand, float min[3], float max[3], const float width)
1066 const float width2 = width * 2.0f;
1070 for (a=0, svert=strand->vert; a<strand->totvert; a++, svert++) {
1071 copy_v3_v3(vec, svert->co);
1072 minmax_v3v3_v3(min, max, vec);
1075 add_v3_fl(vec, width);
1076 minmax_v3v3_v3(min, max, vec);
1077 add_v3_fl(vec, -width2);
1078 minmax_v3v3_v3(min, max, vec);