2D lighting in OpenGL with GLSL, how?

Started by
10 comments, last by mmakrzem 8 years, 6 months ago

Hello everyone!

I am new to this forum so sorry it I posted this in the wrong section.

Anyways, I want to create 2D lightning with GLSL just like here:

And here:

I am using Java for my game. I was just wondering, how would I achieve this?

I am currently able to draw a light using LWJGL's built in functions like glLight but I am not sure how to add lights like in the videos!

Please help I've been searching the net for 2 days but without any success...

Any help would be appreciated!

Thank you very much!

Advertisement
Found this


My current game project Platform RPG

Hey, thanks for you answer!

I already saw this but it doesn't really show me how to do it with code.

Hopefully I will find something :)

Been searching for 2 Days.... Even posted a question on Stack Overflow....

looks liek its 2d so then.

you draw everything in screen coords first of all you put everything between -1..1 in vertex buffer

same for positions of lights you define the radius of light that is properly scaled, you draw the light now you test every fragment with every occluder (or you can make a 2d texture that already makes the shadow volume <- this seems reasonable unless you kniwo how to pass multiple objects to shader)

rest looks obvious... loop through lights apply the correct fragment.

Thanks WiredCat!

I understand the concept fully now :)

Still kind of confused with the actual code...

From other sources on the net I realised that lightning is actually making an image darker. I would need to render my normal scene and a black rectangle on top of it. Then add transparency or coloring where the light is.

Once again, still confused with the code :)

I could REALLY use an example, it would be easier to learn.

Thank you very much for your answer!

since when light darkens the image you start with completely black scene,, then you blend the result for each light with lets say

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

unfortunatetly i never wrote such thing, so i cant provide you with the code.

But first of all you could tell me if you even know how to render a quad that is 32x32 pixels at the center of the screen? well if not, then giving you a sample code would be really unwise

Haha ofc I do! :P That is the easiest thing to do :)

I am now looking at this tutorial, to see if I can come up with something:

http://nehe.gamedev.net/article/glsl_an_introduction/25007/

second video on post above mine actually says how shadows are rendered, you need really to consider how you want to cast shadows yourself, do that on cpu or gpu, anyway you will have to pass (im talking about that second yt vid) a set of points to shader for every occluder that will tell your shader (in gpu shadowcomputing) that everything between those two (actualy 2 volumes, 4 verts cast that) is black even blacker than MicroblackShotgun 55 smile.png

Btw that tutorial on nehe is outdated and uses FixedFunctionPipeline which is not what you want to use in 2015, i am not sure how this could help you since you say its an eaisiest thing to do thats the only thing you need to know, along with sending shadowcasters to the shader) (or making that on cpu whioch will be easier but slower)

so i think you really dont know how to draw that quad in the center

for gpu implementation and without taking into account unifroms and unfirmbuffers you could store in first pixel number of occluders. then for every next pixels you could apply there occluders positions, then in main lighting shader you will form a triangle (or two planes when you loop throug occluders) and if anything is between the shadowvlume you make it black.

since he draws a quad that is somewhat a filled circle with radius of rect_size/2.0 you will need some kind of bias,

Well, since I can't find any good tutorials on how to do lightning in 2D I am stuck with whatever I can find smile.png

I'll see what I can do with shaders. Hoping to implement lightning soon smile.png

Oh, and are you talking about drawing a quad in the center using ONLY GLSL or using Java aswell? Because In Java it's way to easy... glBegin(GL_QUADS); glVertex2f(...).... Sorry I'm to lazy to write it down now tongue.png

EDIT:

Alright just saw you edited your last post. I'm looking at it super confused haha

well after defining the RADIUS and LIGHT COLOR IN THE SHADER you could do that

vertex shader:


attribute vec3 Vpos;
varying vec2 vert;

void main()
{
vert = Vpos.xy; //pass texture coord
gl_Position = vec4(Vpos.xy, 0.0, 1.0);
}

in fragment

something liek that:



varying vec2 vert;


varying vec3 vertex_pos;
uniform vec3 LPOS;
uniform vec3 LDIFF;

uniform float LRadius;

float n3ddistance(vec3 first_point, vec3 second_point)
{
float x = first_point.x-second_point.x;
float y = first_point.y-second_point.y;
float z = first_point.z-second_point.z;
float val = x*x + y*y + z*z;
return sqrt(val);
}

void main()
{
	float dst = n3ddistance(LPOS, vertex_pos);
	float intensity = clamp(1.0 - dst / LRadius, 0.0, 1.0);
	vec4 color = vec4(LDIFF.x, LDIFF.y, LDIFF.z, 1.0)*intensity;
	gl_FragColor = color;
}

ofc that wont compile i just mixed two different shaders

vertex is from drawing a fullscreen quad on screen

second is a pointlight but in 3d so (additionallyi pass a vertex position to fragment shader)

looks like this:

pointatt.jpg

still one thing to consider you need to know if you will pass light radius in ndc coord (or screen coords ) or in worldspace coords everything is enterely up to you

This topic is closed to new replies.

Advertisement