opengl - 在片段着色器中根据地形高程数据计算法线

标签 opengl glsl shader

该应用程序将地形高程数据加载到浮点纹理中。然后在 float 颜色缓冲区上渲染纹理(使用 *GL_ARB_color_buffer_float*)。

第一遍是获取整体纹理(由多个纹理四边形渲染产生)。为此,我禁用颜色钳位,渲染每个四边形,并将( float )帧缓冲区复制到( float )纹理中:此纹理是用于处理地形高程数据的多个 channel 的基础。

使用假颜色渲染 float 纹理数据没有问题:确实,地形高程信息在第一次传递期间被保留(无颜色限制)。

我在计算地形法线时遇到了麻烦。这是片段着色器的截图:

// Required shader language version (compatibility profile)
#version 150 compatibility

//---------------------------------------------------------------------------
// SYSTEM INPUTS/OUTPUT
//---------------------------------------------------------------------------

// Fragment texture coordinate (in)
in vec4 gl_TexCoord[];
// Fragment color (out)
vec4 gl_FragColor;

    // DTED elevation data as texture.
// Value fetched by the texture if the altitude of the terrain, in meters.
uniform sampler2D rdr_ElevationData;

// Obtain fragment elevation
float GetFragmentElevation(const vec2 texcoord)
{
    // This is OK! Tested with false colors
    return (texture(rdr_ElevationData, gl_TexCoord[0].st).r);
}

vec3 GetFragmentNormal(const vec2 texcoord, const float sOffset, const float tOffset)
{
    const vec2
        texcoordN  = texcoord + vec2( 0.0,      +tOffset),
        texcoordNE = texcoord + vec2(+sOffset,  +tOffset),
        texcoordE  = texcoord + vec2(+sOffset,   0.0),
        texcoordSE = texcoord + vec2(+sOffset,  -tOffset),
        texcoordS  = texcoord + vec2( 0.0,      -tOffset),
        texcoordSW = texcoord + vec2(-sOffset,  -tOffset),
        texcoordW  = texcoord + vec2(-sOffset,   0.0),
        texcoordNW = texcoord + vec2(-sOffset,  +tOffset);
    float TerrainAroundHeights[9];

    // Ensuring clamped texture coordinates
    clamp(texcoordN , 0.0, 1.0);
    clamp(texcoordNE, 0.0, 1.0);
    clamp(texcoordE , 0.0, 1.0);
    clamp(texcoordSE, 0.0, 1.0);
    clamp(texcoordS , 0.0, 1.0);
    clamp(texcoordSW, 0.0, 1.0);
    clamp(texcoordW , 0.0, 1.0);
    clamp(texcoordNW, 0.0, 1.0);
    // Fetch terrain heights around the fragment
    TerrainAroundHeights[0] = GetFragmentElevation(texcoord);
    TerrainAroundHeights[1] = GetFragmentElevation(texcoordN);
    TerrainAroundHeights[2] = GetFragmentElevation(texcoordNE);
    TerrainAroundHeights[3] = GetFragmentElevation(texcoordE);
    TerrainAroundHeights[4] = GetFragmentElevation(texcoordSE);
    TerrainAroundHeights[5] = GetFragmentElevation(texcoordS);
    TerrainAroundHeights[6] = GetFragmentElevation(texcoordSW);
    TerrainAroundHeights[7] = GetFragmentElevation(texcoordW);
    TerrainAroundHeights[8] = GetFragmentElevation(texcoordNW);

    const float NormalLength = 0.001;

    vec3 v0 = vec3(0.0, 0.0, 0.0), v1 = vec3(0.0, NormalLength, 0.0), v2 = vec3(NormalLength, 0.0, 0.0);

    // Compute averaged heights for each fragment corner
    /*v0.z = (TerrainAroundHeights[0] + TerrainAroundHeights[7] + TerrainAroundHeights[6] + TerrainAroundHeights[5]) / 4.0;
    v1.z = (TerrainAroundHeights[0] + TerrainAroundHeights[1] + TerrainAroundHeights[8] + TerrainAroundHeights[7]) / 4.0;
    v2.z = (TerrainAroundHeights[0] + TerrainAroundHeights[5] + TerrainAroundHeights[4] + TerrainAroundHeights[3]) / 4.0;*/

    // MY TESTS... unable to understand what's going on
    if (TerrainAroundHeights[0] < (TerrainAroundHeights[5]))
        v0.z = 1.0;
    return (v0);

    // Compute terrain normal
    return (normalize(cross(v2 - v0, v1 - v0)));
}

void main()
{
    ivec2 rdr_ElevationData_Size = textureSize(rdr_ElevationData, 0);       // Texture size, in textel
    vec3 frag_Normal;                                                       // Fragment normal
    float rdr_ElevationData_S_CoordStep = 1.0 / /*rdr_ElevationData_Size[0]*/ 672.0 /* Tried but not working... * 4.0 */;
    float rdr_ElevationData_T_CoordStep = 1.0 / /*rdr_ElevationData_Size[1]*/ 672.0 /* Tried but not working... * 4.0 */;

    // Determine fragment normal
    frag_Normal = GetFragmentNormal(gl_TexCoord[0].st, rdr_ElevationData_S_CoordStep, rdr_ElevationData_T_CoordStep);

            // Test purpose... (always 0.0!!!)
    gl_FragColor = vec4(frag_Normal.z, 0.0, 0.0, 1.0);
}

您可以在GetFragmentNormal中看到,我尝试从邻居文本中获取高程数据,但是TerrainAroundHeights的值似乎总是相等!!!

我错过了什么大事吗?有没有更好的方法来获取邻居文本?

最佳答案

您在 GetFragmentNormal 中计算偏移 texcoord,然后将其传递给 GetFragmentElevation,但随后您不在纹理查找中使用它 - 您在添加偏移之前使用原始 texcoord。

float GetFragmentElevation(const vec2 texcoord)
{
    // This is OK! Tested with false colors
    return (texture(rdr_ElevationData, gl_TexCoord[0].st).r);
}

因此,毫不奇怪,无论偏移量如何,您都会获得相同的高度。将 gl_TexCoord[0] 更改为 texcoord

关于opengl - 在片段着色器中根据地形高程数据计算法线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6255562/

相关文章:

python - PyOpenGL,使用 glFrustum 和 glTranslate 在屏幕上出现 3D 图形问题

opengl - GLSL 中的正常旋转

swift - 如何仅在 SceneKit 中的球体的一部分应用菲涅耳效果?

c++ - 绘制三角形不起作用 : I just see a black screen and my mouse

c++ - 着色器中的 gl_NormalMatrix 和 gl_ModelViewMatrix 是什么?

opengl - 如何将图像后处理着色器的结果添加到场景中

c++ - GLSL - GLSL 1.2 中的统一位置和着色器中的深度测试

c++ - OpenGL错误编译着色器

c - 防止 OpenGL 缓冲帧