osgEarth的Rex引擎原理分析(五十四)rex引擎默认的几个着色器功能分析

目标:(五十一)中的问题122

1、顶点着色器函数

(1)RexEngine.SDK.vert.glsl

这个着色器文件中的函数会加载到各个阶段的着色器程序中。主要用在osgEarth::Drivers::RexTerrainEngine::RexTerrainEngineNode节点中。所有地形渲染都可以使用此着色器中的函数。从纹理坐标获取高程值,用r值作为高程值。

这个着色器中的函数会进入到顶点、几何、片段等各个着色器阶段。

这里不带uniform的变量,可以认为是该管线阶段各个着色器文件的全局变量。

#version $GLSL_VERSION_STR
$GLSL_DEFAULT_PRECISION_FLOAT

#pragma vp_name Rex Terrain SDK

/**
 * SDK functions for the Rex engine.
 * Declare and call these from any shader that runs on the terrain.
 */

// uniforms from terrain engine
uniform sampler2D oe_tile_elevationTex;//存放高程纹理
uniform mat4 oe_tile_elevationTexMatrix;
uniform vec2 oe_tile_elevTexelCoeff;

uniform sampler2D oe_tile_normalTex;
uniform mat4 oe_tile_normalTexMatrix;

uniform vec4 oe_tile_key;

// Stage global
vec4 oe_layer_tilec;


/**
 * Sample the elevation data at a UV tile coordinate.
 */
float oe_terrain_getElevationUnscaled(in vec2 uv)
{
    // Texel-level scale and bias allow us to sample the elevation texture
    // on texel center instead of edge.
    vec2 elevc = uv
        * oe_tile_elevTexelCoeff.x     // scale
        + oe_tile_elevTexelCoeff.y;

    return texture(oe_tile_elevationTex, elevc).r;
}

/**
 * Sample the elevation data at a UV tile coordinate.
 */
float oe_terrain_getElevation(in vec2 uv)
{
    // Texel-level scale and bias allow us to sample the elevation texture
    // on texel center instead of edge.
    vec2 elevc = uv
        * oe_tile_elevTexelCoeff.x * oe_tile_elevationTexMatrix[0][0]     // scale
        + oe_tile_elevTexelCoeff.x * oe_tile_elevationTexMatrix[3].st     // bias
        + oe_tile_elevTexelCoeff.y;

    return texture(oe_tile_elevationTex, elevc).r;
}

/**
 * Read the elevation at the build-in tile coordinates (convenience)
 */
float oe_terrain_getElevation()
{
    return oe_terrain_getElevation(oe_layer_tilec.st);
}

/**
 * Read the normal vector and curvature at resolved UV tile coordinates.
 */
vec4 oe_terrain_getNormalAndCurvature(in vec2 uv_scaledBiased)
{
    return texture(oe_tile_normalTex, uv_scaledBiased);
}

vec4 oe_terrain_getNormalAndCurvature()
{
    vec2 uv_scaledBiased = oe_layer_tilec.st
        * oe_tile_elevTexelCoeff.x * oe_tile_normalTexMatrix[0][0]
        + oe_tile_elevTexelCoeff.x * oe_tile_normalTexMatrix[3].st
        + oe_tile_elevTexelCoeff.y;

    return texture(oe_tile_normalTex, uv_scaledBiased);
}

/**
 * Scales repeating texture coordinate such that they are [0..1]
 * at a specific reference tile LOD. 
 */
vec2 oe_terrain_scaleCoordsToRefLOD(in vec2 tc, in float refLOD)
{
    float dL = oe_tile_key.z - refLOD;
    float factor = exp2(dL);
    float invFactor = 1.0/factor;
    vec2 result = tc * vec2(invFactor);

    vec2 a = floor(oe_tile_key.xy * invFactor);
    vec2 b = a * factor;
    vec2 c = b + factor;

    float m = floor(clamp(factor,0.0,1.0)); // if factor>=1.0
    result += m*(oe_tile_key.xy-b)/(c-b);

    return result;
}

(2)RexEngine.elevation.glsl

获取高程的着色器,会调用RexEngine.SDK.vert.glsl中的函数。

 

(3)RexEngine.Morphing.vert.glsl

 

(4)RexEngine.NormalMap.vert.glsl

 

(5)RexEngine.vert.glsl

 

(6)RexEngine.vert.view.glsl

 

 

 


 

RexTerrainEngineNode的tileLayer的顶点着色器主函数:(只有函数声明,对着色器而言去哪找这些函数实现,见osgEarth中多个着色器的源代码的编译链接过程

#version 330 compatibility

#pr
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值