Skip to content

Instantly share code, notes, and snippets.

@mousebird
Last active December 20, 2015 18:58
Show Gist options
  • Select an option

  • Save mousebird/6179398 to your computer and use it in GitHub Desktop.

Select an option

Save mousebird/6179398 to your computer and use it in GitHub Desktop.
FlexiVertexBuffer Sphere
// Number of samples in each direction
static const int LongitudeSample=20,LatitudeSample=10;
- (void)addSphereAt:(float *)org sized:(float *)size
{
// We'll generate the coordinates and normals ahead of time
float coords[3*LongitudeSample*LatitudeSample];
float norms[3*LongitudeSample*LatitudeSample];
float texCoords[2*LongitudeSample*LatitudeSample];
// Work our way around the equator, building vertices
int ix=0;
for (float lon=-180.0;ix<LongitudeSample;lon+=360.0/(LongitudeSample-1),ix++)
{
int iy=0;
// Run from the south pole to the north
for (float lat=-90.0;iy<LatitudeSample;lat+=180.0/(LatitudeSample-1),iy++)
{
// Generate a coordinate on the unit sphere
float coord[3];
float z = sinf(lat/180.0*M_PI);
float rad = sqrtf(1.0-z*z);
coord[0] = rad*cosf(lon/180.0*M_PI);
coord[1] = rad*sinf(lon/180.0*M_PI);
coord[2] = z;
// The normal is the coordinate (on the unit sphere)
float *thisNorm = &norms[3*(iy*LongitudeSample+ix)];
thisNorm[0] = coord[0]; thisNorm[1] = coord[1]; thisNorm[2] = coord[2];
// Scale the coordinate and save it off
float *thisCoord = &coords[3*(iy*LongitudeSample+ix)];
thisCoord[0] = coord[0]*size[0]+org[0]; thisCoord[1] = coord[1]*size[1]+org[1]; thisCoord[2] = coord[2]*size[2]+org[2];
// And texture coordinates
float *texCoord = &texCoords[2*(iy*LongitudeSample+ix)];
texCoord[0] = ix/(float)(LongitudeSample-1);
texCoord[1] = iy/(float)(LatitudeSample-1);
}
}
// Now for the triangles, we need two per sample
for (int ix=0;ix<LongitudeSample-1;ix++)
for (int iy=0;iy<LatitudeSample-1;iy++)
{
// Lower left triangle
[_vertices appendBytes:&coords[3*(iy*LongitudeSample+ix)] length:sizeof(float)*3];
[_vertices appendBytes:&norms[3*(iy*LongitudeSample+ix)] length:sizeof(float)*3];
[_vertices appendBytes:&texCoords[2*(iy*LongitudeSample+ix)] length:sizeof(float)*2];
[_vertices appendBytes:&coords[3*(iy*LongitudeSample+ix+1)] length:sizeof(float)*3];
[_vertices appendBytes:&norms[3*(iy*LongitudeSample+ix+1)] length:sizeof(float)*3];
[_vertices appendBytes:&texCoords[2*(iy*LongitudeSample+ix+1)] length:sizeof(float)*2];
[_vertices appendBytes:&coords[3*((iy+1)*LongitudeSample+ix)] length:sizeof(float)*3];
[_vertices appendBytes:&norms[3*((iy+1)*LongitudeSample+ix)] length:sizeof(float)*3];
[_vertices appendBytes:&texCoords[2*((iy+1)*LongitudeSample+ix)] length:sizeof(float)*2];
// Upper right triangle
[_vertices appendBytes:&coords[3*(iy*LongitudeSample+ix+1)] length:sizeof(float)*3];
[_vertices appendBytes:&norms[3*(iy*LongitudeSample+ix+1)] length:sizeof(float)*3];
[_vertices appendBytes:&texCoords[2*(iy*LongitudeSample+ix+1)] length:sizeof(float)*2];
[_vertices appendBytes:&coords[3*((iy+1)*LongitudeSample+ix+1)] length:sizeof(float)*3];
[_vertices appendBytes:&norms[3*((iy+1)*LongitudeSample+ix+1)] length:sizeof(float)*3];
[_vertices appendBytes:&texCoords[2*((iy+1)*LongitudeSample+ix+1)] length:sizeof(float)*2];
[_vertices appendBytes:&coords[3*((iy+1)*LongitudeSample+ix)] length:sizeof(float)*3];
[_vertices appendBytes:&norms[3*((iy+1)*LongitudeSample+ix)] length:sizeof(float)*3];
[_vertices appendBytes:&texCoords[2*((iy+1)*LongitudeSample+ix)] length:sizeof(float)*2];
}
// Coordintes + Normals + Texture coordinates
_vertexSize = 3*sizeof(float)+ 3*sizeof(float)+ 2*sizeof(float);
_hasTextureCoords = true;
_numVertices += (LongitudeSample-1)*(LatitudeSample-1)*2*3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment