I decided to write this short tutorial to make absolutely sure I didn't forget how to clip 3d triangles. Then I realized I could post it on QB Express as well! So why the hell not. Z-clipping triangles Whenever you have a point in 3d space, it will either be in front(z >0), behind you (z<0) or on you (z=0). The easiest way to eliminate this problem would be to make sure all points of a triangle are visible, so if one isn't visible the triangle isn't drawn. Of course, as I discovered, this looks terrible for large polygons, creating dumb-looking see through glitches. So what's the remedy? Remove the parts of the triangle that are not visible, leaving only the visible parts. I suggest for the code you use double-precision(defdbl a-z for the poly clipping sub), I used that cuz sometimes errors occurred that I thought might have been because of accuracy. Starting off.... Lets say you have each point of your triangle in an array of vertex(3) as vtxinf where type vtxinf x as single y as single z as single u as single 'x-coord in texture map, I use between 0..1 but you can use whatever is appropriate v as single 'y-coord in texture map, I use between 0..1 but you can use whatever is appropriate end type The 3 points are in vertex(0), vertex(1), vertex(2). (The extra element is there for a reason.....as you might expect) To simplify coding this routine, we could start off by arranging it so the vertex with the largest z starts at the top, like this If vertex(0).z < vertex(1).z then swap vertex(0).x, vertex(1).x swap vertex(0).y, vertex(1).y swap vertex(0).z, vertex(1).z swap vertex(0).u, vertex(1).u swap vertex(0).v, vertex(1).v endif And so on and so forth until vertex(0).z is highest, and vertex(2).z is the lowest. Now to quickly test whether any portion of the polygon is visible on-screen, just check this if vertex(0).z > zclipplane then polyvisible = true 'where zclipplane is the minimum z can reach. Think of it as a line that extends to forever, and if a poly is behind it, it can't be seen Since vertex(0).z is now the highest z, if that's not visible no point is. Anyway now we go into the actual z-clipping. Just to give you a picture of what's going on, draw a triangle on a paper. This is your poly top-down. Draw a horizontal line(the z clip plane) that removes two points from the poly. If you did it right, you'll notice all that needs to be done is make the two edges to intersect the z-clip plane. Now try the same thing, but this time make the z-clip plane remove only one vertex. You'll see to get the visible poly you'll need to add an extra vertex, resulting in a 4-sided poly. Since clipping when 2 points are invisible is easier, I'll show you that first. If vertex(1).z < zclipplane AND vertex(2).z < zclipplane then 'start off processing the first edge.... dx = vertex(1).x - vertex(0).x dy = vertex(1).y - vertex(0).y dz = vertex(1).z - vertex(0).z du = vertex(1).u - vertex(0).u 'necessary to recalculate the new texture co-ordinates because otherwise it gets warped dv = vertex(1).v - vertex(0).v 'find the positions the edge intersects the z-clip plane 'zclipplane = vertex(0).z + t * dz rearrange to find t t = (zclipplane - vertex(0).z) / dz 'now find the new x, y, z, u and v co-ordinates vertex(1).x = vertex(0).x + t * dx vertex(1).y = vertex(0).y + t * dy vertex(1).u = vertex(0).u + t * du vertex(1).v = vertex(0).v + t * dv vertex(1).z = zclipplane 'if zclipplane <> vertex(0).z + t * dz there is an error in your code somewhere 'repeat the above line of code, but change the vertex(1) to a vertex(2) end if There. Now the array vertex contains your perfect z-clipped triangle. But how do we do it when we have one invisible vertex? Well consider the following. Since vertex(2).z is the smallest z value, it follows that if one vertex is invisible, it has to be vertex(2).z . Think about that if it doesn't make sense. Now go back to that diagram you drew of the triangle with one invisible vertex. Label the point with the largest z as 0, 2nd largest as 1, and the smallest as 2. To find the position of the clipped vertices, we must find where the edge vertex(0) to vertex(2) intersects the z-clip plane, which is the first clipped vertex. The 2nd clipped vertex is where the edge vertex(1) to vertex(2) intersects the z-clip plane. So here's some code for those of us who are lazy... extravtx = 0 if vertex(2).z < zclipplane then extravtx = 1 'so the code will remember there is now an extra vertex dx = vertex(2).x - vertex(0).x dy = vertex(2).y - vertex(0).y dz = vertex(2).z - vertex(0).z du = vertex(2).u - vertex(0).u dv = vertex(2).v - vertex(0).v t = (zclipplane - vertex(0).z) / dz vertex(3).x = vertex(0).x + t * dx vertex(3).y = vertex(0).y + t * dy vertex(3).z = vertex(0).z + t * dz vertex(3).u = vertex(0).u + t * du vertex(3).v = vertex(0).v + t * dv dx = vertex(2).x - vertex(1).x dy = vertex(2).y - vertex(1).y dz = vertex(2).z - vertex(1).z du = vertex(2).u - vertex(1).u dv = vertex(2).v - vertex(1).v t = (zclipplane - vertex(1).z) / dz vertex(2).x = vertex(1).x + t * dx vertex(2).y = vertex(1).y + t * dy vertex(2).z = vertex(1).z + t * dz vertex(2).u = vertex(1).u + t * du vertex(2).v = vertex(1).v + t * dv end if Now we have all the points sitting around in our array vertex. How do we go about displaying it on the screen (I assume you can only tmap triangles)? Well the first triangle would be vertex(0), vertex(1), vertex(2) . Obviously you have to do your 3d to 2d transformations and such....but you take care of that yourself. Now look at extravtx. If extravtx = 1, we have an extra vertex, which means, we have an extra triangle. Look back to the triangle diagram. It makes sense if I say the 2nd triangle would be vertex(0), vertex(2), vertex(3), right? Well yes right. That's it! A perfectly z-clipped triangle! What from here....... This code can be modified to do frustum culling as well, so you don't have to clip the triangle in 2d anymore! I'll leave that for you to figure out. I got lazy with my coding and didn't bother either. UGL does the 2d-clipping for me. If you want to contact me about errors, you need help, or whine about this not working, just contact me, Shashi of SBM Productions at theshashiman@gmail.com