# C API: Lighting and shadows

A key light, ambient and environment light, shadows on the avatars and the ground, and the host's own shadow map: [Lighting and shadows](../../guides/lighting.md).

## sona_lighting_defaults

```c
void sona_lighting_defaults(sona_lighting *l);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `l` | `sona_lighting *` | The [`sona_lighting`](../fields.md#sona_lighting) to fill with the defaults | required |

**Returns** Nothing.

**Example** (also in [Lighting and shadows](../../guides/lighting.md)):

```c
sona_lighting_defaults(&l);
l.key.direction[0] = 0.4f; l.key.direction[1] = -1.0f; l.key.direction[2] = 0.6f;
l.key.intensity = 0.9f;
l.flags = SONA_LIGHTING_GROUND | SONA_LIGHTING_PROXY_CASTERS;   /* a crowd's ground shadows for almost nothing */
sona_renderer_set_lighting(r, &l);
```

`size` set; `ambient` 0, `environment` 1, `specular` 1, `rim` 1, `shadow` FIT, `shadow_size` 2048, `shadow_strength` 1, `shadow_ambient` 0.5, `shadow_bias` 0.03, `ground_y` 0, `ground_opacity` 0.6.

## sona_renderer_set_lighting

```c
sona_result sona_renderer_set_lighting(sona_renderer *r, const sona_lighting *l);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `l` | `const sona_lighting *` | A [`sona_lighting`](../fields.md#sona_lighting) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Lighting and shadows](../../guides/lighting.md)):

```c
sona_lighting_defaults(&l);
l.key.direction[0] = 0.4f; l.key.direction[1] = -1.0f; l.key.direction[2] = 0.6f;
l.key.intensity = 0.9f;
l.flags = SONA_LIGHTING_GROUND | SONA_LIGHTING_PROXY_CASTERS;   /* a crowd's ground shadows for almost nothing */
sona_renderer_set_lighting(r, &l);
```

From the next draw. NULL: no lighting, the original look again. `SONA_E_ARGUMENT` for a size that is not sizeof(`sona_lighting`), a value out of range, an unknown flag or mode, a zero key direction with shadows on, or a matrix that is not orthographic.

## sona_renderer_lighting

```c
sona_result sona_renderer_lighting(const sona_renderer *r, sona_lighting *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `const sona_renderer *` | The renderer | required |
| `out` | `sona_lighting *` | Receives the [`sona_lighting`](../fields.md#sona_lighting) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_lighting x_lighting;
sona_lighting_defaults(&x_lighting);
sona_result result = sona_renderer_lighting(r, &x_lighting);
if (result != SONA_OK) printf("sona_renderer_lighting: %s\n", sona_result_name(result));
```

What is set; `SONA_E_NOT_FOUND` when none is.

## sona_draw_lit

```c
sona_result sona_draw_lit(sona_renderer *r, const sona_view *view, const sona_target *target, const sona_draw_options *opts, const sona_lighting *l);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `view` | `const sona_view *` | The camera, a [`sona_view`](../fields.md#sona_view) | required |
| `target` | `const sona_target *` | Where the frame lands, a [`sona_target`](../fields.md#sona_target) | required |
| `opts` | `const sona_draw_options *` | The options, a [`sona_draw_options`](../fields.md#sona_draw_options) | required |
| `l` | `const sona_lighting *` | A [`sona_lighting`](../fields.md#sona_lighting) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY`, `SONA_E_CONTEXT` ([what each means](../../troubleshooting.md#error-codes)).

`sona_draw` and `sona_snapshot` with l in place of the renderer's lighting for this one frame (one per view of a split screen); l NULL: the renderer's own. The checks of `sona_renderer_set_lighting`.

## sona_snapshot_lit

```c
sona_result sona_snapshot_lit(sona_renderer *r, const sona_view *view, uint32_t width, uint32_t height, const sona_draw_options *opts, const sona_lighting *l, uint8_t *rgba_out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `view` | `const sona_view *` | The camera, a [`sona_view`](../fields.md#sona_view) | required |
| `width` | `uint32_t` | The picture's width in pixels | - |
| `height` | `uint32_t` | The picture's height in pixels | - |
| `opts` | `const sona_draw_options *` | The options, a [`sona_draw_options`](../fields.md#sona_draw_options) | required |
| `l` | `const sona_lighting *` | A [`sona_lighting`](../fields.md#sona_lighting) | required |
| `rgba_out` | `uint8_t *` | Receives straight RGBA8, row 0 at the top: `width * height * 4` bytes | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY`, `SONA_E_CONTEXT`, `SONA_E_READBACK` ([what each means](../../troubleshooting.md#error-codes)).

`sona_snapshot` with `l` in place of the renderer's lighting for this one picture; `l` NULL: the renderer's own. The checks of `sona_renderer_set_lighting`.

## sona_environment_defaults

```c
void sona_environment_defaults(sona_environment *e);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `e` | `sona_environment *` | The [`sona_environment`](../fields.md#sona_environment) to fill with the defaults | required |

**Returns** Nothing.

**Example** (also in [Lighting and shadows](../../guides/lighting.md)):

```c
sona_environment e;
sona_environment_defaults(&e);
e.kind = SONA_ENV_SH9;                       /* or SONA_ENV_CUBE with cube_size and cube */
memcpy(e.sh, my_sh, sizeof e.sh);            /* 9 x RGB, radiance */
sona_renderer_set_environment(r, &e);        /* NULL: the built-in cube */
```

`size` set; `kind` BUILTIN, `skin_red` 1.2 (the built-in's).

## sona_renderer_set_environment

```c
sona_result sona_renderer_set_environment(sona_renderer *r, const sona_environment *e);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `e` | `const sona_environment *` | A [`sona_environment`](../fields.md#sona_environment) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Lighting and shadows](../../guides/lighting.md)):

```c
sona_environment e;
sona_environment_defaults(&e);
e.kind = SONA_ENV_SH9;                       /* or SONA_ENV_CUBE with cube_size and cube */
memcpy(e.sh, my_sh, sizeof e.sh);            /* 9 x RGB, radiance */
sona_renderer_set_environment(r, &e);        /* NULL: the built-in cube */
```

From the next lit draw. NULL: the built-in cube. `SONA_E_ARGUMENT` for a bad size, kind or cube.

## sona_environment_irradiance

```c
sona_result sona_environment_irradiance(const sona_environment *e, float out_sh[27]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `e` | `const sona_environment *` | A [`sona_environment`](../fields.md#sona_environment) | required |
| `out_sh` | `float [27]` | Receives nine RGB spherical-harmonic coefficients | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_environment x_environment;
sona_environment_defaults(&x_environment);
float x_out_sh2[27];
memset(x_out_sh2, 0, sizeof x_out_sh2);
sona_result result = sona_environment_irradiance(&x_environment, x_out_sh2);
if (result != SONA_OK) printf("sona_environment_irradiance: %s\n", sona_result_name(result));
```

The irradiance the shader evaluates for e (cube projected, convolved with the cosine lobe, divided by pi), in the basis above: a host lights its own scene the same way with it. `SONA_E_ARGUMENT` as above and for BUILTIN.

## sona_renderer_set_host_shadow

```c
sona_result sona_renderer_set_host_shadow(sona_renderer *r, const sona_host_shadow *s);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `s` | `const sona_host_shadow *` | A [`sona_host_shadow`](../fields.md#sona_host_shadow) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Lighting and shadows](../../guides/lighting.md)):

```c
sona_host_shadow h;
memset(&h, 0, sizeof h);
h.size = sizeof h;
memcpy(h.matrix, my_light_view_proj_to_uv, sizeof h.matrix);   /* world point to (u, v, depth, w) */
h.bias = 0.0015f;
h.width = h.height = 4096;
h.u.vk.view = my_depth_view;                 /* .gl.texture, .d3d11.srv, .d3d12.resource + .srv_format */
h.u.vk.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
sona_renderer_set_host_shadow(r, &h);
```

NULL: none. `SONA_E_UNSUPPORTED` on a backend without it (Metal), `SONA_E_ARGUMENT` for a bad size or handle.

## sona_renderer_shadow_map

```c
sona_result sona_renderer_shadow_map(const sona_renderer *r, sona_shadow_map *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `const sona_renderer *` | The renderer | required |
| `out` | `sona_shadow_map *` | Receives the [`sona_shadow_map`](../fields.md#sona_shadow_map) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND`, `SONA_E_UNSUPPORTED` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Lighting and shadows](../../guides/lighting.md)):

```c
sona_shadow_map m;
m.size = sizeof m;
if (sona_renderer_shadow_map(r, &m) == SONA_OK) {
  /* m.matrix: world point (row vector) to (u, v, depth, 1); m.u.vk.view etc: the depth texture to sample */
}
```

`SONA_E_NOT_FOUND` before a lit frame with shadows; `SONA_E_UNSUPPORTED` on a backend without it.
