为什么在这个网格中没有正确设置边界框?

时间:2010-11-24 01:09:24

标签: c++ bounding-box irrlicht

我有一些Irrlicht代码生成给定宽度和高度的矩形网格。以下是生成顶点和索引的代码:

int iNumVertices = (width + 1) * (height + 1);
S3DVertex * vertices = new S3DVertex[iNumVertices];
memset(vertices,0,sizeof(S3DVertex) * iNumVertices);

for(int i=0;i<=height;++i)
{
    for(int j=0;j<=width;++j)
    {
        int iIndex = (i*(width + 1)) + j;

        vertices[iIndex].Pos.X = i * 2.0f;
        vertices[iIndex].Pos.Y = 0.0f;
        vertices[iIndex].Pos.Z = j * 2.0f;
        vertices[iIndex].Color.color = 0xFFFFFFFF;

        vertices[iIndex].TCoords.X = i;
        vertices[iIndex].TCoords.Y = j;
    }
}

int iNumIndices = 6 * width * height;
u16 * indices = new u16[iNumIndices];

for(int i=0;i<height;++i)
{
    for(int j=0;j<width;++j)
    {
        int iIndex = ((i*width) + j) * 6;

        int tmp_offset = j + (i * (width + 1));

        indices[iIndex + 0] = tmp_offset + 1;
        indices[iIndex + 1] = tmp_offset + width + 1;
        indices[iIndex + 2] = tmp_offset;
        indices[iIndex + 3] = tmp_offset + 1;
        indices[iIndex + 4] = tmp_offset + width + 2;
        indices[iIndex + 5] = tmp_offset + width + 1;
    }
}

然后将顶点和索引添加到网格中并重新计算边界框:

SMeshBuffer * buffer = new SMeshBuffer();
buffer->append(vertices,iNumVertices,indices,iNumIndices);

buffer->recalculateBoundingBox();

但是,渲染时,边界框无法接近正确的大小:

alt text

这样做的最终结果是当小边界框落在相机后面时,网格不会被渲染。

1 个答案:

答案 0 :(得分:1)

原来问题是我在缓冲区而不是网格上调用recalculateBoundingBox()