From: T DOT Harte AT btinternet DOT com (Thomas Harte) Newsgroups: comp.os.msdos.djgpp Subject: Clipping lines on the third dimension in Allegro Date: Mon, 30 Jun 1997 17:12:55 GMT Organization: BT Internet Message-ID: <33b7e770.184973@news.btinternet.com> NNTP-Posting-Host: host5-99-60-208.btinternet.com Lines: 64 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk I wrote earlier about wanting a 3d lib which clipped polygons at the line z=0. And I haven't received a reply yet, so I've been looking into doing it myself and then using Allegro's polygon functions. I have yet to fix the u and v co-ordinates (shouldn't take more than an hour, probably just a stupid mistype somewhere, as x,y,z & c already work), but I thought I'd let you know because I've seen a few people asking for code which does this. The reason I'm writing now and not then is because this code :- void line3d(BITMAP *bitmap, float x1, float y1, float z1, float x2, float y2, float z2, int colour) { V3D_f points[2]; float ratio; points[0].x=x1; points[0].y=y1; points[0].z=z1; points[1].x=x2; points[1].y=y2; points[1].z=z2; if(!(points[0].z < 1 && points[1].z < 1)) { if(points[0].z < 1) { ratio=(1-points[0].z)/(points[0].z-points[1].z); points[0].x = ((points[0].x-points[1].x) * ratio) + points[0].x; points[0].y = ((points[0].y-points[1].y) * ratio) + points[0].y; points[0].z = 1; } if(points[1].z < 1) { ratio=(1-points[1].z)/(points[1].z-points[0].z); points[1].x = ((points[1].x-points[0].x) * ratio) + points[1].x; points[1].y = ((points[1].y-points[0].y) * ratio) + points[1].y; points[1].z = 1; } persp_project_f(points[0].x, points[0].y, points[0].z, &points[0].x, &points[0].y); persp_project_f(points[1].x, points[1].y, points[1].z, &points[1].x, &points[1].y); line(bitmap, points[0].x, points[0].y, points[1].x, points[1].y, colour); } } . . . which draws a line between two positions in 3d space, clipping at z=1 does work, and I thought someone might want to use it. It's probably not optimised for speed or anything, but it works. Could it be included as a new function in the next Allegro? An important thing to remember is not to perspective project the points before you pass them. Apart from the added z co-ordinate, this works exactly the same as the regular line function in Allegro. Well, anyone who wants to - enjoy using this code! And come back later (hopefully) for a polygon clipping one. -Thomas http://www.softysoft.home.ml.org