【发布时间】: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