This post looks at how to draw the background, or ambient light, and how it interacts with the objects in our scene.

As I said a couple of posts ago, ambient light is the background light. When you look around in the daytime, anything that isn’t in direct sunlight is in ambient light. It is bright in the daytime because the sun’s rays are scattered in all directions and bounce into almost every corner.

So ambient light doesn’t come directly from a light source. It has reflected off at least one object first.

We model all ambient light together, ie there can only be one ambient light, whereas you can have several other types of light sources.

To keep things simple, we simply model ambient light as a color which has a certain strength (brightness), so it is the same for every object in our scene.

Ambient light = color x strength eg color(255, 255, 0) * 0.2

I understand that normally, the strength is kept to between 0.1 to 0.3, so you get a soft backlight on your objects.

How we apply ambient light to objects

Let’s suppose we have a yellow ambient light. How will it affect objects of different colours?

To get to our eye, ambient light has to reflect off objects. Those objects will only reflect certain colours.

For example

• a white object will reflect our yellow ambient light (and look yellow), because white light contains yellow
• a blue object will reflect nothing of our ambient light (ie it will stay black) because blue light contains no yellow

If this isn’t clear, think about why objects have colours in the first place. A blue object is blue because it absorbs all the colours except blue. You might say that it rejects blue by reflecting it.  Suppose we have some children and we pass round a plate of M&M sweets like these, of different colours.

If the plate gets to a child who is very greedy and likes all colours except blue, then the next child will only get blue M&M’s. But if the plate didn’t have any blue M&M’s by the time it got to the greedy child, then the next child will get nothing.

So when we apply ambient light to an object, it combines with the colour of that object. Red ambient light will only reflect off the object if the object reflects red, and the redder the object, the more red ambient light it will reflect.

Mathematically, this is very simple.

Ambient light = ambient strength x ambient colour x object colour

And we shouldn’t forget that our object may not reflect all the light, depending on what it is made of.  A mirror will reflect fully, while black cloth will not reflect at all. So we need to include a factor for that, giving us a final formula

Ambient light = ambient strength x ambient colour x object colour x object reflectivity

Before I show you an example, I have to explain how we can multiply Codea colours such as (120,200,100) and (220,220,220)  together. The answer is that shaders don’t use colour values from 0 to 255, but from 0 to 1. When Codea passes colours to the shader, it divides all the numbers by 255 first. So all the colour values we will be multiplying in the shader will be between 0 and 1.

So let’s take our yellow ambient light and apply it to some coloured objects. Yellow is (255,255,0) in Codea, and (1, 1, 0) in the shader. We’ll assume it has strength 0.2, so we will actually be applying 0.2 x (1, 1, 0) = (0.2, 0.2, 0).

Let’s apply it to a white object, with colour (255,255,255), or (1, 1, 1) in the shader. Multiplying gives us (0.2, 0.2, 0) x (1, 1, 1) = (0.2, 0.2, 0), so all the ambient light is reflected, making the object a darkish yellow colour. If our object only reflects half the light, our reflectivity is 0.5 and the ambient light will be (0.1, 0.1, 0).

A blue object has colour (0, 0, 255), or (0, 0, 1) in the shader. Multiplying gives us (0.2, 0.2, 0) x (0, 0, 1) = (0, 0, 0), so no light is reflected, leaving the object black.

So calculating ambient light is very simple indeed.

But what about the alpha part of colour, ie the fourth number in Codea colours? What if we have a semi transparent object with an alpha of 100, and do we multiply the alpha values of ambient light and objects together?

No we don’t. We always keep alpha at its full value of 255 (1 in the shader).

This is because if alpha is less than full value, then part of our light will pass through it and reach what is behind it. This gets way too hard to model, especially as OpenGL doesn’t draw anything behind a pixel containing a colour, even if it is transparent. So we treat all colours as opaque, ie you can’t see through them.

The result

Ambient light gives you an outline for an object, like a base coat when you are painting a wall. You need more light to show detail.