📄 dxutshapes.cpp
字号:
pwFace[2] = (WORD) (uRowA);
pwFace += 3;
pwFace[0] = (WORD) (uRowA);
pwFace[1] = (WORD) (uRowB + i);
pwFace[2] = (WORD) (uRowB);
pwFace += 3;
}
// Z- pole (uSlices)
uRowA = 1 + (uStacks + 2) * uSlices;
uRowB = uRowA + uSlices;
for(i = 0; i < uSlices - 1; i++)
{
pwFace[0] = (WORD) (uRowA + i);
pwFace[1] = (WORD) (uRowB);
pwFace[2] = (WORD) (uRowA + i + 1);
pwFace += 3;
}
pwFace[0] = (WORD) (uRowA + i);
pwFace[1] = (WORD) (uRowB);
pwFace[2] = (WORD) (uRowA);
pwFace += 3;
}
//----------------------------------------------------------------------------
// DXUTCreateCylinder - create a cylinder mesh
//----------------------------------------------------------------------------
HRESULT WINAPI DXUTCreateCylinder( ID3D10Device* pDevice, float fRadius1, float fRadius2, float fLength, UINT uSlices, UINT uStacks, ID3DX10Mesh** ppMesh )
{
HRESULT hr = S_OK;
WORD *pwIndices = NULL;
VERTEX *pVertices = NULL;
// Set up the defaults
if(D3DX_DEFAULT_FLOAT == fRadius1)
fRadius1 = 1.0f;
if(D3DX_DEFAULT_FLOAT == fRadius2)
fRadius2 = 1.0f;
if(D3DX_DEFAULT_FLOAT == fLength)
fLength = 1.0f;
if(D3DX_DEFAULT == uSlices)
uSlices = 8;
if(D3DX_DEFAULT == uStacks)
uStacks = 8;
// Validate parameters
if(!pDevice)
return D3DERR_INVALIDCALL;
if(!ppMesh)
return D3DERR_INVALIDCALL;
if(fRadius1 < 0.0f)
return D3DERR_INVALIDCALL;
if(fRadius2 < 0.0f)
return D3DERR_INVALIDCALL;
if(fLength < 0.0f)
return D3DERR_INVALIDCALL;
if(uSlices < 2)
return D3DERR_INVALIDCALL;
if(uStacks < 1)
return D3DERR_INVALIDCALL;
if(uSlices >= CACHE_SIZE)
uSlices = CACHE_SIZE-1;
// Create the mesh
UINT cFaces = (uStacks + 1) * uSlices * 2;
UINT cVertices = 2 + (uStacks + 3) * uSlices;
// 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 cylinder
MakeCylinder(pVertices, NULL, pwIndices, fRadius1, fRadius2,
fLength, uSlices, uStacks);
// 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;
}
//--------------------------------------------------------------------------------------
// MakePolygon helper
//--------------------------------------------------------------------------------------
static void
MakePolygon( VERTEX* pVertices,
WORD* pwIndices,
float fLength,
UINT uSides)
{
// Calculate the radius
float radius = fLength * 0.5f / sinf( D3DX_PI / (float) uSides );
float angle = (float) (2.0f * D3DX_PI / (float) uSides);
// Fill in vertices
VERTEX *pVertex = pVertices;
pVertex->pos = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
pVertex->norm = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
pVertex++;
for(UINT j=0; j < uSides; j++)
{
float s, c;
sincosf(angle * j, &s, &c);
pVertex->pos = D3DXVECTOR3( c * radius, s * radius, 0.0f );
pVertex->norm = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
pVertex++;
}
// Fill in indices
WORD *pwFace = pwIndices;
UINT iFace;
for(iFace = 0; iFace < uSides - 1; iFace++)
{
pwFace[0] = 0;
pwFace[1] = (WORD)iFace + 1;
pwFace[2] = (WORD)iFace + 2;
pwFace += 3;
}
// handle the wrapping of the last case
pwFace[0] = 0;
pwFace[1] = (WORD)iFace + 1;
pwFace[2] = 1;
}
//----------------------------------------------------------------------------
// DXUTCreatePolygon - create a polygon mesh
//----------------------------------------------------------------------------
HRESULT WINAPI DXUTCreatePolygon( ID3D10Device* pDevice, float fLength, UINT uSides, ID3DX10Mesh** ppMesh )
{
HRESULT hr = S_OK;
WORD *pwIndices = NULL;
VERTEX *pVertices = NULL;
// Set up the defaults
if(D3DX_DEFAULT == uSides)
uSides = 3;
if(D3DX_DEFAULT_FLOAT == fLength)
fLength = 1.0f;
// Validate parameters
if(!pDevice)
return D3DERR_INVALIDCALL;
if(!ppMesh)
return D3DERR_INVALIDCALL;
if(fLength < 0.0f)
return D3DERR_INVALIDCALL;
if(uSides < 3)
return D3DERR_INVALIDCALL;
// Create the mesh
UINT cFaces = uSides;
UINT cVertices = uSides + 1;
// 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 polygon
MakePolygon(pVertices, pwIndices, fLength, uSides);
// 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;
}
//---------------------------------------------------------------------
// MakeSphere helper
//---------------------------------------------------------------------
static void
MakeSphere(
VERTEX* pVertices,
WORD* pwIndices,
float fRadius,
UINT uSlices,
UINT uStacks)
{
UINT i,j;
// Sin/Cos caches
float sinI[CACHE_SIZE], cosI[CACHE_SIZE];
float sinJ[CACHE_SIZE], cosJ[CACHE_SIZE];
for(i = 0; i < uSlices; i++)
sincosf(2.0f * D3DX_PI * i / uSlices, sinI + i, cosI + i);
for(j = 0; j < uStacks; j++)
sincosf(D3DX_PI * j / uStacks, sinJ + j, cosJ + j);
// Generate vertices
VERTEX *pVertex = pVertices;
// +Z pole
pVertex->pos = D3DXVECTOR3(0.0f, 0.0f, fRadius);
pVertex->norm = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
pVertex++;
// Stacks
for(j = 1; j < uStacks; j++)
{
for(i = 0; i < uSlices; i++)
{
D3DXVECTOR3 norm(sinI[i] * sinJ[j], cosI[i] * sinJ[j], cosJ[j]);
pVertex->pos = norm * fRadius;
pVertex->norm = norm;
pVertex++;
}
}
// Z- pole
pVertex->pos = D3DXVECTOR3(0.0f, 0.0f, -fRadius);
pVertex->norm = D3DXVECTOR3(0.0f, 0.0f, -1.0f);
pVertex++;
// Generate indices
WORD *pwFace = pwIndices;
UINT uRowA, uRowB;
// Z+ pole
uRowA = 0;
uRowB = 1;
for(i = 0; i < uSlices - 1; i++)
{
pwFace[0] = (WORD) (uRowA);
pwFace[1] = (WORD) (uRowB + i + 1);
pwFace[2] = (WORD) (uRowB + i);
pwFace += 3;
}
pwFace[0] = (WORD) (uRowA);
pwFace[1] = (WORD) (uRowB);
pwFace[2] = (WORD) (uRowB + i);
pwFace += 3;
// Interior stacks
for(j = 1; j < uStacks - 1; j++)
{
uRowA = 1 + (j - 1) * uSlices;
uRowB = uRowA + uSlices;
for(i = 0; i < uSlices - 1; i++)
{
pwFace[0] = (WORD) (uRowA + i);
pwFace[1] = (WORD) (uRowA + i + 1);
pwFace[2] = (WORD) (uRowB + i);
pwFace += 3;
pwFace[0] = (WORD) (uRowA + i + 1);
pwFace[1] = (WORD) (uRowB + i + 1);
pwFace[2] = (WORD) (uRowB + i);
pwFace += 3;
}
pwFace[0] = (WORD) (uRowA + i);
pwFace[1] = (WORD) (uRowA);
pwFace[2] = (WORD) (uRowB + i);
pwFace += 3;
pwFace[0] = (WORD) (uRowA);
pwFace[1] = (WORD) (uRowB);
pwFace[2] = (WORD) (uRowB + i);
pwFace += 3;
}
// Z- pole
uRowA = 1 + (uStacks - 2) * uSlices;
uRowB = uRowA + uSlices;
for(i = 0; i < uSlices - 1; i++)
{
pwFace[0] = (WORD) (uRowA + i);
pwFace[1] = (WORD) (uRowA + i + 1);
pwFace[2] = (WORD) (uRowB);
pwFace += 3;
}
pwFace[0] = (WORD) (uRowA + i);
pwFace[1] = (WORD) (uRowA);
pwFace[2] = (WORD) (uRowB);
pwFace += 3;
}
//----------------------------------------------------------------------------
// DXUTCreateSphere - create a sphere mesh
//----------------------------------------------------------------------------
HRESULT WINAPI DXUTCreateSphere( ID3D10Device* pDevice, float fRadius, UINT uSlices, UINT uStacks, ID3DX10Mesh** ppMesh )
{
HRESULT hr = S_OK;
WORD *pwIndices = NULL;
VERTEX *pVertices = NULL;
// Set up the defaults
if(D3DX_DEFAULT_FLOAT == fRadius)
fRadius = 1.0f;
if(D3DX_DEFAULT == uSlices)
uSlices = 8;
if(D3DX_DEFAULT == uStacks)
uStacks = 8;
// Validate parameters
if(!pDevice)
return D3DERR_INVALIDCALL;
if(!ppMesh)
return D3DERR_INVALIDCALL;
if(fRadius < 0.0f)
return D3DERR_INVALIDCALL;
if(uSlices < 2)
return D3DERR_INVALIDCALL;
if(uStacks < 2)
return D3DERR_INVALIDCALL;
if(uSlices > CACHE_SIZE)
uSlices = CACHE_SIZE;
if(uStacks > CACHE_SIZE)
uStacks = CACHE_SIZE;
// Create the mesh
UINT cFaces = 2 * (uStacks - 1) * uSlices;
UINT cVertices = (uStacks - 1) * uSlices + 2;
// 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 sphere
MakeSphere(pVertices, pwIndices, fRadius, uSlices, uStacks);
// 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;
}
//---------------------------------------------------------------------
// MakeTorus helper
//---------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -