[GLSL shader question 1]Shadows

Hello!
I wanna try making a GLSL shader, a test one, but I don’t know how to make a shadow for it. I mean receiving shadows from light sources. Is there a way to get the shadows from light sources?

P.S. I will make this shader not have the light source light in point of shadow instead of darkening it that way allowing to have 2 identic lamps casting shadows at similar point and not making the shadow ultra black even if there is another, not very strong light around:)

Hi! I strongly advise you to look at this link: https://en.wikibooks.org/wiki/GLSL_Programming/Blender (basic lighting). You can read Diffuse Reflection first and follow all the tutorial. (I think it’s a good thing to read all the tutorial in the order). Or you can try to copy/paste code on a sphere to see what’s the result… The shaders handles multiple light types support but you can select only the part you are interested with (if you have only a sun light, just copy the part for directionnal lights…)

This time I am just interested in shadows, I need to know how to make objects have shadows so that my shader can be used in HQ scenes with dynamic lighting that includes shadow mapping.

You mean you want to play with the shadow buffer? I think it’s not possible to access it

Really? What a disappointement…

Maybe something is possible with bgl module but it’s beyond my competence, or with render to texture to recreate a shadow map but I doubt it would be efficient…

Isn’t shadows created with render to texture anyway? If I can just render the depth texture, I can get shadows, but in the case of that I am getting more worried about the next part - making shadow shader…

Yes I think thats the biggest disadvantage of GLSL shaders in the bge.

(It allows us to have shadows from scene objects over custom material GLSL shaders… Great!)

Yes but it takes much performence particularly this part of the Scripte that load the shadowmap in the videotexturebuffer.


        if not hasattr(logic, 'video'):
            # Texture cannot accept single byte image
            size = bglWidth[0] * bglHeight[0] * 3
            depth = bytearray(size)
            i = 0
            for r in image:
                for j in range(0,bglWidth[0]):
                    depth[i] = r[j]
                    i = i+1
                    depth[i] = r[j]
                    i = i+1
                    depth[i] = r[j]
                    i = i+1
            #####
            matID = texture.materialID(obj, 'IMLamp_dummy_shadow_image')
            logic.video = texture.Texture(obj, matID)
            logic.video.source = texture.ImageBuff()
            logic.video.source.load(depth, bglWidth[0], bglHeight[0])

At the moment this part only run once when the shadowmap will loaded the first time into the videotexture buffer but it seems like the shadows won’t be updated (if positions of objects or lamp changed). Looks like

logic.video.refresh(True)

not the right thing and that the part above must run everytime the shadows will be updated.:frowning:

So I think it’s difficult to get dynamic shadows but maybe that is an interessting thing for static shadows so you only must run this script once… better than nothing.

Yes, for the moment, it’s preferable to run the script only one time for static shadow… I don’t know how we could do improvements to have dynamically shadow buffer

You setup the video buffer once, then call its refresh every frame to get real time shadows.
I have real time shadows working in my own GLSL shader using the video buffer. My only problem is getting the correct shadow matrix passed into the shader. If I apply all transforms to the models then run the game the shadows look good, but if you move the objects without applying the transforms then the shadows do not move with the objects.

shadowMatrix = shadowCam.projection_matrix * shadowCam.modelview_matrix

The above code is what I am using for the shadow matrix.

I can upload a blend file later for those interested in taking a look at it.

You setup the video buffer once, then call its refresh every frame to get real time shadows.

Yes? it works?
And your are sure that the shadow matrix is the problem? Because I have applied the texture which is used for the videotexture on a simple plane without shader and when I move the objects the texture doesn’t changed after the first script run. But when I edit the script so that it setup the video buffer every frame (with very very bad performance) the shadowtexture changed so it’s look like refresh don’t update the shadows.

But I would be happy if you get it work…

From you, guys, I don’t understand it - is it that we are not sure yet on how to make shadows work or is it that Blender requires patch to make theese shadows work? And are you aiming now to just get depth buffer to the shader or get already working shadows?

Can’t we just render to texture a depth buffer and simply access it through the shader?

Have you looked to the first link that youle sent in post #8?
There is a attempt with render to texture. But this take much performance too and it have a limited resolution (1024x).

Oh… Yeah - that’s not good. That means that Blender needs new patch, an access to shadow buffer through GLSL shaders…

Here is the the blend file.

It does not have to recreate the texture buffer everytime, it just updates the shadow by calling a refresh on it.

Attachments

shadowGLSL.blend (526 KB)

The shadows are tiled:D
It works fine.
The cube which rotates has wrong shadows/shading.

Ya, it’s because the shadow matrix is not setup correctly, I’m having difficulty setting it to apply the model transforms.
Right now when you move models the shadow matrix does not update this and only sees the models in their original positions.
Other than that it is working.

Great! Another solution, but with ImageRender… :slight_smile:

Ah okay, I misunderstood you, I don’t knew that you talk about imageRender I thought you mean the OpenGL-way.

But your work looks good and would be nice when you get the shadow matrix working correctly.