Tag Archives: light

For my next project, I wanted to do something a bit more challenging than simply ray casting against spheres, so I chose something deceptively simple: I chose to ray trace some stained glass. Now, this seemed like a good idea at the time, and I’m pretty happy with the result, but my weekend was eaten by this little project because once I got one part working, another idea popped into my head – “wouldn’t it be cool if…” was a recurring theme.

There were more than a few subtle (and non-subtle) challenges with this little shader:

Up until this point, I’ve been sticking to a single ray cast, with the exception of the Light and Shadow shader, which also cast rays to check if a position was in shadow. For this shader, I am doing multiple bounces.

A stained glass window does not cast a simple shadow. It transmits some of its received light.

The original thought was to simply have a “window pane” cast its light at the ground and be done with it. It turned out that it was an extremely dull and uninteresting scene. So I added a sphere, thinking “how much complexity could it add?”

I kept running into more ideas like: “I could add light attenuation!” or, “what if I made it look like my light is rising and falling like the Sun?” And who could forget, “what if I made this light a simple flare instead of a janky looking sphere?”

So, all told, I managed to try a number of new things in this latest shader, and as a result it took me far longer than the original vision. I think it was the right choice, although there are certainly nitpicks that I have that I may have to revisit someday. For example, I’m not happy with how unconvincing the “glass” material is (I didn’t refract my light!), or with how shiny the sphere is.

What this little function does is to test for plane intersection via iPlane(), but then it also takes in some constraints, such as a starting (u,v) coordinate and a width and height from those coordinates, creating a rectangle. Granted, this only works for axis-aligned planes, but that was fine for this particular shader. However, the real purpose of this function was to return a UV coordinate of where a ray hit, allowing me to map my color scheme for the glass to the coordinates, essentially allowing me to texture a quad.

Once I textured a quad, the rest was fairly simple – firing a primary ray at the quad would give me a color, which I would apply to the shadow ray, resulting in a stained glass lighting effect.

Adding a sphere to the scene caused a few headaches, such as doing shadowing, but the most pain probably came from taking a whole bunch of crazy special purpose code and simplifying it so that it would survive a ray trace loop. There’s still quite a bit of it in the shader, but I’ve tried to comment it and use meaningful variable identifiers so that it’s clear what I’m trying to do in each section.

Also new for this post, I’m going to embed a live version of the shader in this post. To rotate the scene, simply click and drag below. If you’d like to see the full listing of code, click on “Stained” in the upper left after you mouse over. Enjoy!

This is the initial intersection with the scene. We send out a ray from our origin point with a direction, and in return we get a t (ray length) and sphere (defined by a vec4) as well as an identifier for which sphere was hit. If nothing is hit, we’ll get -1.0 for this identifier.

If we determine that we’ve hit something, we’ll figure out exactly where we hit it and calculate Lambertian reflectance for that point.

//check to see if this point is in shadowvec2 shadowT;vec4 shadowHit;//check for intersect between the sphere and the lightfloat shadowId = intersect(pos,normalize(light-pos), shadowT, shadowHit);//if we have a non-negative id, we've hit something other than the lightif(shadowId >=0.0){
col =vec4(0.0);}}

Then, we try to intersect the scene from our original intersection point with a ray in the direction toward the light. If we hit something, our point is in shadow, so we’ll replace the color with black, which will give us a hard shadow.

//If we hit the lightelseif(id ==-2.0){
col =vec4(1.0);}

Finally, we’ll add the light to the scene as its own special case, making it totally white. And that’s it!