Generating rounded cube geometryWritten by Paul Bourke
The following will briefly describe a way of creating a rounded cube shape, example code is supplied here: roundcube.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.
The roundedness of the cube is determined by the power the expressions in the polar coordinate calculation of the sphere. A power of 1 is a perfect sphere, smaller powers generate sharper and sharper cubes. The image below is a power of 0.5.
The normals are straightforward to calculate, they are just the vectors to the vertices of the roundcube centered at the origin. There are many options for the texture coordinates, the one chosen here is the projection of a sphere onto the rounded cube. 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. Examples given the texture map (spherical projection) given above is shown below for power of 0.2 and 0.1 respectively.
power = 0.2
power = 0.1
Note that this is a less general formulation of the superelipsoid.