Merge branch 'master' into blender2.8
[blender.git] / source / blender / render / intern / source / zbuf.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributors: Hos, RPW
22  *               2004-2006 Blender Foundation, full recode
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/render/intern/source/zbuf.c
28  *  \ingroup render
29  */
30
31
32
33 /*---------------------------------------------------------------------------*/
34 /* Common includes                                                           */
35 /*---------------------------------------------------------------------------*/
36
37 #include <math.h>
38 #include <float.h>
39 #include <stdlib.h>
40 #include <limits.h>
41 #include <string.h>
42
43 #include "BLI_math.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_jitter.h"
46 #include "BLI_threads.h"
47 #include "BLI_utildefines.h"
48
49 #include "MEM_guardedalloc.h"
50
51 #include "DNA_lamp_types.h"
52 #include "DNA_node_types.h"
53 #include "DNA_meshdata_types.h"
54 #include "DNA_material_types.h"
55
56 #include "BKE_global.h"
57 #include "BKE_material.h"
58
59
60 #include "RE_render_ext.h"
61
62 /* local includes */
63 #include "pixelblending.h"
64 #include "render_result.h"
65 #include "render_types.h"
66 #include "renderdatabase.h"
67 #include "rendercore.h"
68 #include "shadbuf.h"
69 #include "shading.h"
70 #include "strand.h"
71
72 /* own includes */
73 #include "zbuf.h"
74
75 /* could enable at some point but for now there are far too many conversions */
76 #ifdef __GNUC__
77 #  pragma GCC diagnostic ignored "-Wdouble-promotion"
78 #endif
79
80 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
81 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
82 /* only to be used here in this file, it's for speed */
83 extern struct Render R;
84 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
85
86
87 /* ****************** Spans ******************************* */
88
89 /* each zbuffer has coordinates transformed to local rect coordinates, so we can simply clip */
90 void zbuf_alloc_span(ZSpan *zspan, int rectx, int recty, float clipcrop)
91 {
92         memset(zspan, 0, sizeof(ZSpan));
93         
94         zspan->rectx= rectx;
95         zspan->recty= recty;
96         
97         zspan->span1= MEM_mallocN(recty*sizeof(float), "zspan");
98         zspan->span2= MEM_mallocN(recty*sizeof(float), "zspan");
99
100         zspan->clipcrop= clipcrop;
101 }
102
103 void zbuf_free_span(ZSpan *zspan)
104 {
105         if (zspan) {
106                 if (zspan->span1) MEM_freeN(zspan->span1);
107                 if (zspan->span2) MEM_freeN(zspan->span2);
108                 zspan->span1= zspan->span2= NULL;
109         }
110 }
111
112 /* reset range for clipping */
113 static void zbuf_init_span(ZSpan *zspan)
114 {
115         zspan->miny1= zspan->miny2= zspan->recty+1;
116         zspan->maxy1= zspan->maxy2= -1;
117         zspan->minp1= zspan->maxp1= zspan->minp2= zspan->maxp2= NULL;
118 }
119
120 static void zbuf_add_to_span(ZSpan *zspan, const float v1[2], const float v2[2])
121 {
122         const float *minv, *maxv;
123         float *span;
124         float xx1, dx0, xs0;
125         int y, my0, my2;
126         
127         if (v1[1]<v2[1]) {
128                 minv= v1; maxv= v2;
129         }
130         else {
131                 minv= v2; maxv= v1;
132         }
133         
134         my0= ceil(minv[1]);
135         my2= floor(maxv[1]);
136         
137         if (my2<0 || my0>= zspan->recty) return;
138         
139         /* clip top */
140         if (my2>=zspan->recty) my2= zspan->recty-1;
141         /* clip bottom */
142         if (my0<0) my0= 0;
143         
144         if (my0>my2) return;
145         /* if (my0>my2) should still fill in, that way we get spans that skip nicely */
146         
147         xx1= maxv[1]-minv[1];
148         if (xx1>FLT_EPSILON) {
149                 dx0= (minv[0]-maxv[0])/xx1;
150                 xs0= dx0*(minv[1]-my2) + minv[0];
151         }
152         else {
153                 dx0 = 0.0f;
154                 xs0 = min_ff(minv[0], maxv[0]);
155         }
156         
157         /* empty span */
158         if (zspan->maxp1 == NULL) {
159                 span= zspan->span1;
160         }
161         else {  /* does it complete left span? */
162                 if ( maxv == zspan->minp1 || minv==zspan->maxp1) {
163                         span= zspan->span1;
164                 }
165                 else {
166                         span= zspan->span2;
167                 }
168         }
169
170         if (span==zspan->span1) {
171 //              printf("left span my0 %d my2 %d\n", my0, my2);
172                 if (zspan->minp1==NULL || zspan->minp1[1] > minv[1] ) {
173                         zspan->minp1= minv;
174                 }
175                 if (zspan->maxp1==NULL || zspan->maxp1[1] < maxv[1] ) {
176                         zspan->maxp1= maxv;
177                 }
178                 if (my0<zspan->miny1) zspan->miny1= my0;
179                 if (my2>zspan->maxy1) zspan->maxy1= my2;
180         }
181         else {
182 //              printf("right span my0 %d my2 %d\n", my0, my2);
183                 if (zspan->minp2==NULL || zspan->minp2[1] > minv[1] ) {
184                         zspan->minp2= minv;
185                 }
186                 if (zspan->maxp2==NULL || zspan->maxp2[1] < maxv[1] ) {
187                         zspan->maxp2= maxv;
188                 }
189                 if (my0<zspan->miny2) zspan->miny2= my0;
190                 if (my2>zspan->maxy2) zspan->maxy2= my2;
191         }
192
193         for (y=my2; y>=my0; y--, xs0+= dx0) {
194                 /* xs0 is the xcoord! */
195                 span[y]= xs0;
196         }
197 }
198
199 /*-----------------------------------------------------------*/ 
200 /* Functions                                                 */
201 /*-----------------------------------------------------------*/ 
202
203 void fillrect(int *rect, int x, int y, int val)
204 {
205         int len, *drect;
206
207         len= x*y;
208         drect= rect;
209         while (len>0) {
210                 len--;
211                 *drect= val;
212                 drect++;
213         }
214 }
215
216 /* based on Liang&Barsky, for clipping of pyramidical volume */
217 static short cliptestf(float a, float b, float c, float d, float *u1, float *u2)
218 {
219         float p= a + b, q= c + d, r;
220         
221         if (p<0.0f) {
222                 if (q<p) return 0;
223                 else if (q<0.0f) {
224                         r= q/p;
225                         if (r>*u2) return 0;
226                         else if (r>*u1) *u1=r;
227                 }
228         }
229         else {
230                 if (p>0.0f) {
231                         if (q<0.0f) return 0;
232                         else if (q<p) {
233                                 r= q/p;
234                                 if (r<*u1) return 0;
235                                 else if (r<*u2) *u2=r;
236                         }
237                 }
238                 else if (q<0.0f) return 0;
239         }
240         return 1;
241 }
242
243 int testclip(const float v[4])
244 {
245         float abs4;     /* WATCH IT: this function should do the same as cliptestf, otherwise troubles in zbufclip()*/
246         short c=0;
247         
248         /* if we set clip flags, the clipping should be at least larger than epsilon. 
249          * prevents issues with vertices lying exact on borders */
250         abs4= fabsf(v[3]) + FLT_EPSILON;
251         
252         if ( v[0] < -abs4) c+=1;
253         else if ( v[0] > abs4) c+=2;
254         
255         if ( v[1] > abs4) c+=4;
256         else if ( v[1] < -abs4) c+=8;
257         
258         if (v[2] < -abs4) c+=16;                        /* this used to be " if (v[2]<0) ", see clippz() */
259         else if (v[2]> abs4) c+= 32;
260         
261         return c;
262 }
263
264
265
266 /* *************  ACCUMULATION ZBUF ************ */
267
268
269 static APixstr *addpsmainA(ListBase *lb)
270 {
271         APixstrMain *psm;
272
273         psm= MEM_mallocN(sizeof(APixstrMain), "addpsmainA");
274         BLI_addtail(lb, psm);
275         psm->ps= MEM_callocN(4096*sizeof(APixstr), "pixstr");
276
277         return psm->ps;
278 }
279
280 void freepsA(ListBase *lb)
281 {
282         APixstrMain *psm, *psmnext;
283
284         for (psm= lb->first; psm; psm= psmnext) {
285                 psmnext= psm->next;
286                 if (psm->ps)
287                         MEM_freeN(psm->ps);
288                 MEM_freeN(psm);
289         }
290 }
291
292 static APixstr *addpsA(ZSpan *zspan)
293 {
294         /* make new PS */
295         if (zspan->apsmcounter==0) {
296                 zspan->curpstr= addpsmainA(zspan->apsmbase);
297                 zspan->apsmcounter= 4095;
298         }
299         else {
300                 zspan->curpstr++;
301                 zspan->apsmcounter--;
302         }
303         return zspan->curpstr;
304 }
305
306 static void zbuffillAc4(ZSpan *zspan, int obi, int zvlnr,
307                         const float *v1, const float *v2, const float *v3, const float *v4)
308 {
309         APixstr *ap, *apofs, *apn;
310         double zxd, zyd, zy0, zverg;
311         float x0, y0, z0;
312         float x1, y1, z1, x2, y2, z2, xx1;
313         const float *span1, *span2;
314         int *rz, *rm, x, y;
315         int sn1, sn2, rectx, *rectzofs, *rectmaskofs, my0, my2, mask;
316         
317         /* init */
318         zbuf_init_span(zspan);
319         
320         /* set spans */
321         zbuf_add_to_span(zspan, v1, v2);
322         zbuf_add_to_span(zspan, v2, v3);
323         if (v4) {
324                 zbuf_add_to_span(zspan, v3, v4);
325                 zbuf_add_to_span(zspan, v4, v1);
326         }
327         else
328                 zbuf_add_to_span(zspan, v3, v1);
329         
330         /* clipped */
331         if (zspan->minp2==NULL || zspan->maxp2==NULL) return;
332
333         my0 = max_ii(zspan->miny1, zspan->miny2);
334         my2 = min_ii(zspan->maxy1, zspan->maxy2);
335         
336         if (my2<my0) return;
337         
338         /* ZBUF DX DY, in floats still */
339         x1= v1[0]- v2[0];
340         x2= v2[0]- v3[0];
341         y1= v1[1]- v2[1];
342         y2= v2[1]- v3[1];
343         z1= v1[2]- v2[2];
344         z2= v2[2]- v3[2];
345         x0= y1*z2-z1*y2;
346         y0= z1*x2-x1*z2;
347         z0= x1*y2-y1*x2;
348         
349         if (z0==0.0f) return;
350         
351         xx1= (x0*v1[0] + y0*v1[1])/z0 + v1[2];
352         
353         zxd= -(double)x0/(double)z0;
354         zyd= -(double)y0/(double)z0;
355         zy0= ((double)my2)*zyd + (double)xx1;
356         
357         /* start-offset in rect */
358         rectx= zspan->rectx;
359         rectzofs= (int *)(zspan->arectz+rectx*(my2));
360         rectmaskofs= (int *)(zspan->rectmask+rectx*(my2));
361         apofs= (zspan->apixbuf+ rectx*(my2));
362         mask= zspan->mask;
363
364         /* correct span */
365         sn1= (my0 + my2)/2;
366         if (zspan->span1[sn1] < zspan->span2[sn1]) {
367                 span1= zspan->span1+my2;
368                 span2= zspan->span2+my2;
369         }
370         else {
371                 span1= zspan->span2+my2;
372                 span2= zspan->span1+my2;
373         }
374         
375         for (y=my2; y>=my0; y--, span1--, span2--) {
376                 
377                 sn1= floor(*span1);
378                 sn2= floor(*span2);
379                 sn1++; 
380                 
381                 if (sn2>=rectx) sn2= rectx-1;
382                 if (sn1<0) sn1= 0;
383                 
384                 if (sn2>=sn1) {
385                         int intzverg;
386                         
387                         zverg= (double)sn1*zxd + zy0;
388                         rz= rectzofs+sn1;
389                         rm= rectmaskofs+sn1;
390                         ap= apofs+sn1;
391                         x= sn2-sn1;
392                         
393                         zverg-= zspan->polygon_offset;
394                         
395                         while (x>=0) {
396                                 intzverg = round_db_to_int_clamp(zverg);
397
398                                 if ( intzverg < *rz) {
399                                         if (!zspan->rectmask || intzverg > *rm) {
400                                                 
401                                                 apn= ap;
402                                                 while (apn) {
403                                                         if (apn->p[0]==0) {apn->obi[0]= obi; apn->p[0]= zvlnr; apn->z[0]= intzverg; apn->mask[0]= mask; break; }
404                                                         if (apn->p[0]==zvlnr && apn->obi[0]==obi) {apn->mask[0]|= mask; break; }
405                                                         if (apn->p[1]==0) {apn->obi[1]= obi; apn->p[1]= zvlnr; apn->z[1]= intzverg; apn->mask[1]= mask; break; }
406                                                         if (apn->p[1]==zvlnr && apn->obi[1]==obi) {apn->mask[1]|= mask; break; }
407                                                         if (apn->p[2]==0) {apn->obi[2]= obi; apn->p[2]= zvlnr; apn->z[2]= intzverg; apn->mask[2]= mask; break; }
408                                                         if (apn->p[2]==zvlnr && apn->obi[2]==obi) {apn->mask[2]|= mask; break; }
409                                                         if (apn->p[3]==0) {apn->obi[3]= obi; apn->p[3]= zvlnr; apn->z[3]= intzverg; apn->mask[3]= mask; break; }
410                                                         if (apn->p[3]==zvlnr && apn->obi[3]==obi) {apn->mask[3]|= mask; break; }
411                                                         if (apn->next==NULL) apn->next= addpsA(zspan);
412                                                         apn= apn->next;
413                                                 }
414                                         }
415                                 }
416                                 zverg+= zxd;
417                                 rz++; 
418                                 rm++;
419                                 ap++; 
420                                 x--;
421                         }
422                 }
423                 
424                 zy0-=zyd;
425                 rectzofs-= rectx;
426                 rectmaskofs-= rectx;
427                 apofs-= rectx;
428         }
429 }
430
431
432
433 static void zbuflineAc(ZSpan *zspan, int obi, int zvlnr, const float vec1[3], const float vec2[3])
434 {
435         APixstr *ap, *apn;
436         const int *rectz, *rectmask;
437         int start, end, x, y, oldx, oldy, ofs;
438         int dz, vergz, mask, maxtest=0;
439         float dx, dy;
440         float v1[3], v2[3];
441         
442         dx= vec2[0]-vec1[0];
443         dy= vec2[1]-vec1[1];
444         
445         mask= zspan->mask;
446         
447         if (fabsf(dx) > fabsf(dy)) {
448
449                 /* all lines from left to right */
450                 if (vec1[0]<vec2[0]) {
451                         copy_v3_v3(v1, vec1);
452                         copy_v3_v3(v2, vec2);
453                 }
454                 else {
455                         copy_v3_v3(v2, vec1);
456                         copy_v3_v3(v1, vec2);
457                         dx= -dx; dy= -dy;
458                 }
459
460                 start= floor(v1[0]);
461                 end= start+floor(dx);
462                 if (end>=zspan->rectx) end= zspan->rectx-1;
463                 
464                 oldy= floor(v1[1]);
465                 dy/= dx;
466                 
467                 vergz= v1[2];
468                 vergz-= zspan->polygon_offset;
469                 dz= (v2[2]-v1[2])/dx;
470                 if (vergz>0x50000000 && dz>0) maxtest= 1;  /* prevent overflow */
471                 
472                 rectz= (int *)(zspan->arectz+zspan->rectx*(oldy) +start);
473                 rectmask= (int *)(zspan->rectmask+zspan->rectx*(oldy) +start);
474                 ap= (zspan->apixbuf+ zspan->rectx*(oldy) +start);
475
476                 if (dy<0) ofs= -zspan->rectx;
477                 else ofs= zspan->rectx;
478                 
479                 for (x= start; x<=end; x++, rectz++, rectmask++, ap++) {
480                         
481                         y= floor(v1[1]);
482                         if (y!=oldy) {
483                                 oldy= y;
484                                 rectz+= ofs;
485                                 rectmask+= ofs;
486                                 ap+= ofs;
487                         }
488                         
489                         if (x>=0 && y>=0 && y<zspan->recty) {
490                                 if (vergz<*rectz) {
491                                         if (!zspan->rectmask || vergz>*rectmask) {
492                                         
493                                                 apn= ap;
494                                                 while (apn) {   /* loop unrolled */
495                                                         if (apn->p[0]==0) {apn->obi[0]= obi; apn->p[0]= zvlnr; apn->z[0]= vergz; apn->mask[0]= mask; break; }
496                                                         if (apn->p[0]==zvlnr && apn->obi[0]==obi) {apn->mask[0]|= mask; break; }
497                                                         if (apn->p[1]==0) {apn->obi[1]= obi; apn->p[1]= zvlnr; apn->z[1]= vergz; apn->mask[1]= mask; break; }
498                                                         if (apn->p[1]==zvlnr && apn->obi[1]==obi) {apn->mask[1]|= mask; break; }
499                                                         if (apn->p[2]==0) {apn->obi[2]= obi; apn->p[2]= zvlnr; apn->z[2]= vergz; apn->mask[2]= mask; break; }
500                                                         if (apn->p[2]==zvlnr && apn->obi[2]==obi) {apn->mask[2]|= mask; break; }
501                                                         if (apn->p[3]==0) {apn->obi[3]= obi; apn->p[3]= zvlnr; apn->z[3]= vergz; apn->mask[3]= mask; break; }
502                                                         if (apn->p[3]==zvlnr && apn->obi[3]==obi) {apn->mask[3]|= mask; break; }
503                                                         if (apn->next == NULL) apn->next = addpsA(zspan);
504                                                         apn= apn->next;
505                                                 }
506                                         }
507                                 }
508                         }
509                         
510                         v1[1]+= dy;
511                         if (maxtest && (vergz > 0x7FFFFFF0 - dz)) vergz= 0x7FFFFFF0;
512                         else vergz+= dz;
513                 }
514         }
515         else {
516         
517                 /* all lines from top to bottom */
518                 if (vec1[1]<vec2[1]) {
519                         copy_v3_v3(v1, vec1);
520                         copy_v3_v3(v2, vec2);
521                 }
522                 else {
523                         copy_v3_v3(v2, vec1);
524                         copy_v3_v3(v1, vec2);
525                         dx= -dx; dy= -dy;
526                 }
527
528                 start= floor(v1[1]);
529                 end= start+floor(dy);
530                 
531                 if (start>=zspan->recty || end<0) return;
532                 
533                 if (end>=zspan->recty) end= zspan->recty-1;
534                 
535                 oldx= floor(v1[0]);
536                 dx/= dy;
537                 
538                 vergz= v1[2];
539                 vergz-= zspan->polygon_offset;
540                 dz= (v2[2]-v1[2])/dy;
541                 if (vergz>0x50000000 && dz>0) maxtest= 1;  /* prevent overflow */
542
543                 rectz= (int *)( zspan->arectz+ (start)*zspan->rectx+ oldx );
544                 rectmask= (int *)( zspan->rectmask+ (start)*zspan->rectx+ oldx );
545                 ap= (zspan->apixbuf+ zspan->rectx*(start) +oldx);
546                                 
547                 if (dx<0) ofs= -1;
548                 else ofs= 1;
549
550                 for (y= start; y<=end; y++, rectz+=zspan->rectx, rectmask+=zspan->rectx, ap+=zspan->rectx) {
551                         
552                         x= floor(v1[0]);
553                         if (x!=oldx) {
554                                 oldx= x;
555                                 rectz+= ofs;
556                                 rectmask+= ofs;
557                                 ap+= ofs;
558                         }
559                         
560                         if (x>=0 && y>=0 && x<zspan->rectx) {
561                                 if (vergz<*rectz) {
562                                         if (!zspan->rectmask || vergz>*rectmask) {
563                                                 
564                                                 apn= ap;
565                                                 while (apn) {   /* loop unrolled */
566                                                         if (apn->p[0]==0) {apn->obi[0]= obi; apn->p[0]= zvlnr; apn->z[0]= vergz; apn->mask[0]= mask; break; }
567                                                         if (apn->p[0]==zvlnr) {apn->mask[0]|= mask; break; }
568                                                         if (apn->p[1]==0) {apn->obi[1]= obi; apn->p[1]= zvlnr; apn->z[1]= vergz; apn->mask[1]= mask; break; }
569                                                         if (apn->p[1]==zvlnr) {apn->mask[1]|= mask; break; }
570                                                         if (apn->p[2]==0) {apn->obi[2]= obi; apn->p[2]= zvlnr; apn->z[2]= vergz; apn->mask[2]= mask; break; }
571                                                         if (apn->p[2]==zvlnr) {apn->mask[2]|= mask; break; }
572                                                         if (apn->p[3]==0) {apn->obi[3]= obi; apn->p[3]= zvlnr; apn->z[3]= vergz; apn->mask[3]= mask; break; }
573                                                         if (apn->p[3]==zvlnr) {apn->mask[3]|= mask; break; }
574                                                         if (apn->next == NULL) apn->next = addpsA(zspan);
575                                                         apn= apn->next;
576                                                 }
577                                         }
578                                 }
579                         }
580                         
581                         v1[0]+= dx;
582                         if (maxtest && (vergz > 0x7FFFFFF0 - dz)) vergz= 0x7FFFFFF0;
583                         else vergz+= dz;
584                 }
585         }
586 }
587
588 /* *************  NORMAL ZBUFFER ************ */
589
590 static void zbufline(ZSpan *zspan, int obi, int zvlnr, const float vec1[3], const float vec2[3])
591 {
592         int *rectz, *rectp, *recto, *rectmask;
593         int start, end, x, y, oldx, oldy, ofs;
594         int dz, vergz, maxtest= 0;
595         float dx, dy;
596         float v1[3], v2[3];
597         
598         dx= vec2[0]-vec1[0];
599         dy= vec2[1]-vec1[1];
600         
601         if (fabsf(dx) > fabsf(dy)) {
602
603                 /* all lines from left to right */
604                 if (vec1[0]<vec2[0]) {
605                         copy_v3_v3(v1, vec1);
606                         copy_v3_v3(v2, vec2);
607                 }
608                 else {
609                         copy_v3_v3(v2, vec1);
610                         copy_v3_v3(v1, vec2);
611                         dx= -dx; dy= -dy;
612                 }
613
614                 start= floor(v1[0]);
615                 end= start+floor(dx);
616                 if (end>=zspan->rectx) end= zspan->rectx-1;
617                 
618                 oldy= floor(v1[1]);
619                 dy/= dx;
620                 
621                 vergz= floor(v1[2]);
622                 dz= floor((v2[2]-v1[2])/dx);
623                 if (vergz>0x50000000 && dz>0) maxtest= 1;  /* prevent overflow */
624                 
625                 rectz= zspan->rectz + oldy*zspan->rectx+ start;
626                 rectp= zspan->rectp + oldy*zspan->rectx+ start;
627                 recto= zspan->recto + oldy*zspan->rectx+ start;
628                 rectmask= zspan->rectmask + oldy*zspan->rectx+ start;
629                 
630                 if (dy<0) ofs= -zspan->rectx;
631                 else ofs= zspan->rectx;
632                 
633                 for (x= start; x<=end; x++, rectz++, rectp++, recto++, rectmask++) {
634                         
635                         y= floor(v1[1]);
636                         if (y!=oldy) {
637                                 oldy= y;
638                                 rectz+= ofs;
639                                 rectp+= ofs;
640                                 recto+= ofs;
641                                 rectmask+= ofs;
642                         }
643                         
644                         if (x>=0 && y>=0 && y<zspan->recty) {
645                                 if (vergz<*rectz) {
646                                         if (!zspan->rectmask || vergz>*rectmask) {
647                                                 *recto= obi;
648                                                 *rectz= vergz;
649                                                 *rectp= zvlnr;
650                                         }
651                                 }
652                         }
653                         
654                         v1[1]+= dy;
655                         
656                         if (maxtest && (vergz > 0x7FFFFFF0 - dz)) vergz= 0x7FFFFFF0;
657                         else vergz+= dz;
658                 }
659         }
660         else {
661                 /* all lines from top to bottom */
662                 if (vec1[1]<vec2[1]) {
663                         copy_v3_v3(v1, vec1);
664                         copy_v3_v3(v2, vec2);
665                 }
666                 else {
667                         copy_v3_v3(v2, vec1);
668                         copy_v3_v3(v1, vec2);
669                         dx= -dx; dy= -dy;
670                 }
671
672                 start= floor(v1[1]);
673                 end= start+floor(dy);
674                 
675                 if (end>=zspan->recty) end= zspan->recty-1;
676                 
677                 oldx= floor(v1[0]);
678                 dx/= dy;
679                 
680                 vergz= floor(v1[2]);
681                 dz= floor((v2[2]-v1[2])/dy);
682                 if (vergz>0x50000000 && dz>0) maxtest= 1;  /* prevent overflow */
683                 
684                 rectz= zspan->rectz + start*zspan->rectx+ oldx;
685                 rectp= zspan->rectp + start*zspan->rectx+ oldx;
686                 recto= zspan->recto + start*zspan->rectx+ oldx;
687                 rectmask= zspan->rectmask + start*zspan->rectx+ oldx;
688                 
689                 if (dx<0) ofs= -1;
690                 else ofs= 1;
691
692                 for (y= start; y<=end; y++, rectz+=zspan->rectx, rectp+=zspan->rectx, recto+=zspan->rectx, rectmask+=zspan->rectx) {
693                         
694                         x= floor(v1[0]);
695                         if (x!=oldx) {
696                                 oldx= x;
697                                 rectz+= ofs;
698                                 rectp+= ofs;
699                                 recto+= ofs;
700                                 rectmask+= ofs;
701                         }
702                         
703                         if (x>=0 && y>=0 && x<zspan->rectx) {
704                                 if (vergz<*rectz) {
705                                         if (!zspan->rectmask || vergz>*rectmask) {
706                                                 *rectz= vergz;
707                                                 *rectp= zvlnr;
708                                                 *recto= obi;
709                                         }
710                                 }
711                         }
712                         
713                         v1[0]+= dx;
714                         if (maxtest && (vergz > 0x7FFFFFF0 - dz)) vergz= 0x7FFFFFF0;
715                         else vergz+= dz;
716                 }
717         }
718 }
719
720 static void zbufline_onlyZ(ZSpan *zspan, int UNUSED(obi), int UNUSED(zvlnr), const float vec1[3], const float vec2[3])
721 {
722         int *rectz, *rectz1= NULL;
723         int start, end, x, y, oldx, oldy, ofs;
724         int dz, vergz, maxtest= 0;
725         float dx, dy;
726         float v1[3], v2[3];
727         
728         dx= vec2[0]-vec1[0];
729         dy= vec2[1]-vec1[1];
730         
731         if (fabsf(dx) > fabsf(dy)) {
732                 
733                 /* all lines from left to right */
734                 if (vec1[0]<vec2[0]) {
735                         copy_v3_v3(v1, vec1);
736                         copy_v3_v3(v2, vec2);
737                 }
738                 else {
739                         copy_v3_v3(v2, vec1);
740                         copy_v3_v3(v1, vec2);
741                         dx= -dx; dy= -dy;
742                 }
743                 
744                 start= floor(v1[0]);
745                 end= start+floor(dx);
746                 if (end>=zspan->rectx) end= zspan->rectx-1;
747                 
748                 oldy= floor(v1[1]);
749                 dy/= dx;
750                 
751                 vergz= floor(v1[2]);
752                 dz= floor((v2[2]-v1[2])/dx);
753                 if (vergz>0x50000000 && dz>0) maxtest= 1;  /* prevent overflow */
754                 
755                 rectz= zspan->rectz + oldy*zspan->rectx+ start;
756                 if (zspan->rectz1)
757                         rectz1= zspan->rectz1 + oldy*zspan->rectx+ start;
758                 
759                 if (dy<0) ofs= -zspan->rectx;
760                 else ofs= zspan->rectx;
761                 
762                 for (x= start; x<=end; x++, rectz++) {
763                         
764                         y= floor(v1[1]);
765                         if (y!=oldy) {
766                                 oldy= y;
767                                 rectz+= ofs;
768                                 if (rectz1) rectz1+= ofs;
769                         }
770                         
771                         if (x>=0 && y>=0 && y<zspan->recty) {
772                                 if (vergz < *rectz) {
773                                         if (rectz1) *rectz1= *rectz;
774                                         *rectz= vergz;
775                                 }
776                                 else if (rectz1 && vergz < *rectz1)
777                                         *rectz1= vergz;
778                         }
779                         
780                         v1[1]+= dy;
781                         
782                         if (maxtest && (vergz > 0x7FFFFFF0 - dz)) vergz= 0x7FFFFFF0;
783                         else vergz+= dz;
784                         
785                         if (rectz1) rectz1++;
786                 }
787         }
788         else {
789                 /* all lines from top to bottom */
790                 if (vec1[1]<vec2[1]) {
791                         copy_v3_v3(v1, vec1);
792                         copy_v3_v3(v2, vec2);
793                 }
794                 else {
795                         copy_v3_v3(v2, vec1);
796                         copy_v3_v3(v1, vec2);
797                         dx= -dx; dy= -dy;
798                 }
799                 
800                 start= floor(v1[1]);
801                 end= start+floor(dy);
802                 
803                 if (end>=zspan->recty) end= zspan->recty-1;
804                 
805                 oldx= floor(v1[0]);
806                 dx/= dy;
807                 
808                 vergz= floor(v1[2]);
809                 dz= floor((v2[2]-v1[2])/dy);
810                 if (vergz>0x50000000 && dz>0) maxtest= 1;  /* prevent overflow */
811
812                 rectz= zspan->rectz + start*zspan->rectx+ oldx;
813                 if (zspan->rectz1)
814                         rectz1= zspan->rectz1 + start*zspan->rectx+ oldx;
815                 
816                 if (dx<0) ofs= -1;
817                 else ofs= 1;
818                 
819                 for (y= start; y<=end; y++, rectz+=zspan->rectx) {
820                         
821                         x= floor(v1[0]);
822                         if (x!=oldx) {
823                                 oldx= x;
824                                 rectz+= ofs;
825                                 if (rectz1) rectz1+= ofs;
826                         }
827                         
828                         if (x>=0 && y>=0 && x<zspan->rectx) {
829                                 if (vergz < *rectz) {
830                                         if (rectz1) *rectz1= *rectz;
831                                         *rectz= vergz;
832                                 }
833                                 else if (rectz1 && vergz < *rectz1)
834                                         *rectz1= vergz;
835                         }
836                         
837                         v1[0]+= dx;
838                         if (maxtest && (vergz > 0x7FFFFFF0 - dz)) vergz= 0x7FFFFFF0;
839                         else vergz+= dz;
840                         
841                         if (rectz1)
842                                 rectz1+=zspan->rectx;
843                 }
844         }
845 }
846
847
848 static int clipline(float v1[4], float v2[4])   /* return 0: do not draw */
849 {
850         float dz, dw, u1=0.0, u2=1.0;
851         float dx, dy, v13;
852         
853         dz= v2[2]-v1[2];
854         dw= v2[3]-v1[3];
855         
856         /* this 1.01 is for clipping x and y just a tinsy larger. that way it is
857          * filled in with zbufwire correctly when rendering in parts. otherwise
858          * you see line endings at edges... */
859         
860         if (cliptestf(-dz, -dw, v1[3], v1[2], &u1, &u2)) {
861                 if (cliptestf(dz, -dw, v1[3], -v1[2], &u1, &u2)) {
862                         
863                         dx= v2[0]-v1[0];
864                         dz= 1.01f*(v2[3]-v1[3]);
865                         v13= 1.01f*v1[3];
866                         
867                         if (cliptestf(-dx, -dz, v1[0], v13, &u1, &u2)) {
868                                 if (cliptestf(dx, -dz, v13, -v1[0], &u1, &u2)) {
869                                         
870                                         dy= v2[1]-v1[1];
871                                         
872                                         if (cliptestf(-dy, -dz, v1[1], v13, &u1, &u2)) {
873                                                 if (cliptestf(dy, -dz, v13, -v1[1], &u1, &u2)) {
874                                                         
875                                                         if (u2<1.0f) {
876                                                                 v2[0]= v1[0]+u2*dx;
877                                                                 v2[1]= v1[1]+u2*dy;
878                                                                 v2[2]= v1[2]+u2*dz;
879                                                                 v2[3]= v1[3]+u2*dw;
880                                                         }
881                                                         if (u1>0.0f) {
882                                                                 v1[0]= v1[0]+u1*dx;
883                                                                 v1[1]= v1[1]+u1*dy;
884                                                                 v1[2]= v1[2]+u1*dz;
885                                                                 v1[3]= v1[3]+u1*dw;
886                                                         }
887                                                         return 1;
888                                                 }
889                                         }
890                                 }
891                         }
892                 }
893         }
894         
895         return 0;
896 }
897
898 void hoco_to_zco(ZSpan *zspan, float zco[3], const float hoco[4])
899 {
900         float div;
901         
902         div= 1.0f/hoco[3];
903         zco[0]= zspan->zmulx*(1.0f+hoco[0]*div) + zspan->zofsx;
904         zco[1]= zspan->zmuly*(1.0f+hoco[1]*div) + zspan->zofsy;
905         zco[2]= 0x7FFFFFFF *(hoco[2]*div);
906 }
907
908 void zbufclipwire(ZSpan *zspan, int obi, int zvlnr, int ec,
909                   const float ho1[4], const float ho2[4], const float ho3[4], const float ho4[4],
910                   const int c1, const int c2, const int c3, const int c4)
911 {
912         float vez[20];
913         int and, or;
914
915         /* edgecode: 1= draw */
916         if (ec==0) return;
917
918         if (ho4) {
919                 and= (c1 & c2 & c3 & c4);
920                 or= (c1 | c2 | c3 | c4);
921         }
922         else {
923                 and= (c1 & c2 & c3);
924                 or= (c1 | c2 | c3);
925         }
926         
927         if (or) {       /* not in the middle */
928                 if (and) {      /* out completely */
929                         return;
930                 }
931                 else {  /* clipping */
932
933                         if (ec & ME_V1V2) {
934                                 copy_v4_v4(vez, ho1);
935                                 copy_v4_v4(vez+4, ho2);
936                                 if ( clipline(vez, vez+4)) {
937                                         hoco_to_zco(zspan, vez, vez);
938                                         hoco_to_zco(zspan, vez+4, vez+4);
939                                         zspan->zbuflinefunc(zspan, obi, zvlnr, vez, vez+4);
940                                 }
941                         }
942                         if (ec & ME_V2V3) {
943                                 copy_v4_v4(vez, ho2);
944                                 copy_v4_v4(vez+4, ho3);
945                                 if ( clipline(vez, vez+4)) {
946                                         hoco_to_zco(zspan, vez, vez);
947                                         hoco_to_zco(zspan, vez+4, vez+4);
948                                         zspan->zbuflinefunc(zspan, obi, zvlnr, vez, vez+4);
949                                 }
950                         }
951                         if (ho4) {
952                                 if (ec & ME_V3V4) {
953                                         copy_v4_v4(vez, ho3);
954                                         copy_v4_v4(vez+4, ho4);
955                                         if ( clipline(vez, vez+4)) {
956                                                 hoco_to_zco(zspan, vez, vez);
957                                                 hoco_to_zco(zspan, vez+4, vez+4);
958                                                 zspan->zbuflinefunc(zspan, obi, zvlnr, vez, vez+4);
959                                         }
960                                 }
961                                 if (ec & ME_V4V1) {
962                                         copy_v4_v4(vez, ho4);
963                                         copy_v4_v4(vez+4, ho1);
964                                         if ( clipline(vez, vez+4)) {
965                                                 hoco_to_zco(zspan, vez, vez);
966                                                 hoco_to_zco(zspan, vez+4, vez+4);
967                                                 zspan->zbuflinefunc(zspan, obi, zvlnr, vez, vez+4);
968                                         }
969                                 }
970                         }
971                         else {
972                                 if (ec & ME_V3V1) {
973                                         copy_v4_v4(vez, ho3);
974                                         copy_v4_v4(vez+4, ho1);
975                                         if ( clipline(vez, vez+4)) {
976                                                 hoco_to_zco(zspan, vez, vez);
977                                                 hoco_to_zco(zspan, vez+4, vez+4);
978                                                 zspan->zbuflinefunc(zspan, obi, zvlnr, vez, vez+4);
979                                         }
980                                 }
981                         }
982                         
983                         return;
984                 }
985         }
986
987         hoco_to_zco(zspan, vez, ho1);
988         hoco_to_zco(zspan, vez+4, ho2);
989         hoco_to_zco(zspan, vez+8, ho3);
990         if (ho4) {
991                 hoco_to_zco(zspan, vez+12, ho4);
992
993                 if (ec & ME_V3V4)  zspan->zbuflinefunc(zspan, obi, zvlnr, vez+8, vez+12);
994                 if (ec & ME_V4V1)  zspan->zbuflinefunc(zspan, obi, zvlnr, vez+12, vez);
995         }
996         else {
997                 if (ec & ME_V3V1)  zspan->zbuflinefunc(zspan, obi, zvlnr, vez+8, vez);
998         }
999
1000         if (ec & ME_V1V2)  zspan->zbuflinefunc(zspan, obi, zvlnr, vez, vez+4);
1001         if (ec & ME_V2V3)  zspan->zbuflinefunc(zspan, obi, zvlnr, vez+4, vez+8);
1002
1003 }
1004
1005 void zbufsinglewire(ZSpan *zspan, int obi, int zvlnr, const float ho1[4], const float ho2[4])
1006 {
1007         float f1[4], f2[4];
1008         int c1, c2;
1009
1010         c1= testclip(ho1);
1011         c2= testclip(ho2);
1012
1013         if (c1 | c2) {  /* not in the middle */
1014                 if (!(c1 & c2)) {       /* not out completely */
1015                         copy_v4_v4(f1, ho1);
1016                         copy_v4_v4(f2, ho2);
1017
1018                         if (clipline(f1, f2)) {
1019                                 hoco_to_zco(zspan, f1, f1);
1020                                 hoco_to_zco(zspan, f2, f2);
1021                                 zspan->zbuflinefunc(zspan, obi, zvlnr, f1, f2);
1022                         }
1023                 }
1024         }
1025         else {
1026                 hoco_to_zco(zspan, f1, ho1);
1027                 hoco_to_zco(zspan, f2, ho2);
1028
1029                 zspan->zbuflinefunc(zspan, obi, zvlnr, f1, f2);
1030         }
1031 }
1032
1033 /**
1034  * Fill the z buffer, but invert z order, and add the face index to
1035  * the corresponding face buffer.
1036  *
1037  * This is one of the z buffer fill functions called in zbufclip() and
1038  * zbufwireclip(). 
1039  *
1040  * \param v1 [4 floats, world coordinates] first vertex
1041  * \param v2 [4 floats, world coordinates] second vertex
1042  * \param v3 [4 floats, world coordinates] third vertex
1043  */
1044
1045 /* WATCH IT: zbuffillGLinv4 and zbuffillGL4 are identical except for a 2 lines,
1046  * commented below */
1047 static void zbuffillGLinv4(ZSpan *zspan, int obi, int zvlnr,
1048                            const float *v1, const float *v2, const float *v3, const float *v4)
1049 {
1050         double zxd, zyd, zy0, zverg;
1051         float x0, y0, z0;
1052         float x1, y1, z1, x2, y2, z2, xx1;
1053         const float *span1, *span2;
1054         int *rectoofs, *ro;
1055         int *rectpofs, *rp;
1056         const int *rectmaskofs, *rm;
1057         int *rz, x, y;
1058         int sn1, sn2, rectx, *rectzofs, my0, my2;
1059
1060         /* init */
1061         zbuf_init_span(zspan);
1062
1063         /* set spans */
1064         zbuf_add_to_span(zspan, v1, v2);
1065         zbuf_add_to_span(zspan, v2, v3);
1066         if (v4) {
1067                 zbuf_add_to_span(zspan, v3, v4);
1068                 zbuf_add_to_span(zspan, v4, v1);
1069         }
1070         else
1071                 zbuf_add_to_span(zspan, v3, v1);
1072
1073         /* clipped */
1074         if (zspan->minp2==NULL || zspan->maxp2==NULL) return;
1075
1076         my0 = max_ii(zspan->miny1, zspan->miny2);
1077         my2 = min_ii(zspan->maxy1, zspan->maxy2);
1078
1079         //      printf("my %d %d\n", my0, my2);
1080         if (my2<my0) return;
1081
1082
1083         /* ZBUF DX DY, in floats still */
1084         x1= v1[0]- v2[0];
1085         x2= v2[0]- v3[0];
1086         y1= v1[1]- v2[1];
1087         y2= v2[1]- v3[1];
1088         z1= v1[2]- v2[2];
1089         z2= v2[2]- v3[2];
1090         x0= y1*z2-z1*y2;
1091         y0= z1*x2-x1*z2;
1092         z0= x1*y2-y1*x2;
1093
1094         if (z0==0.0f) return;
1095
1096         xx1= (x0*v1[0] + y0*v1[1])/z0 + v1[2];
1097
1098         zxd= -(double)x0/(double)z0;
1099         zyd= -(double)y0/(double)z0;
1100         zy0= ((double)my2)*zyd + (double)xx1;
1101
1102         /* start-offset in rect */
1103         rectx= zspan->rectx;
1104         rectzofs= (zspan->rectz+rectx*my2);
1105         rectpofs= (zspan->rectp+rectx*my2);
1106         rectoofs= (zspan->recto+rectx*my2);
1107         rectmaskofs= (zspan->rectmask+rectx*my2);
1108
1109         /* correct span */
1110         sn1= (my0 + my2)/2;
1111         if (zspan->span1[sn1] < zspan->span2[sn1]) {
1112                 span1= zspan->span1+my2;
1113                 span2= zspan->span2+my2;
1114         }
1115         else {
1116                 span1= zspan->span2+my2;
1117                 span2= zspan->span1+my2;
1118         }
1119
1120         for (y=my2; y>=my0; y--, span1--, span2--) {
1121
1122                 sn1= floor(*span1);
1123                 sn2= floor(*span2);
1124                 sn1++;
1125
1126                 if (sn2>=rectx) sn2= rectx-1;
1127                 if (sn1<0) sn1= 0;
1128
1129                 if (sn2>=sn1) {
1130                         int intzverg;
1131
1132                         zverg= (double)sn1*zxd + zy0;
1133                         rz= rectzofs+sn1;
1134                         rp= rectpofs+sn1;
1135                         ro= rectoofs+sn1;
1136                         rm= rectmaskofs+sn1;
1137                         x= sn2-sn1;
1138
1139                         while (x>=0) {
1140                                 intzverg = round_db_to_int_clamp(zverg);
1141
1142                                 if ( intzverg > *rz || *rz==0x7FFFFFFF) { /* UNIQUE LINE: see comment above */
1143                                         if (!zspan->rectmask || intzverg > *rm) {
1144                                                 *ro= obi; /* UNIQUE LINE: see comment above (order differs) */
1145                                                 *rz= intzverg;
1146                                                 *rp= zvlnr;
1147                                         }
1148                                 }
1149                                 zverg+= zxd;
1150                                 rz++;
1151                                 rp++;
1152                                 ro++;
1153                                 rm++;
1154                                 x--;
1155                         }
1156                 }
1157
1158                 zy0-=zyd;
1159                 rectzofs-= rectx;
1160                 rectpofs-= rectx;
1161                 rectoofs-= rectx;
1162                 rectmaskofs-= rectx;
1163         }
1164 }
1165
1166 /* uses spanbuffers */
1167
1168 /* WATCH IT: zbuffillGLinv4 and zbuffillGL4 are identical except for a 2 lines,
1169  * commented below */
1170 static void zbuffillGL4(ZSpan *zspan, int obi, int zvlnr,
1171                         const float *v1, const float *v2, const float *v3, const float *v4)
1172 {
1173         double zxd, zyd, zy0, zverg;
1174         float x0, y0, z0;
1175         float x1, y1, z1, x2, y2, z2, xx1;
1176         const float *span1, *span2;
1177         int *rectoofs, *ro;
1178         int *rectpofs, *rp;
1179         const int *rectmaskofs, *rm;
1180         int *rz, x, y;
1181         int sn1, sn2, rectx, *rectzofs, my0, my2;
1182
1183         /* init */
1184         zbuf_init_span(zspan);
1185
1186         /* set spans */
1187         zbuf_add_to_span(zspan, v1, v2);
1188         zbuf_add_to_span(zspan, v2, v3);
1189         if (v4) {
1190                 zbuf_add_to_span(zspan, v3, v4);
1191                 zbuf_add_to_span(zspan, v4, v1);
1192         }
1193         else
1194                 zbuf_add_to_span(zspan, v3, v1);
1195
1196         /* clipped */
1197         if (zspan->minp2==NULL || zspan->maxp2==NULL) return;
1198
1199         my0 = max_ii(zspan->miny1, zspan->miny2);
1200         my2 = min_ii(zspan->maxy1, zspan->maxy2);
1201
1202         //      printf("my %d %d\n", my0, my2);
1203         if (my2<my0) return;
1204
1205
1206         /* ZBUF DX DY, in floats still */
1207         x1= v1[0]- v2[0];
1208         x2= v2[0]- v3[0];
1209         y1= v1[1]- v2[1];
1210         y2= v2[1]- v3[1];
1211         z1= v1[2]- v2[2];
1212         z2= v2[2]- v3[2];
1213         x0= y1*z2-z1*y2;
1214         y0= z1*x2-x1*z2;
1215         z0= x1*y2-y1*x2;
1216
1217         if (z0==0.0f) return;
1218
1219         xx1= (x0*v1[0] + y0*v1[1])/z0 + v1[2];
1220
1221         zxd= -(double)x0/(double)z0;
1222         zyd= -(double)y0/(double)z0;
1223         zy0= ((double)my2)*zyd + (double)xx1;
1224
1225         /* start-offset in rect */
1226         rectx= zspan->rectx;
1227         rectzofs= (zspan->rectz+rectx*my2);
1228         rectpofs= (zspan->rectp+rectx*my2);
1229         rectoofs= (zspan->recto+rectx*my2);
1230         rectmaskofs= (zspan->rectmask+rectx*my2);
1231
1232         /* correct span */
1233         sn1= (my0 + my2)/2;
1234         if (zspan->span1[sn1] < zspan->span2[sn1]) {
1235                 span1= zspan->span1+my2;
1236                 span2= zspan->span2+my2;
1237         }
1238         else {
1239                 span1= zspan->span2+my2;
1240                 span2= zspan->span1+my2;
1241         }
1242
1243         for (y=my2; y>=my0; y--, span1--, span2--) {
1244
1245                 sn1= floor(*span1);
1246                 sn2= floor(*span2);
1247                 sn1++;
1248
1249                 if (sn2>=rectx) sn2= rectx-1;
1250                 if (sn1<0) sn1= 0;
1251
1252                 if (sn2>=sn1) {
1253                         int intzverg;
1254
1255                         zverg= (double)sn1*zxd + zy0;
1256                         rz= rectzofs+sn1;
1257                         rp= rectpofs+sn1;
1258                         ro= rectoofs+sn1;
1259                         rm= rectmaskofs+sn1;
1260                         x= sn2-sn1;
1261
1262                         while (x>=0) {
1263                                 intzverg = round_db_to_int_clamp(zverg);
1264
1265                                 if (intzverg < *rz) { /* ONLY UNIQUE LINE: see comment above */
1266                                         if (!zspan->rectmask || intzverg > *rm) {
1267                                                 *rz= intzverg;
1268                                                 *rp= zvlnr;
1269                                                 *ro= obi; /* UNIQUE LINE: see comment above (order differs) */
1270                                         }
1271                                 }
1272                                 zverg+= zxd;
1273                                 rz++;
1274                                 rp++;
1275                                 ro++;
1276                                 rm++;
1277                                 x--;
1278                         }
1279                 }
1280
1281                 zy0-=zyd;
1282                 rectzofs-= rectx;
1283                 rectpofs-= rectx;
1284                 rectoofs-= rectx;
1285                 rectmaskofs-= rectx;
1286         }
1287 }
1288
1289 /**
1290  * Fill the z buffer. The face buffer is not operated on!
1291  *
1292  * This is one of the z buffer fill functions called in zbufclip() and
1293  * zbufwireclip(). 
1294  *
1295  * \param v1 [4 floats, world coordinates] first vertex
1296  * \param v2 [4 floats, world coordinates] second vertex
1297  * \param v3 [4 floats, world coordinates] third vertex
1298  */
1299
1300 /* now: filling two Z values, the closest and 2nd closest */
1301 static void zbuffillGL_onlyZ(ZSpan *zspan, int UNUSED(obi), int UNUSED(zvlnr),
1302                              const float *v1, const float *v2, const float *v3, const float *v4)
1303 {
1304         double zxd, zyd, zy0, zverg;
1305         float x0, y0, z0;
1306         float x1, y1, z1, x2, y2, z2, xx1;
1307         const float *span1, *span2;
1308         int *rz, *rz1, x, y;
1309         int sn1, sn2, rectx, *rectzofs, *rectzofs1= NULL, my0, my2;
1310         
1311         /* init */
1312         zbuf_init_span(zspan);
1313         
1314         /* set spans */
1315         zbuf_add_to_span(zspan, v1, v2);
1316         zbuf_add_to_span(zspan, v2, v3);
1317         if (v4) {
1318                 zbuf_add_to_span(zspan, v3, v4);
1319                 zbuf_add_to_span(zspan, v4, v1);
1320         }
1321         else 
1322                 zbuf_add_to_span(zspan, v3, v1);
1323         
1324         /* clipped */
1325         if (zspan->minp2==NULL || zspan->maxp2==NULL) return;
1326         
1327         my0 = max_ii(zspan->miny1, zspan->miny2);
1328         my2 = min_ii(zspan->maxy1, zspan->maxy2);
1329         
1330         //      printf("my %d %d\n", my0, my2);
1331         if (my2<my0) return;
1332         
1333         
1334         /* ZBUF DX DY, in floats still */
1335         x1= v1[0]- v2[0];
1336         x2= v2[0]- v3[0];
1337         y1= v1[1]- v2[1];
1338         y2= v2[1]- v3[1];
1339         z1= v1[2]- v2[2];
1340         z2= v2[2]- v3[2];
1341         x0= y1*z2-z1*y2;
1342         y0= z1*x2-x1*z2;
1343         z0= x1*y2-y1*x2;
1344         
1345         if (z0==0.0f) return;
1346         
1347         xx1= (x0*v1[0] + y0*v1[1])/z0 + v1[2];
1348         
1349         zxd= -(double)x0/(double)z0;
1350         zyd= -(double)y0/(double)z0;
1351         zy0= ((double)my2)*zyd + (double)xx1;
1352         
1353         /* start-offset in rect */
1354         rectx= zspan->rectx;
1355         rectzofs= (zspan->rectz+rectx*my2);
1356         if (zspan->rectz1)
1357                 rectzofs1= (zspan->rectz1+rectx*my2);
1358         
1359         /* correct span */
1360         sn1= (my0 + my2)/2;
1361         if (zspan->span1[sn1] < zspan->span2[sn1]) {
1362                 span1= zspan->span1+my2;
1363                 span2= zspan->span2+my2;
1364         }
1365         else {
1366                 span1= zspan->span2+my2;
1367                 span2= zspan->span1+my2;
1368         }
1369         
1370         for (y=my2; y>=my0; y--, span1--, span2--) {
1371                 
1372                 sn1= floor(*span1);
1373                 sn2= floor(*span2);
1374                 sn1++; 
1375                 
1376                 if (sn2>=rectx) sn2= rectx-1;
1377                 if (sn1<0) sn1= 0;
1378                 
1379                 if (sn2>=sn1) {
1380                         zverg= (double)sn1*zxd + zy0;
1381                         rz= rectzofs+sn1;
1382                         rz1= rectzofs1+sn1;
1383                         x= sn2-sn1;
1384                         
1385                         while (x>=0) {
1386                                 int zvergi = round_db_to_int_clamp(zverg);
1387
1388                                 /* option: maintain two depth values, closest and 2nd closest */
1389                                 if (zvergi < *rz) {
1390                                         if (rectzofs1) *rz1= *rz;
1391                                         *rz= zvergi;
1392                                 }
1393                                 else if (rectzofs1 && zvergi < *rz1)
1394                                         *rz1= zvergi;
1395
1396                                 zverg+= zxd;
1397                                 
1398                                 rz++; 
1399                                 rz1++;
1400                                 x--;
1401                         }
1402                 }
1403                 
1404                 zy0-=zyd;
1405                 rectzofs-= rectx;
1406                 if (rectzofs1) rectzofs1-= rectx;
1407         }
1408 }
1409
1410 /* 2d scanconvert for tria, calls func for each x, y coordinate and gives UV barycentrics */
1411 void zspan_scanconvert_strand(ZSpan *zspan, void *handle, float *v1, float *v2, float *v3, void (*func)(void *, int, int, float, float, float) )
1412 {
1413         float x0, y0, x1, y1, x2, y2, z0, z1, z2, z;
1414         float u, v, uxd, uyd, vxd, vyd, uy0, vy0, zxd, zyd, zy0, xx1;
1415         const float *span1, *span2;
1416         int x, y, sn1, sn2, rectx= zspan->rectx, my0, my2;
1417         
1418         /* init */
1419         zbuf_init_span(zspan);
1420         
1421         /* set spans */
1422         zbuf_add_to_span(zspan, v1, v2);
1423         zbuf_add_to_span(zspan, v2, v3);
1424         zbuf_add_to_span(zspan, v3, v1);
1425         
1426         /* clipped */
1427         if (zspan->minp2==NULL || zspan->maxp2==NULL) return;
1428         
1429         my0 = max_ii(zspan->miny1, zspan->miny2);
1430         my2 = min_ii(zspan->maxy1, zspan->maxy2);
1431         
1432         //      printf("my %d %d\n", my0, my2);
1433         if (my2<my0) return;
1434         
1435         /* ZBUF DX DY, in floats still */
1436         x1= v1[0]- v2[0];
1437         x2= v2[0]- v3[0];
1438         y1= v1[1]- v2[1];
1439         y2= v2[1]- v3[1];
1440         z1= v1[2]- v2[2];
1441         z2= v2[2]- v3[2];
1442
1443         x0= y1*z2-z1*y2;
1444         y0= z1*x2-x1*z2;
1445         z0= x1*y2-y1*x2;
1446         
1447         if (z0==0.0f) return;
1448
1449         xx1= (x0*v1[0] + y0*v1[1])/z0 + v1[2];
1450         zxd= -(double)x0/(double)z0;
1451         zyd= -(double)y0/(double)z0;
1452         zy0= ((double)my2)*zyd + (double)xx1;
1453
1454         z1= 1.0f; /* (u1 - u2) */
1455         z2= 0.0f; /* (u2 - u3) */
1456
1457         x0= y1*z2-z1*y2;
1458         y0= z1*x2-x1*z2;
1459
1460         xx1= (x0*v1[0] + y0*v1[1])/z0 + 1.0f;
1461         uxd= -(double)x0/(double)z0;
1462         uyd= -(double)y0/(double)z0;
1463         uy0= ((double)my2)*uyd + (double)xx1;
1464
1465         z1= -1.0f; /* (v1 - v2) */
1466         z2= 1.0f;  /* (v2 - v3) */
1467
1468         x0= y1*z2-z1*y2;
1469         y0= z1*x2-x1*z2;
1470         
1471         xx1= (x0*v1[0] + y0*v1[1])/z0;
1472         vxd= -(double)x0/(double)z0;
1473         vyd= -(double)y0/(double)z0;
1474         vy0= ((double)my2)*vyd + (double)xx1;
1475         
1476         /* correct span */
1477         sn1= (my0 + my2)/2;
1478         if (zspan->span1[sn1] < zspan->span2[sn1]) {
1479                 span1= zspan->span1+my2;
1480                 span2= zspan->span2+my2;
1481         }
1482         else {
1483                 span1= zspan->span2+my2;
1484                 span2= zspan->span1+my2;
1485         }
1486         
1487         for (y=my2; y>=my0; y--, span1--, span2--) {
1488                 
1489                 sn1= floor(*span1);
1490                 sn2= floor(*span2);
1491                 sn1++; 
1492                 
1493                 if (sn2>=rectx) sn2= rectx-1;
1494                 if (sn1<0) sn1= 0;
1495                 
1496                 u= (double)sn1*uxd + uy0;
1497                 v= (double)sn1*vxd + vy0;
1498                 z= (double)sn1*zxd + zy0;
1499                 
1500                 for (x= sn1; x<=sn2; x++, u+=uxd, v+=vxd, z+=zxd)
1501                         func(handle, x, y, u, v, z);
1502                 
1503                 uy0 -= uyd;
1504                 vy0 -= vyd;
1505                 zy0 -= zyd;
1506         }
1507 }
1508
1509 /* scanconvert for strand triangles, calls func for each x, y coordinate and gives UV barycentrics and z */
1510
1511 void zspan_scanconvert(ZSpan *zspan, void *handle, float *v1, float *v2, float *v3, void (*func)(void *, int, int, float, float) )
1512 {
1513         float x0, y0, x1, y1, x2, y2, z0, z1, z2;
1514         float u, v, uxd, uyd, vxd, vyd, uy0, vy0, xx1;
1515         const float *span1, *span2;
1516         int i, j, x, y, sn1, sn2, rectx = zspan->rectx, my0, my2;
1517         
1518         /* init */
1519         zbuf_init_span(zspan);
1520         
1521         /* set spans */
1522         zbuf_add_to_span(zspan, v1, v2);
1523         zbuf_add_to_span(zspan, v2, v3);
1524         zbuf_add_to_span(zspan, v3, v1);
1525         
1526         /* clipped */
1527         if (zspan->minp2==NULL || zspan->maxp2==NULL) return;
1528         
1529         my0 = max_ii(zspan->miny1, zspan->miny2);
1530         my2 = min_ii(zspan->maxy1, zspan->maxy2);
1531         
1532         //      printf("my %d %d\n", my0, my2);
1533         if (my2<my0) return;
1534         
1535         /* ZBUF DX DY, in floats still */
1536         x1= v1[0]- v2[0];
1537         x2= v2[0]- v3[0];
1538         y1= v1[1]- v2[1];
1539         y2= v2[1]- v3[1];
1540         
1541         z1= 1.0f; /* (u1 - u2) */
1542         z2= 0.0f; /* (u2 - u3) */
1543
1544         x0= y1*z2-z1*y2;
1545         y0= z1*x2-x1*z2;
1546         z0= x1*y2-y1*x2;
1547
1548         if (z0==0.0f) return;
1549
1550         xx1= (x0*v1[0] + y0*v1[1])/z0 + 1.0f;
1551         uxd= -(double)x0/(double)z0;
1552         uyd= -(double)y0/(double)z0;
1553         uy0= ((double)my2)*uyd + (double)xx1;
1554
1555         z1= -1.0f; /* (v1 - v2) */
1556         z2= 1.0f;  /* (v2 - v3) */
1557
1558         x0= y1*z2-z1*y2;
1559         y0= z1*x2-x1*z2;
1560         
1561         xx1= (x0*v1[0] + y0*v1[1])/z0;
1562         vxd= -(double)x0/(double)z0;
1563         vyd= -(double)y0/(double)z0;
1564         vy0= ((double)my2)*vyd + (double)xx1;
1565         
1566         /* correct span */
1567         span1= zspan->span1+my2;
1568         span2= zspan->span2+my2;
1569         
1570         for (i = 0, y = my2; y >= my0; i++, y--, span1--, span2--) {
1571                 
1572                 sn1= floor(min_ff(*span1, *span2));
1573                 sn2= floor(max_ff(*span1, *span2));
1574                 sn1++; 
1575                 
1576                 if (sn2>=rectx) sn2= rectx-1;
1577                 if (sn1<0) sn1= 0;
1578                 
1579                 u = (((double)sn1 * uxd) + uy0) - (i * uyd);
1580                 v = (((double)sn1 * vxd) + vy0) - (i * vyd);
1581                 
1582                 for (j = 0, x = sn1; x <= sn2; j++, x++) {
1583                         func(handle, x, y, u + (j * uxd), v + (j * vxd));
1584                 }
1585         }
1586 }
1587
1588
1589
1590 /**
1591  * (clip pyramid)
1592  * Sets lambda: flag, and parametrize the clipping of vertices in
1593  * viewspace coordinates. lambda = -1 means no clipping, lambda in [0, 1] means a clipping.
1594  * Note: uses globals.
1595  * \param v1 start coordinate s
1596  * \param v2 target coordinate t
1597  * \param b2 
1598  * \param b3
1599  * \param a index for coordinate (x, y, or z)
1600  */
1601
1602 static void clippyra(float *lambda, float *v1, float *v2, int *b2, int *b3, int a, float clipcrop)
1603 {
1604         float da, dw, u1=0.0, u2=1.0;
1605         float v13;
1606         
1607         lambda[0]= -1.0;
1608         lambda[1]= -1.0;
1609
1610         da= v2[a]-v1[a];
1611         /* prob; we clip slightly larger, osa renders add 2 pixels on edges, should become variable? */
1612         /* or better; increase r.winx/y size, but thats quite a complex one. do it later */
1613         if (a==2) {
1614                 dw= (v2[3]-v1[3]);
1615                 v13= v1[3];
1616         }
1617         else {
1618                 dw= clipcrop*(v2[3]-v1[3]);
1619                 v13= clipcrop*v1[3];
1620         }
1621         /* according the original article by Liang&Barsky, for clipping of
1622          * homogeneous coordinates with viewplane, the value of "0" is used instead of "-w" .
1623          * This differs from the other clipping cases (like left or top) and I considered
1624          * it to be not so 'homogenic'. But later it has proven to be an error,
1625          * who would have thought that of L&B!
1626          */
1627
1628         if (cliptestf(-da, -dw, v13, v1[a], &u1, &u2)) {
1629                 if (cliptestf(da, -dw, v13, -v1[a], &u1, &u2)) {
1630                         *b3=1;
1631                         if (u2<1.0f) {
1632                                 lambda[1]= u2;
1633                                 *b2=1;
1634                         }
1635                         else lambda[1]=1.0;  /* u2 */
1636                         if (u1>0.0f) {
1637                                 lambda[0] = u1;
1638                                 *b2 = 1;
1639                         }
1640                         else {
1641                                 lambda[0] = 0.0;
1642                         }
1643                 }
1644         }
1645 }
1646
1647 /**
1648  * (make vertex pyramide clip)
1649  * Checks lambda and uses this to make decision about clipping the line
1650  * segment from v1 to v2. lambda is the factor by which the vector is
1651  * cut. ( calculate s + l * ( t - s )). The result is appended to the
1652  * vertex list of this face.
1653  * 
1654  * 
1655  * \param v1 start coordinate s
1656  * \param v2 target coordinate t
1657  * \param b1 
1658  * \param b2 
1659  * \param clve vertex vector.
1660  */
1661
1662 static void makevertpyra(float *vez, float *lambda, float **trias, float *v1, float *v2, int *b1, int *clve)
1663 {
1664         float l1, l2, *adr;
1665
1666         l1= lambda[0];
1667         l2= lambda[1];
1668
1669         if (l1!= -1.0f) {
1670                 if (l1!= 0.0f) {
1671                         adr= vez+4*(*clve);
1672                         trias[*b1]=adr;
1673                         (*clve)++;
1674                         adr[0]= v1[0]+l1*(v2[0]-v1[0]);
1675                         adr[1]= v1[1]+l1*(v2[1]-v1[1]);
1676                         adr[2]= v1[2]+l1*(v2[2]-v1[2]);
1677                         adr[3]= v1[3]+l1*(v2[3]-v1[3]);
1678                 }
1679                 else trias[*b1]= v1;
1680                 
1681                 (*b1)++;
1682         }
1683         if (l2!= -1.0f) {
1684                 if (l2!= 1.0f) {
1685                         adr= vez+4*(*clve);
1686                         trias[*b1]=adr;
1687                         (*clve)++;
1688                         adr[0]= v1[0]+l2*(v2[0]-v1[0]);
1689                         adr[1]= v1[1]+l2*(v2[1]-v1[1]);
1690                         adr[2]= v1[2]+l2*(v2[2]-v1[2]);
1691                         adr[3]= v1[3]+l2*(v2[3]-v1[3]);
1692                         (*b1)++;
1693                 }
1694         }
1695 }
1696
1697 /* ------------------------------------------------------------------------- */
1698
1699 void projectverto(const float v1[3], float winmat[4][4], float adr[4])
1700 {
1701         /* calcs homogenic coord of vertex v1 */
1702         float x, y, z;
1703
1704         x = v1[0];
1705         y = v1[1];
1706         z = v1[2];
1707         adr[0] = x * winmat[0][0] + z * winmat[2][0] + winmat[3][0];
1708         adr[1] = y * winmat[1][1] + z * winmat[2][1] + winmat[3][1];
1709         adr[2] =                    z * winmat[2][2] + winmat[3][2];
1710         adr[3] =                    z * winmat[2][3] + winmat[3][3];
1711
1712         //printf("hoco %f %f %f %f\n", adr[0], adr[1], adr[2], adr[3]);
1713 }
1714
1715 /* ------------------------------------------------------------------------- */
1716
1717 void projectvert(const float v1[3], float winmat[4][4], float adr[4])
1718 {
1719         /* calcs homogenic coord of vertex v1 */
1720         float x, y, z;
1721
1722         x = v1[0];
1723         y = v1[1];
1724         z = v1[2];
1725         adr[0] = x * winmat[0][0] + y * winmat[1][0] + z * winmat[2][0] + winmat[3][0];
1726         adr[1] = x * winmat[0][1] + y * winmat[1][1] + z * winmat[2][1] + winmat[3][1];
1727         adr[2] = x * winmat[0][2] + y * winmat[1][2] + z * winmat[2][2] + winmat[3][2];
1728         adr[3] = x * winmat[0][3] + y * winmat[1][3] + z * winmat[2][3] + winmat[3][3];
1729 }
1730
1731 /* ------------------------------------------------------------------------- */
1732
1733 #define ZBUF_PROJECT_CACHE_SIZE 256
1734
1735 typedef struct ZbufProjectCache {
1736         int index, clip;
1737         float ho[4];
1738 } ZbufProjectCache;
1739
1740 static void zbuf_project_cache_clear(ZbufProjectCache *cache, int size)
1741 {
1742         int i;
1743
1744         if (size > ZBUF_PROJECT_CACHE_SIZE)
1745                 size= ZBUF_PROJECT_CACHE_SIZE;
1746
1747         memset(cache, 0, sizeof(ZbufProjectCache)*size);
1748         for (i=0; i<size; i++)
1749                 cache[i].index= -1;
1750 }
1751
1752 static int zbuf_shadow_project(ZbufProjectCache *cache, int index, float winmat[4][4], float *co, float *ho)
1753 {
1754         int cindex= index & 255;
1755
1756         if (cache[cindex].index == index) {
1757                 copy_v4_v4(ho, cache[cindex].ho);
1758                 return cache[cindex].clip;
1759         }
1760         else {
1761                 int clipflag;
1762                 projectvert(co, winmat, ho);
1763                 clipflag= testclip(ho);
1764
1765                 copy_v4_v4(cache[cindex].ho, ho);
1766                 cache[cindex].clip= clipflag;
1767                 cache[cindex].index= index;
1768
1769                 return clipflag;
1770         }
1771 }
1772
1773 static void zbuffer_part_bounds(int winx, int winy, RenderPart *pa, float *bounds)
1774 {
1775         bounds[0]= (2*pa->disprect.xmin - winx-1)/(float)winx;
1776         bounds[1]= (2*pa->disprect.xmax - winx+1)/(float)winx;
1777         bounds[2]= (2*pa->disprect.ymin - winy-1)/(float)winy;
1778         bounds[3]= (2*pa->disprect.ymax - winy+1)/(float)winy;
1779 }
1780
1781 static int zbuf_part_project(ZbufProjectCache *cache, int index, float winmat[4][4], float *bounds, float *co, float *ho)
1782 {
1783         float vec[3];
1784         int cindex= index & 255;
1785
1786         if (cache[cindex].index == index) {
1787                 copy_v4_v4(ho, cache[cindex].ho);
1788                 return cache[cindex].clip;
1789         }
1790         else {
1791                 float wco;
1792                 int clipflag= 0;
1793                 copy_v3_v3(vec, co);
1794                 projectvert(co, winmat, ho);
1795
1796                 wco= ho[3];
1797                 if (ho[0] < bounds[0]*wco) clipflag |= 1;
1798                 else if (ho[0] > bounds[1]*wco) clipflag |= 2;
1799                 if (ho[1] > bounds[3]*wco) clipflag |= 4;
1800                 else if (ho[1] < bounds[2]*wco) clipflag |= 8;
1801
1802                 copy_v4_v4(cache[cindex].ho, ho);
1803                 cache[cindex].clip= clipflag;
1804                 cache[cindex].index= index;
1805
1806                 return clipflag;
1807         }
1808 }
1809
1810 void zbuf_render_project(float winmat[4][4], const float co[3], float ho[4])
1811 {
1812         float vec[3];
1813
1814         copy_v3_v3(vec, co);
1815         projectvert(vec, winmat, ho);
1816 }
1817
1818 void zbuf_make_winmat(Render *re, float winmat[4][4])
1819 {
1820         if (re->r.mode & R_PANORAMA) {
1821                 float panomat[4][4];
1822
1823                 unit_m4(panomat);
1824
1825                 panomat[0][0]= re->panoco;
1826                 panomat[0][2]= re->panosi;
1827                 panomat[2][0]= -re->panosi;
1828                 panomat[2][2]= re->panoco;
1829
1830                 mul_m4_m4m4(winmat, re->winmat, panomat);
1831         }
1832         else
1833                 copy_m4_m4(winmat, re->winmat);
1834 }
1835
1836 /* do zbuffering and clip, f1 f2 f3 are hocos, c1 c2 c3 are clipping flags */
1837
1838 void zbufclip(ZSpan *zspan, int obi, int zvlnr,
1839               const float f1[4], const float f2[4], const float f3[4],
1840               const int c1, const int c2, const int c3)
1841 {
1842         float *vlzp[32][3], lambda[3][2];
1843         float vez[400], *trias[40];
1844         
1845         if (c1 | c2 | c3) {     /* not in middle */
1846                 if (c1 & c2 & c3) {     /* completely out */
1847                         return;
1848                 }
1849                 else {  /* clipping */
1850                         int arg, v, b, clipflag[3], b1, b2, b3, c4, clve=3, clvlo, clvl=1;
1851                         float *fp;
1852
1853                         vez[0]= f1[0]; vez[1]= f1[1]; vez[2]= f1[2]; vez[3]= f1[3];
1854                         vez[4]= f2[0]; vez[5]= f2[1]; vez[6]= f2[2]; vez[7]= f2[3];
1855                         vez[8]= f3[0]; vez[9]= f3[1]; vez[10]= f3[2];vez[11]= f3[3];
1856
1857                         vlzp[0][0]= vez;
1858                         vlzp[0][1]= vez+4;
1859                         vlzp[0][2]= vez+8;
1860
1861                         clipflag[0]= ( (c1 & 48) | (c2 & 48) | (c3 & 48) );
1862                         if (clipflag[0]==0) {   /* othwerwise it needs to be calculated again, after the first (z) clip */
1863                                 clipflag[1]= ( (c1 & 3) | (c2 & 3) | (c3 & 3) );
1864                                 clipflag[2]= ( (c1 & 12) | (c2 & 12) | (c3 & 12) );
1865                         }
1866                         else clipflag[1]=clipflag[2]= 0;
1867                         
1868                         for (b=0;b<3;b++) {
1869                                 
1870                                 if (clipflag[b]) {
1871                                 
1872                                         clvlo= clvl;
1873                                         
1874                                         for (v=0; v<clvlo; v++) {
1875                                         
1876                                                 if (vlzp[v][0]!=NULL) { /* face is still there */
1877                                                         b2= b3 =0;      /* clip flags */
1878
1879                                                         if (b==0) arg= 2;
1880                                                         else if (b==1) arg= 0;
1881                                                         else arg= 1;
1882                                                         
1883                                                         clippyra(lambda[0], vlzp[v][0], vlzp[v][1], &b2, &b3, arg, zspan->clipcrop);
1884                                                         clippyra(lambda[1], vlzp[v][1], vlzp[v][2], &b2, &b3, arg, zspan->clipcrop);
1885                                                         clippyra(lambda[2], vlzp[v][2], vlzp[v][0], &b2, &b3, arg, zspan->clipcrop);
1886
1887                                                         if (b2==0 && b3==1) {
1888                                                                 /* completely 'in', but we copy because of last for () loop in this section */;
1889                                                                 vlzp[clvl][0]= vlzp[v][0];
1890                                                                 vlzp[clvl][1]= vlzp[v][1];
1891                                                                 vlzp[clvl][2]= vlzp[v][2];
1892                                                                 vlzp[v][0]= NULL;
1893                                                                 clvl++;
1894                                                         }
1895                                                         else if (b3==0) {
1896                                                                 vlzp[v][0]= NULL;
1897                                                                 /* completely 'out' */;
1898                                                         }
1899                                                         else {
1900                                                                 b1=0;
1901                                                                 makevertpyra(vez, lambda[0], trias, vlzp[v][0], vlzp[v][1], &b1, &clve);
1902                                                                 makevertpyra(vez, lambda[1], trias, vlzp[v][1], vlzp[v][2], &b1, &clve);
1903                                                                 makevertpyra(vez, lambda[2], trias, vlzp[v][2], vlzp[v][0], &b1, &clve);
1904
1905                                                                 /* after front clip done: now set clip flags */
1906                                                                 if (b==0) {
1907                                                                         clipflag[1]= clipflag[2]= 0;
1908                                                                         f1= vez;
1909                                                                         for (b3=0; b3<clve; b3++) {
1910                                                                                 c4= testclip(f1);
1911                                                                                 clipflag[1] |= (c4 & 3);
1912                                                                                 clipflag[2] |= (c4 & 12);
1913                                                                                 f1+= 4;
1914                                                                         }
1915                                                                 }
1916                                                                 
1917                                                                 vlzp[v][0]= NULL;
1918                                                                 if (b1>2) {
1919                                                                         for (b3=3; b3<=b1; b3++) {
1920                                                                                 vlzp[clvl][0]= trias[0];
1921                                                                                 vlzp[clvl][1]= trias[b3-2];
1922                                                                                 vlzp[clvl][2]= trias[b3-1];
1923                                                                                 clvl++;
1924                                                                         }
1925                                                                 }
1926                                                         }
1927                                                 }
1928                                         }
1929                                 }
1930                         }
1931
1932                         /* warning, clip overflow, this should never happen! */
1933                         BLI_assert(!(clve > 38 || clvl > 31));
1934
1935                         /* perspective division */
1936                         fp = vez;
1937                         for (b = 0; b < clve; b++) {
1938                                 hoco_to_zco(zspan, fp, fp);
1939                                 fp += 4;
1940                         }
1941                         for (b = 1; b < clvl; b++) {
1942                                 if (vlzp[b][0]) {
1943                                         zspan->zbuffunc(zspan, obi, zvlnr, vlzp[b][0], vlzp[b][1], vlzp[b][2], NULL);
1944                                 }
1945                         }
1946                         return;
1947                 }
1948         }
1949
1950         /* perspective division: HCS to ZCS */
1951         hoco_to_zco(zspan, vez, f1);
1952         hoco_to_zco(zspan, vez+4, f2);
1953         hoco_to_zco(zspan, vez+8, f3);
1954         zspan->zbuffunc(zspan, obi, zvlnr, vez, vez+4, vez+8, NULL);
1955 }
1956
1957 void zbufclip4(ZSpan *zspan, int obi, int zvlnr,
1958                const float f1[4], const float f2[4], const float f3[4], const float f4[4],
1959                const int c1, const int c2, const int c3, const int c4)
1960 {
1961         float vez[16];
1962         
1963         if (c1 | c2 | c3 | c4) {        /* not in middle */
1964                 if (c1 & c2 & c3 & c4) {        /* completely out */
1965                         return;
1966                 }
1967                 else {  /* clipping */
1968                         zbufclip(zspan, obi, zvlnr, f1, f2, f3, c1, c2, c3);
1969                         zbufclip(zspan, obi, zvlnr, f1, f3, f4, c1, c3, c4);
1970                 }
1971                 return;
1972         }
1973
1974         /* perspective division: HCS to ZCS */
1975         hoco_to_zco(zspan, vez, f1);
1976         hoco_to_zco(zspan, vez+4, f2);
1977         hoco_to_zco(zspan, vez+8, f3);
1978         hoco_to_zco(zspan, vez+12, f4);
1979
1980         zspan->zbuffunc(zspan, obi, zvlnr, vez, vez+4, vez+8, vez+12);
1981 }
1982
1983 /* ************** ZMASK ******************************** */
1984
1985 #define EXTEND_PIXEL(a) if (temprectp[a]) { z += rectz[a]; tot++; } (void)0
1986
1987 /* changes the zbuffer to be ready for z-masking: applies an extend-filter, and then clears */
1988 static void zmask_rect(int *rectz, int *rectp, int xs, int ys, int neg)
1989 {
1990         int len=0, x, y;
1991         int *temprectp;
1992         int row1, row2, row3, *curp, *curz;
1993         
1994         temprectp= MEM_dupallocN(rectp);
1995         
1996         /* extend: if pixel is not filled in, we check surrounding pixels and average z value  */
1997         
1998         for (y=1; y<=ys; y++) {
1999                 /* setup row indices */
2000                 row1= (y-2)*xs;
2001                 row2= row1 + xs;
2002                 row3= row2 + xs;
2003                 if (y==1)
2004                         row1= row2;
2005                 else if (y==ys)
2006                         row3= row2;
2007                 
2008                 curp= rectp + (y-1)*xs;
2009                 curz= rectz + (y-1)*xs;
2010                 
2011                 for (x=0; x<xs; x++, curp++, curz++) {
2012                         if (curp[0]==0) {
2013                                 int tot= 0;
2014                                 float z= 0.0f;
2015                                 
2016                                 EXTEND_PIXEL(row1);
2017                                 EXTEND_PIXEL(row2);
2018                                 EXTEND_PIXEL(row3);
2019                                 EXTEND_PIXEL(row1 + 1);
2020                                 EXTEND_PIXEL(row3 + 1);
2021                                 if (x!=xs-1) {
2022                                         EXTEND_PIXEL(row1 + 2);
2023                                         EXTEND_PIXEL(row2 + 2);
2024                                         EXTEND_PIXEL(row3 + 2);
2025                                 }
2026                                 if (tot) {
2027                                         len++;
2028                                         curz[0]= (int)(z/(float)tot);
2029                                         curp[0]= -1;    /* env */
2030                                 }
2031                         }
2032                         
2033                         if (x!=0) {
2034                                 row1++; row2++; row3++;
2035                         }
2036                 }
2037         }
2038
2039         MEM_freeN(temprectp);
2040         
2041         if (neg) {
2042                 /* z values for negative are already correct */
2043         }
2044         else {
2045                 /* clear not filled z values */
2046                 for (len= xs*ys -1; len>=0; len--) {
2047                         if (rectp[len]==0) {
2048                                 rectz[len] = -0x7FFFFFFF;
2049                                 rectp[len]= -1; /* env code */
2050                         }
2051                 }
2052         }
2053 }
2054
2055
2056 /* ***************** ZBUFFER MAIN ROUTINES **************** */
2057
2058 void zbuffer_solid(RenderPart *pa, RenderLayer *rl, void(*fillfunc)(RenderPart *, ZSpan *, int, void *), void *data)
2059 {
2060         ZbufProjectCache cache[ZBUF_PROJECT_CACHE_SIZE];
2061         ZSpan zspans[16], *zspan; /* 16 = RE_MAX_OSA */
2062         VlakRen *vlr= NULL;
2063         VertRen *v1, *v2, *v3, *v4;
2064         Material *ma = NULL;
2065         ObjectInstanceRen *obi;
2066         ObjectRen *obr;
2067         float obwinmat[4][4], winmat[4][4], bounds[4];
2068         float ho1[4], ho2[4], ho3[4], ho4[4]={0};
2069         unsigned int lay= rl->lay, lay_zmask= rl->lay_zmask;
2070         int i, v, zvlnr, zsample, samples, c1, c2, c3, c4=0;
2071         short nofill=0, env=0, wire=0, zmaskpass=0;
2072         const bool all_z = (rl->layflag & SCE_LAY_ALL_Z) && !(rl->layflag & SCE_LAY_ZMASK);
2073         const bool neg_zmask = (rl->layflag & SCE_LAY_ZMASK) && (rl->layflag & SCE_LAY_NEG_ZMASK);
2074
2075         zbuf_make_winmat(&R, winmat);
2076         
2077         samples= (R.osa? R.osa: 1);
2078         samples= MIN2(4, samples-pa->sample);
2079
2080         for (zsample=0; zsample<samples; zsample++) {
2081                 zspan= &zspans[zsample];
2082
2083                 zbuffer_part_bounds(R.winx, R.winy, pa, bounds);
2084                 zbuf_alloc_span(zspan, pa->rectx, pa->recty, R.clipcrop);
2085                 
2086                 /* needed for transform from hoco to zbuffer co */
2087                 zspan->zmulx= ((float)R.winx)/2.0f;
2088                 zspan->zmuly= ((float)R.winy)/2.0f;
2089                 
2090                 if (R.osa) {
2091                         zspan->zofsx= -pa->disprect.xmin - R.jit[pa->sample+zsample][0];
2092                         zspan->zofsy= -pa->disprect.ymin - R.jit[pa->sample+zsample][1];
2093                 }
2094                 else if (R.i.curblur) {
2095                         zspan->zofsx= -pa->disprect.xmin - R.mblur_jit[R.i.curblur-1][0];
2096                         zspan->zofsy= -pa->disprect.ymin - R.mblur_jit[R.i.curblur-1][1];
2097                 }
2098                 else {
2099                         zspan->zofsx= -pa->disprect.xmin;
2100                         zspan->zofsy= -pa->disprect.ymin;
2101                 }
2102                 /* to center the sample position */
2103                 zspan->zofsx -= 0.5f;
2104                 zspan->zofsy -= 0.5f;
2105                 
2106                 /* the buffers */
2107                 if (zsample == samples-1) {
2108                         zspan->rectp= pa->rectp;
2109                         zspan->recto= pa->recto;
2110
2111                         if (neg_zmask)
2112                                 zspan->rectz= pa->rectmask;
2113                         else
2114                                 zspan->rectz= pa->rectz;
2115                 }
2116                 else {
2117                         zspan->recto= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "recto");
2118                         zspan->rectp= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectp");
2119                         zspan->rectz= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectz");
2120                 }
2121
2122                 fillrect(zspan->rectz, pa->rectx, pa->recty, 0x7FFFFFFF);
2123                 fillrect(zspan->rectp, pa->rectx, pa->recty, 0);
2124                 fillrect(zspan->recto, pa->rectx, pa->recty, 0);
2125         }
2126
2127         /* in case zmask we fill Z for objects in lay_zmask first, then clear Z, and then do normal zbuffering */
2128         if (rl->layflag & SCE_LAY_ZMASK)
2129                 zmaskpass= 1;
2130         
2131         for (; zmaskpass >=0; zmaskpass--) {
2132                 ma= NULL;
2133
2134                 /* filling methods */
2135                 for (zsample=0; zsample<samples; zsample++) {
2136                         zspan= &zspans[zsample];
2137
2138                         if (zmaskpass && neg_zmask)
2139                                 zspan->zbuffunc= zbuffillGLinv4;
2140                         else
2141                                 zspan->zbuffunc= zbuffillGL4;
2142                         zspan->zbuflinefunc= zbufline;
2143                 }
2144
2145                 /* regular zbuffering loop, does all sample buffers */
2146                 for (i=0, obi=R.instancetable.first; obi; i++, obi=obi->next) {
2147                         obr= obi->obr;
2148
2149                         /* continue happens in 2 different ways... zmaskpass only does lay_zmask stuff */
2150                         if (zmaskpass) {
2151                                 if ((obi->lay & lay_zmask)==0)
2152                                         continue;
2153                         }
2154                         else if (!all_z && !(obi->lay & (lay|lay_zmask)))
2155                                 continue;
2156                         
2157                         if (obi->flag & R_TRANSFORMED)
2158                                 mul_m4_m4m4(obwinmat, winmat, obi->mat);
2159                         else
2160                                 copy_m4_m4(obwinmat, winmat);
2161
2162                         if (clip_render_object(obi->obr->boundbox, bounds, obwinmat))
2163                                 continue;
2164
2165                         zbuf_project_cache_clear(cache, obr->totvert);
2166
2167                         for (v=0; v<obr->totvlak; v++) {
2168                                 if ((v & 255)==0) vlr= obr->vlaknodes[v>>8].vlak;
2169                                 else vlr++;
2170
2171                                 /* the cases: visible for render, only z values, zmask, nothing */
2172                                 if (obi->lay & lay) {
2173                                         if (vlr->mat!=ma) {
2174                                                 ma= vlr->mat;
2175                                                 nofill= (ma->mode & MA_ONLYCAST) || ((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP));
2176                                                 env= (ma->mode & MA_ENV);
2177                                                 wire= (ma->material_type == MA_TYPE_WIRE);
2178                                                 
2179                                                 for (zsample=0; zsample<samples; zsample++) {
2180                                                         if (ma->mode & MA_ZINV || (zmaskpass && neg_zmask))
2181                                                                 zspans[zsample].zbuffunc= zbuffillGLinv4;
2182                                                         else
2183                                                                 zspans[zsample].zbuffunc= zbuffillGL4;
2184                                                 }
2185                                         }
2186                                 }
2187                                 else if (all_z || (obi->lay & lay_zmask)) {
2188                                         env= 1;
2189                                         nofill= 0;
2190                                         ma= NULL; 
2191                                 }
2192                                 else {
2193                                         nofill= 1;
2194                                         ma= NULL;       /* otherwise nofill can hang */
2195                                 }
2196
2197                                 if (!(vlr->flag & R_HIDDEN) && nofill==0) {
2198                                         unsigned short partclip;
2199                                         
2200                                         v1= vlr->v1;
2201                                         v2= vlr->v2;
2202                                         v3= vlr->v3;
2203                                         v4= vlr->v4;
2204
2205                                         c1= zbuf_part_project(cache, v1->index, obwinmat, bounds, v1->co, ho1);
2206                                         c2= zbuf_part_project(cache, v2->index, obwinmat, bounds, v2->co, ho2);
2207                                         c3= zbuf_part_project(cache, v3->index, obwinmat, bounds, v3->co, ho3);
2208
2209                                         /* partclipping doesn't need viewplane clipping */
2210                                         partclip= c1 & c2 & c3;
2211                                         if (v4) {
2212                                                 c4= zbuf_part_project(cache, v4->index, obwinmat, bounds, v4->co, ho4);
2213                                                 partclip &= c4;
2214                                         }
2215
2216                                         if (partclip==0) {
2217                                                 
2218                                                 if (env) zvlnr= -1;
2219                                                 else zvlnr= v+1;
2220
2221                                                 c1= testclip(ho1);
2222                                                 c2= testclip(ho2);
2223                                                 c3= testclip(ho3);
2224                                                 if (v4)
2225                                                         c4= testclip(ho4);
2226
2227                                                 for (zsample=0; zsample<samples; zsample++) {
2228                                                         zspan= &zspans[zsample];
2229
2230                                                         if (wire) {
2231                                                                 if (v4)
2232                                                                         zbufclipwire(zspan, i, zvlnr, vlr->ec, ho1, ho2, ho3, ho4, c1, c2, c3, c4);
2233                                                                 else
2234                                                                         zbufclipwire(zspan, i, zvlnr, vlr->ec, ho1, ho2, ho3, NULL, c1, c2, c3, 0);
2235                                                         }
2236                                                         else {
2237                                                                 /* strands allow to be filled in as quad */
2238                                                                 if (v4 && (vlr->flag & R_STRAND)) {
2239                                                                         zbufclip4(zspan, i, zvlnr, ho1, ho2, ho3, ho4, c1, c2, c3, c4);
2240                                                                 }
2241                                                                 else {
2242                                                                         zbufclip(zspan, i, zvlnr, ho1, ho2, ho3, c1, c2, c3);
2243                                                                         if (v4)
2244                                                                                 zbufclip(zspan, i, (env)? zvlnr: zvlnr+RE_QUAD_OFFS, ho1, ho3, ho4, c1, c3, c4);
2245                                                                 }
2246                                                         }
2247                                                 }
2248                                         }
2249                                 }
2250                         }
2251                 }
2252                 
2253                 /* clear all z to close value, so it works as mask for next passes (ztra+strand) */
2254                 if (zmaskpass) {
2255                         for (zsample=0; zsample<samples; zsample++) {
2256                                 zspan= &zspans[zsample];
2257
2258                                 if (neg_zmask) {
2259                                         zspan->rectmask= zspan->rectz;
2260                                         if (zsample == samples-1)
2261                                                 zspan->rectz= pa->rectz;
2262                                         else
2263                                                 zspan->rectz= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectz");
2264                                         fillrect(zspan->rectz, pa->rectx, pa->recty, 0x7FFFFFFF);
2265
2266                                         zmask_rect(zspan->rectmask, zspan->rectp, pa->rectx, pa->recty, 1);
2267                                 }
2268                                 else
2269                                         zmask_rect(zspan->rectz, zspan->rectp, pa->rectx, pa->recty, 0);
2270                         }
2271                 }
2272         }
2273
2274         for (zsample=0; zsample<samples; zsample++) {
2275                 zspan= &zspans[zsample];
2276
2277                 if (fillfunc)
2278                         fillfunc(pa, zspan, pa->sample+zsample, data);
2279
2280                 if (zsample != samples-1) {
2281                         MEM_freeN(zspan->rectz);
2282                         MEM_freeN(zspan->rectp);
2283                         MEM_freeN(zspan->recto);
2284                         if (zspan->rectmask)
2285                                 MEM_freeN(zspan->rectmask);
2286                 }
2287
2288                 zbuf_free_span(zspan);
2289         }
2290 }
2291
2292 void zbuffer_shadow(Render *re, float winmat[4][4], LampRen *lar, int *rectz, int size, float jitx, float jity)
2293 {
2294         ZbufProjectCache cache[ZBUF_PROJECT_CACHE_SIZE];
2295         ZSpan zspan;
2296         ObjectInstanceRen *obi;
2297         ObjectRen *obr;
2298         VlakRen *vlr= NULL;
2299         Material *ma= NULL;
2300         StrandSegment sseg;
2301         StrandRen *strand= NULL;
2302         StrandVert *svert;
2303         StrandBound *sbound;
2304         float obwinmat[4][4], ho1[4], ho2[4], ho3[4], ho4[4];
2305         int a, b, c, i, c1, c2, c3, c4, ok=1, lay= -1;
2306
2307         if (lar->mode & (LA_LAYER|LA_LAYER_SHADOW)) lay= lar->lay;
2308
2309         /* 1.0f for clipping in clippyra()... bad stuff actually */
2310         zbuf_alloc_span(&zspan, size, size, 1.0f);
2311         zspan.zmulx=  ((float)size)/2.0f;
2312         zspan.zmuly=  ((float)size)/2.0f;
2313         /* -0.5f to center the sample position */
2314         zspan.zofsx= jitx - 0.5f;
2315         zspan.zofsy= jity - 0.5f;
2316         
2317         /* the buffers */
2318         zspan.rectz= rectz;
2319         fillrect(rectz, size, size, 0x7FFFFFFE);
2320         if (lar->buftype==LA_SHADBUF_HALFWAY) {
2321                 zspan.rectz1= MEM_mallocN(size*size*sizeof(int), "seconday z buffer");
2322                 fillrect(zspan.rectz1, size, size, 0x7FFFFFFE);
2323         }
2324         
2325         /* filling methods */
2326         zspan.zbuflinefunc= zbufline_onlyZ;
2327         zspan.zbuffunc= zbuffillGL_onlyZ;
2328
2329         for (i=0, obi=re->instancetable.first; obi; i++, obi=obi->next) {
2330                 obr= obi->obr;
2331
2332                 if (obr->ob==re->excludeob)
2333                         continue;
2334                 else if (!(obi->lay & lay))
2335                         continue;
2336
2337                 if (obi->flag & R_TRANSFORMED)
2338                         mul_m4_m4m4(obwinmat, winmat, obi->mat);
2339                 else
2340                         copy_m4_m4(obwinmat, winmat);
2341
2342                 if (clip_render_object(obi->obr->boundbox, NULL, obwinmat))
2343                         continue;
2344
2345                 zbuf_project_cache_clear(cache, obr->totvert);
2346
2347                 /* faces */
2348                 for (a=0; a<obr->totvlak; a++) {
2349
2350                         if ((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
2351                         else vlr++;
2352
2353                         /* note, these conditions are copied in shadowbuf_autoclip() */
2354                         if (vlr->mat!= ma) {
2355                                 ma= vlr->mat;
2356                                 ok= 1;
2357                                 if ((ma->mode2 & MA_CASTSHADOW)==0 || (ma->mode & MA_SHADBUF)==0) ok= 0;
2358                         }
2359
2360                         if (ok && (obi->lay & lay) && !(vlr->flag & R_HIDDEN)) {
2361                                 c1= zbuf_shadow_project(cache, vlr->v1->index, obwinmat, vlr->v1->co, ho1);
2362                                 c2= zbuf_shadow_project(cache, vlr->v2->index, obwinmat, vlr->v2->co, ho2);
2363                                 c3= zbuf_shadow_project(cache, vlr->v3->index, obwinmat, vlr->v3->co, ho3);
2364
2365                                 if ((ma->material_type == MA_TYPE_WIRE) || (vlr->flag & R_STRAND)) {
2366                                         if (vlr->v4) {
2367                                                 c4= zbuf_shadow_project(cache, vlr->v4->index, obwinmat, vlr->v4->co, ho4);
2368                                                 zbufclipwire(&zspan, 0, a+1, vlr->ec, ho1, ho2, ho3, ho4, c1, c2, c3, c4);
2369                                         }
2370                                         else
2371                                                 zbufclipwire(&zspan, 0, a+1, vlr->ec, ho1, ho2, ho3, NULL, c1, c2, c3, 0);
2372                                 }
2373                                 else {
2374                                         if (vlr->v4) {
2375                                                 c4= zbuf_shadow_project(cache, vlr->v4->index, obwinmat, vlr->v4->co, ho4);
2376                                                 zbufclip4(&zspan, 0, 0, ho1, ho2, ho3, ho4, c1, c2, c3, c4);
2377                                         }
2378                                         else
2379                                                 zbufclip(&zspan, 0, 0, ho1, ho2, ho3, c1, c2, c3);
2380                                 }
2381                         }
2382
2383                         if ((a & 255)==255 && re->test_break(re->tbh)) 
2384                                 break;
2385                 }
2386
2387                 /* strands */
2388                 if (obr->strandbuf) {
2389                         /* for each bounding box containing a number of strands */
2390                         sbound= obr->strandbuf->bound;
2391                         for (c=0; c<obr->strandbuf->totbound; c++, sbound++) {
2392                                 if (clip_render_object(sbound->boundbox, NULL, obwinmat))
2393                                         continue;
2394
2395                                 /* for each strand in this bounding box */
2396                                 for (a=sbound->start; a<sbound->end; a++) {
2397                                         strand= RE_findOrAddStrand(obr, a);
2398
2399                                         sseg.obi= obi;
2400                                         sseg.buffer= strand->buffer;
2401                                         sseg.sqadaptcos= sseg.buffer->adaptcos;
2402                                         sseg.sqadaptcos *= sseg.sqadaptcos;
2403                                         sseg.strand= strand;
2404                                         svert= strand->vert;
2405
2406                                         /* note, these conditions are copied in shadowbuf_autoclip() */
2407                                         if (sseg.buffer->ma!= ma) {
2408                                                 ma= sseg.buffer->ma;
2409                                                 ok= 1;
2410                                                 if ((ma->mode2 & MA_CASTSHADOW)==0 || (ma->mode & MA_SHADBUF)==0) ok= 0;
2411                                         }
2412
2413                                         if (ok && (sseg.buffer->lay & lay)) {
2414                                                 zbuf_project_cache_clear(cache, strand->totvert);
2415
2416                                                 for (b=0; b<strand->totvert-1; b++, svert++) {
2417                                                         sseg.v[0]= (b > 0)? (svert-1): svert;
2418                                                         sseg.v[1]= svert;
2419                                                         sseg.v[2]= svert+1;
2420                                                         sseg.v[3]= (b < strand->totvert-2)? svert+2: svert+1;
2421
2422                                                         c1= zbuf_shadow_project(cache, sseg.v[0]-strand->vert, obwinmat, sseg.v[0]->co, ho1);
2423                                                         c2= zbuf_shadow_project(cache, sseg.v[1]-strand->vert, obwinmat, sseg.v[1]->co, ho2);
2424                                                         c3= zbuf_shadow_project(cache, sseg.v[2]-strand->vert, obwinmat, sseg.v[2]->co, ho3);
2425                                                         c4= zbuf_shadow_project(cache, sseg.v[3]-strand->vert, obwinmat, sseg.v[3]->co, ho4);
2426
2427                                                         if (!(c1 & c2 & c3 & c4))
2428                                                                 render_strand_segment(re, winmat, NULL, &zspan, 1, &sseg);
2429                                                 }
2430                                         }
2431
2432                                         if ((a & 255)==255 && re->test_break(re->tbh)) 
2433                                                 break;
2434                                 }
2435                         }
2436                 }
2437
2438                 if (re->test_break(re->tbh)) 
2439                         break;
2440         }
2441         
2442         /* merge buffers */
2443         if (lar->buftype==LA_SHADBUF_HALFWAY) {
2444                 for (a=size*size -1; a>=0; a--)
2445                         rectz[a]= (rectz[a]>>1) + (zspan.rectz1[a]>>1);
2446                 
2447                 MEM_freeN(zspan.rectz1);
2448         }
2449         
2450         zbuf_free_span(&zspan);
2451 }
2452
2453 static void zbuffill_sss(ZSpan *zspan, int obi, int zvlnr,
2454                          const float *v1, const float *v2, const float *v3, const float *v4)
2455 {
2456         double zxd, zyd, zy0, z;
2457         float x0, y0, x1, y1, x2, y2, z0, z1, z2, xx1, *span1, *span2;
2458         int x, y, sn1, sn2, rectx= zspan->rectx, my0, my2;
2459         /* init */
2460         zbuf_init_span(zspan);
2461         
2462         /* set spans */
2463         zbuf_add_to_span(zspan, v1, v2);
2464         zbuf_add_to_span(zspan, v2, v3);
2465         if (v4) {
2466                 zbuf_add_to_span(zspan, v3, v4);
2467                 zbuf_add_to_span(zspan, v4, v1);
2468         }
2469         else 
2470                 zbuf_add_to_span(zspan, v3, v1);
2471         
2472         /* clipped */
2473         if (zspan->minp2==NULL || zspan->maxp2==NULL) return;
2474         
2475         my0 = max_ii(zspan->miny1, zspan->miny2);
2476         my2 = min_ii(zspan->maxy1, zspan->maxy2);
2477         
2478         if (my2<my0) return;
2479         
2480         /* ZBUF DX DY, in floats still */
2481         x1= v1[0]- v2[0];
2482         x2= v2[0]- v3[0];
2483         y1= v1[1]- v2[1];
2484         y2= v2[1]- v3[1];
2485         z1= v1[2]- v2[2];
2486         z2= v2[2]- v3[2];
2487         
2488         x0= y1*z2-z1*y2;
2489         y0= z1*x2-x1*z2;
2490         z0= x1*y2-y1*x2;
2491         
2492         if (z0==0.0f) return;
2493         
2494         xx1= (x0*v1[0] + y0*v1[1])/z0 + v1[2];
2495         zxd= -(double)x0/(double)z0;
2496         zyd= -(double)y0/(double)z0;
2497         zy0= ((double)my2)*zyd + (double)xx1;
2498         
2499         /* correct span */
2500         sn1= (my0 + my2)/2;
2501         if (zspan->span1[sn1] < zspan->span2[sn1]) {
2502                 span1= zspan->span1+my2;
2503                 span2= zspan->span2+my2;
2504         }
2505         else {
2506                 span1= zspan->span2+my2;
2507                 span2= zspan->span1+my2;
2508         }
2509         
2510         for (y=my2; y>=my0; y--, span1--, span2--) {
2511                 sn1= floor(*span1);
2512                 sn2= floor(*span2);
2513                 sn1++; 
2514                 
2515                 if (sn2>=rectx) sn2= rectx-1;
2516                 if (sn1<0) sn1= 0;
2517                 
2518                 z= (double)sn1*zxd + zy0;
2519                 
2520                 for (x= sn1; x<=sn2; x++, z+=zxd)
2521                         zspan->sss_func(zspan->sss_handle, obi, zvlnr, x, y, z);
2522                 
2523                 zy0 -= zyd;
2524         }
2525 }
2526
2527 void zbuffer_sss(RenderPart *pa, unsigned int lay, void *handle, void (*func)(void *, int, int, int, int, int))
2528 {
2529         ZbufProjectCache cache[ZBUF_PROJECT_CACHE_SIZE];
2530         ZSpan zspan;
2531         ObjectInstanceRen *obi;
2532         ObjectRen *obr;
2533         VlakRen *vlr= NULL;
2534         VertRen *v1, *v2, *v3, *v4;
2535         Material *ma = NULL, *sss_ma = R.sss_mat;
2536         float obwinmat[4][4], winmat[4][4], bounds[4];
2537         float ho1[4], ho2[4], ho3[4], ho4[4]={0};
2538         int i, v, zvlnr, c1, c2, c3, c4=0;
2539         short nofill=0, env=0, wire=0;
2540         
2541         zbuf_make_winmat(&R, winmat);
2542         zbuffer_part_bounds(R.winx, R.winy, pa, bounds);
2543         zbuf_alloc_span(&zspan, pa->rectx, pa->recty, R.clipcrop);
2544
2545         zspan.sss_handle= handle;
2546         zspan.sss_func= func;
2547         
2548         /* needed for transform from hoco to zbuffer co */
2549         zspan.zmulx=  ((float)R.winx)/2.0f;
2550         zspan.zmuly=  ((float)R.winy)/2.0f;
2551         
2552         /* -0.5f to center the sample position */
2553         zspan.zofsx= -pa->disprect.xmin - 0.5f;
2554         zspan.zofsy= -pa->disprect.ymin - 0.5f;
2555         
2556         /* filling methods */
2557         zspan.zbuffunc= zbuffill_sss;
2558
2559         /* fill front and back zbuffer */
2560         if (pa->rectz) {
2561                 fillrect(pa->recto, pa->rectx, pa->recty, 0); 
2562                 fillrect(pa->rectp, pa->rectx, pa->recty, 0); 
2563                 fillrect(pa->rectz, pa->rectx, pa->recty, 0x7FFFFFFF);
2564         }
2565         if (pa->rectbackz) {
2566                 fillrect(pa->rectbacko, pa->rectx, pa->recty, 0); 
2567                 fillrect(pa->rectbackp, pa->rectx, pa->recty, 0); 
2568                 fillrect(pa->rectbackz, pa->rectx, pa->recty, -0x7FFFFFFF);
2569         }
2570
2571         for (i=0, obi=R.instancetable.first; obi; i++, obi=obi->next) {
2572                 obr= obi->obr;
2573
2574                 if (!(obi->lay & lay))
2575                         continue;
2576
2577                 if (obi->flag & R_TRANSFORMED)
2578                         mul_m4_m4m4(obwinmat, winmat, obi->mat);
2579                 else
2580                         copy_m4_m4(obwinmat, winmat);
2581
2582                 if (clip_render_object(obi->obr->boundbox, bounds, obwinmat))
2583                         continue;
2584
2585                 zbuf_project_cache_clear(cache, obr->totvert);
2586
2587                 for (v=0; v<obr->totvlak; v++) {
2588                         if ((v & 255)==0) vlr= obr->vlaknodes[v>>8].vlak;
2589                         else vlr++;
2590                         
2591                         if (material_in_material(vlr->mat, sss_ma)) {
2592                                 /* three cases, visible for render, only z values and nothing */
2593                                 if (obi->lay & lay) {
2594                                         if (vlr->mat!=ma) {
2595                                                 ma= vlr->mat;
2596                                                 nofill= ma->mode & MA_ONLYCAST;
2597                                                 env= (ma->mode & MA_ENV);
2598                                                 wire= (ma->material_type == MA_TYPE_WIRE);
2599                                         }
2600                                 }
2601                                 else {
2602                                         nofill= 1;
2603                                         ma= NULL;       /* otherwise nofill can hang */
2604                                 }
2605                                 
2606                                 if (nofill==0 && wire==0 && env==0) {
2607                                         unsigned short partclip;
2608                                         
2609                                         v1= vlr->v1;
2610                                         v2= vlr->v2;
2611                                         v3= vlr->v3;
2612                                         v4= vlr->v4;
2613
2614                                         c1= zbuf_part_project(cache, v1->index, obwinmat, bounds, v1->co, ho1);
2615                                         c2= zbuf_part_project(cache, v2->index, obwinmat, bounds, v2->co, ho2);
2616                                         c3= zbuf_part_project(cache, v3->index, obwinmat, bounds, v3->co, ho3);
2617
2618                                         /* partclipping doesn't need viewplane clipping */
2619                                         partclip= c1 & c2 & c3;
2620                                         if (v4) {
2621                                                 c4= zbuf_part_project(cache, v4->index, obwinmat, bounds, v4->co, ho4);
2622                                                 partclip &= c4;
2623                                         }
2624
2625                                         if (partclip==0) {
2626                                                 c1= testclip(ho1);
2627                                                 c2= testclip(ho2);
2628                                                 c3= testclip(ho3);
2629
2630                                                 zvlnr= v+1;
2631                                                 zbufclip(&zspan, i, zvlnr, ho1, ho2, ho3, c1, c2, c3);
2632                                                 if (v4) {
2633                                                         c4= testclip(ho4);
2634                                                         zbufclip(&zspan, i, zvlnr+RE_QUAD_OFFS, ho1, ho3, ho4, c1, c3, c4);
2635                                                 }
2636                                         }
2637                                 }
2638                         }
2639                 }
2640         }
2641                 
2642         zbuf_free_span(&zspan);
2643 }
2644
2645 /* ******************** VECBLUR ACCUM BUF ************************* */
2646
2647 typedef struct DrawBufPixel {
2648         const float *colpoin;
2649         float alpha;
2650 } DrawBufPixel;
2651
2652
2653 static void zbuf_fill_in_rgba(ZSpan *zspan, DrawBufPixel *col, float *v1, float *v2, float *v3, float *v4)
2654 {
2655         DrawBufPixel *rectpofs, *rp;
2656         double zxd, zyd, zy0, zverg;
2657         float x0, y0, z0;
2658         float x1, y1, z1, x2, y2, z2, xx1;
2659         const float *span1, *span2;
2660         float *rectzofs, *rz;
2661         int x, y;
2662         int sn1, sn2, rectx, my0, my2;
2663         
2664         /* init */
2665         zbuf_init_span(zspan);
2666         
2667         /* set spans */
2668         zbuf_add_to_span(zspan, v1, v2);
2669         zbuf_add_to_span(zspan, v2, v3);
2670         zbuf_add_to_span(zspan, v3, v4);
2671         zbuf_add_to_span(zspan, v4, v1);
2672         
2673         /* clipped */
2674         if (zspan->minp2==NULL || zspan->maxp2==NULL) return;
2675         
2676         my0 = max_ii(zspan->miny1, zspan->miny2);
2677         my2 = min_ii(zspan->maxy1, zspan->maxy2);
2678         
2679         //      printf("my %d %d\n", my0, my2);
2680         if (my2<my0) return;
2681         
2682         /* ZBUF DX DY, in floats still */
2683         x1= v1[0]- v2[0];
2684         x2= v2[0]- v3[0];
2685         y1= v1[1]- v2[1];
2686         y2= v2[1]- v3[1];
2687         z1= v1[2]- v2[2];
2688         z2= v2[2]- v3[2];
2689         x0= y1*z2-z1*y2;
2690         y0= z1*x2-x1*z2;
2691         z0= x1*y2-y1*x2;
2692         
2693         if (z0==0.0f) return;
2694         
2695         xx1= (x0*v1[0] + y0*v1[1])/z0 + v1[2];
2696         
2697         zxd= -(double)x0/(double)z0;
2698         zyd= -(double)y0/(double)z0;
2699         zy0= ((double)my2)*zyd + (double)xx1;
2700         
2701         /* start-offset in rect */
2702         rectx= zspan->rectx;
2703         rectzofs= (float *)(zspan->rectz + rectx*my2);
2704         rectpofs= ((DrawBufPixel *)zspan->rectp) + rectx*my2;
2705         
2706         /* correct span */
2707         sn1= (my0 + my2)/2;
2708         if (zspan->span1[sn1] < zspan->span2[sn1]) {
2709                 span1= zspan->span1+my2;
2710                 span2= zspan->span2+my2;
2711         }
2712         else {
2713                 span1= zspan->span2+my2;
2714                 span2= zspan->span1+my2;
2715         }
2716         
2717         for (y=my2; y>=my0; y--, span1--, span2--) {
2718                 
2719                 sn1= floor(*span1);
2720                 sn2= floor(*span2);
2721                 sn1++; 
2722                 
2723                 if (sn2>=rectx) sn2= rectx-1;
2724                 if (sn1<0) sn1= 0;
2725                 
2726                 if (sn2>=sn1) {
2727                         zverg= (double)sn1*zxd + zy0;
2728                         rz= rectzofs+sn1;
2729                         rp= rectpofs+sn1;
2730                         x= sn2-sn1;
2731                         
2732                         while (x>=0) {
2733                                 if (zverg < (double)*rz) {
2734                                         *rz= zverg;
2735                                         *rp= *col;
2736                                 }
2737                                 zverg+= zxd;
2738                                 rz++; 
2739                                 rp++; 
2740                                 x--;
2741                         }
2742                 }
2743                 
2744                 zy0-=zyd;
2745                 rectzofs-= rectx;
2746                 rectpofs-= rectx;
2747         }
2748 }
2749
2750 /* char value==255 is filled in, rest should be zero */
2751 /* returns alpha values, but sets alpha to 1 for zero alpha pixels that have an alpha value as neighbor */
2752 void antialias_tagbuf(int xsize, int ysize, char *rectmove)
2753 {
2754         char *row1, *row2, *row3;
2755         char prev, next;
2756         int a, x, y, step;
2757         
2758         /* 1: tag pixels to be candidate for AA */
2759         for (y=2; y<ysize; y++) {
2760                 /* setup rows */
2761                 row1= rectmove + (y-2)*xsize;
2762                 row2= row1 + xsize;
2763                 row3= row2 + xsize;
2764                 for (x=2; x<xsize; x++, row1++, row2++, row3++) {
2765                         if (row2[1]) {
2766                                 if (row2[0]==0 || row2[2]==0 || row1[1]==0 || row3[1]==0)
2767                                         row2[1]= 128;
2768                         }
2769                 }
2770         }
2771         
2772         /* 2: evaluate horizontal scanlines and calculate alphas */
2773         row1= rectmove;
2774         for (y=0; y<ysize; y++) {
2775                 row1++;
2776                 for (x=1; x<xsize; x++, row1++) {
2777                         if (row1[0]==128 && row1[1]==128) {
2778                                 /* find previous color and next color and amount of steps to blend */
2779                                 prev= row1[-1];
2780                                 step= 1;
2781                                 while (x+step<xsize && row1[step]==128)
2782                                         step++;
2783                                 
2784                                 if (x+step!=xsize) {
2785                                         /* now we can blend values */
2786                                         next= row1[step];
2787
2788                                         /* note, prev value can be next value, but we do this loop to clear 128 then */
2789                                         for (a=0; a<step; a++) {
2790                                                 int fac, mfac;
2791                                                 
2792                                                 fac= ((a+1)<<8)/(step+1);
2793                                                 mfac= 255-fac;
2794                                                 
2795                                                 row1[a]= (prev*mfac + next*fac)>>8; 
2796                                         }
2797                                 }
2798                         }
2799                 }
2800         }
2801         
2802         /* 3: evaluate vertical scanlines and calculate alphas */
2803         /*    use for reading a copy of the original tagged buffer */
2804         for (x=0; x<xsize; x++) {
2805                 row1= rectmove + x+xsize;
2806                 
2807                 for (y=1; y<ysize; y++, row1+=xsize) {
2808                         if (row1[0]==128 && row1[xsize]==128) {
2809                                 /* find previous color and next color and amount of steps to blend */
2810                                 prev= row1[-xsize];
2811                                 step= 1;
2812                                 while (y+step<ysize && row1[step*xsize]==128)
2813                                         step++;
2814                                 
2815                                 if (y+step!=ysize) {
2816                                         /* now we can blend values */
2817                                         next= row1[step*xsize];
2818                                         /* note, prev value can be next value, but we do this loop to clear 128 then */
2819                                         for (a=0; a<step; a++) {
2820                                                 int fac, mfac;
2821                                                 
2822                                                 fac= ((a+1)<<8)/(step+1);
2823                                                 mfac= 255-fac;
2824                                                 
2825                                                 row1[a*xsize]= (prev*mfac + next*fac)>>8; 
2826                                         }
2827                                 }
2828                         }
2829                 }
2830         }
2831         
2832         /* last: pixels with 0 we fill in zbuffer, with 1 we skip for mask */
2833         for (y=2; y<ysize; y++) {
2834                 /* setup rows */
2835                 row1= rectmove + (y-2)*xsize;
2836                 row2= row1 + xsize;
2837                 row3= row2 + xsize;
2838                 for (x=2; x<xsize; x++, row1++, row2++, row3++) {
2839                         if (row2[1]==0) {
2840                                 if (row2[0]>1 || row2[2]>1 || row1[1]>1 || row3[1]>1)
2841                                         row2[1]= 1;
2842                         }
2843                 }
2844         }
2845 }
2846
2847 /* in: two vectors, first vector points from origin back in time, 2nd vector points to future */
2848 /* we make this into 3 points, center point is (0, 0) */
2849 /* and offset the center point just enough to make curve go through midpoint */
2850
2851 static void quad_bezier_2d(float *result, float *v1, float *v2, float *ipodata)
2852 {
2853         float p1[2], p2[2], p3[2];
2854         
2855         p3[0]= -v2[0];
2856         p3[1]= -v2[1];
2857         
2858         p1[0]= v1[0];
2859         p1[1]= v1[1];
2860         
2861         /* official formula 2*p2 - 0.5*p1 - 0.5*p3 */
2862         p2[0]= -0.5f*p1[0] - 0.5f*p3[0];
2863         p2[1]= -0.5f*p1[1] - 0.5f*p3[1];
2864         
2865         result[0]= ipodata[0]*p1[0] + ipodata[1]*p2[0] + ipodata[2]*p3[0];
2866         result[1]= ipodata[0]*p1[1] + ipodata[1]*p2[1] + ipodata[2]*p3[1];
2867 }
2868
2869 static void set_quad_bezier_ipo(float fac, float *data)
2870 {
2871         float mfac= (1.0f-fac);
2872         
2873         data[0]= mfac*mfac;
2874         data[1]= 2.0f*mfac*fac;
2875         data[2]= fac*fac;
2876 }
2877
2878 void RE_zbuf_accumulate_vecblur(
2879         NodeBlurData *nbd, int xsize, int ysize, float *newrect,
2880         const float *imgrect, float *vecbufrect, const float *zbufrect)
2881 {
2882         ZSpan zspan;
2883         DrawBufPixel *rectdraw, *dr;
2884         static float jit[256][2];
2885         float v1[3], v2[3], v3[3], v4[3], fx, fy;
2886         const float *dimg, *dz, *ro;
2887         float *rectvz, *dvz, *dvec1, *dvec2, *dz1, *dz2, *rectz;
2888         float *minvecbufrect= NULL, *rectweight, *rw, *rectmax, *rm;
2889         float maxspeedsq= (float)nbd->maxspeed*nbd->maxspeed;
2890         int y, x, step, maxspeed=nbd->maxspeed, samples= nbd->samples;
2891         int tsktsk= 0;
2892         static int firsttime= 1;
2893         char *rectmove, *dm;
2894         
2895         zbuf_alloc_span(&zspan, xsize, ysize, 1.0f);
2896         zspan.zmulx=  ((float)xsize)/2.0f;
2897         zspan.zmuly=  ((float)ysize)/2.0f;
2898         zspan.zofsx= 0.0f;
2899         zspan.zofsy= 0.0f;
2900         
2901         /* the buffers */
2902         rectz= MEM_mapallocN(sizeof(float)*xsize*ysize, "zbuf accum");
2903         zspan.rectz= (int *)rectz;
2904         
2905         rectmove= MEM_mapallocN(xsize*ysize, "rectmove");
2906         rectdraw= MEM_mapallocN(sizeof(DrawBufPixel)*xsize*ysize, "rect draw");
2907         zspan.rectp= (int *)rectdraw;
2908
2909         rectweight= MEM_mapallocN(sizeof(float)*xsize*ysize, "rect weight");
2910         rectmax= MEM_mapallocN(sizeof(float)*xsize*ysize, "rect max");
2911         
2912         /* debug... check if PASS_VECTOR_MAX still is in buffers */
2913         dvec1= vecbufrect;
2914         for (x= 4*xsize*ysize; x>0; x--, dvec1++) {
2915                 if (dvec1[0]==PASS_VECTOR_MAX) {
2916                         dvec1[0]= 0.0f;
2917                         tsktsk= 1;
2918                 }
2919         }
2920         if (tsktsk) printf("Found uninitialized speed in vector buffer... fixed.\n");
2921         
2922         /* min speed? then copy speedbuffer to recalculate speed vectors */
2923         if (nbd->minspeed) {
2924                 float minspeed= (float)nbd->minspeed;
2925                 float minspeedsq= minspeed*minspeed;
2926                 
2927                 minvecbufrect= MEM_mapallocN(4*sizeof(float)*xsize*ysize, "minspeed buf");
2928                 
2929                 dvec1= vecbufrect;
2930                 dvec2= minvecbufrect;
2931                 for (x= 2*xsize*ysize; x>0; x--, dvec1+=2, dvec2+=2) {
2932                         if (dvec1[0]==0.0f && dvec1[1]==0.0f) {
2933                                 dvec2[0]= dvec1[0];
2934                                 dvec2[1]= dvec1[1];
2935                         }
2936                         else {
2937                                 float speedsq= dvec1[0]*dvec1[0] + dvec1[1]*dvec1[1];
2938                                 if (speedsq <= minspeedsq) {
2939                                         dvec2[0]= 0.0f;
2940                                         dvec2[1]= 0.0f;
2941                                 }
2942                                 else {
2943                                         speedsq = 1.0f - minspeed / sqrtf(speedsq);
2944                                         dvec2[0]= speedsq*dvec1[0];
2945                                         dvec2[1]= speedsq*dvec1[1];
2946                                 }
2947                         }
2948                 }
2949                 SWAP(float *, minvecbufrect, vecbufrect);
2950         }
2951         
2952         /* make vertex buffer with averaged speed and zvalues */
2953         rectvz= MEM_mapallocN(4*sizeof(float)*(xsize+1)*(ysize+1), "vertices");
2954         dvz= rectvz;
2955         for (y=0; y<=ysi