【问题标题】:Mesh generation algorithm gives visual artifacts网格生成算法给出了视觉伪影
【发布时间】:2021-11-14 04:43:40
【问题描述】:

现在我正在尝试实现地形生成。我正在使用 msvc Visual Studio 2019。当我降低函数中的细节级别时,生成开始变得非常有问题。当我不使用任何级别的细节降低值时,一切都很好。但是在 1 次或更多次之后,一切都发生了变化 - 生成开始出现故障并产生 atifacts。下面是一些例子。

这是正确的。在我的函数中,这代表 LevelOfDetails 中的参数 0。

还有不正确的例子。 LevelOfDetails=1:

LevelOfDetails=3-6:

这是一个生成网格的函数:

static void
GenerateTerrainMesh(world* World, noise_map* NoiseMap, float HeightMultiplier, int LevelOfDetail)
{
    int Width  = NoiseMap->Width;
    int Height = NoiseMap->Height;
    //float TopLeftX = (Width - 1)  / (-2.0f);
    //float TopLeftZ = (Height - 1) / ( 2.0f);

    if(LevelOfDetail < 0) LevelOfDetail = 0;
    if(LevelOfDetail > 6) LevelOfDetail = 6;

    int SimplificationInc = (LevelOfDetail == 0) ? 1 : (LevelOfDetail * 2);
    int VerticesPerLineX  = (Width  - 1) / SimplificationInc + 1;
    int VerticesPerLineY  = (Height - 1) / SimplificationInc + 1;

    InitializeMeshTerrain(World, VerticesPerLineX, VerticesPerLineX);

    int VertIndex = 0;
    int TrigIndex = 0;
    for(int Y = 0; Y < Height; Y += SimplificationInc)
    {
        for(int X = 0; X < Width; X += SimplificationInc)
        {
            World->Terrain.Vertices[VertIndex] = V3((float)X, HeightMultiplier * HeightFlatten(World->NoiseMap.Values[Y*World->NoiseMap.Width + X]).y * HeightMultiplier, (float)Y);
            World->Terrain.UVs[VertIndex]      = V2(X/(float)Width, Y/(float)Height);

            if((X < (Width - 1)) && (Y < (Height - 1)))
            {
                AddTriangle(World, &TrigIndex, VertIndex, VertIndex + VerticesPerLineX + 1, VertIndex + VerticesPerLineX);

                AddTriangle(World, &TrigIndex, VertIndex + VerticesPerLineX + 1, VertIndex, VertIndex + 1);
            }

            VertIndex += 1;
        }
    }
}

还有辅助函数,但应该不会有任何错误:

static void
InitializeMeshTerrain(world* World, int Width, int Height)
{
    World->Terrain.VerticesCount = Width * Height;
    World->Terrain.FacesCount    = (Width - 1)*(Height - 1)*2;
    World->Terrain.UVCount       = Width * Height;

    World->Terrain.Faces    = (face*)malloc(sizeof(face)*World->Terrain.FacesCount);
    World->Terrain.Vertices = (v3*)malloc(sizeof(v3)*World->Terrain.VerticesCount);
    World->Terrain.UVs      = (v2*)malloc(sizeof(v2)*World->Terrain.UVCount);
}

static void
AddTriangle(world* World, int* Index, int a, int b, int c)
{
    World->Terrain.Faces[*Index].a = a;
    World->Terrain.Faces[*Index].b = b;
    World->Terrain.Faces[*Index].c = c;

    *Index += 1;
}

可能有什么问题?谢谢你。 UPD:我为每个 LevelOfDetail 添加了一些额外的数据,例如网格数据和顶点数据:
正确:
LevelOfDetail=0
错误 1:
Incorect LevelOfDetail=1
错误 4:
Incorect LevelOfDetail=4
错误 6:
Incorect LevelOfDetail=6

【问题讨论】:

  • 您应该转储正在使用的数据以及为好坏图像生成的数字,而不是查看图像,并检查差异。
  • 猜测一下,您在迭代网格的代码中有一些非一的逻辑。例如,一个 5x5 的点网格足以定义一个 4x4 的正方形网格。将细节减少 2 倍会得到一个 3x3 的点网格,定义一个 2x2 的正方形网格。编写代码来遍历所有这些可能非常容易出错,因为顶点数不是您期望的 2 的幂。您可能希望使用基于正方形而不是角的索引
  • 我添加了生成的网格和顶点数据以获取更多信息

标签: c++ algorithm visual-c++ graphics mesh


【解决方案1】:

就像 PaulMcKenzie 提到的,接缝我只是使用偶数而不是奇数,这完全是问题所在。甚至更多,有时它不能正常工作,因为我使用太小的数字来生成像 90 或 100 这样的网格网格。所以,为了正确工作这个网格算法,宽度应该可以在 1、2、4、6、8 上整除, 10 和 12,然后它应该工作得很好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    相关资源
    最近更新 更多