3c8c1640b7585a1d4347a3e82e29d1867df84bf7
[blender.git] / source / blender / render / intern / source / strand.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: none of this file.
24  *
25  * Contributors: Brecht Van Lommel.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_key_types.h"
37 #include "DNA_material_types.h"
38 #include "DNA_meshdata_types.h"
39
40 #include "BLI_arithb.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_ghash.h"
43 #include "BLI_memarena.h"
44
45 #include "BKE_DerivedMesh.h"
46 #include "BKE_key.h"
47 #include "BKE_utildefines.h"
48
49 #include "render_types.h"
50 #include "initrender.h"
51 #include "rendercore.h"
52 #include "renderdatabase.h"
53 #include "renderpipeline.h"
54 #include "pixelblending.h"
55 #include "shading.h"
56 #include "strand.h"
57 #include "zbuf.h"
58
59 /* to be removed */
60 void hoco_to_zco(ZSpan *zspan, float *zco, float *hoco);
61 void zspan_scanconvert_strand(ZSpan *zspan, void *handle, float *v1, float *v2, float *v3, void (*func)(void *, int, int, float, float, float) );
62 void zbufsinglewire(ZSpan *zspan, int obi, int zvlnr, float *ho1, float *ho2);
63
64 /* *************** */
65
66 static float strand_eval_width(Material *ma, float strandco)
67 {
68         float fac;
69
70         strandco= 0.5f*(strandco + 1.0f);
71
72         if(ma->strand_ease!=0.0f) {
73                 if(ma->strand_ease<0.0f)
74                         fac= pow(strandco, 1.0+ma->strand_ease);
75                 else
76                         fac= pow(strandco, 1.0/(1.0f-ma->strand_ease));
77         }
78         else fac= strandco;
79         
80         return ((1.0f-fac)*ma->strand_sta + (fac)*ma->strand_end);
81 }
82
83 void strand_eval_point(StrandSegment *sseg, StrandPoint *spoint)
84 {
85         Material *ma;
86         StrandBuffer *strandbuf;
87         float *simplify;
88         float p[4][3], data[4], cross[3], crosslen, w, dx, dy, t;
89         int type;
90
91         strandbuf= sseg->buffer;
92         ma= sseg->buffer->ma;
93         t= spoint->t;
94         type= (strandbuf->flag & R_STRAND_BSPLINE)? KEY_BSPLINE: KEY_CARDINAL;
95
96         VECCOPY(p[0], sseg->v[0]->co);
97         VECCOPY(p[1], sseg->v[1]->co);
98         VECCOPY(p[2], sseg->v[2]->co);
99         VECCOPY(p[3], sseg->v[3]->co);
100
101         if(sseg->obi->flag & R_TRANSFORMED) {
102                 Mat4MulVecfl(sseg->obi->mat, p[0]);
103                 Mat4MulVecfl(sseg->obi->mat, p[1]);
104                 Mat4MulVecfl(sseg->obi->mat, p[2]);
105                 Mat4MulVecfl(sseg->obi->mat, p[3]);
106         }
107
108         if(t == 0.0f) {
109                 VECCOPY(spoint->co, p[1]);
110                 spoint->strandco= sseg->v[1]->strandco;
111
112                 spoint->dtstrandco= (sseg->v[2]->strandco - sseg->v[0]->strandco);
113                 if(sseg->v[0] != sseg->v[1])
114                         spoint->dtstrandco *= 0.5f;
115         }
116         else if(t == 1.0f) {
117                 VECCOPY(spoint->co, p[2]);
118                 spoint->strandco= sseg->v[2]->strandco;
119
120                 spoint->dtstrandco= (sseg->v[3]->strandco - sseg->v[1]->strandco);
121                 if(sseg->v[3] != sseg->v[2])
122                         spoint->dtstrandco *= 0.5f;
123         }
124         else {
125                 set_four_ipo(t, data, type);
126                 spoint->co[0]= data[0]*p[0][0] + data[1]*p[1][0] + data[2]*p[2][0] + data[3]*p[3][0];
127                 spoint->co[1]= data[0]*p[0][1] + data[1]*p[1][1] + data[2]*p[2][1] + data[3]*p[3][1];
128                 spoint->co[2]= data[0]*p[0][2] + data[1]*p[1][2] + data[2]*p[2][2] + data[3]*p[3][2];
129                 spoint->strandco= (1.0f-t)*sseg->v[1]->strandco + t*sseg->v[2]->strandco;
130         }
131
132         set_afgeleide_four_ipo(t, data, type);
133         spoint->dtco[0]= data[0]*p[0][0] + data[1]*p[1][0] + data[2]*p[2][0] + data[3]*p[3][0];
134         spoint->dtco[1]= data[0]*p[0][1] + data[1]*p[1][1] + data[2]*p[2][1] + data[3]*p[3][1];
135         spoint->dtco[2]= data[0]*p[0][2] + data[1]*p[1][2] + data[2]*p[2][2] + data[3]*p[3][2];
136
137         VECCOPY(spoint->tan, spoint->dtco);
138         Normalize(spoint->tan);
139
140         VECCOPY(spoint->nor, spoint->co);
141         VECMUL(spoint->nor, -1.0f);
142         Normalize(spoint->nor);
143
144         spoint->width= strand_eval_width(ma, spoint->strandco);
145         
146         /* simplification */
147         simplify= RE_strandren_get_simplify(strandbuf->obr, sseg->strand, 0);
148         spoint->alpha= (simplify)? simplify[1]: 1.0f;
149
150         /* outer points */
151         Crossf(cross, spoint->co, spoint->tan);
152
153         w= spoint->co[2]*strandbuf->winmat[2][3] + strandbuf->winmat[3][3];
154         dx= strandbuf->winx*cross[0]*strandbuf->winmat[0][0]/w;
155         dy= strandbuf->winy*cross[1]*strandbuf->winmat[1][1]/w;
156         w= sqrt(dx*dx + dy*dy);
157
158         if(w > 0.0f) {
159                 if(strandbuf->flag & R_STRAND_B_UNITS) {
160                         crosslen= VecLength(cross);
161                         w= 2.0f*crosslen*strandbuf->minwidth/w;
162
163                         if(spoint->width < w) {
164                                 spoint->alpha= spoint->width/w;
165                                 spoint->width= w;
166                         }
167
168                         if(simplify)
169                                 /* squared because we only change width, not length */
170                                 spoint->width *= simplify[0]*simplify[0];
171
172                         VecMulf(cross, spoint->width*0.5f/crosslen);
173                 }
174                 else
175                         VecMulf(cross, spoint->width/w);
176         }
177
178         VecSubf(spoint->co1, spoint->co, cross);
179         VecAddf(spoint->co2, spoint->co, cross);
180
181         VECCOPY(spoint->dsco, cross);
182 }
183
184 /* *************** */
185
186 static void interpolate_vec1(float *v1, float *v2, float t, float negt, float *v)
187 {
188         v[0]= negt*v1[0] + t*v2[0];
189 }
190
191 static void interpolate_vec3(float *v1, float *v2, float t, float negt, float *v)
192 {
193         v[0]= negt*v1[0] + t*v2[0];
194         v[1]= negt*v1[1] + t*v2[1];
195         v[2]= negt*v1[2] + t*v2[2];
196 }
197
198 static void interpolate_vec4(float *v1, float *v2, float t, float negt, float *v)
199 {
200         v[0]= negt*v1[0] + t*v2[0];
201         v[1]= negt*v1[1] + t*v2[1];
202         v[2]= negt*v1[2] + t*v2[2];
203         v[3]= negt*v1[3] + t*v2[3];
204 }
205
206 void interpolate_shade_result(ShadeResult *shr1, ShadeResult *shr2, float t, ShadeResult *shr, int addpassflag)
207 {
208         float negt= 1.0f - t;
209
210         interpolate_vec4(shr1->combined, shr2->combined, t, negt, shr->combined);
211
212         if(addpassflag & SCE_PASS_VECTOR) {
213                 interpolate_vec4(shr1->winspeed, shr2->winspeed, t, negt, shr->winspeed);
214         }
215         /* optim... */
216         if(addpassflag & ~(SCE_PASS_VECTOR)) {
217                 if(addpassflag & SCE_PASS_Z)
218                         interpolate_vec1(&shr1->z, &shr2->z, t, negt, &shr->z);
219                 if(addpassflag & SCE_PASS_RGBA)
220                         interpolate_vec4(shr1->col, shr2->col, t, negt, shr->col);
221                 if(addpassflag & SCE_PASS_NORMAL) {
222                         interpolate_vec3(shr1->nor, shr2->nor, t, negt, shr->nor);
223                         Normalize(shr->nor);
224                 }
225                 if(addpassflag & SCE_PASS_DIFFUSE)
226                         interpolate_vec3(shr1->diff, shr2->diff, t, negt, shr->diff);
227                 if(addpassflag & SCE_PASS_SPEC)
228                         interpolate_vec3(shr1->spec, shr2->spec, t, negt, shr->spec);
229                 if(addpassflag & SCE_PASS_SHADOW)
230                         interpolate_vec3(shr1->shad, shr2->shad, t, negt, shr->shad);
231                 if(addpassflag & SCE_PASS_AO)
232                         interpolate_vec3(shr1->ao, shr2->ao, t, negt, shr->ao);
233                 if(addpassflag & SCE_PASS_REFLECT)
234                         interpolate_vec3(shr1->refl, shr2->refl, t, negt, shr->refl);
235                 if(addpassflag & SCE_PASS_REFRACT)
236                         interpolate_vec3(shr1->refr, shr2->refr, t, negt, shr->refr);
237                 if(addpassflag & SCE_PASS_RADIO)
238                         interpolate_vec3(shr1->rad, shr2->rad, t, negt, shr->rad);
239                 if(addpassflag & SCE_PASS_MIST)
240                         interpolate_vec1(&shr1->mist, &shr2->mist, t, negt, &shr->mist);
241         }
242 }
243
244 void strand_apply_shaderesult_alpha(ShadeResult *shr, float alpha)
245 {
246         if(alpha < 1.0f) {
247                 shr->combined[0] *= alpha;
248                 shr->combined[1] *= alpha;
249                 shr->combined[2] *= alpha;
250                 shr->combined[3] *= alpha;
251
252                 shr->col[0] *= alpha;
253                 shr->col[1] *= alpha;
254                 shr->col[2] *= alpha;
255                 shr->col[3] *= alpha;
256
257                 shr->alpha *= alpha;
258         }
259 }
260
261 void strand_shade_point(Render *re, ShadeSample *ssamp, StrandSegment *sseg, StrandPoint *spoint)
262 {
263         ShadeInput *shi= ssamp->shi;
264         ShadeResult *shr= ssamp->shr;
265         VlakRen vlr;
266
267         memset(&vlr, 0, sizeof(vlr));
268         vlr.flag= R_SMOOTH;
269         if(sseg->buffer->ma->mode & MA_TANGENT_STR)
270                 vlr.flag |= R_TANGENT;
271
272         shi->vlr= &vlr;
273         shi->strand= sseg->strand;
274         shi->obi= sseg->obi;
275         shi->obr= sseg->obi->obr;
276
277         /* cache for shadow */
278         shi->samplenr= re->shadowsamplenr[shi->thread]++;
279
280         shade_input_set_strand(shi, sseg->strand, spoint);
281         shade_input_set_strand_texco(shi, sseg->strand, sseg->v[1], spoint);
282         
283         /* init material vars */
284         // note, keep this synced with render_types.h
285         memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
286         shi->har= shi->mat->har;
287         
288         /* shade */
289         shade_samples_do_AO(ssamp);
290         shade_input_do_shade(shi, shr);
291
292         /* apply simplification */
293         strand_apply_shaderesult_alpha(shr, spoint->alpha);
294
295         /* include lamphalos for strand, since halo layer was added already */
296         if(re->flag & R_LAMPHALO)
297                 if(shi->layflag & SCE_LAY_HALO)
298                         renderspothalo(shi, shr->combined, shr->combined[3]);
299         
300         shi->strand= NULL;
301 }
302
303 /* *************** */
304
305 struct StrandShadeCache {
306         GHash *resulthash;
307         GHash *refcounthash;
308         MemArena *memarena;
309 };
310
311 StrandShadeCache *strand_shade_cache_create()
312 {
313         StrandShadeCache *cache;
314
315         cache= MEM_callocN(sizeof(StrandShadeCache), "StrandShadeCache");
316         cache->resulthash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
317         cache->refcounthash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
318         cache->memarena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
319         
320         return cache;
321 }
322
323 void strand_shade_cache_free(StrandShadeCache *cache)
324 {
325         BLI_ghash_free(cache->refcounthash, NULL, NULL);
326         BLI_ghash_free(cache->resulthash, NULL, (GHashValFreeFP)MEM_freeN);
327         BLI_memarena_free(cache->memarena);
328         MEM_freeN(cache);
329 }
330
331 static void strand_shade_get(Render *re, StrandShadeCache *cache, ShadeSample *ssamp, StrandSegment *sseg, StrandVert *svert)
332 {
333         ShadeResult *hashshr;
334         StrandPoint p;
335         int *refcount;
336
337         hashshr= BLI_ghash_lookup(cache->resulthash, svert);
338         refcount= BLI_ghash_lookup(cache->refcounthash, svert);
339
340         if(!hashshr) {
341                 /* not shaded yet, shade and insert into hash */
342                 p.t= (sseg->v[1] == svert)? 0.0f: 1.0f;
343                 strand_eval_point(sseg, &p);
344                 strand_shade_point(re, ssamp, sseg, &p);
345
346                 hashshr= MEM_callocN(sizeof(ShadeResult), "HashShadeResult");
347                 *hashshr= ssamp->shr[0];
348                 BLI_ghash_insert(cache->resulthash, svert, hashshr);
349         }
350         else
351                 /* already shaded, just copy previous result from hash */
352                 ssamp->shr[0]= *hashshr;
353         
354         /* lower reference count and remove if not needed anymore by any samples */
355         (*refcount)--;
356         if(*refcount == 0) {
357                 BLI_ghash_remove(cache->resulthash, svert, NULL, (GHashValFreeFP)MEM_freeN);
358                 BLI_ghash_remove(cache->refcounthash, svert, NULL, NULL);
359         }
360 }
361
362 void strand_shade_segment(Render *re, StrandShadeCache *cache, StrandSegment *sseg, ShadeSample *ssamp, float t, float s, int addpassflag)
363 {
364         ShadeResult shr1, shr2;
365
366         /* get shading for two endpoints and interpolate */
367         strand_shade_get(re, cache, ssamp, sseg, sseg->v[1]);
368         shr1= ssamp->shr[0];
369         strand_shade_get(re, cache, ssamp, sseg, sseg->v[2]);
370         shr2= ssamp->shr[0];
371
372         interpolate_shade_result(&shr1, &shr2, t, ssamp->shr, addpassflag);
373
374         /* apply alpha along width */
375         if(sseg->buffer->widthfade != 0.0f) {
376                 s = 1.0f - pow(fabs(s), sseg->buffer->widthfade);
377
378                 strand_apply_shaderesult_alpha(ssamp->shr, s);
379         }
380 }
381
382 void strand_shade_unref(StrandShadeCache *cache, StrandVert *svert)
383 {
384         int *refcount;
385
386         /* lower reference count and remove if not needed anymore by any samples */
387         refcount= BLI_ghash_lookup(cache->refcounthash, svert);
388
389         (*refcount)--;
390         if(*refcount == 0) {
391                 BLI_ghash_remove(cache->resulthash, svert, NULL, (GHashValFreeFP)MEM_freeN);
392                 BLI_ghash_remove(cache->refcounthash, svert, NULL, NULL);
393         }
394 }
395
396 static void strand_shade_refcount(StrandShadeCache *cache, StrandVert *svert)
397 {
398         int *refcount= BLI_ghash_lookup(cache->refcounthash, svert);
399
400         if(!refcount) {
401                 refcount= BLI_memarena_alloc(cache->memarena, sizeof(int));
402                 *refcount= 1;
403                 BLI_ghash_insert(cache->refcounthash, svert, refcount);
404         }
405         else
406                 (*refcount)++;
407 }
408
409 /* *************** */
410
411 typedef struct StrandPart {
412         Render *re;
413         ZSpan *zspan;
414
415         APixstrand *apixbuf;
416         int *totapixbuf;
417         int *rectz;
418         int *rectmask;
419         intptr_t *rectdaps;
420         int rectx, recty;
421         int sample;
422
423         StrandSegment *segment;
424         float t[3], s[3];
425
426         StrandShadeCache *cache;
427 } StrandPart;
428
429 typedef struct StrandSortSegment {
430         struct StrandSortSegment *next;
431         int obi, strand, segment;
432         float z;
433 } StrandSortSegment;
434
435 static int compare_strand_segment(const void *poin1, const void *poin2)
436 {
437         const StrandSortSegment *seg1= (const StrandSortSegment*)poin1;
438         const StrandSortSegment *seg2= (const StrandSortSegment*)poin2;
439
440         if(seg1->z < seg2->z)
441                 return -1;
442         else if(seg1->z == seg2->z)
443                 return 0;
444         else
445                 return 1;
446 }
447
448 static void do_strand_point_project(float winmat[][4], ZSpan *zspan, float *co, float *hoco, float *zco)
449 {
450         projectvert(co, winmat, hoco);
451         hoco_to_zco(zspan, zco, hoco);
452 }
453
454 static void strand_project_point(float winmat[][4], float winx, float winy, StrandPoint *spoint)
455 {
456         float div;
457
458         projectvert(spoint->co, winmat, spoint->hoco);
459
460         div= 1.0f/spoint->hoco[3];
461         spoint->x= spoint->hoco[0]*div*winx*0.5f;
462         spoint->y= spoint->hoco[1]*div*winy*0.5f;
463 }
464
465 static APixstrand *addpsmainAstrand(ListBase *lb)
466 {
467         APixstrMain *psm;
468
469         psm= MEM_mallocN(sizeof(APixstrMain), "addpsmainA");
470         BLI_addtail(lb, psm);
471         psm->ps= MEM_callocN(4096*sizeof(APixstrand),"pixstr");
472
473         return psm->ps;
474 }
475
476 static APixstrand *addpsAstrand(ZSpan *zspan)
477 {
478         /* make new PS */
479         if(zspan->apstrandmcounter==0) {
480                 zspan->curpstrand= addpsmainAstrand(zspan->apsmbase);
481                 zspan->apstrandmcounter= 4095;
482         }
483         else {
484                 zspan->curpstrand++;
485                 zspan->apstrandmcounter--;
486         }
487         return zspan->curpstrand;
488 }
489
490 #define MAX_ZROW        2000
491
492 static void do_strand_fillac(void *handle, int x, int y, float u, float v, float z)
493 {
494         StrandPart *spart= (StrandPart*)handle;
495         StrandShadeCache *cache= spart->cache;
496         StrandSegment *sseg= spart->segment;
497         APixstrand *apn, *apnew;
498         float t, s;
499         int offset, mask, obi, strnr, seg, zverg, bufferz, maskz=0;
500
501         offset = y*spart->rectx + x;
502         obi= sseg->obi - spart->re->objectinstance;
503         strnr= sseg->strand->index + 1;
504         seg= sseg->v[1] - sseg->strand->vert;
505         mask= (1<<spart->sample);
506
507         /* check against solid z-buffer */
508         zverg= (int)z;
509
510         if(spart->rectdaps) {
511                 /* find the z of the sample */
512                 PixStr *ps;
513                 intptr_t *rd= spart->rectdaps + offset;
514                 
515                 bufferz= 0x7FFFFFFF;
516                 if(spart->rectmask) maskz= 0x7FFFFFFF;
517                 
518                 if(*rd) {       
519                         for(ps= (PixStr *)(*rd); ps; ps= ps->next) {
520                                 if(mask & ps->mask) {
521                                         bufferz= ps->z;
522                                         if(spart->rectmask)
523                                                 maskz= ps->maskz;
524                                         break;
525                                 }
526                         }
527                 }
528         }
529         else {
530                 bufferz= spart->rectz[offset];
531                 if(spart->rectmask)
532                         maskz= spart->rectmask[offset];
533         }
534
535 #define CHECK_ADD(n) \
536         if(apn->p[n]==strnr && apn->obi[n]==obi && apn->seg[n]==seg) \
537         { if(!(apn->mask[n] & mask)) { apn->mask[n] |= mask; apn->v[n] += t; apn->u[n] += s; } break; }
538 #define CHECK_ASSIGN(n) \
539         if(apn->p[n]==0) \
540         {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; }
541
542         /* add to pixel list */
543         if(zverg < bufferz && (spart->totapixbuf[offset] < MAX_ZROW)) {
544                 if(!spart->rectmask || zverg > maskz) {
545                         t = u*spart->t[0] + v*spart->t[1] + (1.0f-u-v)*spart->t[2];
546                         s = fabs(u*spart->s[0] + v*spart->s[1] + (1.0f-u-v)*spart->s[2]);
547
548                         apn= spart->apixbuf + offset;
549                         while(apn) {
550                                 CHECK_ADD(0);
551                                 CHECK_ADD(1);
552                                 CHECK_ADD(2);
553                                 CHECK_ADD(3);
554                                 CHECK_ASSIGN(0);
555                                 CHECK_ASSIGN(1);
556                                 CHECK_ASSIGN(2);
557                                 CHECK_ASSIGN(3);
558
559                                 apnew= addpsAstrand(spart->zspan);
560                                 SWAP(APixstrand, *apnew, *apn);
561                                 apn->next= apnew;
562                                 CHECK_ASSIGN(0);
563                         }
564
565                         strand_shade_refcount(cache, sseg->v[1]);
566                         strand_shade_refcount(cache, sseg->v[2]);
567                         spart->totapixbuf[offset]++;
568                 }
569         }
570 }
571
572 static int strand_test_clip(float winmat[][4], ZSpan *zspan, float *bounds, float *co, float *zcomp)
573 {
574         float hoco[4];
575         int clipflag= 0;
576
577         projectvert(co, winmat, hoco);
578
579         /* we compare z without perspective division for segment sorting */
580         *zcomp= hoco[2];
581
582         if(hoco[0] > bounds[1]*hoco[3]) clipflag |= 1;
583         else if(hoco[0]< bounds[0]*hoco[3]) clipflag |= 2;
584         else if(hoco[1] > bounds[3]*hoco[3]) clipflag |= 4;
585         else if(hoco[1]< bounds[2]*hoco[3]) clipflag |= 8;
586
587         clipflag |= testclip(hoco);
588
589         return clipflag;
590 }
591
592 static void do_scanconvert_strand(Render *re, StrandPart *spart, ZSpan *zspan, float t, float dt, float *co1, float *co2, float *co3, float *co4, int sample)
593 {
594         float jco1[3], jco2[3], jco3[3], jco4[3], jx, jy;
595
596         VECCOPY(jco1, co1);
597         VECCOPY(jco2, co2);
598         VECCOPY(jco3, co3);
599         VECCOPY(jco4, co4);
600
601         if(re->osa) {
602                 jx= -re->jit[sample][0];
603                 jy= -re->jit[sample][1];
604
605                 jco1[0] += jx; jco1[1] += jy;
606                 jco2[0] += jx; jco2[1] += jy;
607                 jco3[0] += jx; jco3[1] += jy;
608                 jco4[0] += jx; jco4[1] += jy;
609         }
610         else if(re->i.curblur) {
611                 jx= -re->jit[re->i.curblur-1][0];
612                 jy= -re->jit[re->i.curblur-1][1];
613
614                 jco1[0] += jx; jco1[1] += jy;
615                 jco2[0] += jx; jco2[1] += jy;
616                 jco3[0] += jx; jco3[1] += jy;
617                 jco4[0] += jx; jco4[1] += jy;
618         }
619
620         spart->sample= sample;
621
622         spart->t[0]= t-dt;
623         spart->s[0]= -1.0f;
624         spart->t[1]= t-dt;
625         spart->s[1]= 1.0f;
626         spart->t[2]= t;
627         spart->s[2]= 1.0f;
628         zspan_scanconvert_strand(zspan, spart, jco1, jco2, jco3, do_strand_fillac);
629         spart->t[0]= t-dt;
630         spart->s[0]= -1.0f;
631         spart->t[1]= t;
632         spart->s[1]= 1.0f;
633         spart->t[2]= t;
634         spart->s[2]= -1.0f;
635         zspan_scanconvert_strand(zspan, spart, jco1, jco3, jco4, do_strand_fillac);
636 }
637
638 static void strand_render(Render *re, StrandSegment *sseg, float winmat[][4], StrandPart *spart, ZSpan *zspan, int totzspan, StrandPoint *p1, StrandPoint *p2)
639 {
640         if(spart) {
641                 float t= p2->t;
642                 float dt= p2->t - p1->t;
643                 int a;
644
645                 if(re->osa) {
646                         for(a=0; a<re->osa; a++)
647                                 do_scanconvert_strand(re, spart, zspan, t, dt, p1->zco2, p1->zco1, p2->zco1, p2->zco2, a);
648                 }
649                 else
650                         do_scanconvert_strand(re, spart, zspan, t, dt, p1->zco2, p1->zco1, p2->zco1, p2->zco2, 0);
651         }
652         else {
653                 float hoco1[4], hoco2[4];
654                 int a, obi, index;
655   
656                 obi= sseg->obi - re->objectinstance;
657                 index= sseg->strand->index;
658
659                 projectvert(p1->co, winmat, hoco1);
660                 projectvert(p2->co, winmat, hoco2);
661   
662                 for(a=0; a<totzspan; a++) {
663 #if 0
664                         /* render both strand and single pixel wire to counter aliasing */
665                         zbufclip4(re, &zspan[a], obi, index, p1->hoco2, p1->hoco1, p2->hoco1, p2->hoco2, p1->clip2, p1->clip1, p2->clip1, p2->clip2);
666 #endif
667                         /* only render a line for now, which makes the shadow map more
668                            similiar across frames, and so reduces flicker */
669                         zbufsinglewire(&zspan[a], obi, index, hoco1, hoco2);
670                 }
671         }
672 }
673   
674 static int strand_segment_recursive(Render *re, float winmat[][4], StrandPart *spart, ZSpan *zspan, int totzspan, StrandSegment *sseg, StrandPoint *p1, StrandPoint *p2, int depth)
675 {
676         StrandPoint p;
677         StrandBuffer *buffer= sseg->buffer;
678         float dot, d1[2], d2[2], len1, len2;
679
680         if(depth == buffer->maxdepth)
681                 return 0;
682
683         p.t= (p1->t + p2->t)*0.5f;
684         strand_eval_point(sseg, &p);
685         strand_project_point(buffer->winmat, buffer->winx, buffer->winy, &p);
686
687         d1[0]= (p.x - p1->x);
688         d1[1]= (p.y - p1->y);
689         len1= d1[0]*d1[0] + d1[1]*d1[1];
690
691         d2[0]= (p2->x - p.x);
692         d2[1]= (p2->y - p.y);
693         len2= d2[0]*d2[0] + d2[1]*d2[1];
694
695         if(len1 == 0.0f || len2 == 0.0f)
696                 return 0;
697         
698         dot= d1[0]*d2[0] + d1[1]*d2[1];
699         if(dot*dot > sseg->sqadaptcos*len1*len2)
700                 return 0;
701
702         if(spart) {
703                 do_strand_point_project(winmat, zspan, p.co1, p.hoco1, p.zco1);
704                 do_strand_point_project(winmat, zspan, p.co2, p.hoco2, p.zco2);
705         }
706         else {
707 #if 0
708                 projectvert(p.co1, winmat, p.hoco1);
709                 projectvert(p.co2, winmat, p.hoco2);
710                 p.clip1= testclip(p.hoco1);
711                 p.clip2= testclip(p.hoco2);
712 #endif
713         }
714
715         if(!strand_segment_recursive(re, winmat, spart, zspan, totzspan, sseg, p1, &p, depth+1))
716                 strand_render(re, sseg, winmat, spart, zspan, totzspan, p1, &p);
717         if(!strand_segment_recursive(re, winmat, spart, zspan, totzspan, sseg, &p, p2, depth+1))
718                 strand_render(re, sseg, winmat, spart, zspan, totzspan, &p, p2);
719         
720         return 1;
721 }
722
723 void render_strand_segment(Render *re, float winmat[][4], StrandPart *spart, ZSpan *zspan, int totzspan, StrandSegment *sseg)
724 {
725         StrandBuffer *buffer= sseg->buffer;
726         StrandPoint *p1= &sseg->point1;
727         StrandPoint *p2= &sseg->point2;
728
729         p1->t= 0.0f;
730         p2->t= 1.0f;
731
732         strand_eval_point(sseg, p1);
733         strand_project_point(buffer->winmat, buffer->winx, buffer->winy, p1);
734         strand_eval_point(sseg, p2);
735         strand_project_point(buffer->winmat, buffer->winx, buffer->winy, p2);
736
737         if(spart) {
738                 do_strand_point_project(winmat, zspan, p1->co1, p1->hoco1, p1->zco1);
739                 do_strand_point_project(winmat, zspan, p1->co2, p1->hoco2, p1->zco2);
740                 do_strand_point_project(winmat, zspan, p2->co1, p2->hoco1, p2->zco1);
741                 do_strand_point_project(winmat, zspan, p2->co2, p2->hoco2, p2->zco2);
742         }
743         else {
744 #if 0
745                 projectvert(p1->co1, winmat, p1->hoco1);
746                 projectvert(p1->co2, winmat, p1->hoco2);
747                 projectvert(p2->co1, winmat, p2->hoco1);
748                 projectvert(p2->co2, winmat, p2->hoco2);
749                 p1->clip1= testclip(p1->hoco1);
750                 p1->clip2= testclip(p1->hoco2);
751                 p2->clip1= testclip(p2->hoco1);
752                 p2->clip2= testclip(p2->hoco2);
753 #endif
754         }
755
756         if(!strand_segment_recursive(re, winmat, spart, zspan, totzspan, sseg, p1, p2, 0))
757                 strand_render(re, sseg, winmat, spart, zspan, totzspan, p1, p2);
758 }
759
760 /* render call to fill in strands */
761 int zbuffer_strands_abuf(Render *re, RenderPart *pa, RenderLayer *rl, APixstrand *apixbuf, ListBase *apsmbase, StrandShadeCache *cache)
762 {
763         ObjectRen *obr;
764         ObjectInstanceRen *obi;
765         ZSpan zspan;
766         StrandRen *strand=0;
767         StrandVert *svert;
768         StrandBound *sbound;
769         StrandPart spart;
770         StrandSegment sseg;
771         StrandSortSegment *sortsegments = NULL, *sortseg, *firstseg;
772         MemArena *memarena;
773         float z[4], bounds[4], winmat[4][4];
774         int a, b, c, i, totsegment, clip[4];
775
776         if(re->test_break(re->tbh))
777                 return 0;
778         if(re->totstrand == 0)
779                 return 0;
780
781         /* setup StrandPart */
782         memset(&spart, 0, sizeof(spart));
783
784         spart.re= re;
785         spart.rectx= pa->rectx;
786         spart.recty= pa->recty;
787         spart.apixbuf= apixbuf;
788         spart.zspan= &zspan;
789         spart.rectdaps= pa->rectdaps;
790         spart.rectz= pa->rectz;
791         spart.rectmask= pa->rectmask;
792         spart.cache= cache;
793
794         zbuf_alloc_span(&zspan, pa->rectx, pa->recty, re->clipcrop);
795
796         /* needed for transform from hoco to zbuffer co */
797         zspan.zmulx= ((float)re->winx)/2.0;
798         zspan.zmuly= ((float)re->winy)/2.0;
799         
800         zspan.zofsx= -pa->disprect.xmin;
801         zspan.zofsy= -pa->disprect.ymin;
802
803         /* to center the sample position */
804         zspan.zofsx -= 0.5f;
805         zspan.zofsy -= 0.5f;
806
807         zspan.apsmbase= apsmbase;
808
809         /* clipping setup */
810         bounds[0]= (2*pa->disprect.xmin - re->winx-1)/(float)re->winx;
811         bounds[1]= (2*pa->disprect.xmax - re->winx+1)/(float)re->winx;
812         bounds[2]= (2*pa->disprect.ymin - re->winy-1)/(float)re->winy;
813         bounds[3]= (2*pa->disprect.ymax - re->winy+1)/(float)re->winy;
814
815         memarena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
816         firstseg= NULL;
817         sortseg= sortsegments;
818         totsegment= 0;
819
820         /* for all object instances */
821         for(obi=re->instancetable.first, i=0; obi; obi=obi->next, i++) {
822                 obr= obi->obr;
823
824                 if(!obr->strandbuf || !(obr->strandbuf->lay & rl->lay))
825                         continue;
826
827                 /* compute matrix and try clipping whole object */
828                 if(obi->flag & R_TRANSFORMED)
829                         zbuf_make_winmat(re, obi->mat, winmat);
830                 else
831                         zbuf_make_winmat(re, NULL, winmat);
832
833                 if(clip_render_object(obi->obr->boundbox, bounds, winmat))
834                         continue;
835
836                 /* for each bounding box containing a number of strands */
837                 sbound= obr->strandbuf->bound;
838                 for(c=0; c<obr->strandbuf->totbound; c++, sbound++) {
839                         if(clip_render_object(sbound->boundbox, bounds, winmat))
840                                 continue;
841
842                         /* for each strand in this bounding box */
843                         for(a=sbound->start; a<sbound->end; a++) {
844                                 strand= RE_findOrAddStrand(obr, a);
845                                 svert= strand->vert;
846
847                                 /* keep clipping and z depth for 4 control points */
848                                 clip[1]= strand_test_clip(winmat, &zspan, bounds, svert->co, &z[1]);
849                                 clip[2]= strand_test_clip(winmat, &zspan, bounds, (svert+1)->co, &z[2]);
850                                 clip[0]= clip[1]; z[0]= z[1];
851
852                                 for(b=0; b<strand->totvert-1; b++, svert++) {
853                                         /* compute 4th point clipping and z depth */
854                                         if(b < strand->totvert-2) {
855                                                 clip[3]= strand_test_clip(winmat, &zspan, bounds, (svert+2)->co, &z[3]);
856                                         }
857                                         else {
858                                                 clip[3]= clip[2]; z[3]= z[2];
859                                         }
860
861                                         /* check clipping and add to sortsegments buffer */
862                                         if(!(clip[0] & clip[1] & clip[2] & clip[3])) {
863                                                 sortseg= BLI_memarena_alloc(memarena, sizeof(StrandSortSegment));
864                                                 sortseg->obi= i;
865                                                 sortseg->strand= strand->index;
866                                                 sortseg->segment= b;
867
868                                                 sortseg->z= 0.5f*(z[1] + z[2]);
869
870                                                 sortseg->next= firstseg;
871                                                 firstseg= sortseg;
872                                                 totsegment++;
873                                         }
874
875                                         /* shift clipping and z depth */
876                                         clip[0]= clip[1]; z[0]= z[1];
877                                         clip[1]= clip[2]; z[1]= z[2];
878                                         clip[2]= clip[3]; z[2]= z[3];
879                                 }
880                         }
881                 }
882         }
883
884         if(!re->test_break(re->tbh)) {
885                 /* convert list to array and sort */
886                 sortsegments= MEM_mallocN(sizeof(StrandSortSegment)*totsegment, "StrandSortSegment");
887                 for(a=0, sortseg=firstseg; a<totsegment; a++, sortseg=sortseg->next)
888                         sortsegments[a]= *sortseg;
889                 qsort(sortsegments, totsegment, sizeof(StrandSortSegment), compare_strand_segment);
890         }
891
892         BLI_memarena_free(memarena);
893
894         spart.totapixbuf= MEM_callocN(sizeof(int)*pa->rectx*pa->recty, "totapixbuf");
895
896         if(!re->test_break(re->tbh)) {
897                 /* render segments in sorted order */
898                 sortseg= sortsegments;
899                 for(a=0; a<totsegment; a++, sortseg++) {
900                         if(re->test_break(re->tbh))
901                                 break;
902
903                         obi= &re->objectinstance[sortseg->obi];
904                         obr= obi->obr;
905                         zbuf_make_winmat(re, NULL, winmat);
906
907                         sseg.obi= obi;
908                         sseg.strand= RE_findOrAddStrand(obr, sortseg->strand);
909                         sseg.buffer= sseg.strand->buffer;
910                         sseg.sqadaptcos= sseg.buffer->adaptcos;
911                         sseg.sqadaptcos *= sseg.sqadaptcos;
912
913                         svert= sseg.strand->vert + sortseg->segment;
914                         sseg.v[0]= (sortseg->segment > 0)? (svert-1): svert;
915                         sseg.v[1]= svert;
916                         sseg.v[2]= svert+1;
917                         sseg.v[3]= (sortseg->segment < sseg.strand->totvert-2)? svert+2: svert+1;
918                         sseg.shaded= 0;
919
920                         spart.segment= &sseg;
921
922                         render_strand_segment(re, winmat, &spart, &zspan, 1, &sseg);
923                 }
924         }
925
926         if(sortsegments)
927                 MEM_freeN(sortsegments);
928         MEM_freeN(spart.totapixbuf);
929         
930         zbuf_free_span(&zspan);
931
932         return totsegment;
933 }
934
935 /* *************** */
936
937 StrandSurface *cache_strand_surface(Render *re, ObjectRen *obr, DerivedMesh *dm, float mat[][4], int timeoffset)
938 {
939         StrandSurface *mesh;
940         MFace *mface;
941         MVert *mvert;
942         float (*co)[3];
943         int a, totvert, totface;
944
945         totvert= dm->getNumVerts(dm);
946         totface= dm->getNumFaces(dm);
947
948         for(mesh=re->strandsurface.first; mesh; mesh=mesh->next)
949                 if(mesh->obr.ob == obr->ob && mesh->obr.par == obr->par
950                         && mesh->obr.index == obr->index && mesh->totvert==totvert && mesh->totface==totface)
951                         break;
952
953         if(!mesh) {
954                 mesh= MEM_callocN(sizeof(StrandSurface), "StrandSurface");
955                 mesh->obr= *obr;
956                 mesh->totvert= totvert;
957                 mesh->totface= totface;
958                 mesh->face= MEM_callocN(sizeof(int)*4*mesh->totface, "StrandSurfFaces");
959                 mesh->col= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfCol");
960                 BLI_addtail(&re->strandsurface, mesh);
961         }
962
963         if(timeoffset == -1 && !mesh->prevco)
964                 mesh->prevco= co= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfCo");
965         else if(timeoffset == 0 && !mesh->co)
966                 mesh->co= co= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfCo");
967         else if(timeoffset == 1 && !mesh->nextco)
968                 mesh->nextco= co= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfCo");
969         else
970                 return mesh;
971
972         mvert= dm->getVertArray(dm);
973         for(a=0; a<mesh->totvert; a++, mvert++) {
974                 VECCOPY(co[a], mvert->co);
975                 Mat4MulVecfl(mat, co[a]);
976         }
977
978         mface= dm->getFaceArray(dm);
979         for(a=0; a<mesh->totface; a++, mface++) {
980                 mesh->face[a][0]= mface->v1;
981                 mesh->face[a][1]= mface->v2;
982                 mesh->face[a][2]= mface->v3;
983                 mesh->face[a][3]= mface->v4;
984         }
985
986         return mesh;
987 }
988
989 void free_strand_surface(Render *re)
990 {
991         StrandSurface *mesh;
992
993         for(mesh=re->strandsurface.first; mesh; mesh=mesh->next) {
994                 if(mesh->co) MEM_freeN(mesh->co);
995                 if(mesh->prevco) MEM_freeN(mesh->prevco);
996                 if(mesh->nextco) MEM_freeN(mesh->nextco);
997                 if(mesh->col) MEM_freeN(mesh->col);
998                 if(mesh->face) MEM_freeN(mesh->face);
999         }
1000
1001         BLI_freelistN(&re->strandsurface);
1002 }
1003
1004 void strand_minmax(StrandRen *strand, float *min, float *max)
1005 {
1006         StrandVert *svert;
1007         int a;
1008
1009         for(a=0, svert=strand->vert; a<strand->totvert; a++, svert++)
1010                 DO_MINMAX(svert->co, min, max)
1011 }
1012