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

发布于 2024-10-04 02:05:30 字数 1494 浏览 0 评论 0原文

我有一些 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

最终结果是,当小边界框位于相机后面时,网格不会被渲染。

I have some Irrlicht code that generates a rectangular mesh given a width and height. Here is the code that generates the vertices and indices:

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;
    }
}

Then the vertices and indices are added to the mesh and the bounding box is recalculated:

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

buffer->recalculateBoundingBox();

However, when rendered, the bounding box is nowhere close to the right size:

alt text

The end result of this is that the mesh doesn't get rendered when the small bounding box goes behind the camera.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老子叫无熙 2024-10-11 02:05:30

事实证明,问题是我在缓冲区而不是网格上调用 recalculateBoundingBox()

Turns out that the problem was that I was calling recalculateBoundingBox() on the buffer instead of the mesh.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文