2013-07-09 84 views
1

我刚刚开始工作(它看起来工作得非常快)雷射系统可以制作基本的照明(像demo demo这样工作:http://www.redblobgames.com/articles/visibility/)。它可以像我想要的那样工作,但是当我设置每个顶点距离玩家/光源的点亮区域时,它的插值非常糟糕,例如,如果我想在远处修剪光线/阴影到X,我没有但圆形和多边形/正方形之间的东西。图片应该解释。OpenGL GLSL基本雷电距离插值精度不高(2D)

此外 - 我用它为我的3D项目(游戏)与自上而下的相机,如果有关系。 (见下图)。算法效果很好,不需要在shadders本身上工作,而是在其上绘制简单的空间。 (无裁剪等)

这里是我的顶点着色器:

#version 330 core 
layout(location = 0) in vec3 vertexPosition_modelspace; //vertex's position 
layout(location = 1) in vec3 Normal; //normal, currently not used, but added for future 
layout(location = 2) in vec2 vertexUV; //texture coords, used and works ok 

out vec2 UV; //here i send tex coords 
out float intensity; //and this is distance from light source to current vertex (its meant to be interpolated to FS) 

uniform mat4 MVP; //mvp matrix 
uniform vec2 light_pos; //light-source's position in 2d 

void main(){ 
    gl_Position = MVP * vec4(vertexPosition_modelspace,1); 
    UV = vertexUV; 
    intensity = distance(light_pos, vertexPosition_modelspace.xz); //distance between two vectors 
} 

这里是我的片段着色器:

#version 330 core 

in vec2 UV; //tex coords 
in float intensity; //distance from light source 
out vec4 color; //output 
uniform sampler2D myTextureSampler; //texture 
uniform float Opacity; //opacity of layer, used and works as expected (though to be change in near future) 

void main() { 
    color = texture(myTextureSampler, UV); 
    color.rgba = vec4(0.0, 0.0, 0.0, 0.5); 
    if(intensity > 5) 
     color.a = 0; 
    else 
     color.a = 0.5; 
} 

此代码应该给我很好的FOV的圆修剪,而是我得到这样的东西: It's clearly not a circle-like shape

我不知道它为什么工作,因为它的工作...

+0

另外,不要介意纹理和模型,它只是当时我在其他更重要的东西上工作的时间。当我完成更紧迫的事情时,我会填补这一点,我希望有一位朋友会帮助我,因为我吸收这种东西;) – RippeR

+0

我们在图片中看到了什么?灰色的alpha覆盖了一个新的多边形还是与背景中的顶点相同,只是由着色器变暗了?如果它是一个新的多边形(我认为是这样),也许你的阈值5太大了。 –

+0

如果我通过片段着色器中的距离东西来关闭剪辑,我会得到标准的[THIS](http://i.imgur.com/fOSixFg.png)。我简单地使用光线投射来获得二维空间中的点(从3D空间,我不在乎OY在这里),从中我可以创建多边形(或使用三角形扇),这是从这个评论的图片较暗。如果我只为这个较暗的多边形使用特殊着色器,并为它提供光源/播放器的位置,我想让那个较暗的效果慢慢消失(因此我计算方向)。不幸的是,它看起来不像内插太好,正如主帖中的图片所示。 – RippeR

回答

0

这不是一个准确性问题,这是算法性的,很容易看出你是否将一个三角形与所有边相等并且将对象放在其中一个顶点上。在其他两个顶点中,您将获得相同的距离,沿着这两个相距较远的点之间的边缘,您将获得相同的距离,这是错误的,因为它应该在该边缘的中心处更大。

如果您在光源处使用角度较小的三角形,可以使其平滑。

更新:如果通过每个三角形的光照位置,并且将使用插值坐标计算片段着色器中的实际距离,则仍然可以获得圆形阴影。