|
|
我用如下代码创建定点坐标并进行绘制:
/****************************************************************************
函数名:MakeSphere
功 能:通过数学计算建立一个半径为一单位的球体
*****************************************************************************/
BOOL CEarthModel::MakeSphere(int nRings , int nSegments)
{
//设置顶点数目和索引数目
DWORD dwVertices = ( nRings + 1 ) * ( nSegments + 1 ) ;
DWORD dwIndices = 2 * nRings * (nSegments + 1) ;
//申请顶点缓存
if(FAILED(m_pD3DDevice->CreateVertexBuffer(
dwVertices * sizeof(SPHEREVERTEX),
D3DUSAGE_WRITEONLY,
SPHEREVERTEX_FVF,
D3DPOOL_MANAGED,
&SphereVertexsBuffer,
0)))
return E_FAIL;
//申请索引缓存
if(FAILED(m_pD3DDevice->CreateIndexBuffer(
//dwIndices * 2 ,
dwIndices * sizeof(DWORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&SphereIndexBuffer,
0)))
return E_FAIL;
//锁定顶点缓存
SPHEREVERTEX * pVertex ;
SphereVertexsBuffer->Lock(0, 0, (VOID**)&pVertex, 0);
//锁定索引缓存
DWORD* pIndices;
if(FAILED(SphereIndexBuffer->Lock(0, 0, (VOID**)&pIndices, 0)))
return E_FAIL;
//设定两个方向的角度增量
FLOAT fDeltaRingAngle = (D3DX_PI / nRings);
FLOAT fDeltaSegAngle = (2.0f * D3DX_PI / nSegments);
DWORD wVerticeIndex = 0 ;
//循环产生球体各环的值
for(int ring = 0; ring < nRings + 1 ; ring++)
{
FLOAT r0 = sinf(ring * fDeltaRingAngle);
FLOAT y0 = cosf(ring * fDeltaRingAngle);
//生成当前纬线圈的各点
for(int seg = 0; seg < nSegments + 1 ; seg++)
{
FLOAT x0 = r0 * sinf(seg * fDeltaSegAngle);
FLOAT z0 = r0 * cosf(seg * fDeltaSegAngle);
//记录此次产生的点的信息
pVertex->p = D3DXVECTOR3(x0, y0, z0); // normalized
pVertex->n = pVertex->p;
pVertex->color = 0xffffffff;
// pVertex->tu = 1.0f - ((FLOAT) seg / (FLOAT) nSegments);
pVertex->tu = (FLOAT)seg / (FLOAT)nSegments;
pVertex->tv = (FLOAT) ring / (FLOAT) nRings;
pVertex++;
//除最后的环外,产生其他环的索引信息
if ( ring != nRings )
{
*pIndices = wVerticeIndex;
pIndices++;
*pIndices = wVerticeIndex + (DWORD)(nSegments + 1);
pIndices++;
wVerticeIndex++;
}
}// end for seg
} // end for ring
SphereVertexsBuffer->Unlock();
SphereIndexBuffer->Unlock();
return S_OK ;
}
并使用如下代码进行绘制:
DWORD iIndexStart = 0;
DWORD iVertexStart = 0;
for (i=0; i<iBallRing; i++)
{
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP,
iVertexStart,
0,
//(iBallRing+1)*(iBallSegment+1),
2*(iBallSegment+1),
iIndexStart,
2*(iBallSegment+1));
iIndexStart += (2 * (iBallSegment+1));
iVertexStart += (iBallSegment+1);
}
这样得到的是很乱的图形,而不是我想要的球体网格。
反复试了很多绘制参数都不对
请的大家指教,谢谢! |
|