9f30e34c893009269a78debbf0c1b98171452c74
[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         if (zspan->miny1 < zspan->miny2) my0= zspan->miny2; else my0= zspan->miny1;
334         if (zspan->maxy1 > zspan->maxy2) my2= zspan->maxy2; else my2= zspan->maxy1;
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= (int)CLAMPIS(zverg, INT_MIN, INT_MAX);
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         if (zspan->miny1 < zspan->miny2) my0= zspan->miny2; else my0= zspan->miny1;
1077         if (zspan->maxy1 > zspan->maxy2) my2= zspan->maxy2; else my2= zspan->maxy1;
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= (int)CLAMPIS(zverg, INT_MIN, INT_MAX);
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         if (zspan->miny1 < zspan->miny2) my0= zspan->miny2; else my0= zspan->miny1;
1200         if (zspan->maxy1 > zspan->maxy2) my2= zspan->maxy2; else my2= zspan->maxy1;
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= (int)CLAMPIS(zverg, INT_MIN, INT_MAX);
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         if (zspan->miny1 < zspan->miny2) my0= zspan->miny2; else my0= zspan->miny1;
1328         if (zspan->maxy1 > zspan->maxy2) my2= zspan->maxy2; else my2= zspan->maxy1;
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= (int)CLAMPIS(zverg, INT_MIN, INT_MAX);
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         if (zspan->miny1 < zspan->miny2) my0= zspan->miny2; else my0= zspan->miny1;
1430         if (zspan->maxy1 > zspan->maxy2) my2= zspan->maxy2; else my2= zspan->maxy1;
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 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         if (zspan->miny1 < zspan->miny2) my0= zspan->miny2; else my0= zspan->miny1;
1530         if (zspan->maxy1 > zspan->maxy2) my2= zspan->maxy2; else my2= zspan->maxy1;
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         sn1= (my0 + my2)/2;
1568         if (zspan->span1[sn1] < zspan->span2[sn1]) {
1569                 span1= zspan->span1+my2;
1570                 span2= zspan->span2+my2;
1571         }
1572         else {
1573                 span1= zspan->span2+my2;
1574                 span2= zspan->span1+my2;
1575         }
1576         
1577         for (y=my2; y>=my0; y--, span1--, span2--) {
1578                 
1579                 sn1= floor(*span1);
1580                 sn2= floor(*span2);
1581                 sn1++; 
1582                 
1583                 if (sn2>=rectx) sn2= rectx-1;
1584                 if (sn1<0) sn1= 0;
1585                 
1586                 u= (double)sn1*uxd + uy0;
1587                 v= (double)sn1*vxd + vy0;
1588                 
1589                 for (x= sn1; x<=sn2; x++, u+=uxd, v+=vxd)
1590                         func(handle, x, y, u, v);
1591                 
1592                 uy0 -= uyd;
1593                 vy0 -= vyd;
1594         }
1595 }
1596
1597
1598
1599 /**
1600  * (clip pyramid)
1601  * Sets lambda: flag, and parametrize the clipping of vertices in
1602  * viewspace coordinates. lambda = -1 means no clipping, lambda in [0, 1] means a clipping.
1603  * Note: uses globals.
1604  * \param v1 start coordinate s
1605  * \param v2 target coordinate t
1606  * \param b1 
1607  * \param b2 
1608  * \param b3
1609  * \param a index for coordinate (x, y, or z)
1610  */
1611
1612 static void clippyra(float *lambda, float *v1, float *v2, int *b2, int *b3, int a, float clipcrop)
1613 {
1614         float da, dw, u1=0.0, u2=1.0;
1615         float v13;
1616         
1617         lambda[0]= -1.0;
1618         lambda[1]= -1.0;
1619
1620         da= v2[a]-v1[a];
1621         /* prob; we clip slightly larger, osa renders add 2 pixels on edges, should become variable? */
1622         /* or better; increase r.winx/y size, but thats quite a complex one. do it later */
1623         if (a==2) {
1624                 dw= (v2[3]-v1[3]);
1625                 v13= v1[3];
1626         }
1627         else {
1628                 dw= clipcrop*(v2[3]-v1[3]);
1629                 v13= clipcrop*v1[3];
1630         }
1631         /* according the original article by Liang&Barsky, for clipping of
1632          * homogeneous coordinates with viewplane, the value of "0" is used instead of "-w" .
1633          * This differs from the other clipping cases (like left or top) and I considered
1634          * it to be not so 'homogenic'. But later it has proven to be an error,
1635          * who would have thought that of L&B!
1636          */
1637
1638         if (cliptestf(-da, -dw, v13, v1[a], &u1, &u2)) {
1639                 if (cliptestf(da, -dw, v13, -v1[a], &u1, &u2)) {
1640                         *b3=1;
1641                         if (u2<1.0f) {
1642                                 lambda[1]= u2;
1643                                 *b2=1;
1644                         }
1645                         else lambda[1]=1.0;  /* u2 */
1646                         if (u1>0.0f) {
1647                                 lambda[0] = u1;
1648                                 *b2 = 1;
1649                         }
1650                         else {
1651                                 lambda[0] = 0.0;
1652                         }
1653                 }
1654         }
1655 }
1656
1657 /**
1658  * (make vertex pyramide clip)
1659  * Checks lambda and uses this to make decision about clipping the line
1660  * segment from v1 to v2. lambda is the factor by which the vector is
1661  * cut. ( calculate s + l * ( t - s )). The result is appended to the
1662  * vertex list of this face.
1663  * 
1664  * 
1665  * \param v1 start coordinate s
1666  * \param v2 target coordinate t
1667  * \param b1 
1668  * \param b2 
1669  * \param clve vertex vector.
1670  */
1671
1672 static void makevertpyra(float *vez, float *lambda, float **trias, float *v1, float *v2, int *b1, int *clve)
1673 {
1674         float l1, l2, *adr;
1675
1676         l1= lambda[0];
1677         l2= lambda[1];
1678
1679         if (l1!= -1.0f) {
1680                 if (l1!= 0.0f) {
1681                         adr= vez+4*(*clve);
1682                         trias[*b1]=adr;
1683                         (*clve)++;
1684                         adr[0]= v1[0]+l1*(v2[0]-v1[0]);
1685                         adr[1]= v1[1]+l1*(v2[1]-v1[1]);
1686                         adr[2]= v1[2]+l1*(v2[2]-v1[2]);
1687                         adr[3]= v1[3]+l1*(v2[3]-v1[3]);
1688                 }
1689                 else trias[*b1]= v1;
1690                 
1691                 (*b1)++;
1692         }
1693         if (l2!= -1.0f) {
1694                 if (l2!= 1.0f) {
1695                         adr= vez+4*(*clve);
1696                         trias[*b1]=adr;
1697                         (*clve)++;
1698                         adr[0]= v1[0]+l2*(v2[0]-v1[0]);
1699                         adr[1]= v1[1]+l2*(v2[1]-v1[1]);
1700                         adr[2]= v1[2]+l2*(v2[2]-v1[2]);
1701                         adr[3]= v1[3]+l2*(v2[3]-v1[3]);
1702                         (*b1)++;
1703                 }
1704         }
1705 }
1706
1707 /* ------------------------------------------------------------------------- */
1708
1709 void projectverto(const float v1[3], float winmat[4][4], float adr[4])
1710 {
1711         /* calcs homogenic coord of vertex v1 */
1712         float x, y, z;
1713
1714         x = v1[0];
1715         y = v1[1];
1716         z = v1[2];
1717         adr[0] = x * winmat[0][0] + z * winmat[2][0] + winmat[3][0];
1718         adr[1] = y * winmat[1][1] + z * winmat[2][1] + winmat[3][1];
1719         adr[2] =                    z * winmat[2][2] + winmat[3][2];
1720         adr[3] =                    z * winmat[2][3] + winmat[3][3];
1721
1722         //printf("hoco %f %f %f %f\n", adr[0], adr[1], adr[2], adr[3]);
1723 }
1724
1725 /* ------------------------------------------------------------------------- */
1726
1727 void projectvert(const float v1[3], float winmat[4][4], float adr[4])
1728 {
1729         /* calcs homogenic coord of vertex v1 */
1730         float x, y, z;
1731
1732         x = v1[0];
1733         y = v1[1];
1734         z = v1[2];
1735         adr[0] = x * winmat[0][0] + y * winmat[1][0] + z * winmat[2][0] + winmat[3][0];
1736         adr[1] = x * winmat[0][1] + y * winmat[1][1] + z * winmat[2][1] + winmat[3][1];
1737         adr[2] = x * winmat[0][2] + y * winmat[1][2] + z * winmat[2][2] + winmat[3][2];
1738         adr[3] = x * winmat[0][3] + y * winmat[1][3] + z * winmat[2][3] + winmat[3][3];
1739 }
1740
1741 /* ------------------------------------------------------------------------- */
1742
1743 #define ZBUF_PROJECT_CACHE_SIZE 256
1744
1745 typedef struct ZbufProjectCache {
1746         int index, clip;
1747         float ho[4];
1748 } ZbufProjectCache;
1749
1750 static void zbuf_project_cache_clear(ZbufProjectCache *cache, int size)
1751 {
1752         int i;
1753
1754         if (size > ZBUF_PROJECT_CACHE_SIZE)
1755                 size= ZBUF_PROJECT_CACHE_SIZE;
1756
1757         memset(cache, 0, sizeof(ZbufProjectCache)*size);
1758         for (i=0; i<size; i++)
1759                 cache[i].index= -1;
1760 }
1761
1762 static int zbuf_shadow_project(ZbufProjectCache *cache, int index, float winmat[4][4], float *co, float *ho)
1763 {
1764         int cindex= index & 255;
1765
1766         if (cache[cindex].index == index) {
1767                 copy_v4_v4(ho, cache[cindex].ho);
1768                 return cache[cindex].clip;
1769         }
1770         else {
1771                 int clipflag;
1772                 projectvert(co, winmat, ho);
1773                 clipflag= testclip(ho);
1774
1775                 copy_v4_v4(cache[cindex].ho, ho);
1776                 cache[cindex].clip= clipflag;
1777                 cache[cindex].index= index;
1778
1779                 return clipflag;
1780         }
1781 }
1782
1783 static void zbuffer_part_bounds(int winx, int winy, RenderPart *pa, float *bounds)
1784 {
1785         bounds[0]= (2*pa->disprect.xmin - winx-1)/(float)winx;
1786         bounds[1]= (2*pa->disprect.xmax - winx+1)/(float)winx;
1787         bounds[2]= (2*pa->disprect.ymin - winy-1)/(float)winy;
1788         bounds[3]= (2*pa->disprect.ymax - winy+1)/(float)winy;
1789 }
1790
1791 static int zbuf_part_project(ZbufProjectCache *cache, int index, float winmat[4][4], float *bounds, float *co, float *ho)
1792 {
1793         float vec[3];
1794         int cindex= index & 255;
1795
1796         if (cache[cindex].index == index) {
1797                 copy_v4_v4(ho, cache[cindex].ho);
1798                 return cache[cindex].clip;
1799         }
1800         else {
1801                 float wco;
1802                 int clipflag= 0;
1803                 copy_v3_v3(vec, co);
1804                 projectvert(co, winmat, ho);
1805
1806                 wco= ho[3];
1807                 if (ho[0] < bounds[0]*wco) clipflag |= 1;
1808                 else if (ho[0] > bounds[1]*wco) clipflag |= 2;
1809                 if (ho[1] > bounds[3]*wco) clipflag |= 4;
1810                 else if (ho[1] < bounds[2]*wco) clipflag |= 8;
1811
1812                 copy_v4_v4(cache[cindex].ho, ho);
1813                 cache[cindex].clip= clipflag;
1814                 cache[cindex].index= index;
1815
1816                 return clipflag;
1817         }
1818 }
1819
1820 void zbuf_render_project(float winmat[4][4], const float co[3], float ho[4])
1821 {
1822         float vec[3];
1823
1824         copy_v3_v3(vec, co);
1825         projectvert(vec, winmat, ho);
1826 }
1827
1828 void zbuf_make_winmat(Render *re, float winmat[4][4])
1829 {
1830         if (re->r.mode & R_PANORAMA) {
1831                 float panomat[4][4];
1832
1833                 unit_m4(panomat);
1834
1835                 panomat[0][0]= re->panoco;
1836                 panomat[0][2]= re->panosi;
1837                 panomat[2][0]= -re->panosi;
1838                 panomat[2][2]= re->panoco;
1839
1840                 mul_m4_m4m4(winmat, re->winmat, panomat);
1841         }
1842         else
1843                 copy_m4_m4(winmat, re->winmat);
1844 }
1845
1846 /* do zbuffering and clip, f1 f2 f3 are hocos, c1 c2 c3 are clipping flags */
1847
1848 void zbufclip(ZSpan *zspan, int obi, int zvlnr,
1849               const float f1[4], const float f2[4], const float f3[4],
1850               const int c1, const int c2, const int c3)
1851 {
1852         float *vlzp[32][3], lambda[3][2];
1853         float vez[400], *trias[40];
1854         
1855         if (c1 | c2 | c3) {     /* not in middle */
1856                 if (c1 & c2 & c3) {     /* completely out */
1857                         return;
1858                 }
1859                 else {  /* clipping */
1860                         int arg, v, b, clipflag[3], b1, b2, b3, c4, clve=3, clvlo, clvl=1;
1861                         float *fp;
1862
1863                         vez[0]= f1[0]; vez[1]= f1[1]; vez[2]= f1[2]; vez[3]= f1[3];
1864                         vez[4]= f2[0]; vez[5]= f2[1]; vez[6]= f2[2]; vez[7]= f2[3];
1865                         vez[8]= f3[0]; vez[9]= f3[1]; vez[10]= f3[2];vez[11]= f3[3];
1866
1867                         vlzp[0][0]= vez;
1868                         vlzp[0][1]= vez+4;
1869                         vlzp[0][2]= vez+8;
1870
1871                         clipflag[0]= ( (c1 & 48) | (c2 & 48) | (c3 & 48) );
1872                         if (clipflag[0]==0) {   /* othwerwise it needs to be calculated again, after the first (z) clip */
1873                                 clipflag[1]= ( (c1 & 3) | (c2 & 3) | (c3 & 3) );
1874                                 clipflag[2]= ( (c1 & 12) | (c2 & 12) | (c3 & 12) );
1875                         }
1876                         else clipflag[1]=clipflag[2]= 0;
1877                         
1878                         for (b=0;b<3;b++) {
1879                                 
1880                                 if (clipflag[b]) {
1881                                 
1882                                         clvlo= clvl;
1883                                         
1884                                         for (v=0; v<clvlo; v++) {
1885                                         
1886                                                 if (vlzp[v][0]!=NULL) { /* face is still there */
1887                                                         b2= b3 =0;      /* clip flags */
1888
1889                                                         if (b==0) arg= 2;
1890                                                         else if (b==1) arg= 0;
1891                                                         else arg= 1;
1892                                                         
1893                                                         clippyra(lambda[0], vlzp[v][0], vlzp[v][1], &b2, &b3, arg, zspan->clipcrop);
1894                                                         clippyra(lambda[1], vlzp[v][1], vlzp[v][2], &b2, &b3, arg, zspan->clipcrop);
1895                                                         clippyra(lambda[2], vlzp[v][2], vlzp[v][0], &b2, &b3, arg, zspan->clipcrop);
1896
1897                                                         if (b2==0 && b3==1) {
1898                                                                 /* completely 'in', but we copy because of last for () loop in this section */;
1899                                                                 vlzp[clvl][0]= vlzp[v][0];
1900                                                                 vlzp[clvl][1]= vlzp[v][1];
1901                                                                 vlzp[clvl][2]= vlzp[v][2];
1902                                                                 vlzp[v][0]= NULL;
1903                                                                 clvl++;
1904                                                         }
1905                                                         else if (b3==0) {
1906                                                                 vlzp[v][0]= NULL;
1907                                                                 /* completely 'out' */;
1908                                                         }
1909                                                         else {
1910                                                                 b1=0;
1911                                                                 makevertpyra(vez, lambda[0], trias, vlzp[v][0], vlzp[v][1], &b1, &clve);
1912                                                                 makevertpyra(vez, lambda[1], trias, vlzp[v][1], vlzp[v][2], &b1, &clve);
1913                                                                 makevertpyra(vez, lambda[2], trias, vlzp[v][2], vlzp[v][0], &b1, &clve);
1914
1915                                                                 /* after front clip done: now set clip flags */
1916                                                                 if (b==0) {
1917                                                                         clipflag[1]= clipflag[2]= 0;
1918                                                                         f1= vez;
1919                                                                         for (b3=0; b3<clve; b3++) {
1920                                                                                 c4= testclip(f1);
1921                                                                                 clipflag[1] |= (c4 & 3);
1922                                                                                 clipflag[2] |= (c4 & 12);
1923                                                                                 f1+= 4;
1924                                                                         }
1925                                                                 }
1926                                                                 
1927                                                                 vlzp[v][0]= NULL;
1928                                                                 if (b1>2) {
1929                                                                         for (b3=3; b3<=b1; b3++) {
1930                                                                                 vlzp[clvl][0]= trias[0];
1931                                                                                 vlzp[clvl][1]= trias[b3-2];
1932                                                                                 vlzp[clvl][2]= trias[b3-1];
1933                                                                                 clvl++;
1934                                                                         }
1935                                                                 }
1936                                                         }
1937                                                 }
1938                                         }
1939                                 }
1940                         }
1941
1942                         /* warning, clip overflow, this should never happen! */
1943                         BLI_assert(!(clve > 38 || clvl > 31));
1944
1945                         /* perspective division */
1946                         fp = vez;
1947                         for (b = 0; b < clve; b++) {
1948                                 hoco_to_zco(zspan, fp, fp);
1949                                 fp += 4;
1950                         }
1951                         for (b = 1; b < clvl; b++) {
1952                                 if (vlzp[b][0]) {
1953                                         zspan->zbuffunc(zspan, obi, zvlnr, vlzp[b][0], vlzp[b][1], vlzp[b][2], NULL);
1954                                 }
1955                         }
1956                         return;
1957                 }
1958         }
1959
1960         /* perspective division: HCS to ZCS */
1961         hoco_to_zco(zspan, vez, f1);
1962         hoco_to_zco(zspan, vez+4, f2);
1963         hoco_to_zco(zspan, vez+8, f3);
1964         zspan->zbuffunc(zspan, obi, zvlnr, vez, vez+4, vez+8, NULL);
1965 }
1966
1967 void zbufclip4(ZSpan *zspan, int obi, int zvlnr,
1968                const float f1[4], const float f2[4], const float f3[4], const float f4[4],
1969                const int c1, const int c2, const int c3, const int c4)
1970 {
1971         float vez[16];
1972         
1973         if (c1 | c2 | c3 | c4) {        /* not in middle */
1974                 if (c1 & c2 & c3 & c4) {        /* completely out */
1975                         return;
1976                 }
1977                 else {  /* clipping */
1978                         zbufclip(zspan, obi, zvlnr, f1, f2, f3, c1, c2, c3);
1979                         zbufclip(zspan, obi, zvlnr, f1, f3, f4, c1, c3, c4);
1980                 }
1981                 return;
1982         }
1983
1984         /* perspective division: HCS to ZCS */
1985         hoco_to_zco(zspan, vez, f1);
1986         hoco_to_zco(zspan, vez+4, f2);
1987         hoco_to_zco(zspan, vez+8, f3);
1988         hoco_to_zco(zspan, vez+12, f4);
1989
1990         zspan->zbuffunc(zspan, obi, zvlnr, vez, vez+4, vez+8, vez+12);
1991 }
1992
1993 /* ************** ZMASK ******************************** */
1994
1995 #define EXTEND_PIXEL(a) if (temprectp[a]) { z += rectz[a]; tot++; } (void)0
1996
1997 /* changes the zbuffer to be ready for z-masking: applies an extend-filter, and then clears */
1998 static void zmask_rect(int *rectz, int *rectp, int xs, int ys, int neg)
1999 {
2000         int len=0, x, y;
2001         int *temprectp;
2002         int row1, row2, row3, *curp, *curz;
2003         
2004         temprectp= MEM_dupallocN(rectp);
2005         
2006         /* extend: if pixel is not filled in, we check surrounding pixels and average z value  */
2007         
2008         for (y=1; y<=ys; y++) {
2009                 /* setup row indices */
2010                 row1= (y-2)*xs;
2011                 row2= row1 + xs;
2012                 row3= row2 + xs;
2013                 if (y==1)
2014                         row1= row2;
2015                 else if (y==ys)
2016                         row3= row2;
2017                 
2018                 curp= rectp + (y-1)*xs;
2019                 curz= rectz + (y-1)*xs;
2020                 
2021                 for (x=0; x<xs; x++, curp++, curz++) {
2022                         if (curp[0]==0) {
2023                                 int tot= 0;
2024                                 float z= 0.0f;
2025                                 
2026                                 EXTEND_PIXEL(row1);
2027                                 EXTEND_PIXEL(row2);
2028                                 EXTEND_PIXEL(row3);
2029                                 EXTEND_PIXEL(row1 + 1);
2030                                 EXTEND_PIXEL(row3 + 1);
2031                                 if (x!=xs-1) {
2032                                         EXTEND_PIXEL(row1 + 2);
2033                                         EXTEND_PIXEL(row2 + 2);
2034                                         EXTEND_PIXEL(row3 + 2);
2035                                 }
2036                                 if (tot) {
2037                                         len++;
2038                                         curz[0]= (int)(z/(float)tot);
2039                                         curp[0]= -1;    /* env */
2040                                 }
2041                         }
2042                         
2043                         if (x!=0) {
2044                                 row1++; row2++; row3++;
2045                         }
2046                 }
2047         }
2048
2049         MEM_freeN(temprectp);
2050         
2051         if (neg) {
2052                 /* z values for negative are already correct */
2053         }
2054         else {
2055                 /* clear not filled z values */
2056                 for (len= xs*ys -1; len>=0; len--) {
2057                         if (rectp[len]==0) {
2058                                 rectz[len] = -0x7FFFFFFF;
2059                                 rectp[len]= -1; /* env code */
2060                         }
2061                 }
2062         }
2063 }
2064
2065
2066
2067
2068 /* ***************** ZBUFFER MAIN ROUTINES **************** */
2069
2070 void zbuffer_solid(RenderPart *pa, RenderLayer *rl, void(*fillfunc)(RenderPart *, ZSpan *, int, void *), void *data)
2071 {
2072         ZbufProjectCache cache[ZBUF_PROJECT_CACHE_SIZE];
2073         ZSpan zspans[16], *zspan; /* 16 = RE_MAX_OSA */
2074         VlakRen *vlr= NULL;
2075         VertRen *v1, *v2, *v3, *v4;
2076         Material *ma = NULL;
2077         ObjectInstanceRen *obi;
2078         ObjectRen *obr;
2079         float obwinmat[4][4], winmat[4][4], bounds[4];
2080         float ho1[4], ho2[4], ho3[4], ho4[4]={0};
2081         unsigned int lay= rl->lay, lay_zmask= rl->lay_zmask;
2082         int i, v, zvlnr, zsample, samples, c1, c2, c3, c4=0;
2083         short nofill=0, env=0, wire=0, zmaskpass=0;
2084         short all_z= (rl->layflag & SCE_LAY_ALL_Z) && !(rl->layflag & SCE_LAY_ZMASK);
2085         short neg_zmask= (rl->layflag & SCE_LAY_ZMASK) && (rl->layflag & SCE_LAY_NEG_ZMASK);
2086
2087         zbuf_make_winmat(&R, winmat);
2088         
2089         samples= (R.osa? R.osa: 1);
2090         samples= MIN2(4, samples-pa->sample);
2091
2092         for (zsample=0; zsample<samples; zsample++) {
2093                 zspan= &zspans[zsample];
2094
2095                 zbuffer_part_bounds(R.winx, R.winy, pa, bounds);
2096                 zbuf_alloc_span(zspan, pa->rectx, pa->recty, R.clipcrop);
2097                 
2098                 /* needed for transform from hoco to zbuffer co */
2099                 zspan->zmulx= ((float)R.winx)/2.0f;
2100                 zspan->zmuly= ((float)R.winy)/2.0f;
2101                 
2102                 if (R.osa) {
2103                         zspan->zofsx= -pa->disprect.xmin - R.jit[pa->sample+zsample][0];
2104                         zspan->zofsy= -pa->disprect.ymin - R.jit[pa->sample+zsample][1];
2105                 }
2106                 else if (R.i.curblur) {
2107                         zspan->zofsx= -pa->disprect.xmin - R.mblur_jit[R.i.curblur-1][0];
2108                         zspan->zofsy= -pa->disprect.ymin - R.mblur_jit[R.i.curblur-1][1];
2109                 }
2110                 else {
2111                         zspan->zofsx= -pa->disprect.xmin;
2112                         zspan->zofsy= -pa->disprect.ymin;
2113                 }
2114                 /* to center the sample position */
2115                 zspan->zofsx -= 0.5f;
2116                 zspan->zofsy -= 0.5f;
2117                 
2118                 /* the buffers */
2119                 if (zsample == samples-1) {
2120                         zspan->rectp= pa->rectp;
2121                         zspan->recto= pa->recto;
2122
2123                         if (neg_zmask)
2124                                 zspan->rectz= pa->rectmask;
2125                         else
2126                                 zspan->rectz= pa->rectz;
2127                 }
2128                 else {
2129                         zspan->recto= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "recto");
2130                         zspan->rectp= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectp");
2131                         zspan->rectz= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectz");
2132                 }
2133
2134                 fillrect(zspan->rectz, pa->rectx, pa->recty, 0x7FFFFFFF);
2135                 fillrect(zspan->rectp, pa->rectx, pa->recty, 0);
2136                 fillrect(zspan->recto, pa->rectx, pa->recty, 0);
2137         }
2138
2139         /* in case zmask we fill Z for objects in lay_zmask first, then clear Z, and then do normal zbuffering */
2140         if (rl->layflag & SCE_LAY_ZMASK)
2141                 zmaskpass= 1;
2142         
2143         for (; zmaskpass >=0; zmaskpass--) {
2144                 ma= NULL;
2145
2146                 /* filling methods */
2147                 for (zsample=0; zsample<samples; zsample++) {
2148                         zspan= &zspans[zsample];
2149
2150                         if (zmaskpass && neg_zmask)
2151                                 zspan->zbuffunc= zbuffillGLinv4;
2152                         else
2153                                 zspan->zbuffunc= zbuffillGL4;
2154                         zspan->zbuflinefunc= zbufline;
2155                 }
2156
2157                 /* regular zbuffering loop, does all sample buffers */
2158                 for (i=0, obi=R.instancetable.first; obi; i++, obi=obi->next) {
2159                         obr= obi->obr;
2160
2161                         /* continue happens in 2 different ways... zmaskpass only does lay_zmask stuff */
2162                         if (zmaskpass) {
2163                                 if ((obi->lay & lay_zmask)==0)
2164                                         continue;
2165                         }
2166                         else if (!all_z && !(obi->lay & (lay|lay_zmask)))
2167                                 continue;
2168                         
2169                         if (obi->flag & R_TRANSFORMED)
2170                                 mul_m4_m4m4(obwinmat, winmat, obi->mat);
2171                         else
2172                                 copy_m4_m4(obwinmat, winmat);
2173
2174                         if (clip_render_object(obi->obr->boundbox, bounds, obwinmat))
2175                                 continue;
2176
2177                         zbuf_project_cache_clear(cache, obr->totvert);
2178
2179                         for (v=0; v<obr->totvlak; v++) {
2180                                 if ((v & 255)==0) vlr= obr->vlaknodes[v>>8].vlak;
2181                                 else vlr++;
2182
2183                                 /* the cases: visible for render, only z values, zmask, nothing */
2184                                 if (obi->lay & lay) {
2185                                         if (vlr->mat!=ma) {
2186                                                 ma= vlr->mat;
2187                                                 nofill= (ma->mode & MA_ONLYCAST) || ((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP));
2188                                                 env= (ma->mode & MA_ENV);
2189                                                 wire= (ma->material_type == MA_TYPE_WIRE);
2190                                                 
2191                                                 for (zsample=0; zsample<samples; zsample++) {
2192                                                         if (ma->mode & MA_ZINV || (zmaskpass && neg_zmask))
2193                                                                 zspans[zsample].zbuffunc= zbuffillGLinv4;
2194                                                         else
2195                                                                 zspans[zsample].zbuffunc= zbuffillGL4;
2196                                                 }
2197                                         }
2198                                 }
2199                                 else if (all_z || (obi->lay & lay_zmask)) {
2200                                         env= 1;
2201                                         nofill= 0;
2202                                         ma= NULL; 
2203                                 }
2204                                 else {
2205                                         nofill= 1;
2206                                         ma= NULL;       /* otherwise nofill can hang */
2207                                 }
2208
2209                                 if (!(vlr->flag & R_HIDDEN) && nofill==0) {
2210                                         unsigned short partclip;
2211                                         
2212                                         v1= vlr->v1;
2213                                         v2= vlr->v2;
2214                                         v3= vlr->v3;
2215                                         v4= vlr->v4;
2216
2217                                         c1= zbuf_part_project(cache, v1->index, obwinmat, bounds, v1->co, ho1);
2218                                         c2= zbuf_part_project(cache, v2->index, obwinmat, bounds, v2->co, ho2);
2219                                         c3= zbuf_part_project(cache, v3->index, obwinmat, bounds, v3->co, ho3);
2220
2221                                         /* partclipping doesn't need viewplane clipping */
2222                                         partclip= c1 & c2 & c3;
2223                                         if (v4) {
2224                                                 c4= zbuf_part_project(cache, v4->index, obwinmat, bounds, v4->co, ho4);
2225                                                 partclip &= c4;
2226                                         }
2227
2228                                         if (partclip==0) {
2229                                                 
2230                                                 if (env) zvlnr= -1;
2231                                                 else zvlnr= v+1;
2232
2233                                                 c1= testclip(ho1);
2234                                                 c2= testclip(ho2);
2235                                                 c3= testclip(ho3);
2236                                                 if (v4)
2237                                                         c4= testclip(ho4);
2238
2239                                                 for (zsample=0; zsample<samples; zsample++) {
2240                                                         zspan= &zspans[zsample];
2241
2242                                                         if (wire) {
2243                                                                 if (v4)
2244                                                                         zbufclipwire(zspan, i, zvlnr, vlr->ec, ho1, ho2, ho3, ho4, c1, c2, c3, c4);
2245                                                                 else
2246                                                                         zbufclipwire(zspan, i, zvlnr, vlr->ec, ho1, ho2, ho3, NULL, c1, c2, c3, 0);
2247                                                         }
2248                                                         else {
2249                                                                 /* strands allow to be filled in as quad */
2250                                                                 if (v4 && (vlr->flag & R_STRAND)) {
2251                                                                         zbufclip4(zspan, i, zvlnr, ho1, ho2, ho3, ho4, c1, c2, c3, c4);
2252                                                                 }
2253                                                                 else {
2254                                                                         zbufclip(zspan, i, zvlnr, ho1, ho2, ho3, c1, c2, c3);
2255                                                                         if (v4)
2256                                                                                 zbufclip(zspan, i, (env)? zvlnr: zvlnr+RE_QUAD_OFFS, ho1, ho3, ho4, c1, c3, c4);
2257                                                                 }
2258                                                         }
2259                                                 }
2260                                         }
2261                                 }
2262                         }
2263                 }
2264                 
2265                 /* clear all z to close value, so it works as mask for next passes (ztra+strand) */
2266                 if (zmaskpass) {
2267                         for (zsample=0; zsample<samples; zsample++) {
2268                                 zspan= &zspans[zsample];
2269
2270                                 if (neg_zmask) {
2271                                         zspan->rectmask= zspan->rectz;
2272                                         if (zsample == samples-1)
2273                                                 zspan->rectz= pa->rectz;
2274                                         else
2275                                                 zspan->rectz= MEM_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectz");
2276                                         fillrect(zspan->rectz, pa->rectx, pa->recty, 0x7FFFFFFF);
2277
2278                                         zmask_rect(zspan->rectmask, zspan->rectp, pa->rectx, pa->recty, 1);
2279                                 }
2280                                 else
2281                                         zmask_rect(zspan->rectz, zspan->rectp, pa->rectx, pa->recty, 0);
2282                         }
2283                 }
2284         }
2285
2286         for (zsample=0; zsample<samples; zsample++) {
2287                 zspan= &zspans[zsample];
2288
2289                 if (fillfunc)
2290                         fillfunc(pa, zspan, pa->sample+zsample, data);
2291
2292                 if (zsample != samples-1) {
2293                         MEM_freeN(zspan->rectz);
2294                         MEM_freeN(zspan->rectp);
2295                         MEM_freeN(zspan->recto);
2296                         if (zspan->rectmask)
2297                                 MEM_freeN(zspan->rectmask);
2298                 }
2299
2300                 zbuf_free_span(zspan);
2301         }
2302 }
2303
2304 void zbuffer_shadow(Render *re, float winmat[4][4], LampRen *lar, int *rectz, int size, float jitx, float jity)
2305 {
2306         ZbufProjectCache cache[ZBUF_PROJECT_CACHE_SIZE];
2307         ZSpan zspan;
2308         ObjectInstanceRen *obi;
2309         ObjectRen *obr;
2310         VlakRen *vlr= NULL;
2311         Material *ma= NULL;
2312         StrandSegment sseg;
2313         StrandRen *strand= NULL;
2314         StrandVert *svert;
2315         StrandBound *sbound;
2316         float obwinmat[4][4], ho1[4], ho2[4], ho3[4], ho4[4];
2317         int a, b, c, i, c1, c2, c3, c4, ok=1, lay= -1;
2318
2319         if (lar->mode & (LA_LAYER|LA_LAYER_SHADOW)) lay= lar->lay;
2320
2321         /* 1.0f for clipping in clippyra()... bad stuff actually */
2322         zbuf_alloc_span(&zspan, size, size, 1.0f);
2323         zspan.zmulx=  ((float)size)/2.0f;
2324         zspan.zmuly=  ((float)size)/2.0f;
2325         /* -0.5f to center the sample position */
2326         zspan.zofsx= jitx - 0.5f;
2327         zspan.zofsy= jity - 0.5f;
2328         
2329         /* the buffers */
2330         zspan.rectz= rectz;
2331         fillrect(rectz, size, size, 0x7FFFFFFE);
2332         if (lar->buftype==LA_SHADBUF_HALFWAY) {
2333                 zspan.rectz1= MEM_mallocN(size*size*sizeof(int), "seconday z buffer");
2334                 fillrect(zspan.rectz1, size, size, 0x7FFFFFFE);
2335         }
2336         
2337         /* filling methods */
2338         zspan.zbuflinefunc= zbufline_onlyZ;
2339         zspan.zbuffunc= zbuffillGL_onlyZ;
2340
2341         for (i=0, obi=re->instancetable.first; obi; i++, obi=obi->next) {
2342                 obr= obi->obr;
2343
2344                 if (obr->ob==re->excludeob)
2345                         continue;
2346                 else if (!(obi->lay & lay))
2347                         continue;
2348
2349                 if (obi->flag & R_TRANSFORMED)
2350                         mul_m4_m4m4(obwinmat, winmat, obi->mat);
2351                 else
2352                         copy_m4_m4(obwinmat, winmat);
2353
2354                 if (clip_render_object(obi->obr->boundbox, NULL, obwinmat))
2355                         continue;
2356
2357                 zbuf_project_cache_clear(cache, obr->totvert);
2358
2359                 /* faces */
2360                 for (a=0; a<obr->totvlak; a++) {
2361
2362                         if ((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
2363                         else vlr++;
2364
2365                         /* note, these conditions are copied in shadowbuf_autoclip() */
2366                         if (vlr->mat!= ma) {
2367                                 ma= vlr->mat;
2368                                 ok= 1;
2369                                 if ((ma->mode2 & MA_CASTSHADOW)==0 || (ma->mode & MA_SHADBUF)==0) ok= 0;
2370                         }
2371
2372                         if (ok && (obi->lay & lay) && !(vlr->flag & R_HIDDEN)) {
2373                                 c1= zbuf_shadow_project(cache, vlr->v1->index, obwinmat, vlr->v1->co, ho1);
2374                                 c2= zbuf_shadow_project(cache, vlr->v2->index, obwinmat, vlr->v2->co, ho2);
2375                                 c3= zbuf_shadow_project(cache, vlr->v3->index, obwinmat, vlr->v3->co, ho3);
2376
2377                                 if ((ma->material_type == MA_TYPE_WIRE) || (vlr->flag & R_STRAND)) {
2378                                         if (vlr->v4) {
2379                                                 c4= zbuf_shadow_project(cache, vlr->v4->index, obwinmat, vlr->v4->co, ho4);
2380                                                 zbufclipwire(&zspan, 0, a+1, vlr->ec, ho1, ho2, ho3, ho4, c1, c2, c3, c4);
2381                                         }
2382                                         else
2383                                                 zbufclipwire(&zspan, 0, a+1, vlr->ec, ho1, ho2, ho3, NULL, c1, c2, c3, 0);
2384                                 }
2385                                 else {
2386                                         if (vlr->v4) {
2387                                                 c4= zbuf_shadow_project(cache, vlr->v4->index, obwinmat, vlr->v4->co, ho4);
2388                                                 zbufclip4(&zspan, 0, 0, ho1, ho2, ho3, ho4, c1, c2, c3, c4);
2389                                         }
2390                                         else
2391                                                 zbufclip(&zspan, 0, 0, ho1, ho2, ho3, c1, c2, c3);
2392                                 }
2393                         }
2394
2395                         if ((a & 255)==255 && re->test_break(re->tbh)) 
2396                                 break;
2397                 }
2398
2399                 /* strands */
2400                 if (obr->strandbuf) {
2401                         /* for each bounding box containing a number of strands */
2402                         sbound= obr->strandbuf->bound;
2403                         for (c=0; c<obr->strandbuf->totbound; c++, sbound++) {
2404                                 if (clip_render_object(sbound->boundbox, NULL, obwinmat))
2405                                         continue;
2406
2407                                 /* for each strand in this bounding box */
2408                                 for (a=sbound->start; a<sbound->end; a++) {
2409                                         strand= RE_findOrAddStrand(obr, a);
2410
2411                                         sseg.obi= obi;
2412                                         sseg.buffer= strand->buffer;
2413                                         sseg.sqadaptcos= sseg.buffer->adaptcos;
2414                                         sseg.sqadaptcos *= sseg.sqadaptcos;
2415                                         sseg.strand= strand;
2416                                         svert= strand->vert;
2417
2418                                         /* note, these conditions are copied in shadowbuf_autoclip() */
2419                                         if (sseg.buffer->ma!= ma) {
2420                                                 ma= sseg.buffer->ma;
2421                                                 ok= 1;
2422                                                 if ((ma->mode2 & MA_CASTSHADOW)==0 || (ma->mode & MA_SHADBUF)==0) ok= 0;
2423                                         }
2424
2425                                         if (ok && (sseg.buffer->lay & lay)) {
2426                                                 zbuf_project_cache_clear(cache, strand->totvert);
2427
2428                                                 for (b=0; b<strand->totvert-1; b++, svert++) {
2429                                                         sseg.v[0]= (b > 0)? (svert-1): svert;
2430                                                         sseg.v[1]= svert;
2431                                                         sseg.v[2]= svert+1;
2432                                                         sseg.v[3]= (b < strand->totvert-2)? svert+2: svert+1;
2433
2434                                                         c1= zbuf_shadow_project(cache, sseg.v[0]-strand->vert, obwinmat, sseg.v[0]->co, ho1);
2435                                                         c2= zbuf_shadow_project(cache, sseg.v[1]-strand->vert, obwinmat, sseg.v[1]->co, ho2);
2436                                                         c3= zbuf_shadow_project(cache, sseg.v[2]-strand->vert, obwinmat, sseg.v[2]->co, ho3);
2437                                                         c4= zbuf_shadow_project(cache, sseg.v[3]-strand->vert, obwinmat, sseg.v[3]->co, ho4);
2438
2439                                                         if (!(c1 & c2 & c3 & c4))
2440                                                                 render_strand_segment(re, winmat, NULL, &zspan, 1, &sseg);
2441                                                 }
2442                                         }
2443
2444                                         if ((a & 255)==255 && re->test_break(re->tbh)) 
2445                                                 break;
2446                                 }
2447                         }
2448                 }
2449
2450                 if (re->test_break(re->tbh)) 
2451                         break;
2452         }
2453         
2454         /* merge buffers */
2455         if (lar->buftype==LA_SHADBUF_HALFWAY) {
2456                 for (a=size*size -1; a>=0; a--)
2457                         rectz[a]= (rectz[a]>>1) + (zspan.rectz1[a]>>1);
2458                 
2459                 MEM_freeN(zspan.rectz1);
2460         }
2461         
2462         zbuf_free_span(&zspan);
2463 }
2464
2465 static void zbuffill_sss(ZSpan *zspan, int obi, int zvlnr,
2466                          const float *v1, const float *v2, const float *v3, const float *v4)
2467 {
2468         double zxd, zyd, zy0, z;
2469         float x0, y0, x1, y1, x2, y2, z0, z1, z2, xx1, *span1, *span2;
2470         int x, y, sn1, sn2, rectx= zspan->rectx, my0, my2;
2471         /* init */
2472         zbuf_init_span(zspan);
2473         
2474         /* set spans */
2475         zbuf_add_to_span(zspan, v1, v2);
2476         zbuf_add_to_span(zspan, v2, v3);
2477         if (v4) {
2478                 zbuf_add_to_span(zspan, v3, v4);
2479                 zbuf_add_to_span(zspan, v4, v1);
2480         }
2481         else 
2482                 zbuf_add_to_span(zspan, v3, v1);
2483         
2484         /* clipped */
2485         if (zspan->minp2==NULL || zspan->maxp2==NULL) return;
2486         
2487         if (zspan->miny1 < zspan->miny2) my0= zspan->miny2; else my0= zspan->miny1;
2488         if (zspan->maxy1 > zspan->maxy2) my2= zspan->maxy2; else my2= zspan->maxy1;
2489         
2490         if (my2<my0) return;
2491         
2492         /* ZBUF DX DY, in floats still */
2493         x1= v1[0]- v2[0];
2494         x2= v2[0]- v3[0];
2495         y1= v1[1]- v2[1];
2496         y2= v2[1]- v3[1];
2497         z1= v1[2]- v2[2];
2498         z2= v2[2]- v3[2];
2499         
2500         x0= y1*z2-z1*y2;
2501         y0= z1*x2-x1*z2;
2502         z0= x1*y2-y1*x2;
2503         
2504         if (z0==0.0f) return;
2505         
2506         xx1= (x0*v1[0] + y0*v1[1])/z0 + v1[2];
2507         zxd= -(double)x0/(double)z0;
2508         zyd= -(double)y0/(double)z0;
2509         zy0= ((double)my2)*zyd + (double)xx1;
2510         
2511         /* correct span */
2512         sn1= (my0 + my2)/2;
2513         if (zspan->span1[sn1] < zspan->span2[sn1]) {
2514                 span1= zspan->span1+my2;
2515                 span2= zspan->span2+my2;
2516         }
2517         else {
2518                 span1= zspan->span2+my2;
2519                 span2= zspan->span1+my2;
2520         }
2521         
2522         for (y=my2; y>=my0; y--, span1--, span2--) {
2523                 sn1= floor(*span1);
2524                 sn2= floor(*span2);
2525                 sn1++; 
2526                 
2527                 if (sn2>=rectx) sn2= rectx-1;
2528                 if (sn1<0) sn1= 0;
2529                 
2530                 z= (double)sn1*zxd + zy0;
2531                 
2532                 for (x= sn1; x<=sn2; x++, z+=zxd)
2533                         zspan->sss_func(zspan->sss_handle, obi, zvlnr, x, y, z);
2534                 
2535                 zy0 -= zyd;
2536         }
2537 }
2538
2539 void zbuffer_sss(RenderPart *pa, unsigned int lay, void *handle, void (*func)(void *, int, int, int, int, int))
2540 {
2541         ZbufProjectCache cache[ZBUF_PROJECT_CACHE_SIZE];
2542         ZSpan zspan;
2543         ObjectInstanceRen *obi;
2544         ObjectRen *obr;
2545         VlakRen *vlr= NULL;
2546         VertRen *v1, *v2, *v3, *v4;
2547         Material *ma = NULL, *sss_ma = R.sss_mat;
2548         float obwinmat[4][4], winmat[4][4], bounds[4];
2549         float ho1[4], ho2[4], ho3[4], ho4[4]={0};
2550         int i, v, zvlnr, c1, c2, c3, c4=0;
2551         short nofill=0, env=0, wire=0;
2552         
2553         zbuf_make_winmat(&R, winmat);
2554         zbuffer_part_bounds(R.winx, R.winy, pa, bounds);
2555         zbuf_alloc_span(&zspan, pa->rectx, pa->recty, R.clipcrop);
2556
2557         zspan.sss_handle= handle;
2558         zspan.sss_func= func;
2559         
2560         /* needed for transform from hoco to zbuffer co */
2561         zspan.zmulx=  ((float)R.winx)/2.0f;
2562         zspan.zmuly=  ((float)R.winy)/2.0f;
2563         
2564         /* -0.5f to center the sample position */
2565         zspan.zofsx= -pa->disprect.xmin - 0.5f;
2566         zspan.zofsy= -pa->disprect.ymin - 0.5f;
2567         
2568         /* filling methods */
2569         zspan.zbuffunc= zbuffill_sss;
2570
2571         /* fill front and back zbuffer */
2572         if (pa->rectz) {
2573                 fillrect(pa->recto, pa->rectx, pa->recty, 0); 
2574                 fillrect(pa->rectp, pa->rectx, pa->recty, 0); 
2575                 fillrect(pa->rectz, pa->rectx, pa->recty, 0x7FFFFFFF);
2576         }
2577         if (pa->rectbackz) {
2578                 fillrect(pa->rectbacko, pa->rectx, pa->recty, 0); 
2579                 fillrect(pa->rectbackp, pa->rectx, pa->recty, 0); 
2580                 fillrect(pa->rectbackz, pa->rectx, pa->recty, -0x7FFFFFFF);
2581         }
2582
2583         for (i=0, obi=R.instancetable.first; obi; i++, obi=obi->next) {
2584                 obr= obi->obr;
2585
2586                 if (!(obi->lay & lay))
2587                         continue;
2588
2589                 if (obi->flag & R_TRANSFORMED)
2590                         mul_m4_m4m4(obwinmat, winmat, obi->mat);
2591                 else
2592                         copy_m4_m4(obwinmat, winmat);
2593
2594                 if (clip_render_object(obi->obr->boundbox, bounds, obwinmat))
2595                         continue;
2596
2597                 zbuf_project_cache_clear(cache, obr->totvert);
2598
2599                 for (v=0; v<obr->totvlak; v++) {
2600                         if ((v & 255)==0) vlr= obr->vlaknodes[v>>8].vlak;
2601                         else vlr++;
2602                         
2603                         if (material_in_material(vlr->mat, sss_ma)) {
2604                                 /* three cases, visible for render, only z values and nothing */
2605                                 if (obi->lay & lay) {
2606                                         if (vlr->mat!=ma) {
2607                                                 ma= vlr->mat;
2608                                                 nofill= ma->mode & MA_ONLYCAST;
2609                                                 env= (ma->mode & MA_ENV);
2610                                                 wire= (ma->material_type == MA_TYPE_WIRE);
2611                                         }
2612                                 }
2613                                 else {
2614                                         nofill= 1;
2615                                         ma= NULL;       /* otherwise nofill can hang */
2616                                 }
2617                                 
2618                                 if (nofill==0 && wire==0 && env==0) {
2619                                         unsigned short partclip;
2620                                         
2621                                         v1= vlr->v1;
2622                                         v2= vlr->v2;
2623                                         v3= vlr->v3;
2624                                         v4= vlr->v4;
2625
2626                                         c1= zbuf_part_project(cache, v1->index, obwinmat, bounds, v1->co, ho1);
2627                                         c2= zbuf_part_project(cache, v2->index, obwinmat, bounds, v2->co, ho2);
2628                                         c3= zbuf_part_project(cache, v3->index, obwinmat, bounds, v3->co, ho3);
2629
2630                                         /* partclipping doesn't need viewplane clipping */
2631                                         partclip= c1 & c2 & c3;
2632                                         if (v4) {
2633                                                 c4= zbuf_part_project(cache, v4->index, obwinmat, bounds, v4->co, ho4);
2634                                                 partclip &= c4;
2635                                         }
2636
2637                                         if (partclip==0) {
2638                                                 c1= testclip(ho1);
2639                                                 c2= testclip(ho2);
2640                                                 c3= testclip(ho3);
2641
2642                                                 zvlnr= v+1;
2643                                                 zbufclip(&zspan, i, zvlnr, ho1, ho2, ho3, c1, c2, c3);
2644                                                 if (v4) {
2645                                                         c4= testclip(ho4);
2646                                                         zbufclip(&zspan, i, zvlnr+RE_QUAD_OFFS, ho1, ho3, ho4, c1, c3, c4);
2647                                                 }
2648                                         }
2649                                 }
2650                         }
2651                 }
2652         }
2653                 
2654         zbuf_free_span(&zspan);
2655 }
2656
2657 /* ******************** VECBLUR ACCUM BUF ************************* */
2658
2659 typedef struct DrawBufPixel {
2660         const float *colpoin;
2661         float alpha;
2662 } DrawBufPixel;
2663
2664
2665 static void zbuf_fill_in_rgba(ZSpan *zspan, DrawBufPixel *col, float *v1, float *v2, float *v3, float *v4)
2666 {
2667         DrawBufPixel *rectpofs, *rp;
2668         double zxd, zyd, zy0, zverg;
2669         float x0, y0, z0;
2670         float x1, y1, z1, x2, y2, z2, xx1;
2671         const float *span1, *span2;
2672         float *rectzofs, *rz;
2673         int x, y;
2674         int sn1, sn2, rectx, my0, my2;
2675         
2676         /* init */
2677         zbuf_init_span(zspan);
2678         
2679         /* set spans */
2680         zbuf_add_to_span(zspan, v1, v2);
2681         zbuf_add_to_span(zspan, v2, v3);
2682         zbuf_add_to_span(zspan, v3, v4);
2683         zbuf_add_to_span(zspan, v4, v1);
2684         
2685         /* clipped */
2686         if (zspan->minp2==NULL || zspan->maxp2==NULL) return;
2687         
2688         if (zspan->miny1 < zspan->miny2) my0= zspan->miny2; else my0= zspan->miny1;
2689         if (zspan->maxy1 > zspan->maxy2) my2= zspan->maxy2; else my2= zspan->maxy1;
2690         
2691         //      printf("my %d %d\n", my0, my2);
2692         if (my2<my0) return;
2693         
2694         /* ZBUF DX DY, in floats still */
2695         x1= v1[0]- v2[0];
2696         x2= v2[0]- v3[0];
2697         y1= v1[1]- v2[1];
2698         y2= v2[1]- v3[1];
2699         z1= v1[2]- v2[2];
2700         z2= v2[2]- v3[2];
2701         x0= y1*z2-z1*y2;
2702         y0= z1*x2-x1*z2;
2703         z0= x1*y2-y1*x2;
2704         
2705         if (z0==0.0f) return;
2706         
2707         xx1= (x0*v1[0] + y0*v1[1])/z0 + v1[2];
2708         
2709         zxd= -(double)x0/(double)z0;
2710         zyd= -(double)y0/(double)z0;
2711         zy0= ((double)my2)*zyd + (double)xx1;
2712         
2713         /* start-offset in rect */
2714         rectx= zspan->rectx;
2715         rectzofs= (float *)(zspan->rectz + rectx*my2);
2716         rectpofs= ((DrawBufPixel *)zspan->rectp) + rectx*my2;
2717         
2718         /* correct span */
2719         sn1= (my0 + my2)/2;
2720         if (zspan->span1[sn1] < zspan->span2[sn1]) {
2721                 span1= zspan->span1+my2;
2722                 span2= zspan->span2+my2;
2723         }
2724         else {
2725                 span1= zspan->span2+my2;
2726                 span2= zspan->span1+my2;
2727         }
2728         
2729         for (y=my2; y>=my0; y--, span1--, span2--) {
2730                 
2731                 sn1= floor(*span1);
2732                 sn2= floor(*span2);
2733                 sn1++; 
2734                 
2735                 if (sn2>=rectx) sn2= rectx-1;
2736                 if (sn1<0) sn1= 0;
2737                 
2738                 if (sn2>=sn1) {
2739                         zverg= (double)sn1*zxd + zy0;
2740                         rz= rectzofs+sn1;
2741                         rp= rectpofs+sn1;
2742                         x= sn2-sn1;
2743                         
2744                         while (x>=0) {
2745                                 if (zverg < (double)*rz) {
2746                                         *rz= zverg;
2747                                         *rp= *col;
2748                                 }
2749                                 zverg+= zxd;
2750                                 rz++; 
2751                                 rp++; 
2752                                 x--;
2753                         }
2754                 }
2755                 
2756                 zy0-=zyd;
2757                 rectzofs-= rectx;
2758                 rectpofs-= rectx;
2759         }
2760 }
2761
2762 /* char value==255 is filled in, rest should be zero */
2763 /* returns alpha values, but sets alpha to 1 for zero alpha pixels that have an alpha value as neighbor */
2764 void antialias_tagbuf(int xsize, int ysize, char *rectmove)
2765 {
2766         char *row1, *row2, *row3;
2767         char prev, next;
2768         int a, x, y, step;
2769         
2770         /* 1: tag pixels to be candidate for AA */
2771         for (y=2; y<ysize; y++) {
2772                 /* setup rows */
2773                 row1= rectmove + (y-2)*xsize;
2774                 row2= row1 + xsize;
2775                 row3= row2 + xsize;
2776                 for (x=2; x<xsize; x++, row1++, row2++, row3++) {
2777                         if (row2[1]) {
2778                                 if (row2[0]==0 || row2[2]==0 || row1[1]==0 || row3[1]==0)
2779                                         row2[1]= 128;
2780                         }
2781                 }
2782         }
2783         
2784         /* 2: evaluate horizontal scanlines and calculate alphas */
2785         row1= rectmove;
2786         for (y=0; y<ysize; y++) {
2787                 row1++;
2788                 for (x=1; x<xsize; x++, row1++) {
2789                         if (row1[0]==128 && row1[1]==128) {
2790                                 /* find previous color and next color and amount of steps to blend */
2791                                 prev= row1[-1];
2792                                 step= 1;
2793                                 while (x+step<xsize && row1[step]==128)
2794                                         step++;
2795                                 
2796                                 if (x+step!=xsize) {
2797                                         /* now we can blend values */
2798                                         next= row1[step];
2799
2800                                         /* note, prev value can be next value, but we do this loop to clear 128 then */
2801                                         for (a=0; a<step; a++) {
2802                                                 int fac, mfac;
2803                                                 
2804                                                 fac= ((a+1)<<8)/(step+1);
2805                                                 mfac= 255-fac;
2806                                                 
2807                                                 row1[a]= (prev*mfac + next*fac)>>8; 
2808                                         }
2809                                 }
2810                         }
2811                 }
2812         }
2813         
2814         /* 3: evaluate vertical scanlines and calculate alphas */
2815         /*    use for reading a copy of the original tagged buffer */
2816         for (x=0; x<xsize; x++) {
2817                 row1= rectmove + x+xsize;
2818                 
2819                 for (y=1; y<ysize; y++, row1+=xsize) {
2820                         if (row1[0]==128 && row1[xsize]==128) {
2821                                 /* find previous color and next color and amount of steps to blend */
2822                                 prev= row1[-xsize];
2823                                 step= 1;
2824                                 while (y+step<ysize && row1[step*xsize]==128)
2825                                         step++;
2826                                 
2827                                 if (y+step!=ysize) {
2828                                         /* now we can blend values */
2829                                         next= row1[step*xsize];
2830                                         /* note, prev value can be next value, but we do this loop to clear 128 then */
2831                                         for (a=0; a<step; a++) {
2832                                                 int fac, mfac;
2833                                                 
2834                                                 fac= ((a+1)<<8)/(step+1);
2835                                                 mfac= 255-fac;
2836                                                 
2837                                                 row1[a*xsize]= (prev*mfac + next*fac)>>8; 
2838                                         }
2839                                 }
2840                         }
2841                 }
2842         }
2843         
2844         /* last: pixels with 0 we fill in zbuffer, with 1 we skip for mask */
2845         for (y=2; y<ysize; y++) {
2846                 /* setup rows */
2847                 row1= rectmove + (y-2)*xsize;
2848                 row2= row1 + xsize;
2849                 row3= row2 + xsize;
2850                 for (x=2; x<xsize; x++, row1++, row2++, row3++) {
2851                         if (row2[1]==0) {
2852                                 if (row2[0]>1 || row2[2]>1 || row1[1]>1 || row3[1]>1)
2853                                         row2[1]= 1;
2854                         }
2855                 }
2856         }
2857 }
2858
2859 /* in: two vectors, first vector points from origin back in time, 2nd vector points to future */
2860 /* we make this into 3 points, center point is (0, 0) */
2861 /* and offset the center point just enough to make curve go through midpoint */
2862
2863 static void quad_bezier_2d(float *result, float *v1, float *v2, float *ipodata)
2864 {
2865         float p1[2], p2[2], p3[2];
2866         
2867         p3[0]= -v2[0];
2868         p3[1]= -v2[1];
2869         
2870         p1[0]= v1[0];
2871         p1[1]= v1[1];
2872         
2873         /* official formula 2*p2 - 0.5*p1 - 0.5*p3 */
2874         p2[0]= -0.5f*p1[0] - 0.5f*p3[0];
2875         p2[1]= -0.5f*p1[1] - 0.5f*p3[1];
2876         
2877         result[0]= ipodata[0]*p1[0] + ipodata[1]*p2[0] + ipodata[2]*p3[0];
2878         result[1]= ipodata[0]*p1[1] + ipodata[1]*p2[1] + ipodata[2]*p3[1];
2879 }
2880
2881 static void set_quad_bezier_ipo(float fac, float *data)
2882 {
2883         float mfac= (1.0f-fac);
2884         
2885         data[0]= mfac*mfac;
2886         data[1]= 2.0f*mfac*fac;
2887         data[2]= fac*fac;
2888 }
2889
2890 void RE_zbuf_accumulate_vecblur(NodeBlurData *nbd, int xsize, int ysize, float *newrect, float *imgrect, float *vecbufrect, float *zbufrect)
2891 {
2892         ZSpan zspan;
2893         DrawBufPixel *rectdraw, *dr;
2894         static float jit[256][2];
2895         float v1[3], v2[3], v3[3], v4[3], fx, fy;
2896         float *rectvz, *dvz, *dimg, *dvec1, *dvec2, *dz, *dz1, *dz2, *rectz;
2897         float *minvecbufrect= NULL, *rectweight, *rw, *rectmax, *rm, *ro;
2898         float maxspeedsq= (float)nbd->maxspeed*nbd->maxspeed;
2899         int y, x, step, maxspeed=nbd->maxspeed, samples= nbd->samples;
2900         int tsktsk= 0;
2901         static int firsttime= 1;
2902         char *rectmove, *dm;
2903         
2904         zbuf_alloc_span(&zspan, xsize, ysize, 1.0f);
2905         zspan.zmulx=  ((float)xsize)/2.0f;
2906         zspan.zmuly=  ((float)ysize)/2.0f;
2907         zspan.zofsx= 0.0f;
2908         zspan.zofsy= 0.0f;
2909         
2910         /* the buffers */
2911         rectz= MEM_mapallocN(sizeof(float)*xsize*ysize, "zbuf accum");
2912         zspan.rectz= (int *)rectz;
2913         
2914         rectmove= MEM_mapallocN(xsize*ysize, "rectmove");
2915         rectdraw= MEM_mapallocN(sizeof(DrawBufPixel)*xsize*ysize, "rect draw");
2916         zspan.rectp= (int *)rectdraw;
2917
2918         rectweight= MEM_mapallocN(sizeof(float)*xsize*ysize, "rect weight");
2919         rectmax= MEM_mapallocN(sizeof(float)*xsize*ysize, "rect max");
2920         
2921         /* debug... check if PASS_VECTOR_MAX still is in buffers */
2922         dvec1= vecbufrect;
2923         for (x= 4*xsize*ysize; x>0; x--, dvec1++) {
2924                 if (dvec1[0]==PASS_VECTOR_MAX) {
2925                         dvec1[0]= 0.0f;
2926                         tsktsk= 1;
2927                 }
2928         }
2929         if (tsktsk) printf("Found uninitialized speed in vector buffer... fixed.\n");
2930         
2931         /* min speed? then copy speedbuffer to recalculate speed vectors */
2932         if (nbd->minspeed) {
2933                 float minspeed= (float)nbd->minspeed;
2934                 float minspeedsq= minspeed*minspeed;
2935                 
2936                 minvecbufrect= MEM_mapallocN(4*sizeof(float)*xsize*ysize, "minspeed buf");
2937                 
2938                 dvec1= vecbufrect;
2939                 dvec2= minvecbufrect;
2940                 for (x= 2*xsize*ysize; x>0; x--, dvec1+=2, dvec2+=2) {
2941                         if (dvec1[0]==0.0f && dvec1[1]==0.0f) {
2942                                 dvec2[0]= dvec1[0];
2943                                 dvec2[1]= dvec1[1];
2944                         }
2945                         else {
2946                                 float speedsq= dvec1[0]*dvec1[0] + dvec1[1]*dvec1[1];
2947                                 if (speedsq <= minspeedsq) {
2948                                         dvec2[0]= 0.0f;
2949                                         dvec2[1]= 0.0f;
2950                                 }
2951    &n