Quote Originally Posted by chronozphere
Now you should have 4 points that would make up the polygon that intersects the cube.
There are up to six points possible. Your code should be prepared to deal with resulting polygons having <= 6 points (for 1 or 2 points you may decide to ignore them, since this means that only a corner/edge of the box touches the plane, but you still have to be prepared for any polygon having between 3 and 6 points).

Uhm, if it's not obvious how to make a case with 6 intersections: imagine a horizontal plane cutting through a box in half. Now rotate this plane, such that it touches two opposite corners of the box. Now rotate just an epsilon more... and you get 6 intersections.

Ok, whole algorithm as I imagine goes like this:

1. First I would just do 12 intersection tests (like chronozphere writes, just test each line (cube edge stretched into infinity) with plane and then cut to the cube). For each intersection, remember the calculated 3D intersection point. Also, for each intersection point remember indexes of 2 neighborhood faces of the intersected edge (these will be needed to connect intersection points into segments).

2. Then merge duplicates on the intersection points list. Remember that if plane crosses exactly the corner of the box, then you will have duplicates (or "almost-by-epsilon-duplicates") on the list of intersection points. During this merging, some intersection points may be determined to have 3 neighboring faces (these are box corners that are intersected by the polygon).

3. Then gather the "neighboring faces" indexes to actually calculate the intersection segments. For each face:
- if it has 0 intersection points, obviously ignore
- if it has 1 intersection point, also ignore (such faces are possible when box cornes are on the list of intersection points)
- if face has 2 intersection points --- cool, add the segment between these two points to the generated polygon

- If any face has more than 2 intersection points, then your plane lies exactly on one of the cube's sides. This is a special case, you should then either abort intersection or return the whole cube face as intersection polygon.

Hope this helps