阴影体积着色器优化 (GLSL)
我想知道是否有办法优化这个顶点着色器。
如果顶点位于阴影中,则此顶点着色器将顶点(在光线方向)投影到远平面。
该着色器的目的是创建一个包围对象本身阴影的阴影体积对象。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不想触及您的主要算法(正如 Michael Daum 在他的评论中建议的那样),您可以替换代码的某些部分:
其中 infiniteProjectionMatrix 是一个自定义的投影矩阵,其中远平面已设置为无穷大(请参阅
幻灯片 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:
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:
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).