#include #include #include #include "paulslib.h" /* Create a capsule as an obj file = cylinder + two spherical caps ps: Designed to illustrate/verify the apporach, not good coding practices. */ int N = 32; // Mesh resolution double radius = 1; double height = 2; typedef struct { double x,y,z; double nx,ny,nz; double u,v; } MESHVERTEX; int main(int argc,char **argv) { int i,j,index = 0; int i1,i2,i3,i4; double theta,phi; MESHVERTEX *p = NULL,n; double len; FILE *fptr; if (argc < 4) { fprintf(stderr,"Usage: %s res radius height\n",argv[0]); exit(-1); } N = atoi(argv[1]); radius = atof(argv[2]); height = atof(argv[3]); // Create data if ((p = malloc((N+1)*(N/2+2)*sizeof(MESHVERTEX))) == NULL) { fprintf(stderr,"malloc failed\n"); exit(-1); } for (j=0;j<=N/4;j++) { // top cap for (i=0;i<=N;i++) { theta = i * TWOPI / N; phi = -PID2 + PI * j / (N/2); p[index].x = radius * cos(phi) * cos(theta); p[index].y = radius * cos(phi) * sin(theta); p[index].z = radius * sin(phi); p[index].nx = p[index].x; p[index].ny = p[index].y; p[index].nz = p[index].z; p[index].z -= height/2; index++; } } for (j=N/4;j<=N/2;j++) { // bottom cap for (i=0;i<=N;i++) { theta = i * TWOPI / N; phi = -PID2 + PI * j / (N/2); p[index].x = radius * cos(phi) * cos(theta); p[index].y = radius * cos(phi) * sin(theta); p[index].z = radius * sin(phi); p[index].nx = p[index].x; p[index].ny = p[index].y; p[index].nz = p[index].z; p[index].z += height/2; index++; } } if (index != (N+1)*(N/2+2)) { fprintf(stderr,"Unexpeced number of vertices, %d != %d\n",index,(N+1)*(N/2+2)); exit(-1); } // Calculate texture coordinates for (i=0;i