|
|

楼主 |
发表于 2006-4-21 17:24:00
|
显示全部楼层
Re:初学者求救:生成球体的这段代码我不太清楚?
bool CSphere::UpdateVertices()
{
//Code adapted from a sample by "Laurent" posted on the GameDev.net DirectX forum
//http://www.gamedev.net/community/forums/topic.asp?topic_id=85779
WORD* pIndices;
SPHERE_CUSTOMVERTEX* pVertex;
WORD wVertexIndex = 0;
int nCurrentRing;
int nCurrentSegment;
D3DXVECTOR3 vNormal;
//Lock the vertex buffer
if(FAILED(m_pVertexBuffer->Lock(0, 0, (BYTE**)&pVertex, 0)))
{
LogError("<li>CSphere: Unable to lock vertex buffer.");
return false;
}
//Lock the index buffer
if(FAILED(m_pIndexBuffer->Lock(0, m_dwNumOfIndices, (BYTE**)&pIndices, 0)))
{
LogError("<li>CSphere: Unable to lock index buffer.");
return false;
}
//Establish constants used in sphere generation
FLOAT rDeltaRingAngle = (D3DX_PI / m_nRings);
FLOAT rDeltaSegAngle = (2.0f * D3DX_PI / m_nSegments);
//Generate the group of rings for the sphere
for(nCurrentRing = 0; nCurrentRing < m_nRings + 1; nCurrentRing++)
{
FLOAT r0 = sinf(nCurrentRing * rDeltaRingAngle);
FLOAT y0 = cosf(nCurrentRing * rDeltaRingAngle);
//Generate the group of segments for the current ring
for(nCurrentSegment = 0; nCurrentSegment < m_nSegments + 1;
nCurrentSegment++)
{
FLOAT x0 = r0 * sinf(nCurrentSegment * rDeltaSegAngle);
FLOAT z0 = r0 * cosf(nCurrentSegment * rDeltaSegAngle);
vNormal.x = x0;
vNormal.y = y0;
vNormal.z = z0;
D3DXVec3Normalize(&vNormal, &vNormal);
//Add one vertex to the strip which makes up the sphere
pVertex->x = x0;
pVertex->y = y0;
pVertex->z = z0;
pVertex->nx = vNormal.x;
pVertex->ny = vNormal.y;
pVertex->nz = vNormal.z;
pVertex->tu = 1.0f - ((FLOAT)nCurrentSegment / (FLOAT)m_nSegments);
pVertex->tv = (FLOAT)nCurrentRing / (FLOAT)m_nRings;
pVertex++;
//Add two indices except for the last ring
if(nCurrentRing != m_nRings)
{
*pIndices = wVertexIndex;
pIndices++;
*pIndices = wVertexIndex + (WORD)(m_nSegments + 1);
pIndices++;
wVertexIndex++;
}
}
}
if(FAILED(m_pIndexBuffer->Unlock()))
{
LogError("<li>CSphere: Unable to unlock index buffer.");
return false;
}
if(FAILED(m_pVertexBuffer->Unlock()))
{
LogError("<li>CSphere: Unable to unlock vertex buffer.");
return false;
}
return true;
}
|
|