2D lighting for a side-scroller?

Started by
4 comments, last by WavyVirus 13 years, 4 months ago
I want to add lighting to my game and there will be both static lights and moving ones. I'm using D3DXSPRITE to draw all the tiles and was wondering if it supports lighting?

If it doesn't, should i manipulate the sprite's ARGB values to add lighting effects or should i ditch it and just use textured quads with normals?
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
Advertisement
What effect are you hoping to achieve? You can have some kind of approximation of lighting if you create a normal map for your sprites and light it using a pixel shader. There's no way of doing it using fixed function. You might struggle to generate a normal map from your sprites without any 3d surface information though.

I don't know what you mean by modifying the ARGB values of your sprite, but if you are referring to doing the lighting yourself on the CPU by modifying the texture directly each frame, then that is almost certainly a bad way to do it. Normal mapped pixel shader would be the better approach.
I meant that when the lights move, i calculate which tiles are affected (i implemented a grid system) and then change their (I'm guessing the vertices) colour. If i want to make the tiles lighter, i just increase the RGB value, if i want it to get dimmer, i decrease it..

Anyway so i take it D3DLIGHT9 has no effect of D3DXSPRITE?
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
Oh ok so you just want to light the sprites as if they were flat quads. In that case you don't need to worry about normals.

If you are happy with each sprite having a uniform lighting colour then the easiest way would be to calculate the amount of light falling on a sprite and render it with that colour. If it's in full light it will be white, full darkness would be black.

If you need the sprite to be lit partially (one corner bright, the other dark) then you will need per-pixel lighting. D3DLIGHT won't be any good for this. I'm afraid you're going to have to delve into the seemingly murky world of shaders and effects (which is actually quite straightforward once you get started).
Okay thanks for your help :)
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
Another approach is to create a "light map" texture, which you can draw over the entire screen (perhaps using a multiply blend) after all of your sprites have been rendered. You can pre-create a texture with all of the static lighting in your level.

If you need moving lights then you can blit circular rings onto the lightmap each frame before drawing the whole lot over the scene - I've used a similar approach before. By projecting lines from light positions to the vertices of any objects within the light's range, you can create black 2D shadow polygons to draw to your lightmap as well.

This topic is closed to new replies.

Advertisement