【问题标题】:Shadow volume shader optimization (GLSL)阴影体积着色器优化 (GLSL)
【发布时间】:2010-03-25 18:30:55
【问题描述】:

我想知道是否有办法优化这个顶点着色器。
如果顶点在阴影中,则此顶点着色器(在光线方向)将顶点投影到远平面。
这个着色器的目的是创建一个包围对象本身阴影的阴影体积对象。

void main(void) {
  vec3 lightDir = (gl_ModelViewMatrix * gl_Vertex 
                   - gl_LightSource[0].position).xyz;

  // if the vertex is lit
  if ( dot(lightDir, gl_NormalMatrix * gl_Normal) < 0.01 ) {

    // don't move it
    gl_Position = ftransform();
  } else {

    // move it far, is the light direction
    vec4 fin = gl_ProjectionMatrix * (
                 gl_ModelViewMatrix * gl_Vertex 
                 + vec4(normalize(lightDir) * 100000.0, 0.0)
               );
    if ( fin.z > fin.w ) // if fin is behind the far plane
      fin.z = fin.w; // move to the far plane (needed for z-fail algo.)
    gl_Position = fin;
  }
}

【问题讨论】:

  • 像这样投影每个顶点似乎每次通过都很痛苦。您是否考虑过明确地找到对象轮廓,然后仅投影它们?对于 N 个顶点网格,大约有 sqrt(N) 个轮廓顶点,所以这是一个很大的胜利。尤其是因为如果灯光和物体平稳移动,您可以有效地计算轮廓。

标签: optimization opengl shader shadow


【解决方案1】:

如果您不想触及您的主要算法(正如 Michael Daum 在他的评论中建议的那样),您可以替换您的代码的某些部分:

uniform mat4 infiniteProjectionMatrix;

if(...) {
   ...
} else {
   gl_Position = infiniteProjectionMatrix * vec4(lightDir, 0.0);
}    

infiniteProjectionMatrix 是一个自定义投影矩阵,其中远平面已设置为无穷大(参见幻灯片 7 上的 http://www.terathon.com/gdc07_lengyel.pdf),看起来像:

*  0  0  0
0  *  0  0
0  0 -1  *
0  0 -1  0

由于您投影到无穷大,因此不需要“100000.0”缩放因子,并且可以忽略“gl_ModelViewMatrix * gl_Vertex”偏移(与 lightDir 向量的无限长度相比)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多