Skip to content

Instantly share code, notes, and snippets.

@dpasca
Created April 8, 2014 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpasca/10142521 to your computer and use it in GitHub Desktop.
Save dpasca/10142521 to your computer and use it in GitHub Desktop.
indicized sphere from cylinder
//--------------------------
// setup geometry
const static u_int NDIV_PHI = 20;
const static u_int NDIV_THE = 20;
mPosBase.reserve( NDIV_PHI * NDIV_THE );
auto &geom = *moBaseMesh->mpGeom;
geom.mNor.reserve( mPosBase.size() );
for (u_int i=0; i != NDIV_PHI; ++i)
{
float v = (float)i / (NDIV_PHI-1);
float phi = DLerp( 0.f, FM_PI, v );
for (u_int j=0; j != NDIV_THE; ++j)
{
float u = (float)j / NDIV_THE;
float the = DLerp( 0.f, 2*FM_PI, u );
Float3 v1( sinf( phi ) * cosf( the ),
cosf( phi ),
sinf( phi ) * sinf( the ) );
mPosBase.push_back( v1 );
geom.mNor.push_back( DE3::MakeNormal(v1) );
}
}
mPosTemp.resize( mPosBase.size() );
auto &idxs = geom.mPrims.mIndices;
idxs.reserve( NDIV_PHI * NDIV_THE * 2*3 );
for (u_int i=0; i != (NDIV_PHI-1); ++i)
{
u_int iBase = i*NDIV_THE;
for (u_int j=0; j != NDIV_THE; ++j)
{
u_int i00 = iBase + j;
u_int i01 = iBase + (j == NDIV_THE-1 ? 0 : j+1);
idxs.push_back( i00 + 0*NDIV_THE );
idxs.push_back( i01 + 1*NDIV_THE );
idxs.push_back( i00 + 1*NDIV_THE );
idxs.push_back( i00 + 0*NDIV_THE );
idxs.push_back( i01 + 0*NDIV_THE );
idxs.push_back( i01 + 1*NDIV_THE );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment