Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (636)
Games in Android Showcase (178)
games submitted by our members
Games in WIP (686)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Per-pixel lighting (NO SHADERS!)  (Read 2480 times)
0 Members and 1 Guest are viewing this topic.
Offline JonnyGingerKid

Junior Devvie

Exp: 3 years

« Posted 2014-04-11 02:31:59 »

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);
            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);
               glVertex2f(x, y);

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!!!!
Offline HeroesGraveDev

JGO Kernel

Medals: 360
Projects: 11
Exp: 3 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

« Reply #1 - Posted 2014-04-11 02:40:42 »

Just learn GLSL. It's easier.

Offline Drenius
« Reply #2 - Posted 2014-04-11 03:05:01 »

Its easier for you and its a lot easier for the program.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JonnyGingerKid

Junior Devvie

Exp: 3 years

« Reply #3 - Posted 2014-04-11 04:22:34 »

Fine then.... Lol
Offline Longarmx
« Reply #4 - Posted 2014-04-11 04:29:28 »

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);

Offline atombrot
« Reply #5 - Posted 2014-04-11 08:43:23 »

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:  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):

Offline Slyth2727
« Reply #6 - Posted 2014-04-12 14:11:34 »

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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline trollwarrior1
« Reply #7 - Posted 2014-04-12 14:25:13 »

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.

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;
Offline atombrot
« Reply #8 - Posted 2014-04-12 14:30:59 »

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

Yeah, that is correct, sir Wink
Offline Plutoniummatt

Senior Newbie

Medals: 1

« Reply #9 - Posted 2014-04-17 21:02:32 »

I will echo the first two replies, just learn GLSL  Wink
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Dwinin (63 views)
2015-11-07 13:29:08

Rems19 (77 views)
2015-10-31 01:36:56

Rems19 (69 views)
2015-10-31 01:32:37

williamwoles (104 views)
2015-10-23 10:42:59

williamwoles (91 views)
2015-10-23 10:42:45

Jervac_ (105 views)
2015-10-18 23:29:12

DarkCart (132 views)
2015-10-16 00:58:11

KaiHH (115 views)
2015-10-11 14:10:14

KaiHH (153 views)
2015-10-11 13:26:18

BurntPizza (167 views)
2015-10-08 03:11:46
Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48

Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!