Team Ai
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes847downloads
BallGen.txt130 linesDownload Raw Back to GrModels
1 2//Makes a mesh ball.3 4#define VERTICES_NUM    10245#define TRIS_NUM        10246 7class Ball8{9  I32 vertex_cnt;10  I32 tri_cnt;11  CD3I32            v[VERTICES_NUM];12  CMeshTri t[TRIS_NUM];13} *b;14 15class BallDefineStruct16{17  F64 radius    format   "$DA-TRM,A=\"Radius         :%8.3f\"$\n";18  I64 n_longitude format "$DA-TRM,A=\"Longitude Faces:%5d\"$\n";19  I64 n_lattitude format "$DA-TRM,A=\"Lattitude Rings:%5d\"$\n";20};21 22U0 BDInit(BallDefineStruct *bd)23{24  MemSet(bd,0,sizeof(BallDefineStruct));25  bd->n_longitude=16;26  bd->n_lattitude=8;27  bd->radius     =20.0;28}29 30U0 BDCorrect(BallDefineStruct *bd)31{32  bd->n_longitude=(bd->n_longitude+1)/2;33  bd->n_lattitude=(bd->n_lattitude+1)/2;34}35 36I64 AddVertex(BallDefineStruct *,I64 x,I64 y,I64 z)37{38  I64 i;39  for (i=0;i<b->vertex_cnt;i++)40    if (b->v[i].x==x && b->v[i].y==y && b->v[i].z==z)41      return i;42  i=b->vertex_cnt++;43  b->v[i].x=x;44  b->v[i].y=y;45  b->v[i].z=z;46  return i;47}48 49I64 AddTri(BallDefineStruct *,I64 c,I64 n0,I64 n1,I64 n2)50{51  I64 res=b->tri_cnt++;52  b->t[res].color=c;53  b->t[res].nums[0]=n0;54  b->t[res].nums[1]=n1;55  b->t[res].nums[2]=n2;56  return res;57}58 59U8 *Ball2CSprite()60{61//See ::/Adam/Gr/GrSpritePlot.HC for how CSprite are stored.62  U8 *res=MAlloc(sizeof(CSpriteMeshU8s)+63        b->vertex_cnt*sizeof(CD3I32)+b->tri_cnt*sizeof(CMeshTri)64  +sprite_elem_base_sizes[SPT_END]),65        *dst=res;66  *dst++ =SPT_MESH;67  *dst(I32 *)++ =b->vertex_cnt;68  *dst(I32 *)++ =b->tri_cnt;69  MemCpy(dst,&b->v,b->vertex_cnt*sizeof(CD3I32));70  dst+=b->vertex_cnt*sizeof(CD3I32);71  MemCpy(dst,&b->t,b->tri_cnt*sizeof(CMeshTri));72  dst+=b->tri_cnt*sizeof(CMeshTri);73  *dst++ =SPT_END;74  return res;75}76 77public U8 *BallGen()78{79  U8 *res;80  I64 i,j,n,m,c,p1,p2,p3,p4;81  BallDefineStruct bd1,bd2;82  F64 r,r1,r2,z1,z2,d_a1,d_a2;83 84  BDInit(&bd1);85 86  while (TRUE) {87    if (!DocForm(&bd1))88      return NULL;89    MemCpy(&bd2,&bd1,sizeof(BallDefineStruct));90    BDCorrect(&bd2);91 92    c=PopUpColorLighting;93    if (c<0)  return NULL;94 95    b=CAlloc(sizeof(Ball));96    r=bd2.radius;97    n=bd2.n_lattitude;98    m=bd2.n_longitude;99    d_a1=2*pi/n/4;100    d_a2=2*pi/m/2;101    for (j=-n;j<n;j++) {102      r1=r*Cos( j   *d_a1);103      r2=r*Cos((j+1)*d_a1);104      z1=r*Sin( j   *d_a1);105      z2=r*Sin((j+1)*d_a1);106      for (i=0;i<m;i++) {107        p1=AddVertex(&bd2,r1*Cos((2*i  +j)*d_a2),r1*Sin((2*i  +j)*d_a2),z1);108        p2=AddVertex(&bd2,r1*Cos((2*i+2+j)*d_a2),r1*Sin((2*i+2+j)*d_a2),z1);109        p3=AddVertex(&bd2,r2*Cos((2*i+1+j)*d_a2),r2*Sin((2*i+1+j)*d_a2),z2);110        p4=AddVertex(&bd2,r2*Cos((2*i+3+j)*d_a2),r2*Sin((2*i+3+j)*d_a2),z2);111        AddTri(&bd2,c,p1,p2,p3);112        AddTri(&bd2,c,p3,p2,p4);113      }114    }115 116    res=Ball2CSprite;117    "%h7c",'\n';118    Sprite(res,"\t\t$SP,\"<1>\",BI=%d$");119    "%h7c",'\n';120    "Vertices:%d\n",b->vertex_cnt;121    Free(b);122    "Do another";123    if (YorN)124      Free(res);125    else126      break;127  }128  return res;129}130