const int circlePoints = 4;//锥低边的定点数
const int radiu = 1;//锥底半径
VB->Lock(0, 0, (void**)&vertices, 0);
// vertices of a unit centrum
for (int i = 0;i<=circlePoints+1;i++)
{
if (i==0)
{
vertices[i] = ColorVertex(0.0f, 1.0f, 0.0f,D3DCOLOR_XRGB(0,255,0));
}else if (i==1)
{
vertices[i] = ColorVertex(0.0f, -1.0f, 0.0f,D3DCOLOR_XRGB(0,0,255));
}
else{
vertices[i] = ColorVertex(radiu*cos((i - 2)*2*D3DX_PI/circlePoints), -1.0f, radiu*sin((i - 2)*2*D3DX_PI/circlePoints),D3DCOLOR_XRGB(255,0,0));
}
}
VB->Unlock();
// define the triangles of the cube:
WORD* indices = 0;
IB->Lock(0, 0, (void**)&indices, 0);
for (int j=0;j < circlePoints;j++)
{
if(j==circlePoints - 1)
{
indices[j*6] = j%2 == 0?0:j+2; indices[j*6+1] = j%2 == 0?j+2:0; indices[j*6+2] = j%2 == 0?2:2;
indices[j*6+3] = j%2 == 0?1:j+2; indices[j*6+4] = j%2 == 0?j+2:1; indices[j*6+5] = j%2 == 0?2:2;
}
else
{
indices[j*6] = j%2 == 0?0:j+2; indices[j*6+1] = j%2 == 0?j+2:0; indices[j*6+2] = j%2 == 0?j+3:j+3;
indices[j*6+3] = j%2 == 0?1:j+2; indices[j*6+4] = j%2 == 0?j+2:1; indices[j*6+5] = j%2 == 0?j+3:j+3;
}
}
可是就是画不出来,当circlePoints是3的时候能画出来一个面,其它值什么都没有
该回答引用ChatGPT-3.5,仅供参考,不保证完全正确
根据您提供的代码,存在一些问题导致椎体无法正确绘制。以下是我发现的问题和建议的修正方法:
在定义顶点的循环中,您只为顶点数组中的前circlePoints+2个元素分配了空间,但在计算顶点坐标时,循环的结束条件是i <= circlePoints+1,这会导致访问超出数组范围。建议将循环条件更改为i < circlePoints+2。
在计算顶点坐标时,您使用了弧度制而不是角度制。cos和sin函数期望弧度作为输入。如果想使用角度制,需要将角度转换为弧度。建议使用D3DXToRadian函数将角度转换为弧度。
在索引序列的计算中,您有一个错误的逻辑。在最后一个循环迭代中,当j等于circlePoints-1时,应该使用特殊的索引计算方式,因为需要连接最后一个顶点和起始顶点。目前的逻辑会导致使用错误的索引值。
下面是修正后的代码:
const int circlePoints = 4; // 锥低边的顶点数
const float radius = 1.0f; // 锥底半径
VB->Lock(0, 0, (void**)&vertices, 0);
// 锥顶点和底面顶点
vertices[0] = ColorVertex(0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0));
vertices[1] = ColorVertex(0.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255));
// 底面顶点
for (int i = 2; i < circlePoints + 2; i++)
{
float angle = (i - 2) * 2 * D3DX_PI / circlePoints;
vertices[i] = ColorVertex(radius * cos(angle), -1.0f, radius * sin(angle), D3DCOLOR_XRGB(255, 0, 0));
}
VB->Unlock();
// 定义椎体的三角形面片
WORD* indices = nullptr;
IB->Lock(0, 0, (void**)&indices, 0);
for (int j = 0; j < circlePoints; j++)
{
indices[j * 6] = 0;
indices[j * 6 + 1] = j + 2;
indices[j * 6 + 2] = (j + 1) % circlePoints + 2;
indices[j * 6 + 3] = 1;
indices[j * 6 + 4] = (j + 1) % circlePoints + 2;
indices[j * 6 + 5] = j + 2;
}
IB->Unlock();
请注意,以上修正是基于您的代码进行的,但我无法验证它们是否与您的整体实现一致。建议在尝试修复问题之前备份原始代码,以免丢失任何重要数据。