0

I am creating a simple little software 3D engine. Right now a polygon doesn't render if all of the vertexes are outside of the frustum, that's all fine and good until you are close to the polygon and all the vertexes are off the screen but the middle is still inside the frustum but it omits it anyways. I would just try render it anyways but I need to do some optimizations to it so that is the first one I thought of.

Here is a gif & some code of the problem if you didn't understand what I was trying to get across.

boolean v1Inside = v1.isInsideViewFrustum();
boolean v2Inside = v2.isInsideViewFrustum();
boolean v3Inside = v3.isInsideViewFrustum();
if (v1Inside && v2Inside && v3Inside) {
   rasterizeTriangle(v1, v2, v3);
   return;
}

if (!(v1Inside || v2Inside || v3Inside)) {
    return;
}

not rendering when all verts are off screen

genpfault
  • 51,148
  • 11
  • 85
  • 139
user1826677
  • 29
  • 1
  • 2
  • What you're looking for is generally called "clipping". If you do a search for "clipping algorithm", you should find plenty of material. For example, one standard algorithm is Sutherland-Hodgman: http://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm. – Reto Koradi Jun 04 '15 at 05:40

2 Answers2

1

You can implement a conservative approach to frustum culling, that occasionally gives false positives (instead of this false negative). The most typical approach is to reject a triangle, if all vertices fall on the wrong side of some frustum plane.

   a       b
A  |    B  |   C
 --+-------+-------  c
D  |       |   E
   |       |
---+-------+-------  d
F  |   G   |   H

Here in 2D case, there are four clipping planes: a, b, c, d. This approach rejects a triangle that is fully above line/plane 'c', below line 'd', left of line 'a' or right of line 'b', not rejecting e.g. your case of vertices in segments B, G and H.

This kind of culling is easy to implement after perspective projection, giving simple equations: e.g. x0,x1,x2 < -1, or using the planar equations of the frustum:

dot_product(vertex, plane) < constant_i.
Aki Suihkonen
  • 19,144
  • 1
  • 36
  • 57
0

The de-facto standard algorithm for this problem is the Cohen-Sutherland Line Clipping algorithm. It's easily extended to convex polygons, since such are visible if anyone of its edges is visible.

EDIT: When it comes to polygons you have to test for loops around or involving the center. You can do that using XORs and helper bitmasks. I'll add this here once I've found a good reference for this.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • What about polygons enclosing the whole clip rectangle? No edge is visible. – maaartinus Jun 04 '15 at 10:21
  • @maaartinus: Two steps: 1st Consider one pivot vertex and test for the edge visibility of that vertex to all of the other vertices in the polygon. 2nd Choen-Sutherland boils down to bit manipulations and for lines uses OR and AND. But you can also test for loops around or involving the center by XOR-ing. – datenwolf Jun 04 '15 at 11:00