int sphereSetup(float r, float **ppVertex, float **ppTexture) { int count = 0; int bw = (int)(360/angleSpan); int bh = (int)(180/angleSpan); count = bw*bh; VertexPointCnt = count * 18; TexturePointCnt = count * 12; pVertex = malloc(VertexPointCnt * sizeof(float)); pTexture = malloc(TexturePointCnt * sizeof(float)); VertexPointCnt = 0; if (!pTexture || !pVertex){ if (pTexture) free(pTexture); if (pVertex) free(pVertex); return -1; } generateVertexData(r); generateTexCoor(); vCount = VertexPointCnt/3; *ppVertex = pVertex; *ppTexture = pTexture; return vCount; }
void Moon::initVertexData(float r) { //顶点坐标数据的初始化================begin============================ const float UNIT_SIZE=0.5f; const float angleSpan=10;//将球进行单位切分的角度 float alVertixs[11664];//36*18*6*3 static float *alVertix; alVertix = alVertixs; for(float vAngle=90;vAngle>-90;vAngle=vAngle-angleSpan){//垂直方向angleSpan度一份 for(float hAngle=360;hAngle>0;hAngle=hAngle-angleSpan){//水平方向angleSpan度一份 //纵向横向各到一个角度后计算对应的此点在球面上的坐标 double xozLength=r*UNIT_SIZE*cos(toRadians(vAngle)); float x1=(float)(xozLength*cos(toRadians(hAngle))); float z1=(float)(xozLength*sin(toRadians(hAngle))); float y1=(float)(r*UNIT_SIZE*sin(toRadians(vAngle))); xozLength=r*UNIT_SIZE*cos(toRadians(vAngle-angleSpan)); float x2=(float)(xozLength*cos(toRadians(hAngle))); float z2=(float)(xozLength*sin(toRadians(hAngle))); float y2=(float)(r*UNIT_SIZE*sin(toRadians(vAngle-angleSpan))); xozLength=r*UNIT_SIZE*cos(toRadians(vAngle-angleSpan)); float x3=(float)(xozLength*cos(toRadians(hAngle-angleSpan))); float z3=(float)(xozLength*sin(toRadians(hAngle-angleSpan))); float y3=(float)(r*UNIT_SIZE*sin(toRadians(vAngle-angleSpan))); xozLength=r*UNIT_SIZE*cos(toRadians(vAngle)); float x4=(float)(xozLength*cos(toRadians(hAngle-angleSpan))); float z4=(float)(xozLength*sin(toRadians(hAngle-angleSpan))); float y4=(float)(r*UNIT_SIZE*sin(toRadians(vAngle))); //构建第一三角形 *alVertix = x1; alVertix++; *alVertix = y1; alVertix++; *alVertix = z1; alVertix++; *alVertix = x2; alVertix++; *alVertix = y2; alVertix++; *alVertix = z2; alVertix++; *alVertix = x4; alVertix++; *alVertix = y4; alVertix++; *alVertix = z4; alVertix++; //构建第二三角形 *alVertix = x4; alVertix++; *alVertix = y4; alVertix++; *alVertix = z4; alVertix++; *alVertix = x2; alVertix++; *alVertix = y2; alVertix++; *alVertix = z2; alVertix++; *alVertix = x3; alVertix++; *alVertix = y3; alVertix++; *alVertix = z3; alVertix++; }} vCount=11664/3;//顶点的数量为坐标值数量的1/3,因为一个顶点有3个坐标 //将alVertix中的坐标值转存到一个float数组中 int count = 11664; static float ver[11664]; for(int i=0;i<count;i++) { ver[i] = alVertixs[i]; } mVertexBuffer = ver; static float tex[7776]; generateTexCoor(36, 18, tex); mTexCoorBuffer = tex; }