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] = 127; cgets(OutputFilename); printf("\nBrief description: "); Description[0] = 127; 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", 2+1+BandsX2*(Bands-1)+1+ (BandsX2*Bands)); /* # of vertices excluding unit normal endpoints */ fprintf(OutputFile, "#define NUM_REAL_VERTS %d\n\n", 2+1+BandsX2*(Bands-1)+1); /* Do the polygon 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; i<Bands; i++) { /* Rotate around Z to the next band's latitude */ XformVec(ZXform, (double *)&BandVec, (double *)&TempVec); WorkingVec = BandVec = TempVec; /* Do the vertices in this band */ for (j=0; j<BandsX2; j++) { WorkingVec = TempVec; PrintVertex(&WorkingVec); /* Now rotate around Y to the next vertex's longitude */ XformVec(YXform, (double *)&WorkingVec, (double *)&TempVec); } } /* Do the point at the bottom */ BaseVec.Y = -Radius; PrintVertex(&BaseVec); /* Now generate the unit normal endpoint for each face, as the last entries in the vertex list */ /* Index in overall vertex list of first unit normal endpoint we'll create; remember where we can find it */ FirstUnitNormalIndex = NumPoints; FaceNum = 0; /* Unit normal endpoints in top band */ for (i=0; i<BandsX2; i++) { UnitNormal(&BaseVec, 0, ((i+1)%BandsX2)+1, i+1); PrintVertex(&BaseVec); FaceNum++; } /* Unit normal endpoints in middle bands */ for (j=0; j<(Bands-2); j++) { TopBandStartIndex = j*BandsX2 + 1; BottomBandStartIndex = (j+1)*BandsX2 + 1; /* Indexes in this band */ for (i=0; i<BandsX2; i++) { UnitNormal(&BaseVec, i+TopBandStartIndex, ((i+1)%BandsX2)+TopBandStartIndex, i+BottomBandStartIndex); PrintVertex(&BaseVec); FaceNum++; } } /* Unit normal endpoints in bottom band */ LastIndex = BandsX2*(Bands-1)+1; LastBandStartIndex = BandsX2*(Bands-2)+1; for (i=0; i<BandsX2; i++) { UnitNormal(&BaseVec, LastBandStartIndex+i, LastBandStartIndex+((i+1)%BandsX2), LastIndex); PrintVertex(&BaseVec); FaceNum++; } /* Done generating points, including both polygon vertices and unit normals */ fprintf(OutputFile, "};\n\n"); /* Do the vertex indexes for each face in each band */ FaceNum = 0; /* Vertex indexes in top band, with unit normal endpoint first */ for (i=0; i<BandsX2; i++) { Print4Indexes(FaceNum++, FirstUnitNormalIndex++, 0, ((i+1)%BandsX2)+1, i+1); } /* Vertex indexes in middle bands, with unit normal endpoints first */ for (j=0; j<(Bands-2); j++) { TopBandStartIndex = j*BandsX2 + 1; BottomBandStartIndex = (j+1)*BandsX2 + 1; /* Indexes in this band */ for (i=0; i<BandsX2; i++) { Print5Indexes(FaceNum++, FirstUnitNormalIndex++, i+TopBandStartIndex, ((i+1)%BandsX2)+TopBandStartIndex, ((i+1)%BandsX2)+BottomBandStartIndex, i+BottomBandStartIndex); } } /* Vertex indexes in bottom band, with unit normal endpoint first */ LastIndex = BandsX2*(Bands-1)+1; LastBandStartIndex = BandsX2*(Bands-2)+1; for (i=0; i<BandsX2; i++) { Print4Indexes(FaceNum++, FirstUnitNormalIndex++, LastBandStartIndex+i, LastBandStartIndex+((i+1)%BandsX2), LastIndex); } /* Do the list of pointers to index arrays for each face */ fprintf(OutputFile, "\nstatic int *VertNumList[] = {\n"); for (i=0; i<(BandsX2*Bands); i++) { fprintf(OutputFile, "Face%d,\n", i); } fprintf(OutputFile, "};\n"); /* Do the # of vertices in each face (3 for the top and bottom bands, 4 for the rest) */ fprintf(OutputFile, "\nstatic int VertsInFace[] = {\n"); for (i=0; i<BandsX2; i++) fprintf(OutputFile, "3,\n"); for (i=0; i<(BandsX2*(Bands-2)); i++) fprintf(OutputFile, "4,\n"); for (i=0; i<BandsX2; i++) fprintf(OutputFile, "3,\n"); fprintf(OutputFile, "};\n"); exit(0); }
float GravityAcceleration(float x) const { return (ga * UnitNormal(x)); }