Suppose, the near and far values of gluPerspective are 1 and 100 respectively. I want to clip the portion of the triangle having z values greater than (-near) or less than (-far) and divide the remaining polygon into two triangles like the following images.
Considering near plane:
Another Case:
I have calculated the intersection points in the following way: z_clip=-far (for considering far plane) or z_clip=-near (for considering near plane)
For example: I f we consider edge between (x1,y1,z1) and (x2,y2,z2), then:
co-efficient = (z_clip - z1) / (z2 - z1)
xc = x1 + co-efficient * (x2 - x1)
yc = y1 + co-efficient * (y2 - y1)
zc = z_clip
(xc,yc,zc) will be a point of intersection. Similarly, I can calculate another point of intersection for other edge. For example: edge between (10, 0, -50) and (0, 0, -150) will be cut in the point (5, 0, -100).
Original Triangle:
0 0 -150
10 0 -50
0 10 -50
After Clipping:
Triangle1:
0 10 -50
5 0 100
0 5 -100
Triangle2:
10 0 -50
0 5 -100
0 10 -50
Am I right in my calculation procedure or is there anything wrong in the procedure?