Clipping a convex polygon against a plane

The following code clips an arbitrary convex polygon against a given plane.

The vertex parameter points to an array containing the number of vertices specified by the vertexCount parameter, wound in counterclockwise order. The plane parameter specifies the plane against which the polygon is clipped. The location array is used for internal processing and must be large enough to hold vertexCount values. The result array is where the clipped polygon is returned and must generally be large enough to hold vertexCount + 1 vertices. The return value of the function is the number of vertices in the clipped polygon.

This code uses the Vector3D and Point3D classes.
This code uses the Vector4D class.

#include "Vector4D.h"


enum
{
    polygonInterior = 1,
    polygonBoundary = 0,
    polygonExterior = -1
};


const float boundaryEpsilon = 1.0e-3F;


long ClipPolygonAgainstPlane(long vertexCount, const Point3D *vertex,
        const Vector4D& plane, signed char *location, Point3D *result)
{
    long positive = 0;
    long negative = 0;
    
    for (long a = 0; a < vertexCount; a++)
    {
        float d = plane * vertex[a];
        if (d > boundaryEpsilon)
        {
            location[a] = polygonInterior;
            positive++;
        }
        else
        {
            if (d < -boundaryEpsilon)
            {
                location[a] = polygonExterior;
                negative++;
            }
            else
            {
                location[a] = polygonBoundary;
            }
        }
    }
    
    if (negative == 0)
    {
        for (long a = 0; a < vertexCount; a++) result[a] = vertex[a];
        return (vertexCount);
    }
    else if (positive == 0)
    {
        return (0);
    }
    
    long count = 0;
    long previous = vertexCount - 1;
    for (long index = 0; index < vertexCount; index++)
    {
        long loc = location[index];
        if (loc == polygonExterior)
        {
            if (location[previous] == polygonInterior)
            {
                const Point3D& v1 = vertex[previous];
                const Point3D& v2 = vertex[index];
                Vector3D dv = v2 - v1;
                
                float t = plane * v2 / (plane * dv);
                result[count++] = v2 - dv * t;
            }
        }
        else
        {
            const Point3D& v1 = vertex[index];
            if ((loc == polygonInterior) && (location[previous] == polygonExterior))
            {
                const Point3D& v2 = vertex[previous];
                Vector3D dv = v2 - v1;
                
                float t = plane * v2 / (plane * dv);
                result[count++] = v2 - dv * t;
            }
            
            result[count++] = v1;
        }
        
        previous = index;
    }
    
    return (count);
}

Public Source Code

The following are links to C++ snippets that we've released for various algorithms and techniques pertinent to 3D game programming.

Books by Eric Lengyel
Book

Mathematics for 3D Game Programming & Computer Graphics, Second Edition

This best-selling book teaches the mathematics and rendering techniques that a game programmer needs to develop a professional-quality 3D engine.
Book

The OpenGL Extensions Guide

This is the ultimate extension reference for the serious OpenGL programmer.
Books See all publications by Eric Lengyel

Conference Slides and Articles
GDC07

Projection Matrix Tricks (2007)
(PowerPoint, 3.26 MB)

This presentation examines the inner workings of the perspective projection matrix and discusses several techniques for modifying the properties of the projection matrix to solve specific rendering problems at zero cost.
GDC06

Advanced Light and Shadow Culling Methods (2006)
(PowerPoint, 852 kB)

This presentation focuses primarily on portal systems and describes algorithms and optimizations that can be applied to a graphics engine supporting completely dynamic lighting and shadows.
GDC05

Advanced Stencil Shadow and Penumbral Wedge Rendering (2005)
(PowerPoint, 1.00 MB)

This presentation reviews advanced implementation techniques of the stencil shadow algorithm and focuses on the relatively new method of penumbral wedge rendering used to generate soft-edged shadows.
Gamasutra

The Mechanics of Robust
Stencil Shadows

This article describes the mathematical details of efficient and robust stencil shadow rendering. These techniques are implemented in the C4 Engine.