/* Generates the vertices for a ball approximated with polygonal facets, in a form suitable for including into a C module. Vertices, faces, and supporting info is generated, in the following format: ----------------------------start------------------------------ #define NUM_FACES #define NUM_VERTS Point3 Verts[] = { }; static int Face0[] = { }; static int Face1[] = { }; : static int Face[] = { }; static int *VertNumList[] = { }; static int VertsInFace[] = { <# vertices in each face> }; ----------------------------end-------------------------------- */ #include #include #include #include #include "polygon.h" #define PI 3.14159265358979323846 #define DOUBLE_TO_FIXED(x) ((long) (x * 65536.0 + 0.5)) void main(void); void PrintVertex(struct Point3 *); void Print3Indexes(int, int, int, int); void Print4Indexes(int, int, int, int, int); /* Used to rotate around the Y axis by one band's width */ static double YXform[4][4] = { {1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0} }; /* Used to rotate around the Z axis by one band's width */ static double ZXform[4][4] = { {1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0} }; static FILE *OutputFile; /* where we'll write to */ void main() { int Radius, Bands, i, j, LastIndex, BandsX2, LastBandStartIndex; int TopBandStartIndex, BottomBandStartIndex, FaceNum; struct Point3 BaseVec, BandVec, WorkingVec, TempVec; char OutputFilename[130]; char Description[130]; printf("Radius: "); scanf("%d",&Radius); printf("Bands: "); scanf("%d",&Bands); printf("Output file: "); OutputFilename[0] = 128; cgets(OutputFilename); printf("\nBrief description: "); Description[0] = 128; cgets(Description); printf("\n"); BandsX2 = Bands*2; if ((OutputFile = fopen(&OutputFilename[2], "w")) == NULL) { printf("Error\n"); exit(1); } /* Descriptive comments */ fprintf(OutputFile, "/* %s */\n", &Description[2]); fprintf(OutputFile, "/* Created with radius = %d, bands = %d */\n", Radius, Bands); /* Defines for # of faces and vertices */ fprintf(OutputFile, "#define NUM_FACES %d\n", BandsX2*Bands); fprintf(OutputFile, "#define NUM_VERTS %d\n\n", BandsX2*(Bands-1)+2); /* Do the vertices */ fprintf(OutputFile, "Point3 Verts[] = {\n"); /* Generate the rotation matrices */ AppendRotationY(YXform, PI / Bands); AppendRotationZ(ZXform, PI / Bands); /* Do the point at the top */ BaseVec.X = 0.0; BaseVec.Y = Radius; BaseVec.Z = 0.0; BaseVec.W = 1.0; PrintVertex(&BaseVec); BandVec = BaseVec; /* Do the vertices in each band in turn */ for (i=1; iX); long Y = DOUBLE_TO_FIXED(Vec->Y); long Z = DOUBLE_TO_FIXED(Vec->Z); fprintf(OutputFile, "{%ld, %ld, %ld},\n", X, Y, Z); }