Two Lights

16 / 17
Learn core lighting terms (diffuse/specular/rim) on a simple sphere.

Adding multiple lights is straightforward: compute diffuse for each light separately, then add them with weights. The key line is:

Let's break down the structure.


Why use weights

Lights have different intensities. Light 1 contributes 70% (0.7) and light 2 contributes 50% (0.5). If both were set to 1.0, areas facing both lights would be overexposed — brighter than anything in nature.

Weights let you set up a key light / fill light relationship: the key light (stronger) defines the main shape, while the fill light (weaker) brings out detail on the shadowed side.


Two light directions

The two lights come from different angles. Every patch on the sphere receives some contribution from both. normalize keeps each direction vector at length 1, which is required for correct diffuse math.


The summing formula

max(..., 0.0) prevents back-facing surfaces from producing negative contributions (which would make the sphere darker than the ambient). The two terms are simply added together for the total diffuse amount.


Try changing it

ChangeEffect
Change l1 weight from 0.7 to 0.3Both lights become similar in strength
Change l2 direction to vec3(-0.6, 0.2, 0.6)Both lights from the same side, full shadow on the other
Add a third light l3 from directly belowThree-point lighting setup
Multiply each term by a different colorWarm/cool light contrast

Exercise

The exercise has both light directions l1 and l2 defined, but diff only uses one light. Add the second light's diffuse contribution with its weight, so the sphere shows the two-light effect.

Answer Breakdown

The starting code has diff = max(dot(n, l1), 0.0) * 0.7 — only one light, so the right side of the sphere gets no direct light and relies entirely on ambient.

Adding + max(dot(n, l2), 0.0) * 0.5 brings in the second light from the right. The sphere's front center receives both lights (brightest), each side receives one, and the back stays near-black. The 0.7 and 0.5 weights make the key light clearly stronger than the fill.

Try multiplying the second term by vec3(0.8, 0.6, 1.0) to tint the fill light with a cool blue-purple, creating a warm/cool contrast.

GLSL Code Editor

Correct Code Preview

Current Code Preview