OpenGL中的2D环境光照

时间:2013-08-12 23:15:04

标签: c++ opengl 2d lighting

我正在制作2D侧卷轴游戏,我目前正在实施灯光。灯光只是在地形顶部渲染的浅梯度纹理倍增,使其变亮区域。但是,我不知道怎么做也不懂如何做环境照明。下面的图片总结了我所拥有的,底部是我想要的。

enter image description here

我对着色器的答案持开放态度,因为我知道如何使用它们。

1 个答案:

答案 0 :(得分:1)

我最终创建了一个与屏幕大小相同的FBO纹理,用环境的颜色清除它并在附近的所有灯光中绘图。然后,我通过我制作的着色器传递了它,它为制服提供了2个纹理。要绘制的纹理和光FBO本身。着色器将正在绘制的纹理与FBO相乘,结果很好。

ambience.frag

uniform sampler2D texture1;
uniform sampler2D texture2;
varying vec2 texCoord;

void main( void ) {
    vec4 color1 = vec4(texture2D(texture1, gl_TexCoord[0].st));
    vec4 color2 = vec4(texture2D(texture2, texCoord));
    gl_FragColor = color1*vec4(color2.r,color2.g,color2.b,1.0);
}

ambience.vs

varying vec2 texCoord;
uniform vec2 screen;
uniform vec2 camera;

void main(){
    gl_Position = ftransform();
    gl_TexCoord[0] = gl_MultiTexCoord0;
    vec2 temp = vec2(gl_Vertex.x,gl_Vertex.y)-camera;
    texCoord = temp/screen;
}