summaryrefslogtreecommitdiffstats
path: root/libdimension/sky_sphere.c
blob: ec3fe2eb1db66800ba5533596e41f14168019b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*************************************************************************
 * Copyright (C) 2009-2010 Tavian Barnes <tavianator@tavianator.com>     *
 *                                                                       *
 * This file is part of The Dimension Library.                           *
 *                                                                       *
 * The Dimension Library is free software; you can redistribute it and/  *
 * or modify it under the terms of the GNU Lesser General Public License *
 * as published by the Free Software Foundation; either version 3 of the *
 * License, or (at your option) any later version.                       *
 *                                                                       *
 * The Dimension Library is distributed in the hope that it will be      *
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 * Lesser General Public License for more details.                       *
 *                                                                       *
 * You should have received a copy of the GNU Lesser General Public      *
 * License along with this program.  If not, see                         *
 * <http://www.gnu.org/licenses/>.                                       *
 *************************************************************************/

/**
 * @file
 * Sky spheres.
 */

#include "dimension.h"

dmnsn_sky_sphere *
dmnsn_new_sky_sphere(void)
{
  dmnsn_sky_sphere *sky_sphere = dmnsn_malloc(sizeof(dmnsn_sky_sphere));
  sky_sphere->pigments = dmnsn_new_array(sizeof(dmnsn_pigment *));
  sky_sphere->trans = dmnsn_identity_matrix();
  return sky_sphere;
}

void
dmnsn_delete_sky_sphere(dmnsn_sky_sphere *sky_sphere)
{
  if (sky_sphere) {
    DMNSN_ARRAY_FOREACH (dmnsn_pigment **, pigment, sky_sphere->pigments) {
      dmnsn_delete_pigment(*pigment);
    }
    dmnsn_delete_array(sky_sphere->pigments);
    dmnsn_free(sky_sphere);
  }
}

void
dmnsn_initialize_sky_sphere(dmnsn_sky_sphere *sky_sphere)
{
  DMNSN_ARRAY_FOREACH (dmnsn_pigment **, pigment, sky_sphere->pigments) {
    (*pigment)->trans = dmnsn_matrix_mul(sky_sphere->trans, (*pigment)->trans);
    dmnsn_initialize_pigment(*pigment);
  }
}

dmnsn_color
dmnsn_sky_sphere_color(const dmnsn_sky_sphere *sky_sphere, dmnsn_vector d)
{
  dmnsn_color color = dmnsn_black;
  color.trans = 1.0;

  DMNSN_ARRAY_FOREACH (const dmnsn_pigment **, pigment, sky_sphere->pigments) {
    dmnsn_pigment_fn *pigment_fn = (*pigment)->pigment_fn;
    if (pigment_fn) {
      dmnsn_color sky = (*pigment_fn)(*pigment, d);
      color = dmnsn_color_add(dmnsn_color_filter(color, sky), sky);
    }
  }

  return color;
}