Opengl 从碎片着色器中的地形高程数据计算法线
应用程序将地形高程数据加载到浮点纹理中。然后在浮动颜色缓冲区(使用*GL\U ARB\U color\U buffer\U float*)上渲染纹理 第一步是获得整体纹理(由多纹理四边形渲染产生)。为此,我禁用颜色钳制,渲染每个四边形,并将(浮动)帧缓冲区复制到(浮动)纹理中:该纹理是处理地形高程数据的多个过程的基础 使用假颜色渲染浮动纹理数据没有问题:实际上,地形高程信息在第一次过程中被保留(没有颜色钳制) 通过计算地形法线,我遇到了麻烦。下面是片段着色器的剪报:Opengl 从碎片着色器中的地形高程数据计算法线,opengl,glsl,shader,Opengl,Glsl,Shader,应用程序将地形高程数据加载到浮点纹理中。然后在浮动颜色缓冲区(使用*GL\U ARB\U color\U buffer\U float*)上渲染纹理 第一步是获得整体纹理(由多纹理四边形渲染产生)。为此,我禁用颜色钳制,渲染每个四边形,并将(浮动)帧缓冲区复制到(浮动)纹理中:该纹理是处理地形高程数据的多个过程的基础 使用假颜色渲染浮动纹理数据没有问题:实际上,地形高程信息在第一次过程中被保留(没有颜色钳制) 通过计算地形法线,我遇到了麻烦。下面是片段着色器的剪报: // Required sh
// 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);
}
//所需的着色器语言版本(兼容性配置文件)
#版本150兼容性
//---------------------------------------------------------------------------
//系统输入/输出
//---------------------------------------------------------------------------
//碎片纹理坐标(英寸)
在vec4 gl_TexCoord[];
//碎片颜色(输出)
vec4 gl_FragColor;
//DTED高程数据作为纹理。
//由纹理获取的值(如果地形的高度为米)。
统一采样器2D rdr_高程数据;
//获得碎片高度
浮动GetFragmentElevation(常量向量2 texcoord)
{
//这没问题!用假颜色测试
返回(纹理(rdr\u高程数据,gl\u TexCoord[0].st).r);
}
vec3 GetFragmentNormal(常量vec2 texcoord、常量浮点sOffset、常量浮点tOffset)
{
常数向量2
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);
浮标地面高度[9];
//确保固定纹理坐标
夹具(texcoordN,0.0,1.0);
夹具(TexCoordine,0.0,1.0);
夹具(texcoordE,0.0,1.0);
夹具(texcoordSE,0.0,1.0);
夹具(texcoordS,0.0,1.0);
夹具(texcoordSW,0.0,1.0);
夹具(texcoordW,0.0,1.0);
夹具(texcoordw,0.0,1.0);
//获取碎片周围的地形高度
TerrainAroundHeights[0]=GetFragmentElevation(texcoord);
TerrainAroundHeights[1]=获取碎片高程(texcoordN);
TerrainAroundHeights[2]=获取碎片高程(TexCoordine);
TerrainAroundHeights[3]=GetFragmentElevation(texcoordE);
TerrainAroundHeights[4]=获取碎片高程(texcoordSE);
TerrainAroundHeights[5]=获取碎片高程(texcoordS);
TerrainAroundHeights[6]=GetFragmentElevation(texcoordSW);
TerrainAroundHeights[7]=GetFragmentElevation(texcoordW);
TerrainAroundHeights[8]=GetFragmentElevation(TexCoordW);
常量浮点法向长度=0.001;
vec3 v0=vec3(0.0,0.0,0.0),v1=vec3(0.0,正常长度,0.0),v2=vec3(正常长度,0.0,0.0);
//计算每个碎片角点的平均高度
/*v0.z=(TerrainarRoundHeights[0]+TerrainarRoundHeights[7]+TerrainarRoundHeights[6]+TerrainarRoundHeights[5])/4.0;
v1.z=(TerrainarRoundHeights[0]+TerrainarRoundHeights[1]+TerrainarRoundHeights[8]+TerrainarRoundHeights[7])/4.0;
v2.z=(TerrainarRoundHeights[0]+TerrainarRoundHeights[5]+TerrainarRoundHeights[4]+TerrainarRoundHeights[3])/4.0*/
//我的测试…无法理解发生了什么
如果(TerrainAroundHeights[0]<(TerrainAroundHeights[5]))
v0.z=1.0;
返回(v0);
//计算地形法线
返回(正常化(交叉(v2-v0,v1-v0));
}
void main()
{
ivec2 rdr_ElevationData_Size=textureSize(rdr_ElevationData,0);//纹理大小,文本形式
vec3 frag_Normal;//片段法线
浮动rdr_高程数据_S_CoordStep=1.0/*rdr_高程数据_大小[0]*/672.0/*已尝试但不起作用…*4.0*/;
浮动rdr_高程数据\u T_CoordStep=1.0/*rdr_高程数据\u大小[1]*/672.0/*已尝试但不起作用…*4.0*/;
//确定片段正常
frag_Normal=GetFragmentNormal(gl_TexCoord[0]。st、rdr_高程数据协调步骤、rdr_高程数据协调步骤);
//测试目的…(始终为0.0!!!)
gl_FragColor=vec4(frag_Normal.z,0.0,0.0,1.0);
}
你可以在GetFragmentNormal中看到,我试图从neightboor文本中获取高程数据,但是TerrainAroundHeights的值似乎总是相等的
我错过了什么重大的事情吗?是否有更好的方法获取neightboor textel?在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
。噢,天哪。。。我需要一些配对编程!非常感谢。