Per-pixel lighting (NO SHADERS!)

Ok guys I recently had the urge to create a per-pixel lighting system. I don’t know GLSL (though I want to learn it soon) but I still wanted to do some lighting stuff. I came up with this:

public void renderLight(Vector2f pos /*position of light */){
float transparency = 0, trans1, trans2, trans3, trans4, trans5; //5 levels of lumosity
trans1 = .1f;
trans2 = .2f;
trans3 = .4f;
trans4 = 7f;
trans5 = 1;

	for(int x = 0; x < Display.getWidth(); x++){
		for(int y = 0; y < Display.getHeight(); y++){
			
			//get distance between current pixel and light source
			Vector2f distance, currentPos = new Vector2f(x, y);
			currentPos.sub(pos);
			distance = new Vector2f(Math.abs(pos.getX() - currentPos.getX()), Math.abs(pos.getY() - currentPos.getY()));
			
			if(distance.getLength() <= 5){
				transparency = trans1; //current pixel is 5 pixels away from source
			
			}else if(distance.getLength() <= 10){
				transparency = trans2; //current pixel is 10 pixels away from source
				
			}else if(distance.getLength() <= 20){
				transparency = trans3; //current pixel is 20 pixels away from source
				
			}else if(distance.getLength() <= 35){
				transparency = trans4; //current pixel is 35 pixels away from source
				
			}else if(distance.getLength() >= 50){
				transparency = trans5; //current pixel is 50 or more pixels away from source
				
			}
			
			glColor4f(0, 0, 0, transparency);
			glBegin(GL_POINTS);
				glVertex2f(x, y);
			glEnd();
		}
	}
}

Pretty simple I know. But I’m really proud of it because I’ve always thought smooth lighting was impossible without shaders. Now obviously the point of per-pixel lighting is for it to be smooth, and I only have 5 levels of lumosity for each pixel. Also you can’t customize the strength of the light. Sure the light doesn’t cast a shadow, and this system might be really slow. But what I’m asking here is how to make it more smooth (lumosity fades out gradually instead of having 5 levels) and how to customize the strength of the light. Thanks guys!!!

Just learn GLSL. It’s easier.

Its easier for you and its a lot easier for the program.

Fine then… Lol

Yep. GLSL will make your life a whole lot easier. But, just to answer your question:


float lightStrength = 1.2f;
transparency = distance.getLength()/(50f * lightStrength);

A nice and easy way to do simple per pixel lighting is to create an offscreen texture, render the lights to the offscreen texture (using additive blending) and than multiply this texture with the screen.

Here is how it could look like: https://www.youtube.com/watch?v=0MiVrE5-IhE You can see that the player pointer and each of the cells emit some light. If many light sources are at the same place, the scene gets lighter, as the light sources add up to eachother (why we use additive blending for the light texture).

I have the idea from a post here (and I think the explanation is much better with the example images as mine): http://www.java-gaming.org/topics/realistic-lighting-in-a-2d-game/20161/view.html

When he says offscreen texture he most likely means an FBO. Just clarifying.

To people who say just use shaders, you’re kinda wrong. The question he posed here is not whether or not to use shaders. If he used shaders, he would still be posting this question.

@OP
Use code tags [.code][./code](Without .) and that what longarmx said.

The way I did my lighting is divide distance by light strength, and then divide the number you get by some kind of constant ( i did 300)

float light=distance/light.strength/300.0;

Yeah, that is correct, sir :wink:

I will echo the first two replies, just learn GLSL :wink: