|
|
今天看到一个例程,是用directx来创建圆柱体.代码如下
YLINDER_CUSTOMVERTEX* pVertex;
WORD wVertexIndex = 0;
int nCurrentSegment;
//Lock the vertex buffer
if(FAILED(m_pVertexBuffer->Lock(0, 0, (void**)&pVertex, 0)))
{
LogError("<li>CCylinder: Unable to lock vertex buffer.");
return false;
}
float rDeltaSegAngle = (2.0f * D3DX_PI / m_nSegments);
float rSegmentLength = 1.0f / (float)m_nSegments;
//Create the sides triangle strip
for(nCurrentSegment = 0; nCurrentSegment <= m_nSegments; nCurrentSegment++)
{
float x0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);//为什么是sin
float z0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);//为什么是cos
pVertex->x = x0;
pVertex->y = 0.0f + (m_rHeight / 2.0f);
pVertex->z = z0;
pVertex->nx = x0;
pVertex->ny = 0.0f;
pVertex->nz = z0;
pVertex->tu = 1.0f - (rSegmentLength * (float)nCurrentSegment);
pVertex->tv = 0.0f;
pVertex++;
pVertex->x = x0;
pVertex->y = 0.0f - (m_rHeight / 2.0f);
pVertex->z = z0;
pVertex->nx = x0;
pVertex->ny = 0.0f;
pVertex->nz = z0;
pVertex->tu = 1.0f - (rSegmentLength * (float)nCurrentSegment);
pVertex->tv = 1.0f;
pVertex++;
}
//Create the top triangle fan: Center
pVertex->x = 0.0f;
pVertex->y = 0.0f + (m_rHeight / 2.0f);
pVertex->z = 0.0f;
pVertex->nx = 0.0f;
pVertex->ny = 1.0f;
pVertex->nz = 0.0f;
pVertex->tu = 0.5f;
pVertex->tv = 0.5f;
pVertex++;
//Create the top triangle fan: Edges
for(nCurrentSegment = 0; nCurrentSegment <= m_nSegments; nCurrentSegment++)
{
float x0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);
float z0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);
pVertex->x = x0;
pVertex->y = 0.0f + (m_rHeight / 2.0f);
pVertex->z = z0;
pVertex->nx = 0.0f;
pVertex->ny = 1.0f;
pVertex->nz = 0.0f;
float tu0 = (0.5f * sinf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;
float tv0 = (0.5f * cosf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;
pVertex->tu = tu0;
pVertex->tv = tv0;
pVertex++;
}
//Create the bottom triangle fan: Center
pVertex->x = 0.0f;
pVertex->y = 0.0f - (m_rHeight / 2.0f);
pVertex->z = 0.0f;
pVertex->nx = 0.0f;
pVertex->ny = -1.0f;
pVertex->nz = 0.0f;
pVertex->tu = 0.5f;
pVertex->tv = 0.5f;
pVertex++;
//Create the bottom triangle fan: Edges
for(nCurrentSegment = m_nSegments; nCurrentSegment >= 0; nCurrentSegment--)
{
float x0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);
float z0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);
pVertex->x = x0;
pVertex->y = 0.0f - (m_rHeight / 2.0f);
pVertex->z = z0;
pVertex->nx = 0.0f;
pVertex->ny = -1.0f;
pVertex->nz = 0.0f;
float tu0 = (0.5f * sinf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;
float tv0 = (0.5f * cosf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;
pVertex->tu = tu0;
pVertex->tv = tv0;
pVertex++;
}
if(FAILED(m_pVertexBuffer->Unlock()))
{
LogError("<li>CCylinder: Unable to unlock vertex buffer.");
return false;
}
return true;
这里觉得这两句
float x0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);
float z0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);
应该改为 float x0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);
float z0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);
但改了之后效果很别扭。但是左思右想不明白 x = r*cos(),z=r*sin();才是正确的,这里为什么反的得到的效果才正确。
望哪位大哥指点迷津
恩。如果是与Z轴的夹角,拿侧面来说,第一个点与Z轴的角度为0,那也就是说是朝里面的侧面的中间的点为第一个点,再依顺时针方向渲染,对吧 |
|