OpenGL高度图渲染器无法产生平滑的地形

时间:2019-02-10 14:40:11

标签: c++ opengl terrain heightmap

我正在使用C ++和OpenGL使用嵌套的网格和高度图编写一个地形渲染器,但是在看起来像块状/梯形的高细节(更近)网格时遇到了麻烦。

最初,我认为问题出在我使用的8位高度图上,但是16位高度图产生了相同的结果(我正在使用l3dt,World Machine和Photoshop生成不同的地图)。

我的代码需要从引擎管道中提取出来,以便使用顶点着色器中的变换反馈将高度图应用于网格:

void main()
{
    float texOffset = 1.0 / mapWidthTexels, mapOffset = scale / mapWidthWorld; //Size of a texel in [0, 1] coordinates and size of a quad in world space
    vec2 texCoord = (vertPos.xz * scale + offset) / mapWidthWorld + 0.5; //Texture coordinate to sample heightmap at. vertPos is the input vertex, scale is pow(2, i) where i is the nested grid number, offset is eye position
    position = vertPos * scale;

    if(vertPos.y == 0.0) //Y coordinate of the input vertex is used as a flag to tell if the vertex is bordering between nested grids
        position.y = texture(heightmap, texCoord).r; //If it's not, just sample the heightmap
    else
    {
        //Otherwise get the two adjacent heights and average them
        vec2 side = vec2(0.0);
        if(abs(vertPos.x) < abs(vertPos.z))
            side.x = mapOffset;
        else
            side.y = mapOffset;
        float a = texture(heightmap, texCoord + side).r, b = texture(heightmap, texCoord - side).r;
        position.y = (a + b) * 0.5;
    }

    float mapF = mapWidthWorld * 0.5;
    position.xz = clamp(position.xz + offset, -mapF, mapF) - offset; //Vertices outside of the heightmap are clamped, creating degenrate triangles
    position.y *= heightMultiplier; //Y component so far is in the [0, 1] range, now multiply it to create the desired height

    //Calculate normal
    float leftHeight = texture(heightmap, texCoord + vec2(-texOffset, 0.0)).r * heightMultiplier, rightHeight = texture(heightmap, texCoord + vec2(texOffset, 0.0)).r * heightMultiplier;
    float downHeight = texture(heightmap, texCoord + vec2(0.0, -texOffset)).r * heightMultiplier, upHeight = texture(heightmap, texCoord + vec2(0.0, texOffset)).r * heightMultiplier;
    normal = normalize(vec3(leftHeight - rightHeight, 2.0, upHeight - downHeight));

    tex = vertTex; //Pass through texture coordinates
}

RAW 16位高度图将按以下方式加载:

std::ifstream file(_path, std::ios::ate | std::ios::binary);
int size = file.tellg();
file.seekg(0, std::ios::beg);
m_heightmapWidth = sqrt(size / 2); //Assume 16-bit greyscale
unsigned short *data = new unsigned short[size / 2];
file.read(reinterpret_cast<char*>(data), size);

if (m_flip16bit) //Dirty endianness fix
{
    for (int i = 0; i < size / 2; i++)
        data[i] = (data[i] << 8) | ((data[i] >> 8) & 0xFF);
}

glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, m_heightmapWidth, m_heightmapWidth, 0, GL_RED, GL_UNSIGNED_SHORT, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
delete[] data;

其他格式也与stb_image一起加载。

生成的地形如下所示: https://imgur.com/a/d8tDPGO

如您所见,几乎不了解坡度的区域具有这种梯田外观。我在做什么错了?

2 个答案:

答案 0 :(得分:1)

  

RAW 16位高度图将按以下方式加载:

[...]
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, m_heightmapWidth, m_heightmapWidth, 0, GL_RED, GL_UNSIGNED_SHORT, data);
                               ^^^^^^

不。 internalFormat 参数控制纹理存储在GPU上的格式,在任何实际情况下,GL_RED仅为8位。您很可能希望GL_R16使用标准化的16位无符号整数格式。

答案 1 :(得分:0)

结果证明,l3dt的纹理是问题所在,原本打算在水下的部分却变成了梯田。此外,如果l3dt中使用的高度范围与着色器中的heightMulptiplier不匹配,则可能会导致伪影。