Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[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                 /* removed 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         shade_input_init_material(shi);
285         
286         /* shade */
287         shade_samples_do_AO(ssamp);
288         shade_input_do_shade(shi, shr);
289
290         /* apply simplification */
291         strand_apply_shaderesult_alpha(shr, spoint->alpha);
292
293         /* include lamphalos for strand, since halo layer was added already */
294         if(re->flag & R_LAMPHALO)
295                 if(shi->layflag & SCE_LAY_HALO)
296                         renderspothalo(shi, shr->combined, shr->combined[3]);
297         
298         shi->strand= NULL;
299 }
300
301 /* *************** */
302
303 struct StrandShadeCache {
304         GHash *resulthash;
305         GHash *refcounthash;
306         MemArena *memarena;
307 };
308
309 StrandShadeCache *strand_shade_cache_create()
310 {
311         StrandShadeCache *cache;
312
313         cache= MEM_callocN(sizeof(StrandShadeCache), "StrandShadeCache");
314         cache->resulthash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
315         cache->refcounthash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
316         cache->memarena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
317         
318         return cache;
319 }
320
321 void strand_shade_cache_free(StrandShadeCache *cache)
322 {
323         BLI_ghash_free(cache->refcounthash, NULL, NULL);
324         BLI_ghash_free(cache->resulthash, NULL, (GHashValFreeFP)MEM_freeN);
325         BLI_memarena_free(cache->memarena);
326         MEM_freeN(cache);
327 }
328
329 static void strand_shade_get(Render *re, StrandShadeCache *cache, ShadeSample *ssamp, StrandSegment *sseg, StrandVert *svert)
330 {
331         ShadeResult *hashshr;
332         StrandPoint p;
333         int *refcount;
334
335         hashshr= BLI_ghash_lookup(cache->resulthash, svert);
336         refcount= BLI_ghash_lookup(cache->refcounthash, svert);
337
338         if(!hashshr) {
339                 /* not shaded yet, shade and insert into hash */
340                 p.t= (sseg->v[1] == svert)? 0.0f: 1.0f;
341                 strand_eval_point(sseg, &p);
342                 strand_shade_point(re, ssamp, sseg, &p);
343
344                 hashshr= MEM_callocN(sizeof(ShadeResult), "HashShadeResult");
345                 *hashshr= ssamp->shr[0];
346                 BLI_ghash_insert(cache->resulthash, svert, hashshr);
347         }
348         else
349                 /* already shaded, just copy previous result from hash */
350                 ssamp->shr[0]= *hashshr;
351         
352         /* lower reference count and remove if not needed anymore by any samples */
353         (*refcount)--;
354         if(*refcount == 0) {
355                 BLI_ghash_remove(cache->resulthash, svert, NULL, (GHashValFreeFP)MEM_freeN);
356                 BLI_ghash_remove(cache->refcounthash, svert, NULL, NULL);
357         }
358 }
359
360 void strand_shade_segment(Render *re, StrandShadeCache *cache, StrandSegment *sseg, ShadeSample *ssamp, float t, float s, int addpassflag)
361 {
362         ShadeResult shr1, shr2;
363
364         /* get shading for two endpoints and interpolate */
365         strand_shade_get(re, cache, ssamp, sseg, sseg->v[1]);
366         shr1= ssamp->shr[0];
367         strand_shade_get(re, cache, ssamp, sseg, sseg->v[2]);
368         shr2= ssamp->shr[0];
369
370         interpolate_shade_result(&shr1, &shr2, t, ssamp->shr, addpassflag);
371
372         /* apply alpha along width */
373         if(sseg->buffer->widthfade != 0.0f) {
374                 s = 1.0f - pow(fabs(s), sseg->buffer->widthfade);
375
376                 strand_apply_shaderesult_alpha(ssamp->shr, s);
377         }
378 }
379
380 void strand_shade_unref(StrandShadeCache *cache, StrandVert *svert)
381 {
382         int *refcount;
383
384         /* lower reference count and remove if not needed anymore by any samples */
385         refcount= BLI_ghash_lookup(cache->refcounthash, svert);
386
387         (*refcount)--;
388         if(*refcount == 0) {
389                 BLI_ghash_remove(cache->resulthash, svert, NULL, (GHashValFreeFP)MEM_freeN);
390                 BLI_ghash_remove(cache->refcounthash, svert, NULL, NULL);
391         }
392 }
393
394 static void strand_shade_refcount(StrandShadeCache *cache, StrandVert *svert)
395 {
396         int *refcount= BLI_ghash_lookup(cache->refcounthash, svert);
397
398         if(!refcount) {
399                 refcount= BLI_memarena_alloc(cache->memarena, sizeof(int));
400                 *refcount= 1;
401                 BLI_ghash_insert(cache->refcounthash, svert, refcount);
402         }
403         else
404                 (*refcount)++;
405 }
406
407 /* *************** */
408
409 typedef struct StrandPart {
410         Render *re;
411         ZSpan *zspan;
412
413         APixstrand *apixbuf;
414         int *totapixbuf;
415         int *rectz;
416         int *rectmask;
417         intptr_t *rectdaps;
418         int rectx, recty;
419         int sample;
420
421         StrandSegment *segment;
422         float t[3], s[3];
423
424         StrandShadeCache *cache;
425 } StrandPart;
426
427 typedef struct StrandSortSegment {
428         struct StrandSortSegment *next;
429         int obi, strand, segment;
430         float z;
431 } StrandSortSegment;
432
433 static int compare_strand_segment(const void *poin1, const void *poin2)
434 {
435         const StrandSortSegment *seg1= (const StrandSortSegment*)poin1;
436         const StrandSortSegment *seg2= (const StrandSortSegment*)poin2;
437
438         if(seg1->z < seg2->z)
439                 return -1;
440         else if(seg1->z == seg2->z)
441                 return 0;
442         else
443                 return 1;
444 }
445
446 static void do_strand_point_project(float winmat[][4], ZSpan *zspan, float *co, float *hoco, float *zco)
447 {
448         projectvert(co, winmat, hoco);
449         hoco_to_zco(zspan, zco, hoco);
450 }
451
452 static void strand_project_point(float winmat[][4], float winx, float winy, StrandPoint *spoint)
453 {
454         float div;
455
456         projectvert(spoint->co, winmat, spoint->hoco);
457
458         div= 1.0f/spoint->hoco[3];
459         spoint->x= spoint->hoco[0]*div*winx*0.5f;
460         spoint->y= spoint->hoco[1]*div*winy*0.5f;
461 }
462
463 static APixstrand *addpsmainAstrand(ListBase *lb)
464 {
465         APixstrMain *psm;
466
467         psm= MEM_mallocN(sizeof(APixstrMain), "addpsmainA");
468         BLI_addtail(lb, psm);
469         psm->ps= MEM_callocN(4096*sizeof(APixstrand),"pixstr");
470
471         return psm->ps;
472 }
473
474 static APixstrand *addpsAstrand(ZSpan *zspan)
475 {
476         /* make new PS */
477         if(zspan->apstrandmcounter==0) {
478                 zspan->curpstrand= addpsmainAstrand(zspan->apsmbase);
479                 zspan->apstrandmcounter= 4095;
480         }
481         else {
482                 zspan->curpstrand++;
483                 zspan->apstrandmcounter--;
484         }
485         return zspan->curpstrand;
486 }
487
488 #define MAX_ZROW        2000
489
490 static void do_strand_fillac(void *handle, int x, int y, float u, float v, float z)
491 {
492         StrandPart *spart= (StrandPart*)handle;
493         StrandShadeCache *cache= spart->cache;
494         StrandSegment *sseg= spart->segment;
495         APixstrand *apn, *apnew;
496         float t, s;
497         int offset, mask, obi, strnr, seg, zverg, bufferz, maskz=0;
498
499         offset = y*spart->rectx + x;
500         obi= sseg->obi - spart->re->objectinstance;
501         strnr= sseg->strand->index + 1;
502         seg= sseg->v[1] - sseg->strand->vert;
503         mask= (1<<spart->sample);
504
505         /* check against solid z-buffer */
506         zverg= (int)z;
507
508         if(spart->rectdaps) {
509                 /* find the z of the sample */
510                 PixStr *ps;
511                 intptr_t *rd= spart->rectdaps + offset;
512                 
513                 bufferz= 0x7FFFFFFF;
514                 if(spart->rectmask) maskz= 0x7FFFFFFF;
515                 
516                 if(*rd) {       
517                         for(ps= (PixStr *)(*rd); ps; ps= ps->next) {
518                                 if(mask & ps->mask) {
519                                         bufferz= ps->z;
520                                         if(spart->rectmask)
521                                                 maskz= ps->maskz;
522                                         break;
523                                 }
524                         }
525                 }
526         }
527         else {
528                 bufferz= spart->rectz[offset];
529                 if(spart->rectmask)
530                         maskz= spart->rectmask[offset];
531         }
532
533 #define CHECK_ADD(n) \
534         if(apn->p[n]==strnr && apn->obi[n]==obi && apn->seg[n]==seg) \
535         { if(!(apn->mask[n] & mask)) { apn->mask[n] |= mask; apn->v[n] += t; apn->u[n] += s; } break; }
536 #define CHECK_ASSIGN(n) \
537         if(apn->p[n]==0) \
538         {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; }
539
540         /* add to pixel list */
541         if(zverg < bufferz && (spart->totapixbuf[offset] < MAX_ZROW)) {
542                 if(!spart->rectmask || zverg > maskz) {
543                         t = u*spart->t[0] + v*spart->t[1] + (1.0f-u-v)*spart->t[2];
544                         s = fabs(u*spart->s[0] + v*spart->s[1] + (1.0f-u-v)*spart->s[2]);
545
546                         apn= spart->apixbuf + offset;
547                         while(apn) {
548                                 CHECK_ADD(0);
549                                 CHECK_ADD(1);
550                                 CHECK_ADD(2);
551                                 CHECK_ADD(3);
552                                 CHECK_ASSIGN(0);
553                                 CHECK_ASSIGN(1);
554                                 CHECK_ASSIGN(2);
555                                 CHECK_ASSIGN(3);
556
557                                 apnew= addpsAstrand(spart->zspan);
558                                 SWAP(APixstrand, *apnew, *apn);
559                                 apn->next= apnew;
560                                 CHECK_ASSIGN(0);
561                         }
562
563                         strand_shade_refcount(cache, sseg->v[1]);
564                         strand_shade_refcount(cache, sseg->v[2]);
565                         spart->totapixbuf[offset]++;
566                 }
567         }
568 }
569
570 static int strand_test_clip(float winmat[][4], ZSpan *zspan, float *bounds, float *co, float *zcomp)
571 {
572         float hoco[4];
573         int clipflag= 0;
574
575         projectvert(co, winmat, hoco);
576
577         /* we compare z without perspective division for segment sorting */
578         *zcomp= hoco[2];
579
580         if(hoco[0] > bounds[1]*hoco[3]) clipflag |= 1;
581         else if(hoco[0]< bounds[0]*hoco[3]) clipflag |= 2;
582         else if(hoco[1] > bounds[3]*hoco[3]) clipflag |= 4;
583         else if(hoco[1]< bounds[2]*hoco[3]) clipflag |= 8;
584
585         clipflag |= testclip(hoco);
586
587         return clipflag;
588 }
589
590 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)
591 {
592         float jco1[3], jco2[3], jco3[3], jco4[3], jx, jy;
593
594         VECCOPY(jco1, co1);
595         VECCOPY(jco2, co2);
596         VECCOPY(jco3, co3);
597         VECCOPY(jco4, co4);
598
599         if(re->osa) {
600                 jx= -re->jit[sample][0];
601                 jy= -re->jit[sample][1];
602
603                 jco1[0] += jx; jco1[1] += jy;
604                 jco2[0] += jx; jco2[1] += jy;
605                 jco3[0] += jx; jco3[1] += jy;
606                 jco4[0] += jx; jco4[1] += jy;
607         }
608         else if(re->i.curblur) {
609                 jx= -re->jit[re->i.curblur-1][0];
610                 jy= -re->jit[re->i.curblur-1][1];
611
612                 jco1[0] += jx; jco1[1] += jy;
613                 jco2[0] += jx; jco2[1] += jy;
614                 jco3[0] += jx; jco3[1] += jy;
615                 jco4[0] += jx; jco4[1] += jy;
616         }
617
618         spart->sample= sample;
619
620         spart->t[0]= t-dt;
621         spart->s[0]= -1.0f;
622         spart->t[1]= t-dt;
623         spart->s[1]= 1.0f;
624         spart->t[2]= t;
625         spart->s[2]= 1.0f;
626         zspan_scanconvert_strand(zspan, spart, jco1, jco2, jco3, do_strand_fillac);
627         spart->t[0]= t-dt;
628         spart->s[0]= -1.0f;
629         spart->t[1]= t;
630         spart->s[1]= 1.0f;
631         spart->t[2]= t;
632         spart->s[2]= -1.0f;
633         zspan_scanconvert_strand(zspan, spart, jco1, jco3, jco4, do_strand_fillac);
634 }
635
636 static void strand_render(Render *re, StrandSegment *sseg, float winmat[][4], StrandPart *spart, ZSpan *zspan, int totzspan, StrandPoint *p1, StrandPoint *p2)
637 {
638         if(spart) {
639                 float t= p2->t;
640                 float dt= p2->t - p1->t;
641                 int a;
642
643                 if(re->osa) {
644                         for(a=0; a<re->osa; a++)
645                                 do_scanconvert_strand(re, spart, zspan, t, dt, p1->zco2, p1->zco1, p2->zco1, p2->zco2, a);
646                 }
647                 else
648                         do_scanconvert_strand(re, spart, zspan, t, dt, p1->zco2, p1->zco1, p2->zco1, p2->zco2, 0);
649         }
650         else {
651                 float hoco1[4], hoco2[4];
652                 int a, obi, index;
653   
654                 obi= sseg->obi - re->objectinstance;
655                 index= sseg->strand->index;
656
657                 projectvert(p1->co, winmat, hoco1);
658                 projectvert(p2->co, winmat, hoco2);
659   
660                 for(a=0; a<totzspan; a++) {
661 #if 0
662                         /* render both strand and single pixel wire to counter aliasing */
663                         zbufclip4(re, &zspan[a], obi, index, p1->hoco2, p1->hoco1, p2->hoco1, p2->hoco2, p1->clip2, p1->clip1, p2->clip1, p2->clip2);
664 #endif
665                         /* only render a line for now, which makes the shadow map more
666                            similiar across frames, and so reduces flicker */
667                         zbufsinglewire(&zspan[a], obi, index, hoco1, hoco2);
668                 }
669         }
670 }
671   
672 static int strand_segment_recursive(Render *re, float winmat[][4], StrandPart *spart, ZSpan *zspan, int totzspan, StrandSegment *sseg, StrandPoint *p1, StrandPoint *p2, int depth)
673 {
674         StrandPoint p;
675         StrandBuffer *buffer= sseg->buffer;
676         float dot, d1[2], d2[2], len1, len2;
677
678         if(depth == buffer->maxdepth)
679                 return 0;
680
681         p.t= (p1->t + p2->t)*0.5f;
682         strand_eval_point(sseg, &p);
683         strand_project_point(buffer->winmat, buffer->winx, buffer->winy, &p);
684
685         d1[0]= (p.x - p1->x);
686         d1[1]= (p.y - p1->y);
687         len1= d1[0]*d1[0] + d1[1]*d1[1];
688
689         d2[0]= (p2->x - p.x);
690         d2[1]= (p2->y - p.y);
691         len2= d2[0]*d2[0] + d2[1]*d2[1];
692
693         if(len1 == 0.0f || len2 == 0.0f)
694                 return 0;
695         
696         dot= d1[0]*d2[0] + d1[1]*d2[1];
697         if(dot*dot > sseg->sqadaptcos*len1*len2)
698                 return 0;
699
700         if(spart) {
701                 do_strand_point_project(winmat, zspan, p.co1, p.hoco1, p.zco1);
702                 do_strand_point_project(winmat, zspan, p.co2, p.hoco2, p.zco2);
703         }
704         else {
705 #if 0
706                 projectvert(p.co1, winmat, p.hoco1);
707                 projectvert(p.co2, winmat, p.hoco2);
708                 p.clip1= testclip(p.hoco1);
709                 p.clip2= testclip(p.hoco2);
710 #endif
711         }
712
713         if(!strand_segment_recursive(re, winmat, spart, zspan, totzspan, sseg, p1, &p, depth+1))
714                 strand_render(re, sseg, winmat, spart, zspan, totzspan, p1, &p);
715         if(!strand_segment_recursive(re, winmat, spart, zspan, totzspan, sseg, &p, p2, depth+1))
716                 strand_render(re, sseg, winmat, spart, zspan, totzspan, &p, p2);
717         
718         return 1;
719 }
720
721 void render_strand_segment(Render *re, float winmat[][4], StrandPart *spart, ZSpan *zspan, int totzspan, StrandSegment *sseg)
722 {
723         StrandBuffer *buffer= sseg->buffer;
724         StrandPoint *p1= &sseg->point1;
725         StrandPoint *p2= &sseg->point2;
726
727         p1->t= 0.0f;
728         p2->t= 1.0f;
729
730         strand_eval_point(sseg, p1);
731         strand_project_point(buffer->winmat, buffer->winx, buffer->winy, p1);
732         strand_eval_point(sseg, p2);
733         strand_project_point(buffer->winmat, buffer->winx, buffer->winy, p2);
734
735         if(spart) {
736                 do_strand_point_project(winmat, zspan, p1->co1, p1->hoco1, p1->zco1);
737                 do_strand_point_project(winmat, zspan, p1->co2, p1->hoco2, p1->zco2);
738                 do_strand_point_project(winmat, zspan, p2->co1, p2->hoco1, p2->zco1);
739                 do_strand_point_project(winmat, zspan, p2->co2, p2->hoco2, p2->zco2);
740         }
741         else {
742 #if 0
743                 projectvert(p1->co1, winmat, p1->hoco1);
744                 projectvert(p1->co2, winmat, p1->hoco2);
745                 projectvert(p2->co1, winmat, p2->hoco1);
746                 projectvert(p2->co2, winmat, p2->hoco2);
747                 p1->clip1= testclip(p1->hoco1);
748                 p1->clip2= testclip(p1->hoco2);
749                 p2->clip1= testclip(p2->hoco1);
750                 p2->clip2= testclip(p2->hoco2);
751 #endif
752         }
753
754         if(!strand_segment_recursive(re, winmat, spart, zspan, totzspan, sseg, p1, p2, 0))
755                 strand_render(re, sseg, winmat, spart, zspan, totzspan, p1, p2);
756 }
757
758 /* render call to fill in strands */
759 int zbuffer_strands_abuf(Render *re, RenderPart *pa, RenderLayer *rl, APixstrand *apixbuf, ListBase *apsmbase, StrandShadeCache *cache)
760 {
761         ObjectRen *obr;
762         ObjectInstanceRen *obi;
763         ZSpan zspan;
764         StrandRen *strand=0;
765         StrandVert *svert;
766         StrandBound *sbound;
767         StrandPart spart;
768         StrandSegment sseg;
769         StrandSortSegment *sortsegments = NULL, *sortseg, *firstseg;
770         MemArena *memarena;
771         float z[4], bounds[4], winmat[4][4];
772         int a, b, c, i, totsegment, clip[4];
773
774         if(re->test_break(re->tbh))
775                 return 0;
776         if(re->totstrand == 0)
777                 return 0;
778
779         /* setup StrandPart */
780         memset(&spart, 0, sizeof(spart));
781
782         spart.re= re;
783         spart.rectx= pa->rectx;
784         spart.recty= pa->recty;
785         spart.apixbuf= apixbuf;
786         spart.zspan= &zspan;
787         spart.rectdaps= pa->rectdaps;
788         spart.rectz= pa->rectz;
789         spart.rectmask= pa->rectmask;
790         spart.cache= cache;
791
792         zbuf_alloc_span(&zspan, pa->rectx, pa->recty, re->clipcrop);
793
794         /* needed for transform from hoco to zbuffer co */
795         zspan.zmulx= ((float)re->winx)/2.0;
796         zspan.zmuly= ((float)re->winy)/2.0;
797         
798         zspan.zofsx= -pa->disprect.xmin;
799         zspan.zofsy= -pa->disprect.ymin;
800
801         /* to center the sample position */
802         zspan.zofsx -= 0.5f;
803         zspan.zofsy -= 0.5f;
804
805         zspan.apsmbase= apsmbase;
806
807         /* clipping setup */
808         bounds[0]= (2*pa->disprect.xmin - re->winx-1)/(float)re->winx;
809         bounds[1]= (2*pa->disprect.xmax - re->winx+1)/(float)re->winx;
810         bounds[2]= (2*pa->disprect.ymin - re->winy-1)/(float)re->winy;
811         bounds[3]= (2*pa->disprect.ymax - re->winy+1)/(float)re->winy;
812
813         memarena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
814         firstseg= NULL;
815         sortseg= sortsegments;
816         totsegment= 0;
817
818         /* for all object instances */
819         for(obi=re->instancetable.first, i=0; obi; obi=obi->next, i++) {
820                 obr= obi->obr;
821
822                 if(!obr->strandbuf || !(obr->strandbuf->lay & rl->lay))
823                         continue;
824
825                 /* compute matrix and try clipping whole object */
826                 if(obi->flag & R_TRANSFORMED)
827                         zbuf_make_winmat(re, obi->mat, winmat);
828                 else
829                         zbuf_make_winmat(re, NULL, winmat);
830
831                 if(clip_render_object(obi->obr->boundbox, bounds, winmat))
832                         continue;
833
834                 /* for each bounding box containing a number of strands */
835                 sbound= obr->strandbuf->bound;
836                 for(c=0; c<obr->strandbuf->totbound; c++, sbound++) {
837                         if(clip_render_object(sbound->boundbox, bounds, winmat))
838                                 continue;
839
840                         /* for each strand in this bounding box */
841                         for(a=sbound->start; a<sbound->end; a++) {
842                                 strand= RE_findOrAddStrand(obr, a);
843                                 svert= strand->vert;
844
845                                 /* keep clipping and z depth for 4 control points */
846                                 clip[1]= strand_test_clip(winmat, &zspan, bounds, svert->co, &z[1]);
847                                 clip[2]= strand_test_clip(winmat, &zspan, bounds, (svert+1)->co, &z[2]);
848                                 clip[0]= clip[1]; z[0]= z[1];
849
850                                 for(b=0; b<strand->totvert-1; b++, svert++) {
851                                         /* compute 4th point clipping and z depth */
852                                         if(b < strand->totvert-2) {
853                                                 clip[3]= strand_test_clip(winmat, &zspan, bounds, (svert+2)->co, &z[3]);
854                                         }
855                                         else {
856                                                 clip[3]= clip[2]; z[3]= z[2];
857                                         }
858
859                                         /* check clipping and add to sortsegments buffer */
860                                         if(!(clip[0] & clip[1] & clip[2] & clip[3])) {
861                                                 sortseg= BLI_memarena_alloc(memarena, sizeof(StrandSortSegment));
862                                                 sortseg->obi= i;
863                                                 sortseg->strand= strand->index;
864                                                 sortseg->segment= b;
865
866                                                 sortseg->z= 0.5f*(z[1] + z[2]);
867
868                                                 sortseg->next= firstseg;
869                                                 firstseg= sortseg;
870                                                 totsegment++;
871                                         }
872
873                                         /* shift clipping and z depth */
874                                         clip[0]= clip[1]; z[0]= z[1];
875                                         clip[1]= clip[2]; z[1]= z[2];
876                                         clip[2]= clip[3]; z[2]= z[3];
877                                 }
878                         }
879                 }
880         }
881
882         if(!re->test_break(re->tbh)) {
883                 /* convert list to array and sort */
884                 sortsegments= MEM_mallocN(sizeof(StrandSortSegment)*totsegment, "StrandSortSegment");
885                 for(a=0, sortseg=firstseg; a<totsegment; a++, sortseg=sortseg->next)
886                         sortsegments[a]= *sortseg;
887                 qsort(sortsegments, totsegment, sizeof(StrandSortSegment), compare_strand_segment);
888         }
889
890         BLI_memarena_free(memarena);
891
892         spart.totapixbuf= MEM_callocN(sizeof(int)*pa->rectx*pa->recty, "totapixbuf");
893
894         if(!re->test_break(re->tbh)) {
895                 /* render segments in sorted order */
896                 sortseg= sortsegments;
897                 for(a=0; a<totsegment; a++, sortseg++) {
898                         if(re->test_break(re->tbh))
899                                 break;
900
901                         obi= &re->objectinstance[sortseg->obi];
902                         obr= obi->obr;
903                         zbuf_make_winmat(re, NULL, winmat);
904
905                         sseg.obi= obi;
906                         sseg.strand= RE_findOrAddStrand(obr, sortseg->strand);
907                         sseg.buffer= sseg.strand->buffer;
908                         sseg.sqadaptcos= sseg.buffer->adaptcos;
909                         sseg.sqadaptcos *= sseg.sqadaptcos;
910
911                         svert= sseg.strand->vert + sortseg->segment;
912                         sseg.v[0]= (sortseg->segment > 0)? (svert-1): svert;
913                         sseg.v[1]= svert;
914                         sseg.v[2]= svert+1;
915                         sseg.v[3]= (sortseg->segment < sseg.strand->totvert-2)? svert+2: svert+1;
916                         sseg.shaded= 0;
917
918                         spart.segment= &sseg;
919
920                         render_strand_segment(re, winmat, &spart, &zspan, 1, &sseg);
921                 }
922         }
923
924         if(sortsegments)
925                 MEM_freeN(sortsegments);
926         MEM_freeN(spart.totapixbuf);
927         
928         zbuf_free_span(&zspan);
929
930         return totsegment;
931 }
932
933 /* *************** */
934
935 StrandSurface *cache_strand_surface(Render *re, ObjectRen *obr, DerivedMesh *dm, float mat[][4], int timeoffset)
936 {
937         StrandSurface *mesh;
938         MFace *mface;
939         MVert *mvert;
940         float (*co)[3];
941         int a, totvert, totface;
942
943         totvert= dm->getNumVerts(dm);
944         totface= dm->getNumFaces(dm);
945
946         for(mesh=re->strandsurface.first; mesh; mesh=mesh->next)
947                 if(mesh->obr.ob == obr->ob && mesh->obr.par == obr->par
948                         && mesh->obr.index == obr->index && mesh->totvert==totvert && mesh->totface==totface)
949                         break;
950
951         if(!mesh) {
952                 mesh= MEM_callocN(sizeof(StrandSurface), "StrandSurface");
953                 mesh->obr= *obr;
954                 mesh->totvert= totvert;
955                 mesh->totface= totface;
956                 mesh->face= MEM_callocN(sizeof(int)*4*mesh->totface, "StrandSurfFaces");
957                 mesh->col= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfCol");
958                 BLI_addtail(&re->strandsurface, mesh);
959         }
960
961         if(timeoffset == -1 && !mesh->prevco)
962                 mesh->prevco= co= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfCo");
963         else if(timeoffset == 0 && !mesh->co)
964                 mesh->co= co= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfCo");
965         else if(timeoffset == 1 && !mesh->nextco)
966                 mesh->nextco= co= MEM_callocN(sizeof(float)*3*mesh->totvert, "StrandSurfCo");
967         else
968                 return mesh;
969
970         mvert= dm->getVertArray(dm);
971         for(a=0; a<mesh->totvert; a++, mvert++) {
972                 VECCOPY(co[a], mvert->co);
973                 Mat4MulVecfl(mat, co[a]);
974         }
975
976         mface= dm->getFaceArray(dm);
977         for(a=0; a<mesh->totface; a++, mface++) {
978                 mesh->face[a][0]= mface->v1;
979                 mesh->face[a][1]= mface->v2;
980                 mesh->face[a][2]= mface->v3;
981                 mesh->face[a][3]= mface->v4;
982         }
983
984         return mesh;
985 }
986
987 void free_strand_surface(Render *re)
988 {
989         StrandSurface *mesh;
990
991         for(mesh=re->strandsurface.first; mesh; mesh=mesh->next) {
992                 if(mesh->co) MEM_freeN(mesh->co);
993                 if(mesh->prevco) MEM_freeN(mesh->prevco);
994                 if(mesh->nextco) MEM_freeN(mesh->nextco);
995                 if(mesh->col) MEM_freeN(mesh->col);
996                 if(mesh->face) MEM_freeN(mesh->face);
997         }
998
999         BLI_freelistN(&re->strandsurface);
1000 }
1001
1002 void strand_minmax(StrandRen *strand, float *min, float *max)
1003 {
1004         StrandVert *svert;
1005         int a;
1006
1007         for(a=0, svert=strand->vert; a<strand->totvert; a++, svert++)
1008                 DO_MINMAX(svert->co, min, max)
1009 }
1010