Generating Capsule GeometryWritten by Paul Bourke
The following will briefly describe a way of creating an capsule shape, example code is supplied here: capsule.c (Tab stops to 3 spaces for correct indenting). which created the shape and saves the result as an obj file with normals and texture coordinates. The code should be easy to modify to create the mesh geometry in other formats or for APIs such as OpenGL. The output from the code plus a sample texture map are given below.
It is fairly obvious that the desired shape is just two halves of a sphere offset along the intended axis of the capsule. Also, that it is not necessary to explicitly add a central cylinder, rather just join up the equator of each half sphere.
The normals are straightforward to calculate, they are just the vectors to the vertices of the normalised sphere before offsetting along the capsule axis. There are many options for the texture coordinates, the one chosen here is the projection of a sphere onto the capsule. These are computed by calculating the longitude and latitude of each vertex of the capsule and mapping those to (u,v) in the usual way. The result given the texture map (spherical projection) given above is shown below.
Coordinate system conventions.