From: T DOT Harte AT btinternet DOT com (Thomas Harte) Newsgroups: comp.os.msdos.djgpp Subject: Clipping Allegro polygons at z=1 Date: Mon, 30 Jun 1997 18:49:10 GMT Organization: BT Internet Message-ID: <33b7f92d.1439383@news.btinternet.com> NNTP-Posting-Host: host5-99-52-213.btinternet.com Lines: 123 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Hi. Earlier I posted code which allows you to draw a line anywhere on screen, even crossing the line z=1, where functions without clipping would fail. I said I would be back with the polygon version, and here I am! It would be nice if something like this was in Allegro 2.3, although I'm sure there are much better implementations that mine, and several ways to speed up the code I have provided if you know what you're doing. I just read the theory last night, and implemented it today. Anyway, the quad3d version (now called clquad3d, 'cos it clips and that) looks like this :- void clquad3d(BITMAP *bitmap, int type, BITMAP *texture, V3D_f v1, V3D_f v2, V3D_f v3, V3D_f v4) { V3D_f points[5], opoints[4]; int numpoints=0,temp[2]; float ratio; if(!(v1.z < 1 && v2.z < 1 && v3.z < 1 && v4.z < 1)) { opoints[0] = v1; opoints[1] = v2; opoints[2] = v3; opoints[3] = v4; for(temp[0]=0;temp[0]<4;temp[0]++) { if(opoints[temp[0]].z >= 1) { points[numpoints]=opoints[temp[0]]; numpoints++; } temp[1]=temp[0]+1; if(temp[1] == 4) temp[1]=0; if(opoints[temp[0]].z >= 1 && opoints[temp[1]].z < 1) { ratio=(1-opoints[temp[1]].z)/(opoints[temp[1]].z-opoints[temp[0]].z); points[numpoints].x = ((opoints[temp[1]].x-opoints[temp[0]].x) * ratio) + opoints[temp[1]].x; points[numpoints].y = ((opoints[temp[1]].y-opoints[temp[0]].y) * ratio) + opoints[temp[1]].y; points[numpoints].u = ((opoints[temp[1]].u-opoints[temp[0]].u) * ratio) + opoints[temp[1]].u; points[numpoints].v = ((opoints[temp[1]].v-opoints[temp[0]].v) * ratio) + opoints[temp[1]].v; points[numpoints].c = ((opoints[temp[1]].c-opoints[temp[0]].c) * ratio) + opoints[temp[1]].c; points[numpoints].z = 1; numpoints++; } if(opoints[temp[0]].z < 1 && opoints[temp[1]].z >= 1) { ratio=(1-opoints[temp[0]].z)/(opoints[temp[0]].z-opoints[temp[1]].z); points[numpoints].x = ((opoints[temp[0]].x-opoints[temp[1]].x) * ratio) + opoints[temp[0]].x; points[numpoints].y = ((opoints[temp[0]].y-opoints[temp[1]].y) * ratio) + opoints[temp[0]].y; points[numpoints].u = ((opoints[temp[0]].u-opoints[temp[1]].u) * ratio) + opoints[temp[0]].u; points[numpoints].v = ((opoints[temp[0]].v-opoints[temp[1]].v) * ratio) + opoints[temp[0]].v; points[numpoints].c = ((opoints[temp[0]].c-opoints[temp[1]].c) * ratio) + opoints[temp[0]].c; points[numpoints].z = 1; numpoints++; } } for(temp[0]=0;temp[0]