Author: Mark Danks

## Lighting

Lighting is an important factor is how we perceive the quality of an image. For example, without lighting and shading, a sphere would just look like a circle. GEM provides two types of lights, a local light and world light.

OpenGL uses a vertex lighting model. This means that for every vertex in the scene, the influence of the light is calculated. The color for the polygon is then modified by the light value of all of the vertices. This generally produces a very smooth effect, but you will occasionally run into rendering artifacts, especially if you use local lights. For example, imagine you have a local light close a large square. The corners of the square are far away from the light, so none of them will be lit very brightly, even though the light itself is very close to the surface of the square.

It is important to realize that lighting is an expensive operation to use. The number of polygons that you will be able to render will be much lower if lighting is turned on. As usual, the complexity of the scene and the speed of your computer and graphics card will greatly affect your frame rate.

GEM has only a maximum of 8 lights at one time. If you try to create more lights than that, you will get an error message.

Activate lighting
[world_light] - A directional light
[light] - A point light in the world
Moving lights

### Activate lighting

Lighting is activated by sending a message to [gemwin]. If you send "lighting 1", then lighting will be turned on. If you send "lighting 0", then lighting will be turned off. The lighting state is kept even if you destroy the gemwin. This means that if you close a patch and open another one, the lighting will still be the same.

Individual lights can be turned on and off by sending a 1 or 0 to the left inlet of the light object itself.

### [world_light]

A [world_light] is a light which exists infintely far away. This reduces the computation needed, so your patch can run faster, but it means that all of the light rays are parallel. The [world_light] is good for objects like the sun and other lighting affects. This means that translating a [world_light] has no effect, although rotation does.

The following patch is 03.lighting/01.world_light.pd.

The [world_light] has one extra inlet. The right inlet accepts three floats to set the color of the light. A [color] object would do nothing. In this case, the light is being set to purple. The [world_light] also accepts a debug message. The debug message turns on and off a graphical representation of the light in the scene. The [world_light] looks like a cone. The cone shows the direction that the light is coming from. Remember that the actual position of the light does not matter, so geos behind the cone will still be lit. It is the direction of the light that matters. This is why you can rotate the light.

### [light]

A [light] object generates a point light in the world. Because the light is local to the scene, there is more math to generate the effect of the light on the vertices. However, unlike a [world_light], you can translate the [light] object.

Below is the patch 03.lighting/02.light.pd.

The [light] object has a right inlet for the color, just light the [world_light] object. As this patch shows, the light can be moved around the scene with both [rotate] and [translate] objects. If you were to set the translate X value equal to 1.0, then the sphere would not be lit at all. This is because the light would be inside of the sphere. When you turn on the debug representation, it is a sphere with its origin where the light it. The [light] object does not have any size. It exists as a point source.

### Moving lights

The patch 03.lighting/03.controlLights.pd allows you to move a [light] and [world_light] object in the same scene to see the difference between the two objects.

The patch 03.lighting/04.moveSpheres.pd is an example which moves two spheres around the world. Turn on and off the individual lights for a demonstration of a local versus infinite light.

The patch 03.lighting/05.materials.pd uses the material objects to selectively control the color of the object. Notice that the diffuse object sets the "overall" color, while the specular objects sets the bright reflective area where the light directly shines.

[return]