質問

Im making a 2D side scroller game and I am currently implementing lights. The lights are just a light gradient texture rendered on top of the terrain multiplied to make it brighten up the area. However, I dont know how to nor understand how to do Ambient lighting. The following picture sums up what I have and the bottom part is what I want.

enter image description here

I am open to answers regarding shaders for I know how to use them.

役に立ちましたか?

解決

I ended up creating an FBO texture the size of the screen, clearing it with the color of the ambience and drawing in all nearby lights. Then, I passed it through a shader I made which takes in 2 textures for uniforms. The texture to draw and the light FBO itself. The shader multiplies the textures being drawn with the FBO and it came out nicely.

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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top