Hello,
I am attempting to implement my first lighting shader using a lightmap for some spotlights by rendering to a render texture and then blending this render texture over the game world for and additional amibent lighting effect, which is working nicely. (I am using SFML.Net)
The problem i'm having is with my scrolling View. When the view scrolls, the lights remain rendered in the same position and scrolling with the view, rather than being rendered in their original position.
My spotlights are sprites which remain in the same position throughout, which leads me to the conclusion that the issue lies with either the render texture used as the lightmap or the shader itself.
Any help would be greatly appreciated, this is one of my first attempts at lighting techniques, so i'm at a loss.
I'm using the standard passthrough vertex shader, so i don't specify one in my Shader instance.
my lighting fragment shader:
uniform sampler2D texture;
uniform sampler2D lightmap;
uniform vec4 ambient;
uniform vec2 resolution;
void main()
{
vec4 diffuse = texture2D(texture, gl_TexCoord[0].xy);
vec2 lightcoord = (gl_FragCoord.xy / resolution.xy);
vec4 light = texture2D(lightmap, lightcoord);
vec4 ambientLight = ambient;
//ambientLight.a is used as intensity to pass through as vec4
vec3 ambientColour = ambientLight.rgb * ambientLight.a;
vec3 intensity = ambientColour + light.rgb;
vec3 result = diffuse.rgb * intensity;
gl_FragColor = vec4(result, diffuse.a);
}
Rendering:
public class RenderSystem
: IDisposable
{ private readonly RenderWindow _window
; public View View
{ get; } private RenderTexture _renderTexture
; private Sprite _lightmap
; public RenderStates States
{ get; set; } public RenderSystem
(RenderWindow window, IUiLayer uiLayer
) { _window
= window
; _renderTexture
= new RenderTexture
(_window
.Size.X, _window
.Size.Y); _lightmap
= new Sprite
(_renderTexture
.Texture); UiLayer
= uiLayer
; View
= new View
((Vector2f
)_window
.Size / 2,
(Vector2f
)_window
.Size); } public void RenderLights
(IEnumerable
<Sprite
> lights
) { _renderTexture
.Clear(Color
.Transparent); foreach (var light
in lights
) { _renderTexture
.Draw(light
); } _renderTexture
.Display(); States
.Shader.SetUniform("lightmap", _renderTexture
.Texture); } public void RenderDrawable
(Drawable drawable
) { _window
.Draw(drawable, States
); }} Render Method in my game loop:
public void Render()
{
RenderSystem.RenderDrawable(_levelSprite);
RenderSystem.RenderLights(_lights);
foreach(Sprite gameObject in _gameObjects)
{
RenderSystem.RenderDrawable(gameObject);
}
}
Initialisation of RenderSystem.States pre-gameloop:
public void Initialise
(){ //Sig: Shader.FromString(string vertexShader, string geometryShader, string fragmentShader); Shader shader
= Shader
.FromString(null, File
.ReadAllText("lighting.frag"),
null); RenderSystem
.States = new RenderStates
(shader
); RenderSystem
.States.Shader.SetUniform("ambient",
new Vec4
(161,
93,
30,
167)); RenderSystem
.States.Shader.SetUniform("resolution",
new Vec2
(640,
480));}