阴影体积着色器优化 (GLSL)

发布于 2024-08-26 05:23:57 字数 777 浏览 4 评论 0原文

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

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;
  }
}

I wondering if there is a way to optimize this vertex shader.
This vertex shader projects (in the light direction) a vertex to the far plane if it is in the shadow.
The aim of this shader is to create a shadow volume object that enclose the shadow of the object itself.

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;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2024-09-02 05:23:57

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

uniform mat4 infiniteProjectionMatrix;

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

其中 infiniteProjectionMatrix 是一个自定义的投影矩阵,其中远平面已设置为无穷大(请参阅

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

幻灯片 7 上的 ="http://www.terathon.com/gdc07_lengyel.pdf" rel="nofollow">http://www.terathon.com/gdc07_lengyel.pdf) , 投影到无穷大,您不需要“100000.0”缩放因子,并且“gl_ModelViewMatrix * gl_Vertex”偏移量可以忽略(与 lightDir 向量的无限长度相比)。

If you don't want do touch your principal algorithm (as Michael Daum suggested in his comment) you could replace some parts of your code:

uniform mat4 infiniteProjectionMatrix;

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

where infiniteProjectionMatrix is a customized projection matrix where the far plane has been set to infinity (see http://www.terathon.com/gdc07_lengyel.pdf on slide 7) and looks something like:

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

Since you are projecting to infinity you don't need the "100000.0" scaling factor and the "gl_ModelViewMatrix * gl_Vertex" offset can be neglected (compared to the infinite length of the lightDir vector).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文