📄 dxutshapes.cpp
字号:
static void
MakeTorus(
VERTEX* pVertices,
WORD* pwIndices,
float fInnerRadius,
float fOuterRadius,
UINT uSides,
UINT uRings)
{
UINT i, j;
//
// Compute the vertices
//
VERTEX *pVertex = pVertices;
for (i = 0; i < uRings; i++)
{
float theta = (float) i * 2.0f * D3DX_PI / (float)uRings;
float st, ct;
sincosf(theta, &st, &ct);
for (j = 0; j < uSides; j++)
{
float phi = (float) j * 2.0f * D3DX_PI / uSides;
float sp, cp;
sincosf(phi, &sp, &cp);
pVertex->pos.x = ct * (fOuterRadius + fInnerRadius * cp);
pVertex->pos.y = -st * (fOuterRadius + fInnerRadius * cp);
pVertex->pos.z = sp * fInnerRadius;
pVertex->norm.x = ct * cp;
pVertex->norm.y = -st * cp;
pVertex->norm.z = sp;
pVertex++;
}
}
//
// Compute the indices:
// There are uRings * uSides faces
// Each face has 2 triangles (6 indices)
//
// Tube i has indices:
// Left Edge: i*(uSides+1) -- i*(uSides+1)+uSides
// Right Edge: (i+1)*(uSides+1) -- (i+1)*(uSides+1)+uSides
//
// Face j on tube i has the 4 indices:
// Left Edge: i*(uSides+1)+j -- i*(uSides+1)+j+1
// Right Edge: (i+1)*(uSides+1)+j -- (i+1)*(uSides+1)+j+1
//
WORD *pwFace = pwIndices;
for (i = 0; i < uRings - 1; i++)
{
for (j = 0; j < uSides - 1; j++)
{
// Tri 1 (Top-Left tri, CCW)
pwFace[0] = (WORD) (i * uSides + j);
pwFace[1] = (WORD) (i * uSides + j + 1);
pwFace[2] = (WORD) ((i + 1) * uSides + j);
pwFace += 3;
// Tri 2 (Bottom-Right tri, CCW)
pwFace[0] = (WORD) ((i + 1) * uSides + j);
pwFace[1] = (WORD) (i * uSides + j + 1);
pwFace[2] = (WORD) ((i + 1) * uSides + j + 1);
pwFace += 3;
}
// Tri 1 (Top-Left tri, CCW)
pwFace[0] = (WORD) (i * uSides + j);
pwFace[1] = (WORD) (i * uSides);
pwFace[2] = (WORD) ((i + 1) * uSides + j);
pwFace += 3;
// Tri 2 (Bottom-Right tri, CCW)
pwFace[0] = (WORD) ((i + 1) * uSides + j);
pwFace[1] = (WORD) (i * uSides + 0);
pwFace[2] = (WORD) ((i + 1) * uSides + 0);
pwFace += 3;
}
// join the two ends of the tube
for (j = 0; j < uSides - 1; j++)
{
// Tri 1 (Top-Left tri, CCW)
pwFace[0] = (WORD) (i * uSides + j);
pwFace[1] = (WORD) (i * uSides + j + 1);
pwFace[2] = (WORD) (j);
pwFace += 3;
// Tri 2 (Bottom-Right tri, CCW)
pwFace[0] = (WORD) (j);
pwFace[1] = (WORD) (i * uSides + j + 1);
pwFace[2] = (WORD) (j + 1);
pwFace += 3;
}
// Tri 1 (Top-Left tri, CCW)
pwFace[0] = (WORD) (i * uSides + j);
pwFace[1] = (WORD) (i * uSides);
pwFace[2] = (WORD) (j);
pwFace += 3;
// Tri 2 (Bottom-Right tri, CCW)
pwFace[0] = (WORD) (j);
pwFace[1] = (WORD) (i * uSides);
pwFace[2] = (WORD) (0);
pwFace += 3;
}
//----------------------------------------------------------------------------
// DXUTCreateTorus - create a torus mesh
//----------------------------------------------------------------------------
HRESULT WINAPI DXUTCreateTorus( ID3D10Device* pDevice, float fInnerRadius, float fOuterRadius, UINT uSides, UINT uRings, ID3DX10Mesh** ppMesh )
{
HRESULT hr = S_OK;
WORD *pwIndices = NULL;
VERTEX *pVertices = NULL;
// Set up the defaults
if(D3DX_DEFAULT_FLOAT == fInnerRadius)
fInnerRadius = 1.0f;
if(D3DX_DEFAULT_FLOAT == fOuterRadius)
fOuterRadius = 2.0f;
if(D3DX_DEFAULT == uSides)
uSides = 8;
if(D3DX_DEFAULT == uRings)
uRings = 15;
// Validate parameters
if(!pDevice)
return D3DERR_INVALIDCALL;
if(!ppMesh)
return D3DERR_INVALIDCALL;
if(fInnerRadius < 0.0f)
return D3DERR_INVALIDCALL;
if(fOuterRadius < 0.0f)
return D3DERR_INVALIDCALL;
if(uSides < 3)
return D3DERR_INVALIDCALL;
if(uRings < 3)
return D3DERR_INVALIDCALL;
// Create the mesh
UINT cFaces = 2 * uSides * uRings;
UINT cVertices = uRings * uSides;
// Create enough memory for the vertices and indices
pVertices = new VERTEX[ cVertices ];
if(!pVertices)
return E_OUTOFMEMORY;
pwIndices = new WORD[ cFaces*3 ];
if(!pwIndices)
return E_OUTOFMEMORY;
// Create a torus
MakeTorus(pVertices, pwIndices, fInnerRadius, fOuterRadius,
uSides, uRings);
// Create a mesh
hr = CreateShapeMesh( pDevice, ppMesh, pVertices, cVertices, pwIndices, cFaces*3 );
// Free up the memory
SAFE_DELETE_ARRAY( pVertices );
SAFE_DELETE_ARRAY( pwIndices );
return hr;
}
//----------------------------------------------------------------------------
// Teapot data
//----------------------------------------------------------------------------
#define NUMTEAPOTVERTICES 1178
#define NUMTEAPOTINDICES 6768
static float teapotPositionsFloats[NUMTEAPOTVERTICES*3] =
{
0.678873f, 0.330678f, 0.000000f,
0.669556f, 0.358022f, 0.000000f,
0.671003f, 0.374428f, 0.000000f,
0.680435f, 0.379897f, 0.000000f,
0.695077f, 0.374428f, 0.000000f,
0.712148f, 0.358022f, 0.000000f,
0.728873f, 0.330678f, 0.000000f,
0.654243f, 0.330678f, 0.187963f,
0.645254f, 0.358022f, 0.185461f,
0.646650f, 0.374428f, 0.185850f,
0.655751f, 0.379897f, 0.188383f,
0.669877f, 0.374428f, 0.192314f,
0.686348f, 0.358022f, 0.196898f,
0.702484f, 0.330678f, 0.201389f,
0.584502f, 0.330678f, 0.355704f,
0.576441f, 0.358022f, 0.350969f,
0.577693f, 0.374428f, 0.351704f,
0.585854f, 0.379897f, 0.356498f,
0.598522f, 0.374428f, 0.363938f,
0.613292f, 0.358022f, 0.372613f,
0.627762f, 0.330678f, 0.381111f,
0.475873f, 0.330678f, 0.497000f,
0.469258f, 0.358022f, 0.490385f,
0.470285f, 0.374428f, 0.491412f,
0.476982f, 0.379897f, 0.498109f,
0.487377f, 0.374428f, 0.508505f,
0.499498f, 0.358022f, 0.520626f,
0.511373f, 0.330678f, 0.532500f,
0.334576f, 0.330678f, 0.605630f,
0.329842f, 0.358022f, 0.597569f,
0.330577f, 0.374428f, 0.598820f,
0.335370f, 0.379897f, 0.606982f,
0.342810f, 0.374428f, 0.619649f,
0.351485f, 0.358022f, 0.634419f,
0.359984f, 0.330678f, 0.648889f,
0.166836f, 0.330678f, 0.675370f,
0.164334f, 0.358022f, 0.666381f,
0.164722f, 0.374428f, 0.667777f,
0.167255f, 0.379897f, 0.676878f,
0.171187f, 0.374428f, 0.691004f,
0.175771f, 0.358022f, 0.707475f,
0.180262f, 0.330678f, 0.723611f,
-0.021127f, 0.330678f, 0.700000f,
-0.021127f, 0.358022f, 0.690683f,
-0.021127f, 0.374428f, 0.692130f,
-0.021127f, 0.379897f, 0.701563f,
-0.021127f, 0.374428f, 0.716204f,
-0.021127f, 0.358022f, 0.733276f,
-0.021127f, 0.330678f, 0.750000f,
-0.224715f, 0.330678f, 0.675370f,
-0.215631f, 0.358022f, 0.666381f,
-0.211606f, 0.374428f, 0.667777f,
-0.211463f, 0.379897f, 0.676878f,
-0.214020f, 0.374428f, 0.691004f,
-0.218098f, 0.358022f, 0.707475f,
-0.222516f, 0.330678f, 0.723611f,
-0.396831f, 0.330678f, 0.605630f,
-0.383671f, 0.358022f, 0.597569f,
-0.378758f, 0.374428f, 0.598820f,
-0.380125f, 0.379897f, 0.606982f,
-0.385806f, 0.374428f, 0.619649f,
-0.393832f, 0.358022f, 0.634419f,
-0.402238f, 0.330678f, 0.648889f,
-0.535002f, 0.330678f, 0.497000f,
-0.521278f, 0.358022f, 0.490385f,
-0.517539f, 0.374428f, 0.491412f,
-0.521346f, 0.379897f, 0.498109f,
-0.530257f, 0.374428f, 0.508505f,
-0.541831f, 0.358022f, 0.520626f,
-0.553627f, 0.330678f, 0.532500f,
-0.636757f, 0.330678f, 0.355704f,
-0.624483f, 0.358022f, 0.350969f,
-0.622910f, 0.374428f, 0.351704f,
-0.629359f, 0.379897f, 0.356498f,
-0.641146f, 0.374428f, 0.363938f,
-0.655593f, 0.358022f, 0.372613f,
-0.670016f, 0.330678f, 0.381111f,
-0.699623f, 0.330678f, 0.187963f,
-0.689317f, 0.358022f, 0.185461f,
-0.689830f, 0.374428f, 0.185850f,
-0.698396f, 0.379897f, 0.188382f,
-0.712247f, 0.374428f, 0.192314f,
-0.728617f, 0.358022f, 0.196898f,
-0.744738f, 0.330678f, 0.201389f,
-0.721127f, 0.330678f, 0.000000f,
-0.711810f, 0.358022f, 0.000000f,
-0.713257f, 0.374428f, 0.000000f,
-0.722690f, 0.379897f, 0.000000f,
-0.737331f, 0.374428f, 0.000000f,
-0.754403f, 0.358022f, 0.000000f,
-0.771127f, 0.330678f, 0.000000f,
-0.696498f, 0.330678f, -0.187963f,
-0.687508f, 0.358022f, -0.185461f,
-0.688904f, 0.374428f, -0.185850f,
-0.698005f, 0.379897f, -0.188383f,
-0.712131f, 0.374428f, -0.192314f,
-0.728602f, 0.358022f, -0.196898f,
-0.744738f, 0.330678f, -0.201389f,
-0.626757f, 0.330678f, -0.355704f,
-0.618696f, 0.358022f, -0.350969f,
-0.619948f, 0.374428f, -0.351704f,
-0.628109f, 0.379897f, -0.356498f,
-0.640776f, 0.374428f, -0.363938f,
-0.655546f, 0.358022f, -0.372613f,
-0.670016f, 0.330678f, -0.381111f,
-0.518127f, 0.330678f, -0.497000f,
-0.511512f, 0.358022f, -0.490385f,
-0.512539f, 0.374428f, -0.491412f,
-0.519237f, 0.379897f, -0.498109f,
-0.529632f, 0.374428f, -0.508505f,
-0.541753f, 0.358022f, -0.520626f,
-0.553627f, 0.330678f, -0.532500f,
-0.376831f, 0.330678f, -0.605630f,
-0.372096f, 0.358022f, -0.597569f,
-0.372832f, 0.374428f, -0.598820f,
-0.377625f, 0.379897f, -0.606982f,
-0.385065f, 0.374428f, -0.619649f,
-0.393740f, 0.358022f, -0.634419f,
-0.402238f, 0.330678f, -0.648889f,
-0.209090f, 0.330678f, -0.675370f,
-0.206588f, 0.358022f, -0.666381f,
-0.206977f, 0.374428f, -0.667777f,
-0.209510f, 0.379897f, -0.676878f,
-0.213441f, 0.374428f, -0.691004f,
-0.218025f, 0.358022f, -0.707475f,
-0.222516f, 0.330678f, -0.723611f,
-0.021127f, 0.330678f, -0.700000f,
-0.021127f, 0.358022f, -0.690683f,
-0.021127f, 0.374428f, -0.692130f,
-0.021127f, 0.379897f, -0.701563f,
-0.021127f, 0.374428f, -0.716204f,
-0.021127f, 0.358022f, -0.733276f,
-0.021127f, 0.330678f, -0.750000f,
0.166836f, 0.330678f, -0.675370f,
0.164334f, 0.358022f, -0.666381f,
0.164722f, 0.374428f, -0.667777f,
0.167255f, 0.379897f, -0.676878f,
0.171187f, 0.374428f, -0.691004f,
0.175771f, 0.358022f, -0.707475f,
0.180262f, 0.330678f, -0.723611f,
0.334576f, 0.330678f, -0.605630f,
0.329842f, 0.358022f, -0.597569f,
0.330577f, 0.374428f, -0.598820f,
0.335370f, 0.379897f, -0.606982f,
0.342810f, 0.374428f, -0.619649f,
0.351485f, 0.358022f, -0.634419f,
0.359984f, 0.330678f, -0.648889f,
0.475873f, 0.330678f, -0.497000f,
0.469258f, 0.358022f, -0.490385f,
0.470285f, 0.374428f, -0.491412f,
0.476982f, 0.379897f, -0.498109f,
0.487377f, 0.374428f, -0.508505f,
0.499498f, 0.358022f, -0.520626f,
0.511373f, 0.330678f, -0.532500f,
0.584502f, 0.330678f, -0.355704f,
0.576441f, 0.358022f, -0.350969f,
0.577693f, 0.374428f, -0.351704f,
0.585854f, 0.379897f, -0.356498f,
0.598522f, 0.374428f, -0.363938f,
0.613292f, 0.358022f, -0.372613f,
0.627762f, 0.330678f, -0.381111f,
0.654243f, 0.330678f, -0.187963f,
0.645254f, 0.358022f, -0.185461f,
0.646650f, 0.374428f, -0.185850f,
0.655751f, 0.379897f, -0.188382f,
0.669877f, 0.374428f, -0.192314f,
0.686348f, 0.358022f, -0.196898f,
0.702484f, 0.330678f, -0.201389f,
0.790794f, 0.199602f, 0.000000f,
0.849243f, 0.069567f, 0.000000f,
0.900748f, -0.058384f, 0.000000f,
0.941836f, -0.183211f, 0.000000f,
0.969035f, -0.303870f, 0.000000f,
0.978873f, -0.419322f, 0.000000f,
0.762227f, 0.199602f, 0.218016f,
0.818619f, 0.069567f, 0.233711f,
0.868312f, -0.058384f, 0.247541f,
0.907954f, -0.183211f, 0.258573f,
0.934196f, -0.303870f, 0.265877f,
0.943688f, -0.419322f, 0.268519f,
0.681335f, 0.199602f, 0.412576f,
0.731904f, 0.069567f, 0.442277f,
0.776465f, -0.058384f, 0.468449f,
0.812014f, -0.183211f, 0.489328f,
0.835546f, -0.303870f, 0.503149f,
0.844058f, -0.419322f, 0.508148f,
0.555337f, 0.199602f, 0.576464f,
0.596836f, 0.069567f, 0.617963f,
0.633404f, -0.058384f, 0.654531f,
0.662577f, -0.183211f, 0.683704f,
0.681888f, -0.303870f, 0.703015f,
0.688873f, -0.419322f, 0.710000f,
0.391449f, 0.199602f, 0.702462f,
0.421150f, 0.069567f, 0.753032f,
0.447322f, -0.058384f, 0.797593f,
0.468201f, -0.183211f, 0.833141f,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -